diff options
| author | 0scar <qgt268@alumni.ku.dk> | 2024-02-05 08:03:07 +0000 |
|---|---|---|
| committer | 0scar <qgt268@alumni.ku.dk> | 2024-02-05 12:02:19 +0000 |
| commit | 74674101bb45931748e084cc647cfb84f7609a6d (patch) | |
| tree | 6c06c84c8234194bdeb340f18ea6e18214151359 /src | |
| parent | d1fe7e56d12dea3a123d1ad610de4de88abcba13 (diff) | |
Rotating box example
Diffstat (limited to 'src')
| -rw-r--r-- | src/engine.c | 146 | ||||
| -rw-r--r-- | src/rendering.c | 10 |
2 files changed, 147 insertions, 9 deletions
diff --git a/src/engine.c b/src/engine.c index 98395db..cb74e4e 100644 --- a/src/engine.c +++ b/src/engine.c @@ -10,6 +10,8 @@ #undef GLFW_INCLUDE_NONE #include <GLFW/glfw3.h> +#include <cglm/cglm.h> + #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) /* include winapi */ #include <Windows.h> @@ -313,7 +315,6 @@ struct glfw_ctx initialize_GLFW( GladGLContext *ctx = create_context(window); //printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); - //ctx->Viewport(0, 0, 200, 200); if (ctx == NULL) { @@ -321,12 +322,17 @@ struct glfw_ctx initialize_GLFW( exit(EXIT_FAILURE); } + glfwSetFramebufferSizeCallback(window, window_size_callback); + #ifdef _DEBUG ctx->ClearColor((float)0x10 / 255.f, (float)0x0a / 255.f, (float)0x33 / 255.f, 0.f); #else ctx->ClearColor(0x0, 0x0, 0x0, 0.f); #endif + ctx->Enable(GL_DEPTH_TEST); + ctx->DepthFunc(GL_LESS); + return (struct glfw_ctx){.w = window, .c = ctx}; } @@ -392,21 +398,99 @@ Platform* engine_init(const char* windowtitle, v2_i32 windowsize, testobject->g_vertex_buffer_data[8] = 0.0f; static const float bufdata[] = { - -1.0f, -1.0f, 0.0f, - 1.0f, -1.0f, 0.0f, - 0.0f, 1.0f, 0.0f, + -1.0f,-1.0f,-1.0f, // triangle 1 : begin + -1.0f,-1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, // triangle 1 : end + 1.0f, 1.0f,-1.0f, // triangle 2 : begin + -1.0f,-1.0f,-1.0f, + -1.0f, 1.0f,-1.0f, // triangle 2 : end + 1.0f,-1.0f, 1.0f, + -1.0f,-1.0f,-1.0f, + 1.0f,-1.0f,-1.0f, + 1.0f, 1.0f,-1.0f, + 1.0f,-1.0f,-1.0f, + -1.0f,-1.0f,-1.0f, + -1.0f,-1.0f,-1.0f, + -1.0f, 1.0f, 1.0f, + -1.0f, 1.0f,-1.0f, + 1.0f,-1.0f, 1.0f, + -1.0f,-1.0f, 1.0f, + -1.0f,-1.0f,-1.0f, + -1.0f, 1.0f, 1.0f, + -1.0f,-1.0f, 1.0f, + 1.0f,-1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f,-1.0f,-1.0f, + 1.0f, 1.0f,-1.0f, + 1.0f,-1.0f,-1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f,-1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f,-1.0f, + -1.0f, 1.0f,-1.0f, + 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f,-1.0f, + -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + 1.0f,-1.0f, 1.0f }; - LOG("sizeof(bufdata) = %lu", sizeof(bufdata)); - LOG("sizeof(g_vertex_buffer_data) = %lu", sizeof(testobject->g_vertex_buffer_data)); + static const GLfloat g_color_buffer_data[] = { + 0.583f, 0.771f, 0.014f, + 0.609f, 0.115f, 0.436f, + 0.327f, 0.483f, 0.844f, + 0.822f, 0.569f, 0.201f, + 0.435f, 0.602f, 0.223f, + 0.310f, 0.747f, 0.185f, + 0.597f, 0.770f, 0.761f, + 0.559f, 0.436f, 0.730f, + 0.359f, 0.583f, 0.152f, + 0.483f, 0.596f, 0.789f, + 0.559f, 0.861f, 0.639f, + 0.195f, 0.548f, 0.859f, + 0.014f, 0.184f, 0.576f, + 0.771f, 0.328f, 0.970f, + 0.406f, 0.615f, 0.116f, + 0.676f, 0.977f, 0.133f, + 0.971f, 0.572f, 0.833f, + 0.140f, 0.616f, 0.489f, + 0.997f, 0.513f, 0.064f, + 0.945f, 0.719f, 0.592f, + 0.543f, 0.021f, 0.978f, + 0.279f, 0.317f, 0.505f, + 0.167f, 0.620f, 0.077f, + 0.347f, 0.857f, 0.137f, + 0.055f, 0.953f, 0.042f, + 0.714f, 0.505f, 0.345f, + 0.783f, 0.290f, 0.734f, + 0.722f, 0.645f, 0.174f, + 0.302f, 0.455f, 0.848f, + 0.225f, 0.587f, 0.040f, + 0.517f, 0.713f, 0.338f, + 0.053f, 0.959f, 0.120f, + 0.393f, 0.621f, 0.362f, + 0.673f, 0.211f, 0.457f, + 0.820f, 0.883f, 0.371f, + 0.982f, 0.099f, 0.879f +}; + +// LOG("sizeof(bufdata) = %lu", sizeof(bufdata)); +// LOG("sizeof(g_vertex_buffer_data) = %lu", sizeof(testobject->g_vertex_buffer_data)); +// // Generate 1 buffer, put the resulting identifier in vertexbuffer gl->GenBuffers(1, &(testobject->vbo)); // The following commands will talk about our 'vertexbuffer' buffer gl->BindBuffer(GL_ARRAY_BUFFER, testobject->vbo); // Give our vertices to OpenGL. - gl->BufferData(GL_ARRAY_BUFFER, sizeof(float) * 9, testobject->g_vertex_buffer_data, GL_STATIC_DRAW); + gl->BufferData(GL_ARRAY_BUFFER, sizeof(bufdata), bufdata, GL_STATIC_DRAW); + + // Same for the color buffer + gl->GenBuffers(1, &(testobject->col)); + gl->BindBuffer(GL_ARRAY_BUFFER, testobject->col); + gl->BufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); testobject->shaderprogram = LoadShaders(gl, "shader.vertexshader", "shader.fragmentshader"); INFO("Shaderprogram %d", testobject->shaderprogram); @@ -724,7 +808,36 @@ i32 engine_run(Platform* p, StateType initial_state) { //#endif render_begin(p->window); + gl->UseProgram(p->testobject->shaderprogram); + + { + vec3 cam = {4., 3., 3.}; // perspective + mat4 per; // perspective + mat4 v; // view + mat4 model = GLM_MAT4_IDENTITY_INIT; + mat4 modelviewprojection; + + f32 ratio = (float)p->window->windowsize.x / (float)p->window->windowsize.y; + //glm_perspective(45.f , 600.f / 400.f, 0.1, 100.0f, per); + glm_ortho(-10 * ratio, 10 * ratio, -10, 10, -10, 10, per); + + glm_lookat(cam, GLM_VEC3_ZERO, GLM_YUP, v); + + { mat4 t; + //modelviewprojection = p * v * model + glm_mat4_mul(v, model, t); + glm_rotate_at(t, (vec3){0,0,0}, glfwGetTime() / 2.f, (vec3){0,1,0}); //, (vec3)({0,1,0})); + glm_mat4_mul(per, t, modelviewprojection); + } + + // TODO: Do this only once during initialization + u32 matrix = gl->GetUniformLocation(p->testobject->shaderprogram, "MVP"); + + gl->UniformMatrix4fv(matrix, 1, GL_FALSE, &modelviewprojection[0][0]); + } + + gl->EnableVertexAttribArray(0); gl->BindBuffer(GL_ARRAY_BUFFER, p->testobject->vbo); gl->VertexAttribPointer( @@ -735,11 +848,28 @@ i32 engine_run(Platform* p, StateType initial_state) { 0, // stride (void*)0 // array buffer offset ); + + // Do the color buffer (?) + gl->EnableVertexAttribArray(1); + gl->BindBuffer(GL_ARRAY_BUFFER, p->testobject->col); + gl->VertexAttribPointer( + 1, // attribute. No particular reason for 1, but must match the layout in the shader. + 3, // size + GL_FLOAT, // type + GL_FALSE, // normalized? + 0, // stride + (void*)0 // array buffer offset + ); + + + + gl->UseProgram(p->testobject->shaderprogram); // Draw the triangle ! - gl->DrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle + gl->DrawArrays(GL_TRIANGLES, 0, 3*12); // Starting from vertex 0; 3 vertices total -> 1 triangle gl->DisableVertexAttribArray(0); + gl->DisableVertexAttribArray(1); render_present(p->window); // } diff --git a/src/rendering.c b/src/rendering.c index 06c14e8..dff3432 100644 --- a/src/rendering.c +++ b/src/rendering.c @@ -28,7 +28,7 @@ i32 drawcall_len = 0; void render_begin(Window* w) { //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glfwMakeContextCurrent(w->window); - w->context->Clear(GL_COLOR_BUFFER_BIT); + w->context->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } void render_present(Window* w) { @@ -69,6 +69,14 @@ void render_present(Window* w) { void drawcall_reset(void) { drawcall_len = 0; } +void window_size_callback(GLFWwindow* window, int width, int height) { + GLOBAL_PLATFORM->window->context->Viewport(0,0, width, height); + //*GLOBAL_PLATFORM->window->game_w = width; + //*GLOBAL_PLATFORM->window->game_h = height; + GLOBAL_PLATFORM->window->windowsize.x = width; + GLOBAL_PLATFORM->window->windowsize.y = height; +} + void engine_window_resize_pointers(i32* w, i32* h) { GLOBAL_PLATFORM->window->game_w = w; GLOBAL_PLATFORM->window->game_h = h; |
