separate block and ball shader programs
This commit is contained in:
parent
cea6f38aec
commit
44cc1f8a6b
@ -14,8 +14,17 @@ extern "C" {
|
|||||||
uint length;
|
uint length;
|
||||||
};
|
};
|
||||||
|
|
||||||
void render(struct mesh brick_mesh,
|
void render_blocks(struct mesh block_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_balls(struct mesh ball_mesh,
|
||||||
uint attrib_position,
|
uint attrib_position,
|
||||||
uint attrib_texture,
|
uint attrib_texture,
|
||||||
uint attrib_normal,
|
uint attrib_normal,
|
||||||
|
|||||||
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
|
||||||
63
src/main.c
63
src/main.c
@ -14,6 +14,7 @@
|
|||||||
#include "shader/background.fp.glsl.h"
|
#include "shader/background.fp.glsl.h"
|
||||||
#include "shader/background.vp.glsl.h"
|
#include "shader/background.vp.glsl.h"
|
||||||
#include "shader/paddle.fp.glsl.h"
|
#include "shader/paddle.fp.glsl.h"
|
||||||
|
#include "shader/block.fp.glsl.h"
|
||||||
|
|
||||||
#include "font/ter_u32n.data.h"
|
#include "font/ter_u32n.data.h"
|
||||||
|
|
||||||
@ -136,17 +137,31 @@ int main()
|
|||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
uint program = compile_shader(src_shader_vertex_color_vp_glsl_start,
|
uint ball_program = compile_shader(src_shader_vertex_color_vp_glsl_start,
|
||||||
src_shader_vertex_color_vp_glsl_size,
|
src_shader_vertex_color_vp_glsl_size,
|
||||||
src_shader_vertex_color_fp_glsl_start,
|
src_shader_vertex_color_fp_glsl_start,
|
||||||
src_shader_vertex_color_fp_glsl_size);
|
src_shader_vertex_color_fp_glsl_size);
|
||||||
uint attrib_position = glGetAttribLocation(program, "position");
|
uint ball__attrib_position = glGetAttribLocation(ball_program, "position");
|
||||||
uint attrib_texture = glGetAttribLocation(program, "_texture");
|
uint ball__attrib_texture = glGetAttribLocation(ball_program, "_texture");
|
||||||
uint attrib_normal = glGetAttribLocation(program, "normal");
|
uint ball__attrib_normal = glGetAttribLocation(ball_program, "normal");
|
||||||
uint uniform_trans = glGetUniformLocation(program, "trans");
|
uint ball__uniform_trans = glGetUniformLocation(ball_program, "trans");
|
||||||
uint uniform_normal_trans = glGetUniformLocation(program, "normal_trans");
|
uint ball__uniform_normal_trans = glGetUniformLocation(ball_program, "normal_trans");
|
||||||
uint uniform_base_color = glGetUniformLocation(program, "base_color");
|
uint ball__uniform_base_color = glGetUniformLocation(ball_program, "base_color");
|
||||||
uint uniform_light_pos = glGetUniformLocation(program, "light_pos");
|
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
|
// font
|
||||||
|
|
||||||
@ -308,16 +323,28 @@ int main()
|
|||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
glDepthFunc(GL_GREATER);
|
glDepthFunc(GL_GREATER);
|
||||||
glUseProgram(program);
|
glUseProgram(block_program);
|
||||||
render(block_mesh,
|
render_blocks(block_mesh,
|
||||||
ball_mesh,
|
block__attrib_position,
|
||||||
attrib_position,
|
block__attrib_texture,
|
||||||
attrib_texture,
|
block__attrib_normal,
|
||||||
attrib_normal,
|
block__uniform_trans,
|
||||||
uniform_trans,
|
block__uniform_normal_trans,
|
||||||
uniform_normal_trans,
|
block__uniform_base_color,
|
||||||
uniform_base_color,
|
block__uniform_light_pos,
|
||||||
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);
|
&state);
|
||||||
|
|
||||||
glDisable(GL_BLEND);
|
glDisable(GL_BLEND);
|
||||||
|
|||||||
@ -92,8 +92,7 @@ static inline vec3 _light_pos()
|
|||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
void render(mesh block_mesh,
|
void render_blocks(mesh block_mesh,
|
||||||
mesh ball_mesh,
|
|
||||||
uint attrib_position,
|
uint attrib_position,
|
||||||
uint attrib_texture,
|
uint attrib_texture,
|
||||||
uint attrib_normal,
|
uint attrib_normal,
|
||||||
@ -105,10 +104,6 @@ void render(mesh block_mesh,
|
|||||||
{
|
{
|
||||||
light_pos_theta += 0.01;
|
light_pos_theta += 0.01;
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////
|
|
||||||
// render blocks
|
|
||||||
//////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, block_mesh.vtx);
|
glBindBuffer(GL_ARRAY_BUFFER, block_mesh.vtx);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, block_mesh.idx);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, block_mesh.idx);
|
||||||
|
|
||||||
@ -137,6 +132,10 @@ void render(mesh block_mesh,
|
|||||||
glEnableVertexAttribArray(attrib_texture);
|
glEnableVertexAttribArray(attrib_texture);
|
||||||
glEnableVertexAttribArray(attrib_normal);
|
glEnableVertexAttribArray(attrib_normal);
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
// render blocks
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
mat4x4 a = aspect_mat();
|
mat4x4 a = aspect_mat();
|
||||||
vec3 light_pos = _light_pos();
|
vec3 light_pos = _light_pos();
|
||||||
|
|
||||||
@ -186,35 +185,18 @@ void render(mesh block_mesh,
|
|||||||
glDrawElements(GL_TRIANGLES, block_mesh.length, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, block_mesh.length, GL_UNSIGNED_INT, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////
|
|
||||||
// render balls
|
|
||||||
//////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
for (int i = 0; i < state->balls_launched; i++) {
|
|
||||||
struct ball_state& ball = state->balls[i];
|
|
||||||
|
|
||||||
mat4x4 rx = rotate_x((float)state->time);
|
|
||||||
mat4x4 ry = rotate_y((float)state->time * 0.5f);
|
|
||||||
vec3 ball_position = vec3(ball.ball_x * 4.0f, -ball.ball_y * 2.0f, 0.0);
|
|
||||||
mat4x4 t = translate(ball_position);
|
|
||||||
|
|
||||||
mat4x4 trans = a * t * ry * rx;
|
|
||||||
mat3x3 normal_trans = submatrix(ry * rx, 3, 3);
|
|
||||||
|
|
||||||
vec4 base_color = vec4(0.5f, 0.5f, 0.5f, 1.0f);
|
|
||||||
if (ball.super_ball) {
|
|
||||||
float hue = state->time - ball.launch_time;
|
|
||||||
hue = hue - floorf(hue);
|
|
||||||
vec3 c = hsv_to_rgb(hue, 1.0f, 1.0f);
|
|
||||||
base_color = vec4(c.x, c.y, c.z, 1.0f);
|
|
||||||
} else if (0) {
|
|
||||||
float hue = sin(ball.launch_time) * 0.5 - 0.5;
|
|
||||||
hue = hue - floorf(hue);
|
|
||||||
vec3 c = hsv_to_rgb(hue, 1.0f, 1.0f);
|
|
||||||
base_color = vec4(c.x, c.y, c.z, 1.0f) * 0.5f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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_ARRAY_BUFFER, ball_mesh.vtx);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ball_mesh.idx);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ball_mesh.idx);
|
||||||
|
|
||||||
@ -243,6 +225,37 @@ void render(mesh block_mesh,
|
|||||||
glEnableVertexAttribArray(attrib_texture);
|
glEnableVertexAttribArray(attrib_texture);
|
||||||
glEnableVertexAttribArray(attrib_normal);
|
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];
|
||||||
|
|
||||||
|
mat4x4 rx = rotate_x((float)state->time);
|
||||||
|
mat4x4 ry = rotate_y((float)state->time * 0.5f);
|
||||||
|
vec3 ball_position = vec3(ball.ball_x * 4.0f, -ball.ball_y * 2.0f, 0.0);
|
||||||
|
mat4x4 t = translate(ball_position);
|
||||||
|
|
||||||
|
mat4x4 trans = a * t * ry * rx;
|
||||||
|
mat3x3 normal_trans = submatrix(ry * rx, 3, 3);
|
||||||
|
|
||||||
|
vec4 base_color = vec4(0.5f, 0.5f, 0.5f, 1.0f);
|
||||||
|
if (ball.super_ball) {
|
||||||
|
float hue = state->time - ball.launch_time;
|
||||||
|
hue = hue - floorf(hue);
|
||||||
|
vec3 c = hsv_to_rgb(hue, 1.0f, 1.0f);
|
||||||
|
base_color = vec4(c.x, c.y, c.z, 1.0f);
|
||||||
|
} else if (0) {
|
||||||
|
float hue = sin(ball.launch_time) * 0.5 - 0.5;
|
||||||
|
hue = hue - floorf(hue);
|
||||||
|
vec3 c = hsv_to_rgb(hue, 1.0f, 1.0f);
|
||||||
|
base_color = vec4(c.x, c.y, c.z, 1.0f) * 0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
glUniform4fv(uniform_trans, 4, &trans[0][0]);
|
glUniform4fv(uniform_trans, 4, &trans[0][0]);
|
||||||
glUniform3fv(uniform_normal_trans, 3, &normal_trans[0][0]);
|
glUniform3fv(uniform_normal_trans, 3, &normal_trans[0][0]);
|
||||||
glUniform4fv(uniform_base_color, 1, &base_color[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()
|
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);
|
vec3 light_dir = normalize(light_pos - fp_position);
|
||||||
float diffuse = max(dot(fp_normal, light_dir), 0.0);
|
float diffuse = max(dot(fp_normal, light_dir), 0.0);
|
||||||
|
|
||||||
vec3 color = (diffuse + 0.5) * base_color.xyz;
|
vec3 color = (diffuse + 0.5) * base_color.xyz;
|
||||||
|
|
||||||
gl_FragColor = vec4(color, base_color.w);
|
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