draw paddle

This commit is contained in:
Zack Buhman 2025-12-04 14:00:17 -06:00
parent edf47747f9
commit 38f3219936
9 changed files with 8996 additions and 20 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.o *.o
*.d *.d
*.blend1
main main

BIN
blend/block.blend Normal file

Binary file not shown.

BIN
blend/paddle.blend Normal file

Binary file not shown.

5428
include/model/paddle.h Normal file

File diff suppressed because it is too large Load Diff

3435
include/model/paddle.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,14 +6,22 @@ extern "C" {
typedef unsigned int uint; typedef unsigned int uint;
void render(uint vtx, uint idx, struct mesh {
uint vtx;
uint idx;
uint length;
};
void render(struct mesh paddle_mesh,
struct mesh brick_mesh,
uint attrib_position, uint attrib_position,
uint attrib_normal, uint attrib_normal,
uint uniform_trans, uint uniform_trans,
uint uniform_normal_trans, uint uniform_normal_trans,
uint uniform_base_color, uint uniform_base_color,
uint uniform_light_pos, uint uniform_light_pos,
int length); float paddle_x);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -12,6 +12,7 @@
#include "render.hpp" #include "render.hpp"
#include "model/brick.h" #include "model/brick.h"
#include "model/paddle.h"
static int vp_width = 1200; static int vp_width = 1200;
static int vp_height = 1200; static int vp_height = 1200;
@ -23,12 +24,14 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
vp_height = height; vp_height = height;
} }
/*
static const float triangle_vertex_buffer_data[] = { static const float triangle_vertex_buffer_data[] = {
// position // color // position // color
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
}; };
*/
int main() int main()
{ {
@ -71,8 +74,15 @@ int main()
(sizeof (triangle_vertex_buffer_data))); (sizeof (triangle_vertex_buffer_data)));
*/ */
uint vtx = make_buffer(GL_ARRAY_BUFFER, brick_vertices, (sizeof (brick_vertices))); struct mesh paddle_mesh;
uint idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, brick_Cube_triangles, (sizeof (brick_Cube_triangles))); struct mesh brick_mesh;
brick_mesh.vtx = make_buffer(GL_ARRAY_BUFFER, brick_vertices, (sizeof (brick_vertices)));
brick_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, brick_Cube_triangles, (sizeof (brick_Cube_triangles)));
brick_mesh.length = brick_Cube_triangles_length;
paddle_mesh.vtx = make_buffer(GL_ARRAY_BUFFER, paddle_vertices, (sizeof (paddle_vertices)));
paddle_mesh.idx = make_buffer(GL_ELEMENT_ARRAY_BUFFER, paddle_Cylinder_triangles, (sizeof (paddle_Cylinder_triangles)));
paddle_mesh.length = paddle_Cylinder_triangles_length;
uint program = compile_shader(src_shader_vertex_color_vp_glsl_start, uint program = compile_shader(src_shader_vertex_color_vp_glsl_start,
src_shader_vertex_color_vp_glsl_size, src_shader_vertex_color_vp_glsl_size,
@ -94,6 +104,10 @@ int main()
const double first_frame = glfwGetTime(); const double first_frame = glfwGetTime();
double last_frame = first_frame; double last_frame = first_frame;
double frames = 0; double frames = 0;
const char * last_gamepad_name = NULL;
float paddle_x = 0.0;
while(!glfwWindowShouldClose(window)) { while(!glfwWindowShouldClose(window)) {
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
@ -104,14 +118,41 @@ int main()
glClearColor(0.1, 0.2, 0.3, 1.0); glClearColor(0.1, 0.2, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render(vtx, idx, float paddle_dx = 0.0;
for (int i = 0; i < 16; i++) {
int present = glfwJoystickPresent(GLFW_JOYSTICK_1 + i);
int is_gamepad = glfwJoystickIsGamepad(GLFW_JOYSTICK_1 + i);
int count;
const float * axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1 + i, &count);
//printf("present, %d %d %s %d\n", i, count, name, is_gamepad);
if (present && is_gamepad && count == 6) {
const char * name = glfwGetJoystickName(GLFW_JOYSTICK_1 + i);
if (name != last_gamepad_name) {
printf("active gamepad: `%s`; axes: %d\n", name, count);
last_gamepad_name = name;
}
float left = axes[2] * 0.5 + 0.5;
float right = axes[5] * 0.5 + 0.5;
paddle_dx = right - left;
break;
}
}
paddle_x += paddle_dx * 0.4;
if (paddle_x < 0.5)
paddle_x = 0.5;
if (paddle_x > 11.5)
paddle_x = 11.5;
render(paddle_mesh,
brick_mesh,
attrib_position, attrib_position,
attrib_normal, attrib_normal,
uniform_trans, uniform_trans,
uniform_normal_trans, uniform_normal_trans,
uniform_base_color, uniform_base_color,
uniform_light_pos, uniform_light_pos,
brick_Cube_triangles_length); paddle_x);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();

View File

@ -58,21 +58,27 @@ mat4x4 perspective(float low1, float high1,
levels are 13x28 levels are 13x28
*/ */
void render(uint vtx, uint idx, void render(mesh paddle_mesh,
mesh brick_mesh,
uint attrib_position, uint attrib_position,
uint attrib_normal, uint attrib_normal,
uint uniform_trans, uint uniform_trans,
uint uniform_normal_trans, uint uniform_normal_trans,
uint uniform_base_color, uint uniform_base_color,
uint uniform_light_pos, uint uniform_light_pos,
int length) float paddle_x)
{ {
static float theta = 0; static float theta = 0;
theta += 0.01; theta += 0.01;
glBindBuffer(GL_ARRAY_BUFFER, vtx); //////////////////////////////////////////////////////////////////////
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, idx); // render bricks
//////////////////////////////////////////////////////////////////////
glBindBuffer(GL_ARRAY_BUFFER, brick_mesh.vtx);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, brick_mesh.idx);
glVertexAttribPointer(attrib_position, glVertexAttribPointer(attrib_position,
3, 3,
GL_FLOAT, GL_FLOAT,
@ -103,6 +109,8 @@ void render(uint vtx, uint idx,
const uint8_t * level = (const uint8_t *)src_level_level1_data_start; const uint8_t * level = (const uint8_t *)src_level_level1_data_start;
const uint8_t * pal = (const uint8_t *)src_level_level1_data_pal_start; const uint8_t * pal = (const uint8_t *)src_level_level1_data_pal_start;
vec3 light_pos = normalize(rotate_z(theta) * vec3(1, 1, 1));
for (int y = 0; y < 28; y++) { for (int y = 0; y < 28; y++) {
for (int x = 0; x < 13; x++) { for (int x = 0; x < 13; x++) {
char tile = level[y * 13 + x]; char tile = level[y * 13 + x];
@ -130,15 +138,69 @@ void render(uint vtx, uint idx,
//mat3x3 normal_trans = transpose(inverse(submatrix(trans, 0, 0))); //mat3x3 normal_trans = transpose(inverse(submatrix(trans, 0, 0)));
mat3x3 normal_trans = submatrix(rx, 3, 3); mat3x3 normal_trans = submatrix(rx, 3, 3);
vec3 light_pos = normalize(rotate_z(theta) * vec3(1, 1, 1)); glUniform4fv(uniform_trans, 4, &trans[0][0]);
glUniform3fv(uniform_normal_trans, 3, &normal_trans[0][0]);
glUniform3fv(uniform_base_color, 1, &base_color[0]);
glUniform3fv(uniform_light_pos, 1, &light_pos[0]);
glDrawElements(GL_TRIANGLES, brick_mesh.length, GL_UNSIGNED_INT, 0);
}
}
//////////////////////////////////////////////////////////////////////
// render paddle
//////////////////////////////////////////////////////////////////////
{
mat4x4 rx = rotate_y(PI / 2.0f);
mat4x4 s = scale(vec3(1.0f,
1.3f,
1.5f));
float px = ((float)paddle_x / 13.0) * 2.0 - 1.0 + 1.0f / 13.0f;
float py = ((float)26 / 28.0) * -2.0 + 1.0 - 1.0f / 28.0f;
mat4x4 t = translate(vec3(px, py, 0.0));
mat4x4 trans = t * rx * s;
mat3x3 normal_trans = submatrix(rx, 3, 3);
//vec3 base_color = vec3(1, 1, 1);
vec3 base_color = vec3(1, 1, 1) * 0.5f;
//vec3 light_pos = vec3(-1, -1, 1);
glBindBuffer(GL_ARRAY_BUFFER, paddle_mesh.vtx);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, paddle_mesh.idx);
glVertexAttribPointer(attrib_position,
3,
GL_FLOAT,
GL_FALSE,
(sizeof (float)) * 8,
(void*)(0 * 4)
);
/*
glVertexAttribPointer(vertex_color_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_normal);
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]);
glUniform3fv(uniform_base_color, 1, &base_color[0]); glUniform3fv(uniform_base_color, 1, &base_color[0]);
glUniform3fv(uniform_light_pos, 1, &light_pos[0]); glUniform3fv(uniform_light_pos, 1, &light_pos[0]);
//glDrawArrays(GL_TRIANGLES, 0, 3); glDrawElements(GL_TRIANGLES, paddle_mesh.length, GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, length, GL_UNSIGNED_INT, 0);
}
} }
} }

View File

@ -17,6 +17,7 @@ void main()
vec3 color = (diffuse + 0.5) * base_color; vec3 color = (diffuse + 0.5) * base_color;
gl_FragColor = vec4(color, 1.0); gl_FragColor = vec4(color, 1.0);
//gl_FragColor = vec4(fp_normal * 0.5 + 0.5, 1.0);
} }
// normal // normal
// x (0.0 left 1.0 right) // x (0.0 left 1.0 right)