diff options
Diffstat (limited to 'source/main.cpp')
-rw-r--r-- | source/main.cpp | 850 |
1 files changed, 196 insertions, 654 deletions
diff --git a/source/main.cpp b/source/main.cpp index b4b9e2c..a9e5c81 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,4 +1,5 @@ #include <stdio.h> +#include <stdlib.h> #include <SDL2/SDL.h> #include <glad/glad.h> #include <assimp/Importer.hpp> @@ -24,6 +25,12 @@ * that ofcourse everything is actually happening from the cameras' perspective. I would still love to figure this out. */ +/* + * @note: + * Model loading is busted and is missing a lot of things, not that it matters for now, + * but it will be useful for the future + */ + typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; @@ -317,27 +324,92 @@ class Mesh { glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); glBindVertexArray(0); } + + void draw_instanced(u32 shader_program, u32 instance_count) + { + glUseProgram(shader_program); + + u32 diffuse_num = 1; + u32 specular_num = 1; + char tex_unit_name[64]; + // set shininess + s32 mat_shine_loc = glGetUniformLocation(shader_program, "material.shininess"); + glUniform1f(mat_shine_loc, 32.0f); + + for (u32 i=0; i<textures.size(); i++) + { + struct Texture curr_tex = textures[i]; + if (curr_tex.type == TextureDiffuse) + { + sprintf(tex_unit_name, "material.diffuse[%i]", diffuse_num); + } + else if (curr_tex.type == TextureSpecular) + { + sprintf(tex_unit_name, "material.diffuse[%i]", specular_num); + } + + glActiveTexture(GL_TEXTURE0 + i); + s32 tex_unit_loc = glGetUniformLocation(shader_program, tex_unit_name); + glUniform1i(tex_unit_loc, i); + glBindTexture(GL_TEXTURE_2D, curr_tex.id); + } + glActiveTexture(GL_TEXTURE0); + + glBindVertexArray(vao); + glDrawElementsInstanced(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0, instance_count); + glBindVertexArray(0); + } }; class Model { public: + std::vector<Texture> loaded_textures; + std::vector<Mesh> meshes; + std::string directory; + Model(std::string path) { load_model(path); } + void instance_mesh(); void draw(u32 shader_program); + void draw_instanced(u32 shader_program, u32 instance_count); private: - std::vector<Texture> loaded_textures; - std::vector<Mesh> meshes; - std::string directory; - void load_model(std::string path); void process_node(aiNode *node, const aiScene *scene); Mesh process_mesh(aiMesh *mesh, const aiScene *scene); std::vector<Texture> load_material_textures(aiMaterial *mat, aiTextureType type, TextureType type_name); }; +void Model::instance_mesh() +{ + for (u32 i=0; i < meshes.size(); i++) + { + Mesh curr_mesh = meshes[i]; + glBindVertexArray(curr_mesh.vao); + glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 16 * sizeof(u32), (void*)0); + glEnableVertexAttribArray(3); + + glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, 16 * sizeof(u32), (void*)(4*sizeof(u32))); + glEnableVertexAttribArray(4); + + glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 16 * sizeof(u32), (void*)(8*sizeof(u32))); + glEnableVertexAttribArray(5); + + glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, 16 * sizeof(u32), (void*)(12*sizeof(u32))); + glEnableVertexAttribArray(6); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glVertexAttribDivisor(3, 1); + glVertexAttribDivisor(4, 1); + glVertexAttribDivisor(5, 1); + glVertexAttribDivisor(6, 1); + + glBindVertexArray(0); + } +} + void Model::draw(u32 shader_program) { for (int i=0; i < meshes.size(); i++) @@ -346,6 +418,14 @@ void Model::draw(u32 shader_program) } } +void Model::draw_instanced(u32 shader_program, u32 instance_count) +{ + for (int i=0; i < meshes.size(); i++) + { + meshes[i].draw_instanced(shader_program, instance_count); + } +} + void Model::load_model(std::string path) { Assimp::Importer import; @@ -551,370 +631,96 @@ int main(int argc, char* argv[]) printf("Failed to initialize Glad\n"); return 1; } + + // vsync controls: 0 = OFF | 1 = ON (Default) + // SDL_GL_SetSwapInterval(0); // filesystem playground stuff size_t read_count; - char* vertex_source = (char*)SDL_LoadFile("./source/shaders/depth_test.vs.glsl", &read_count); - char* fragment_source = (char*)SDL_LoadFile("./source/shaders/depth_test.fs.glsl", &read_count); - char* blend_shader_source = (char*)SDL_LoadFile("./source/shaders/blend_test.fs.glsl", &read_count); - char* fbo_vs = (char*)SDL_LoadFile("./source/shaders/fbo.vs.glsl", &read_count); - char* fbo_fs = (char*)SDL_LoadFile("./source/shaders/fbo.fs.glsl", &read_count); - char* cubemap_vs = (char*)SDL_LoadFile("./source/shaders/cubemap.vs.glsl", &read_count); - char* cubemap_fs = (char*)SDL_LoadFile("./source/shaders/cubemap.fs.glsl", &read_count); - char* refl_vs = (char*)SDL_LoadFile("./source/shaders/refl.vs.glsl", &read_count); - char* refl_fs = (char*)SDL_LoadFile("./source/shaders/refl.fs.glsl", &read_count); - char* refr_vs = (char*)SDL_LoadFile("./source/shaders/refr.vs.glsl", &read_count); - char* refr_fs = (char*)SDL_LoadFile("./source/shaders/refr.fs.glsl", &read_count); + char* vertex_source = (char*)SDL_LoadFile("./source/shaders/model.vs.glsl", &read_count); + char* inst_vertex_source = (char*)SDL_LoadFile("./source/shaders/instanced_model.vs.glsl", &read_count); + char* fragment_source = (char*)SDL_LoadFile("./source/shaders/model.fs.glsl", &read_count); u32 shader_program = gl_shader_program(vertex_source, fragment_source); - u32 blend_shader_program = gl_shader_program(vertex_source, blend_shader_source); - u32 fbo_shader_program = gl_shader_program(fbo_vs, fbo_fs); - u32 cubemap_shader_program = gl_shader_program(cubemap_vs, cubemap_fs); - u32 refl_shader_program = gl_shader_program(refl_vs, refl_fs); - u32 refr_shader_program = gl_shader_program(refr_vs, refr_fs); - - printf("Successfully compiled shaders.\n"); + u32 inst_shader_program = gl_shader_program(inst_vertex_source, fragment_source); - float cubeVertices[] = { - // Back face - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // Bottom-left - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // bottom-right - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // bottom-left - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left - // Front face - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-right - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // top-right - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // top-right - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, // top-left - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left - // Left face - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-right - -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-left - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-right - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-right - // Right face - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-left - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-left - 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left - // Bottom face - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right - 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, // top-left - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-left - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-left - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-right - -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right - // Top face - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // bottom-right - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // bottom-right - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f // bottom-left - }; + stbi_set_flip_vertically_on_load(1); - float planeVertices[] = { + Model planet_model = Model(std::string("./assets/planet/planet.obj")); + Model rock_model = Model(std::string("assets/rock/rock.obj")); + + float inst_vertices[] = { // positions // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat) // BottomFace - -5.0f, -1.0f, -5.0f, 0.0f, 2.0f, // top-right - 5.0f, -1.0f, -5.0f, 2.0f, 2.0f, // top-left - 5.0f, -1.0f, 5.0f, 2.0f, 0.0f, // bottom-left - 5.0f, -1.0f, 5.0f, 2.0f, 0.0f, // bottom-left - -5.0f, -1.0f, 5.0f, 0.0f, 0.0f, // bottom-right - -5.0f, -1.0f, -5.0f, 0.0f, 2.0f, // top-right + -0.5f, -1.0f, -0.5f, // top-right + 0.5f, -1.0f, -0.5f, // top-left + 0.5f, -1.0f, 0.5f, // bottom-left + 0.5f, -1.0f, 0.5f, // bottom-left + -0.5f, -1.0f, 0.5f, // bottom-right + -0.5f, -1.0f, -0.5f, // top-right // Top face - -5.0f, -0.5f, -5.0f, 0.0f, 2.0f, // top-left - 5.0f, -0.5f, 5.0f, 2.0f, 0.0f, // bottom-right - 5.0f, -0.5f, -5.0f, 2.0f, 2.0f, // top-right - 5.0f, -0.5f, 5.0f, 2.0f, 0.0f, // bottom-right - -5.0f, -0.5f, -5.0f, 0.0f, 2.0f, // top-left - -5.0f, -0.5f, 5.0f, 0.0f, 0.0f // bottom-left - }; - - float quadVertices[] = { - // Front face - -0.5f, -0.5f, 0.0f, 0.0f, // bottom-left - 0.5f, -0.5f, 1.0f, 0.0f, // bottom-right - 0.5f, 0.5f, 1.0f, 1.0f, // top-right - 0.5f, 0.5f, 1.0f, 1.0f, // top-right - -0.5f, 0.5f, 0.0f, 1.0f, // top-left - -0.5f, -0.5f, 0.0f, 0.0f, // bottom-left - // Back face - -0.5f, -0.5f, 0.0f, 0.0f, // Bottom-left - 0.5f, 0.5f, 1.0f, 1.0f, // top-right - 0.5f, -0.5f, 1.0f, 0.0f, // bottom-right - 0.5f, 0.5f, 1.0f, 1.0f, // top-right - -0.5f, -0.5f, 0.0f, 0.0f, // bottom-left - -0.5f, 0.5f, 0.0f, 1.0f, // top-left - }; - - float fboVertices[] = { - -1.0f, -1.0f, 0.0f, 0.0f, // bottom-left - 1.0f, -1.0f, 1.0f, 0.0f, // bottom-right - 1.0f, 1.0f, 1.0f, 1.0f, // top-right - 1.0f, 1.0f, 1.0f, 1.0f, // top-right - -1.0f, 1.0f, 0.0f, 1.0f, // top-left - -1.0f, -1.0f, 0.0f, 0.0f, // bottom-left - }; - - float skyboxVertices[] = { - // positions - -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, - - -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 - }; - float reflVertices[] = { - // positions // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat) - // Top face - //-5.0f, 0.5f, -5.0f, 0.0f, 1.0f, 0.0f,// top-left - // 5.0f, 0.5f, 5.0f, 0.0f, 1.0f, 0.0f,// bottom-right - // 5.0f, 0.5f, -5.0f, 0.0f, 1.0f, 0.0f,// top-right - // 5.0f, 0.5f, 5.0f, 0.0f, 1.0f, 0.0f,// bottom-right - //-5.0f, 0.5f, -5.0f, 0.0f, 1.0f, 0.0f,// top-left - //-5.0f, 0.5f, 5.0f, 0.0f, 1.0f, 0.0f,// bottom-left -#if REFLECTIVE_PLANE - // Back face - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // Bottom-left - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // top-right - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // bottom-right - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // top-right - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // bottom-left - -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, // top-left - // Front face - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // bottom-left - 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // bottom-right - 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // top-right - 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // top-right - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // top-left - -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // bottom-left - // Left face - -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, // top-right - -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, // top-left - -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, // bottom-left - -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, // bottom-left - -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, // bottom-right - -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, // top-right - // Right face - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // top-left - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // bottom-right - 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // top-right - 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // bottom-right - 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // top-left - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // bottom-left - // Bottom face - -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, // top-right - 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, // top-left - 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, // bottom-left - 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, // bottom-left - -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, // bottom-right - -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, // top-right -#endif - // Top face - -4.0f, 0.0f, -4.0f, 0.0f, 1.0f, 0.0f, // top-left - 4.0f, 0.0f, 4.0f, 0.0f, 1.0f, 0.0f, // bottom-right - 4.0f, 0.0f, -4.0f, 0.0f, 1.0f, 0.0f, // top-right - 4.0f, 0.0f, 4.0f, 0.0f, 1.0f, 0.0f, // bottom-right - -4.0f, 0.0f, -4.0f, 0.0f, 1.0f, 0.0f, // top-left - -4.0f, 0.0f, 4.0f, 0.0f, 1.0f, 0.0f // bottom-left - }; - - float refrVertices[] = { - // Left face - -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, // top-right - -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, // top-left - -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, // bottom-left - -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, // bottom-left - -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, // bottom-right - -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, // top-right - // Right face - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // top-left - -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // bottom-right - -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // top-right - -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, // bottom-right - -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // top-left - -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // bottom-left + -0.5f, -0.9f, -0.5f, // top-left + 0.5f, -0.9f, 0.5f, // bottom-right + 0.5f, -0.9f, -0.5f, // top-right + 0.5f, -0.9f, 0.5f, // bottom-right + -0.5f, -0.9f, -0.5f, // top-left + -0.5f, -0.9f, 0.5f // bottom-left }; - - stbi_set_flip_vertically_on_load(1); - - u32 cube_vao, cube_vbo, plane_vao, plane_vbo, quad_vao, quad_vbo, fbo_vao, fbo_vbo, - skybox_vao, skybox_vbo, refl_vao, refl_vbo, refr_vao, refr_vbo; - - glGenVertexArrays(1, &cube_vao); - glGenBuffers(1, &cube_vbo); - - glBindVertexArray(cube_vao); - glBindBuffer(GL_ARRAY_BUFFER, cube_vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float))); - glBindVertexArray(0); - - glGenVertexArrays(1, &plane_vao); - glGenBuffers(1, &plane_vbo); - - glBindVertexArray(plane_vao); - glBindBuffer(GL_ARRAY_BUFFER, plane_vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), &planeVertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float))); - glBindVertexArray(0); - - glGenVertexArrays(1, &quad_vao); - glGenBuffers(1, &quad_vbo); - - glBindVertexArray(quad_vao); - glBindBuffer(GL_ARRAY_BUFFER, quad_vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2*sizeof(float))); - glBindVertexArray(0); - - // framebuffer objects - glGenVertexArrays(1, &fbo_vao); - glGenBuffers(1, &fbo_vbo); - - glBindVertexArray(fbo_vao); - glBindBuffer(GL_ARRAY_BUFFER, fbo_vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(fboVertices), &fboVertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2*sizeof(float))); - glBindVertexArray(0); - - // skybox objects - glGenVertexArrays(1, &skybox_vao); - glGenBuffers(1, &skybox_vbo); - - glBindVertexArray(skybox_vao); - glBindBuffer(GL_ARRAY_BUFFER, skybox_vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); - glBindVertexArray(0); - - // reflective surface objects - glGenVertexArrays(1, &refl_vao); - glGenBuffers(1, &refl_vbo); - - glBindVertexArray(refl_vao); - glBindBuffer(GL_ARRAY_BUFFER, refl_vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(reflVertices), &reflVertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float))); - glBindVertexArray(0); - - // refractive surface objects - glGenVertexArrays(1, &refr_vao); - glGenBuffers(1, &refr_vbo); - - glBindVertexArray(refr_vao); - glBindBuffer(GL_ARRAY_BUFFER, refr_vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(refrVertices), &refrVertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float))); - glBindVertexArray(0); - - u32 window_tex_id; - glGenTextures(1, &window_tex_id); - glActiveTexture(GL_TEXTURE2); + const u32 instance_count = 100000; + Mat4 *offsets = (Mat4 *)calloc(instance_count, sizeof(Mat4)); + u32 amount = instance_count; + r32 offset = 7.0f; + r32 radius = 50.0f; + srand(SDL_GetTicks64()); + for (u32 i = 0; i < amount; i++) { - char *path = "assets/window.png"; - s32 _width, _height, nrChannels; - unsigned char *data = stbi_load(path, &_width, &_height, &nrChannels, 0); - if (data) - { - GLenum format; - if (nrChannels == 1) - format = GL_RED; - else if (nrChannels == 3) - format = GL_RGB; - else if (nrChannels == 4) - format = GL_RGBA; - - glBindTexture(GL_TEXTURE_2D, window_tex_id); - glTexImage2D(GL_TEXTURE_2D, 0, format, _width, _height, 0, format, GL_UNSIGNED_BYTE, data); - glGenerateMipmap(GL_TEXTURE_2D); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - stbi_image_free(data); - } - else - { - printf("failed to load image texture at path: %s", path); - stbi_image_free(data); - } + Mat4 model = init_value4m(1.0f); + + // 1. translation + u32 angle = (i*360)/amount; + r32 displacement = (rand()%(u32)(2 * offset * 100)) / 100.0f - offset; + r32 x = sin(angle) * radius + displacement; + displacement = (rand()%(u32)(2 * offset * 100)) / 100.0f - offset; + r32 y = displacement * 1.5f; + displacement = (rand()%(u32)(2 * offset * 100)) / 100.0f - offset; + r32 z = cos(angle) * radius + displacement; + Mat4 translation = translation_matrix4m(x, y, z); + + // 2. scale + r32 rnd_scale = (rand()%20) / 100.0f + 0.05; + Mat4 scale = scaling_matrix4m(rnd_scale, rnd_scale, rnd_scale); + + // 3. rotation + r32 rot_angle = To_Radian(rand()%360); + Mat4 rotation = rotation_matrix4m(rot_angle, Vec3{0.4f, 0.6f, 0.8f}); + + model = multiply4m(rotation, model); + model = multiply4m(scale, model); + model = multiply4m(translation, model); + + offsets[i] = to_col_major4m(model); } + + u32 instance_vbo; + glGenBuffers(1, &instance_vbo); + glBindBuffer(GL_ARRAY_BUFFER, instance_vbo); + +#if 0 + u32 offset_sz = offsets.size(); + Mat4 *offset_ptr = offsets.data(); +#endif + u32 offset_sz = instance_count * sizeof(Mat4); + Mat4 *offset_ptr = &offsets[0]; + glBufferData(GL_ARRAY_BUFFER, offset_sz, offset_ptr, GL_STATIC_DRAW); - u32 cube_tex_id; - glGenTextures(1, &cube_tex_id); - glActiveTexture(GL_TEXTURE0); - gl_load_texture(cube_tex_id, "assets/container.jpg"); + rock_model.instance_mesh(); - u32 plane_tex_id; - glGenTextures(1, &plane_tex_id); - glActiveTexture(GL_TEXTURE1); - gl_load_texture(plane_tex_id, "assets/smiling.png"); + // uniform buffer objects + u32 ubo_camera_block; + glGenBuffers(1, &ubo_camera_block); + glBindBuffer(GL_UNIFORM_BUFFER, ubo_camera_block); + glBufferData(GL_UNIFORM_BUFFER, 128, NULL, GL_STATIC_DRAW); + glBindBuffer(GL_UNIFORM_BUFFER, 0); // objects Vec3 model_translations[] = { @@ -927,7 +733,7 @@ int main(int argc, char* argv[]) Vec3{ -1.0, 0.0, -8.0}, // 6: reflective plane Vec3{ -1.0, 2.0, -8.0}, // 6: refractive "window" }; - + r32 FOV = 90.0; r32 time_curr; r32 time_prev = SDL_GetTicks64() / 100.0; @@ -943,128 +749,29 @@ int main(int argc, char* argv[]) // @todo: remove this, I dont like this and think that this is unnecessary Vec3 camera_look_increment; - r32 camera_speed = 0.5f; + r32 camera_speed = 2.5f; Mat4 view = camera_create4m(camera_pos, camera_look, preset_up_dir); - Mat4 proj = perspective4m((r32)To_Radian(90.0), (r32)width / (r32)height, 0.1f, 100.0f); - // needs this - glUseProgram(shader_program); - uint32_t proj_loc = glGetUniformLocation(shader_program, "Projection"); - glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer); - - glUseProgram(blend_shader_program); - proj_loc = glGetUniformLocation(blend_shader_program, "Projection"); - glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer); - - glUseProgram(cubemap_shader_program); - proj_loc = glGetUniformLocation(cubemap_shader_program, "Projection"); - glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer); - - glUseProgram(refl_shader_program); - proj_loc = glGetUniformLocation(refl_shader_program, "Projection"); - glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer); - - glUseProgram(refr_shader_program); - proj_loc = glGetUniformLocation(refr_shader_program, "Projection"); - glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer); + Mat4 proj = perspective4m((r32)To_Radian(90.0), (r32)width / (r32)height, 0.1f, 1000.0f); + + u32 block_binding = 0; + u32 matrices_ind; + matrices_ind = glGetUniformBlockIndex(shader_program, "Matrices"); + glUniformBlockBinding(shader_program, matrices_ind, block_binding); + matrices_ind = glGetUniformBlockIndex(inst_shader_program, "Matrices"); + glUniformBlockBinding(inst_shader_program, matrices_ind, block_binding); + // or glBindBufferBase(); + glBindBufferRange(GL_UNIFORM_BUFFER, block_binding, ubo_camera_block, 0, 128); + + Mat4 col_major_proj = to_col_major4m(proj); + glBindBuffer(GL_UNIFORM_BUFFER, ubo_camera_block); + glBufferSubData(GL_UNIFORM_BUFFER, 64, 64, col_major_proj.buffer); + glBindBuffer(GL_UNIFORM_BUFFER, 0); glEnable(GL_DEPTH_TEST); - glEnable(GL_BLEND); glEnable(GL_CULL_FACE); - glDepthFunc(GL_LESS); glCullFace(GL_BACK); - //glFrontFace(GL_CW); // front-faces are clock-wise, this will make the back-faces visible (since the arrays used were - // setup as counter-clockwise) - u32 fbo; - glGenFramebuffers(1, &fbo); - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - - // create a texture for the framebuffer to render data to - u32 fbo_tex; - glGenTextures(1, &fbo_tex); - glActiveTexture(GL_TEXTURE3); - glBindTexture(GL_TEXTURE_2D, fbo_tex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glBindTexture(GL_TEXTURE_2D, 0); - - u32 rbo; - glGenRenderbuffers(1, &rbo); - glBindRenderbuffer(GL_RENDERBUFFER, rbo); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); - glBindRenderbuffer(GL_RENDERBUFFER, 0); - - // attach to current bound framebuffer object - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo_tex, 0); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); - - if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { - printf("Error! Framebuffer is not completely setup\n"); - return -1; - } - glBindFramebuffer(GL_FRAMEBUFFER, 0); - - // generate_cubemap - const char *cm_face_right = "assets/skybox/right.jpg"; - const char *cm_face_left = "assets/skybox/left.jpg"; - const char *cm_face_top = "assets/skybox/top.jpg"; - const char *cm_face_bottom = "assets/skybox/bottom.jpg"; - const char *cm_face_back = "assets/skybox/back.jpg"; - const char *cm_face_front = "assets/skybox/front.jpg"; - - u32 cm_tex_id; - { - stbi_set_flip_vertically_on_load(0); - glGenTextures(1, &cm_tex_id); - glBindTexture(GL_TEXTURE_CUBE_MAP, cm_tex_id); - int cm_width, cm_height, cm_nrChannels; - unsigned char *data; - // right face - data = stbi_load(cm_face_right, &cm_width, &cm_height, &cm_nrChannels, 0); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 0, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); - // left face - data = stbi_load(cm_face_left, &cm_width, &cm_height, &cm_nrChannels, 0); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 1, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); - // top face - data = stbi_load(cm_face_top, &cm_width, &cm_height, &cm_nrChannels, 0); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 2, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); - // bottom face - data = stbi_load(cm_face_bottom, &cm_width, &cm_height, &cm_nrChannels, 0); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 3, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); - // back face - data = stbi_load(cm_face_back, &cm_width, &cm_height, &cm_nrChannels, 0); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 4, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); - // front face - data = stbi_load(cm_face_front, &cm_width, &cm_height, &cm_nrChannels, 0); - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 5, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); - - // texture filtering methods - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - - glUseProgram(cubemap_shader_program); - glActiveTexture(GL_TEXTURE0); - s32 skybox_loc = glGetUniformLocation(cubemap_shader_program, "skybox"); - glUniform1i(skybox_loc, 0); - stbi_set_flip_vertically_on_load(1); - } - - { - glUseProgram(refl_shader_program); - s32 skybox_loc = glGetUniformLocation(refl_shader_program, "skybox"); - glUniform1i(skybox_loc, 0); - } - - { - glUseProgram(refr_shader_program); - s32 skybox_loc = glGetUniformLocation(refr_shader_program, "skybox"); - glUniform1i(skybox_loc, 0); - } u8 game_running = true; @@ -1185,205 +892,40 @@ int main(int argc, char* argv[]) view = camera_create4m(camera_pos, add3v(camera_pos, camera_look), preset_up_dir); // object shader program stuff - glUseProgram(shader_program); - uint32_t view_loc = glGetUniformLocation(shader_program, "View"); - glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer); + Mat4 col_major_view = to_col_major4m(view); + glBindBuffer(GL_UNIFORM_BUFFER, ubo_camera_block); + glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, col_major_view.buffer); + glBindBuffer(GL_UNIFORM_BUFFER, 0); - glUseProgram(blend_shader_program); - view_loc = glGetUniformLocation(blend_shader_program, "View"); - glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer); - - glUseProgram(refl_shader_program); - view_loc = glGetUniformLocation(refl_shader_program, "View"); - glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer); - - glUseProgram(refr_shader_program); - view_loc = glGetUniformLocation(refr_shader_program, "View"); - glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer); - - glUseProgram(cubemap_shader_program); - view_loc = glGetUniformLocation(cubemap_shader_program, "View"); - view.xyzw[0].w = 0.0f; - view.xyzw[1].w = 0.0f; - view.xyzw[2].w = 0.0f; - glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer); - time_prev = time_curr; // OUTPUT - glBindFramebuffer(GL_FRAMEBUFFER, fbo); glClearColor(1.0f, 0.6f, .6f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glEnable(GL_DEPTH_TEST); - glDisable(GL_BLEND); - { //plane - glUseProgram(shader_program); - // @note: this is just a test, I dont need to do this here; - s32 tex_id_loc = glGetUniformLocation(shader_program, "TexId"); - glUniform1i(tex_id_loc, 1); - Vec3 translation_iter = model_translations[1]; - Mat4 model = init_value4m(1.0); - Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); - model = multiply4m(model_translation, model); - uint32_t model_loc = glGetUniformLocation(shader_program, "Model"); - glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); - glBindVertexArray(plane_vao); - glBindTexture(GL_TEXTURE_2D, plane_tex_id); - glDrawArrays(GL_TRIANGLES, 0, 12); - } - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - {//origin square - glUseProgram(shader_program); - glBindTexture(GL_TEXTURE_2D, cube_tex_id); - s32 tex_id_loc = glGetUniformLocation(shader_program, "TexId"); - glUniform1i(tex_id_loc, 0); - Vec3 translation_iter = model_translations[0]; - Mat4 model = init_value4m(1.0); - Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); - model = multiply4m(model_translation, model); - uint32_t model_loc = glGetUniformLocation(shader_program, "Model"); - glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); - glBindVertexArray(cube_vao); - glDrawArrays(GL_TRIANGLES, 0, 36); - } - {//square to the left - glUseProgram(shader_program); - glBindTexture(GL_TEXTURE_2D, cube_tex_id); - s32 tex_id_loc = glGetUniformLocation(shader_program, "TexId"); - glUniform1i(tex_id_loc, 0); - Vec3 translation_iter = model_translations[4]; - Mat4 model = init_value4m(1.0); - Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); - model = multiply4m(model_translation, model); - uint32_t model_loc = glGetUniformLocation(shader_program, "Model"); - glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); - glBindVertexArray(cube_vao); - glDrawArrays(GL_TRIANGLES, 0, 36); - } - {//random square behind window between squares + { glUseProgram(shader_program); - s32 tex_id_loc = glGetUniformLocation(shader_program, "TexId"); - glUniform1i(tex_id_loc, 0); - Vec3 translation_iter = model_translations[5]; - Mat4 model = init_value4m(1.0); - Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); - model = multiply4m(model_translation, model); - uint32_t model_loc = glGetUniformLocation(shader_program, "Model"); - glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); - glBindVertexArray(cube_vao); - glBindTexture(GL_TEXTURE_2D, cube_tex_id); - glDrawArrays(GL_TRIANGLES, 0, 36); - } - { // reflective plane - glUseProgram(refl_shader_program); - Vec3 translation_iter = model_translations[6]; - Mat4 model = init_value4m(1.0); - Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); - - model = multiply4m(model_translation, model); - uint32_t model_loc = glGetUniformLocation(refl_shader_program, "Model"); - glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); - - uint32_t camera_pos_loc = glGetUniformLocation(refl_shader_program, "cameraPos"); - glUniform3fv(camera_pos_loc, 1, camera_pos.data); - glBindVertexArray(refl_vao); - glDrawArrays(GL_TRIANGLES, 0, 6); - } - { // reflective plane - glUseProgram(refr_shader_program); - Vec3 translation_iter = model_translations[7]; - Mat4 model = init_value4m(1.0); - Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); - - model = multiply4m(model_translation, model); - uint32_t model_loc = glGetUniformLocation(refr_shader_program, "Model"); - glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); - - uint32_t camera_pos_loc = glGetUniformLocation(refr_shader_program, "cameraPos"); - glUniform3fv(camera_pos_loc, 1, camera_pos.data); - glBindVertexArray(refr_vao); - glDrawArrays(GL_TRIANGLES, 0, 12); - } - { - glDepthFunc(GL_LEQUAL); - glUseProgram(cubemap_shader_program); - glActiveTexture(GL_TEXTURE0); - glBindVertexArray(skybox_vao); - glBindTexture(GL_TEXTURE_CUBE_MAP, cm_tex_id); - glDrawArrays(GL_TRIANGLES, 0, 36); - glDepthFunc(GL_LESS); - } + Vec3 translation_iter = model_translations[0]; + Mat4 model = init_value4m(1.0); + Mat4 model_scale = scaling_matrix4m(5.0f, 5.0f, 5.0f); + model = multiply4m(model_scale, model); + Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); + model = multiply4m(model_translation, model); + u32 model_loc = glGetUniformLocation(shader_program, "Model"); + glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); + planet_model.draw(shader_program); + } + { + rock_model.draw_instanced(inst_shader_program, instance_count); + } - // @note: this is to demonstrate the limitations of blending with the depth buffer. - // Currently, this will show a weird behavior where since we render the transparent object - // closest to the camera first, the second window, further from the player will be discarded by the - // depth buffer and will be invisible. - // This is a limiation of transparent objects with the depth buffer. This does not happen with opaque objects. - // - // The "fix" if we can even call it that, is to render objects in order, furthest first and closest second, in layers. - - {//window infront of origin square - glUseProgram(blend_shader_program); - s32 tex_id_loc = glGetUniformLocation(blend_shader_program, "TexId"); - glUniform1i(tex_id_loc, 2); - Vec3 translation_iter = model_translations[3]; - Mat4 model = init_value4m(1.0); - Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); - model = multiply4m(model_translation, model); - uint32_t model_loc = glGetUniformLocation(blend_shader_program, "Model"); - glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); - glBindVertexArray(quad_vao); - glBindTexture(GL_TEXTURE_2D, window_tex_id); - glDrawArrays(GL_TRIANGLES, 0, 12); - } - {//window between squares - glUseProgram(blend_shader_program); - s32 tex_id_loc = glGetUniformLocation(blend_shader_program, "TexId"); - glUniform1i(tex_id_loc, 2); - Vec3 translation_iter = model_translations[2]; - Mat4 model = init_value4m(1.0); - Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); - model = multiply4m(model_translation, model); - uint32_t model_loc = glGetUniformLocation(blend_shader_program, "Model"); - glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); - glBindVertexArray(quad_vao); - glBindTexture(GL_TEXTURE_2D, window_tex_id); - glDrawArrays(GL_TRIANGLES, 0, 12); - } - glBindFramebuffer(GL_FRAMEBUFFER, 0); - // draw everything to a framebuffer object - glDisable(GL_DEPTH_TEST); - glClearColor(1.0f, 1.0f, 1.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - { - glUseProgram(fbo_shader_program); - glBindVertexArray(fbo_vao); - // @note: this glActiveTexture call was the bane of my existence for the day, this is done to check this particular - // thing regarding which texture unit the framebuffer object gets called to and it turns out, it needs a glActiveTexture - // call (atleast in my case) since I am rendering multiple textures which are bound to different texture units, - // primarily because of the different shaders I am using. - // - // So this is why I need to call this, but what I could also do is call glActiveTexture when creating the texture - // and it would have the same effect as this - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, fbo_tex); - s32 tex_id_loc = glGetUniformLocation(fbo_shader_program, "TexId"); - glUniform1i(tex_id_loc, 0); - glDrawArrays(GL_TRIANGLES, 0, 6); - } - glEnable(GL_DEPTH_TEST); SDL_GL_SwapWindow(window); } // opengl free calls - //glDeleteVertexArrays(1, &VAO); - //glDeleteBuffers(1, &VBO); + //glDeleteVertexArrays(1, &vao); + //glDeleteBuffers(1, &vbo); glDeleteProgram(shader_program); - glDeleteProgram(blend_shader_program); - glDeleteProgram(fbo_shader_program); // sdl free calls SDL_GL_DeleteContext(context); |