#define _USE_MATH_DEFINES #include #include "dbc.hpp" #include #include #include #include #include #include "shader.hpp" #include #include #include #include #include "data.hpp" #include "physics.hpp" void framebuffer_size_callback(GLFWwindow *window, int width, int height); void processInput(GLFWwindow *window); const unsigned int SCR_WIDTH = 800; const unsigned int SCR_HEIGHT = 600; struct Config { Shader shader; unsigned int VAO = 0; unsigned int VBO = 0; unsigned int texture1 = 0; unsigned int texture2 = 0; }; void init_glfw() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); } void framebuffer_size_callback(GLFWwindow *, int width, int height) { glViewport(0, 0, width, height); } GLFWwindow* create_window() { GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); dbc::check(window != NULL, "failed to open window"); glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); auto good = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); dbc::check(good, "failed to load GLAD"); return window; } void processInput(GLFWwindow *window, BoxTest &box) { if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { glfwSetWindowShouldClose(window, true); } if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { b2Vec2 force(0.0f, 0.5f); for(size_t i = 0; i < BODY_COUNT; i++) { box.bodies[i]->ApplyForceToCenter(force, true); box.bodies[i]->ApplyTorque(0.1f, true); } } } unsigned int load_texture(const std::string& image_file) { unsigned int texture_id; glGenTextures(1, &texture_id); glBindTexture(GL_TEXTURE_2D, texture_id); // set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); int width = 0; int height = 0; int nrChannels = 0; stbi_set_flip_vertically_on_load(true); unsigned char *data = stbi_load(image_file.c_str(), &width, &height, &nrChannels, 0); dbc::check(data != nullptr, std::format("Failed to load texture: {}", image_file)); dbc::check(nrChannels == 3 || nrChannels == 4, std::format("Image {} has invalid channels {} should be 3 or 4.", image_file, nrChannels)); auto rgb_or_a = nrChannels == 3 ? GL_RGB : GL_RGBA; glTexImage2D(GL_TEXTURE_2D, 0, rgb_or_a, width, height, 0, rgb_or_a, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); stbi_image_free(data); return texture_id; } Config setup() { glEnable(GL_DEPTH_TEST); Config config{{"shaders/4.2.texture.vs", "shaders/4.2.texture.fs"}}; unsigned int VBO = 0; unsigned int VAO = 0; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_3d), vertices_3d, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); config.texture1 = load_texture("resources/textures/container.jpg"); config.texture2 = load_texture("resources/textures/awesomeface.png"); config.shader.use(); config.shader.setInt("texture1", 0); config.shader.setInt("texture2", 1); config.VAO = VAO; config.VBO = VBO; return config; } void render(GLFWwindow* window, const Config& config, BoxTest& box, b2World& world) { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, config.texture1); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, config.texture2); config.shader.use(); // this is position float time = glfwGetTime(); Box2d_step_world(world); glm::mat4 view = glm::mat4(1.0f); view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); config.shader.setMat4("view", view); glm::mat4 projection = glm::mat4(1.0f); projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); config.shader.setMat4("projection", projection); glBindVertexArray(config.VAO); for(unsigned int i = 0; i < BODY_COUNT; i++) { glm::mat4 model = glm::mat4(1.0f); // get position from box b2Vec2 position = box.bodies[i]->GetPosition(); float angle = box.bodies[i]->GetAngle(); model = glm::translate(model, glm::vec3(position.x, position.y, 0.0f)); model = glm::rotate(model, angle / 5.0f, glm::vec3(1.0f, 0.0f, 0.0f)); model = glm::rotate(model, glm::radians(time * 10.0f), glm::vec3(0.0f, 1.0f, 1.0f)); config.shader.setMat4("model", model); glDrawArrays(GL_TRIANGLES, 0, 36); } glfwSwapBuffers(window); glfwPollEvents(); } void cleanup(Config& config) { glDeleteVertexArrays(1, &config.VAO); glDeleteBuffers(1, &config.VAO); glDeleteBuffers(1, &config.VBO); config.shader.cleanup(); } int main() { init_glfw(); b2Vec2 gravity(0.0f, -10.0f); b2World world(gravity); BoxTest box = Box2d_setup(world); auto window = create_window(); auto config = setup(); while(!glfwWindowShouldClose(window)) { processInput(window, box); render(window, config, box, world); } cleanup(config); glfwTerminate(); return 0; }