separate block and ball shader programs
This commit is contained in:
parent
cea6f38aec
commit
44cc1f8a6b
@ -14,16 +14,25 @@ extern "C" {
|
||||
uint length;
|
||||
};
|
||||
|
||||
void render(struct mesh brick_mesh,
|
||||
struct mesh ball_mesh,
|
||||
uint attrib_position,
|
||||
uint attrib_texture,
|
||||
uint attrib_normal,
|
||||
uint uniform_trans,
|
||||
uint uniform_normal_trans,
|
||||
uint uniform_base_color,
|
||||
uint uniform_light_pos,
|
||||
struct game_state * state);
|
||||
void render_blocks(struct mesh block_mesh,
|
||||
uint attrib_position,
|
||||
uint attrib_texture,
|
||||
uint attrib_normal,
|
||||
uint uniform_trans,
|
||||
uint uniform_normal_trans,
|
||||
uint uniform_base_color,
|
||||
uint uniform_light_pos,
|
||||
struct game_state * state);
|
||||
|
||||
void render_balls(struct mesh ball_mesh,
|
||||
uint attrib_position,
|
||||
uint attrib_texture,
|
||||
uint attrib_normal,
|
||||
uint uniform_trans,
|
||||
uint uniform_normal_trans,
|
||||
uint uniform_base_color,
|
||||
uint uniform_light_pos,
|
||||
struct game_state * state);
|
||||
|
||||
void render_font(struct mesh plane_mesh,
|
||||
uint attrib_position,
|
||||
|
||||
19
include/shader/block.fp.glsl.h
Normal file
19
include/shader/block.fp.glsl.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_src_shader_block_fp_glsl_start __asm("_binary_src_shader_block_fp_glsl_start");
|
||||
extern uint32_t _binary_src_shader_block_fp_glsl_end __asm("_binary_src_shader_block_fp_glsl_end");
|
||||
extern uint32_t _binary_src_shader_block_fp_glsl_size __asm("_binary_src_shader_block_fp_glsl_size");
|
||||
|
||||
#define src_shader_block_fp_glsl_start ((const char *)&_binary_src_shader_block_fp_glsl_start)
|
||||
#define src_shader_block_fp_glsl_end ((const char *)&_binary_src_shader_block_fp_glsl_end)
|
||||
#define src_shader_block_fp_glsl_size (src_shader_block_fp_glsl_end - src_shader_block_fp_glsl_start)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
71
src/main.c
71
src/main.c
@ -14,6 +14,7 @@
|
||||
#include "shader/background.fp.glsl.h"
|
||||
#include "shader/background.vp.glsl.h"
|
||||
#include "shader/paddle.fp.glsl.h"
|
||||
#include "shader/block.fp.glsl.h"
|
||||
|
||||
#include "font/ter_u32n.data.h"
|
||||
|
||||
@ -136,17 +137,31 @@ int main()
|
||||
|
||||
//
|
||||
|
||||
uint program = compile_shader(src_shader_vertex_color_vp_glsl_start,
|
||||
src_shader_vertex_color_vp_glsl_size,
|
||||
src_shader_vertex_color_fp_glsl_start,
|
||||
src_shader_vertex_color_fp_glsl_size);
|
||||
uint attrib_position = glGetAttribLocation(program, "position");
|
||||
uint attrib_texture = glGetAttribLocation(program, "_texture");
|
||||
uint attrib_normal = glGetAttribLocation(program, "normal");
|
||||
uint uniform_trans = glGetUniformLocation(program, "trans");
|
||||
uint uniform_normal_trans = glGetUniformLocation(program, "normal_trans");
|
||||
uint uniform_base_color = glGetUniformLocation(program, "base_color");
|
||||
uint uniform_light_pos = glGetUniformLocation(program, "light_pos");
|
||||
uint ball_program = compile_shader(src_shader_vertex_color_vp_glsl_start,
|
||||
src_shader_vertex_color_vp_glsl_size,
|
||||
src_shader_vertex_color_fp_glsl_start,
|
||||
src_shader_vertex_color_fp_glsl_size);
|
||||
uint ball__attrib_position = glGetAttribLocation(ball_program, "position");
|
||||
uint ball__attrib_texture = glGetAttribLocation(ball_program, "_texture");
|
||||
uint ball__attrib_normal = glGetAttribLocation(ball_program, "normal");
|
||||
uint ball__uniform_trans = glGetUniformLocation(ball_program, "trans");
|
||||
uint ball__uniform_normal_trans = glGetUniformLocation(ball_program, "normal_trans");
|
||||
uint ball__uniform_base_color = glGetUniformLocation(ball_program, "base_color");
|
||||
uint ball__uniform_light_pos = glGetUniformLocation(ball_program, "light_pos");
|
||||
|
||||
// block
|
||||
|
||||
uint block_program = compile_shader(src_shader_vertex_color_vp_glsl_start,
|
||||
src_shader_vertex_color_vp_glsl_size,
|
||||
src_shader_block_fp_glsl_start,
|
||||
src_shader_block_fp_glsl_size);
|
||||
uint block__attrib_position = glGetAttribLocation(block_program, "position");
|
||||
uint block__attrib_texture = glGetAttribLocation(block_program, "_texture");
|
||||
uint block__attrib_normal = glGetAttribLocation(block_program, "normal");
|
||||
uint block__uniform_trans = glGetUniformLocation(block_program, "trans");
|
||||
uint block__uniform_normal_trans = glGetUniformLocation(block_program, "normal_trans");
|
||||
uint block__uniform_base_color = glGetUniformLocation(block_program, "base_color");
|
||||
uint block__uniform_light_pos = glGetUniformLocation(block_program, "light_pos");
|
||||
|
||||
// font
|
||||
|
||||
@ -308,17 +323,29 @@ int main()
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glDepthFunc(GL_GREATER);
|
||||
glUseProgram(program);
|
||||
render(block_mesh,
|
||||
ball_mesh,
|
||||
attrib_position,
|
||||
attrib_texture,
|
||||
attrib_normal,
|
||||
uniform_trans,
|
||||
uniform_normal_trans,
|
||||
uniform_base_color,
|
||||
uniform_light_pos,
|
||||
&state);
|
||||
glUseProgram(block_program);
|
||||
render_blocks(block_mesh,
|
||||
block__attrib_position,
|
||||
block__attrib_texture,
|
||||
block__attrib_normal,
|
||||
block__uniform_trans,
|
||||
block__uniform_normal_trans,
|
||||
block__uniform_base_color,
|
||||
block__uniform_light_pos,
|
||||
&state);
|
||||
|
||||
glDisable(GL_BLEND);
|
||||
glDepthFunc(GL_GREATER);
|
||||
glUseProgram(ball_program);
|
||||
render_balls(ball_mesh,
|
||||
ball__attrib_position,
|
||||
ball__attrib_texture,
|
||||
ball__attrib_normal,
|
||||
ball__uniform_trans,
|
||||
ball__uniform_normal_trans,
|
||||
ball__uniform_base_color,
|
||||
ball__uniform_light_pos,
|
||||
&state);
|
||||
|
||||
glDisable(GL_BLEND);
|
||||
glDepthFunc(GL_GREATER);
|
||||
|
||||
@ -92,23 +92,18 @@ static inline vec3 _light_pos()
|
||||
return pos;
|
||||
}
|
||||
|
||||
void render(mesh block_mesh,
|
||||
mesh ball_mesh,
|
||||
uint attrib_position,
|
||||
uint attrib_texture,
|
||||
uint attrib_normal,
|
||||
uint uniform_trans,
|
||||
uint uniform_normal_trans,
|
||||
uint uniform_base_color,
|
||||
uint uniform_light_pos,
|
||||
struct game_state * state)
|
||||
void render_blocks(mesh block_mesh,
|
||||
uint attrib_position,
|
||||
uint attrib_texture,
|
||||
uint attrib_normal,
|
||||
uint uniform_trans,
|
||||
uint uniform_normal_trans,
|
||||
uint uniform_base_color,
|
||||
uint uniform_light_pos,
|
||||
struct game_state * state)
|
||||
{
|
||||
light_pos_theta += 0.01;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// render blocks
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, block_mesh.vtx);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, block_mesh.idx);
|
||||
|
||||
@ -137,6 +132,10 @@ void render(mesh block_mesh,
|
||||
glEnableVertexAttribArray(attrib_texture);
|
||||
glEnableVertexAttribArray(attrib_normal);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// render blocks
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
mat4x4 a = aspect_mat();
|
||||
vec3 light_pos = _light_pos();
|
||||
|
||||
@ -186,11 +185,53 @@ void render(mesh block_mesh,
|
||||
glDrawElements(GL_TRIANGLES, block_mesh.length, GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void render_balls(mesh ball_mesh,
|
||||
uint attrib_position,
|
||||
uint attrib_texture,
|
||||
uint attrib_normal,
|
||||
uint uniform_trans,
|
||||
uint uniform_normal_trans,
|
||||
uint uniform_base_color,
|
||||
uint uniform_light_pos,
|
||||
struct game_state * state)
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ball_mesh.vtx);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ball_mesh.idx);
|
||||
|
||||
glVertexAttribPointer(attrib_position,
|
||||
3,
|
||||
GL_FLOAT,
|
||||
GL_FALSE,
|
||||
(sizeof (float)) * 8,
|
||||
(void*)(0 * 4)
|
||||
);
|
||||
glVertexAttribPointer(attrib_texture,
|
||||
2,
|
||||
GL_FLOAT,
|
||||
GL_FALSE,
|
||||
(sizeof (float)) * 8,
|
||||
(void*)(3 * 4)
|
||||
);
|
||||
glVertexAttribPointer(attrib_normal,
|
||||
3,
|
||||
GL_FLOAT,
|
||||
GL_FALSE,
|
||||
(sizeof (float)) * 8,
|
||||
(void*)(5 * 4)
|
||||
);
|
||||
glEnableVertexAttribArray(attrib_position);
|
||||
glEnableVertexAttribArray(attrib_texture);
|
||||
glEnableVertexAttribArray(attrib_normal);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// render balls
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
mat4x4 a = aspect_mat();
|
||||
vec3 light_pos = _light_pos();
|
||||
|
||||
for (int i = 0; i < state->balls_launched; i++) {
|
||||
struct ball_state& ball = state->balls[i];
|
||||
|
||||
@ -215,34 +256,6 @@ void render(mesh block_mesh,
|
||||
base_color = vec4(c.x, c.y, c.z, 1.0f) * 0.5f;
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ball_mesh.vtx);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ball_mesh.idx);
|
||||
|
||||
glVertexAttribPointer(attrib_position,
|
||||
3,
|
||||
GL_FLOAT,
|
||||
GL_FALSE,
|
||||
(sizeof (float)) * 8,
|
||||
(void*)(0 * 4)
|
||||
);
|
||||
glVertexAttribPointer(attrib_texture,
|
||||
2,
|
||||
GL_FLOAT,
|
||||
GL_FALSE,
|
||||
(sizeof (float)) * 8,
|
||||
(void*)(3 * 4)
|
||||
);
|
||||
glVertexAttribPointer(attrib_normal,
|
||||
3,
|
||||
GL_FLOAT,
|
||||
GL_FALSE,
|
||||
(sizeof (float)) * 8,
|
||||
(void*)(5 * 4)
|
||||
);
|
||||
glEnableVertexAttribArray(attrib_position);
|
||||
glEnableVertexAttribArray(attrib_texture);
|
||||
glEnableVertexAttribArray(attrib_normal);
|
||||
|
||||
glUniform4fv(uniform_trans, 4, &trans[0][0]);
|
||||
glUniform3fv(uniform_normal_trans, 3, &normal_trans[0][0]);
|
||||
glUniform4fv(uniform_base_color, 1, &base_color[0]);
|
||||
|
||||
18
src/shader/block.fp.glsl
Normal file
18
src/shader/block.fp.glsl
Normal file
@ -0,0 +1,18 @@
|
||||
#version 120
|
||||
|
||||
uniform vec4 base_color;
|
||||
uniform vec3 light_pos;
|
||||
|
||||
varying vec3 fp_position;
|
||||
varying vec2 fp_texture;
|
||||
varying vec3 fp_normal;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 light_dir = normalize(light_pos - fp_position);
|
||||
float diffuse = max(dot(fp_normal, light_dir), 0.0);
|
||||
|
||||
vec3 color = (diffuse + 0.5) * base_color.xyz;
|
||||
|
||||
gl_FragColor = vec4(color, base_color.w);
|
||||
}
|
||||
@ -9,19 +9,10 @@ varying vec3 fp_normal;
|
||||
|
||||
void main()
|
||||
{
|
||||
//vec3 color_normal = fp_normal * 0.5 + 0.5;
|
||||
//vec3 light_pos = vec3(1, 1, 1);
|
||||
|
||||
vec3 light_dir = normalize(light_pos - fp_position);
|
||||
float diffuse = max(dot(fp_normal, light_dir), 0.0);
|
||||
|
||||
vec3 color = (diffuse + 0.5) * base_color.xyz;
|
||||
|
||||
gl_FragColor = vec4(color, base_color.w);
|
||||
//gl_FragColor = vec4(fp_normal * 0.5 + 0.5, 1.0);
|
||||
//gl_FragColor = vec4(fp_texture, 0.0, 1.0);
|
||||
}
|
||||
// normal
|
||||
// x (0.0 left 1.0 right)
|
||||
// y (0.0 bot 1.0 top)
|
||||
// z (0.0 far 1.0 near)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user