Last active
January 23, 2020 12:59
-
-
Save untodesu/27ebbf096b98260e958fe1c2ad63b4c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <GL/glew.h> | |
| #include <GLFW/glfw3.h> | |
| #include <glm/glm.hpp> | |
| #include <glm/gtc/matrix_transform.hpp> | |
| #include <glm/gtc/type_ptr.hpp> | |
| #include <cmath> | |
| #include "gfx/glsl.h" | |
| #include "gfx/img.h" | |
| #include "gfx/camera.h" | |
| static CFreeCamera g_camera; | |
| //----------------------------------------------------------------------------- | |
| // GLFW_ErrorCallback | |
| // Callback function | |
| //----------------------------------------------------------------------------- | |
| void GLFW_ErrorCallback(int code, const char *msg) | |
| { | |
| printf("GLFW: Error %d (%s)\n", code, msg); | |
| } | |
| //----------------------------------------------------------------------------- | |
| // GLFW_FramebufferSizeCallback | |
| // Callback function | |
| //----------------------------------------------------------------------------- | |
| void GLFW_FramebufferSizeCallback(GLFWwindow *window, int width, int height) | |
| { | |
| glfwMakeContextCurrent(window); | |
| glViewport(0, 0, width, height); | |
| } | |
| //----------------------------------------------------------------------------- | |
| // GLFW_KeyCallback | |
| // Callback function | |
| //----------------------------------------------------------------------------- | |
| void GLFW_KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) | |
| { | |
| static bool s_maximized = false; | |
| if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { | |
| glfwSetWindowShouldClose(window, true); | |
| return; | |
| } | |
| if(key == GLFW_KEY_F10 && action == GLFW_PRESS) { | |
| if(s_maximized) { | |
| glfwRestoreWindow(window); | |
| } | |
| else { | |
| glfwMaximizeWindow(window); | |
| } | |
| } | |
| } | |
| //----------------------------------------------------------------------------- | |
| // GLFW_MouseCallback | |
| // Callback function | |
| //----------------------------------------------------------------------------- | |
| void GLFW_MouseCallback(GLFWwindow* window, double xpos, double ypos) | |
| { | |
| static bool s_firstTime = true; | |
| static double s_lastX = 0.0; | |
| static double s_lastY = 0.0; | |
| // setup | |
| if(s_firstTime) { | |
| s_lastX = xpos; | |
| s_lastY = ypos; | |
| s_firstTime = false; | |
| } | |
| // offsets | |
| double dx = (xpos - s_lastX); | |
| double dy = (ypos - s_lastY); | |
| s_lastX = xpos; | |
| s_lastY = ypos; | |
| g_camera.Rotate(dx, dy); | |
| } | |
| //----------------------------------------------------------------------------- | |
| // GLFW_ScrollCallback | |
| // Callback function | |
| //----------------------------------------------------------------------------- | |
| void GLFW_ScrollCallback(GLFWwindow* window, double dx, double dy) | |
| { | |
| g_camera.Zoom(dy); | |
| } | |
| //----------------------------------------------------------------------------- | |
| // GLFW_KeyInput | |
| // | |
| //----------------------------------------------------------------------------- | |
| void GLFW_HandleKeys(GLFWwindow *window, double delta) | |
| { | |
| if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { | |
| g_camera.Move(CFreeCamera::Direction::Forward, delta); | |
| } | |
| if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { | |
| g_camera.Move(CFreeCamera::Direction::Back, delta); | |
| } | |
| if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { | |
| g_camera.Move(CFreeCamera::Direction::Left, delta); | |
| } | |
| if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { | |
| g_camera.Move(CFreeCamera::Direction::Right, delta); | |
| } | |
| } | |
| int main(void) | |
| { | |
| glfwSetErrorCallback(GLFW_ErrorCallback); | |
| if(!glfwInit()) { | |
| return 1; | |
| } | |
| GLFWwindow *window = glfwCreateWindow(640, 480, "title", nullptr, nullptr); | |
| if(!window) { | |
| return 2; | |
| } | |
| glfwMakeContextCurrent(window); | |
| GLenum glewError = glewInit(); | |
| if(glewError != GLEW_OK) { | |
| GLFW_ErrorCallback(-1, (const char *)glewGetErrorString(glewError)); | |
| glfwTerminate(); | |
| return 3; | |
| } | |
| // setup gl | |
| glEnable(GL_DEPTH_TEST); | |
| glDepthFunc(GL_LESS); | |
| glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
| // setup callbacks | |
| glfwSetFramebufferSizeCallback(window, GLFW_FramebufferSizeCallback); | |
| glfwSetKeyCallback(window, GLFW_KeyCallback); | |
| glfwSetCursorPosCallback(window, GLFW_MouseCallback); | |
| glfwSetScrollCallback(window, GLFW_ScrollCallback); | |
| double lastTime = 0.0; | |
| double time = 0.0; | |
| double delta = 0.0; | |
| CShader shader; | |
| shader.CompileFile_V("../shaders/main_vs.glsl"); | |
| shader.CompileFile_F("../shaders/tri1_fs.glsl"); | |
| shader.LinkProgram(); | |
| GLuint texture = 0; | |
| if(!Img_CreateFromFile("../assets/bruh2.png", texture)) { | |
| return 4; | |
| } | |
| // The cube | |
| const GLdouble cubeData[] = { | |
| // VERTICES TEXCOORD | |
| -0.5, -0.5, -0.5, 1.0, 1.0, | |
| 0.5, -0.5, -0.5, 0.0, 1.0, | |
| 0.5, 0.5, -0.5, 0.0, 0.0, | |
| 0.5, 0.5, -0.5, 0.0, 0.0, | |
| -0.5, 0.5, -0.5, 1.0, 0.0, | |
| -0.5, -0.5, -0.5, 1.0, 1.0, | |
| -0.5, -0.5, 0.5, 1.0, 1.0, | |
| 0.5, -0.5, 0.5, 0.0, 1.0, | |
| 0.5, 0.5, 0.5, 0.0, 0.0, | |
| 0.5, 0.5, 0.5, 0.0, 0.0, | |
| -0.5, 0.5, 0.5, 1.0, 0.0, | |
| -0.5, -0.5, 0.5, 1.0, 1.0, | |
| -0.5, 0.5, 0.5, 0.0, 1.0, | |
| -0.5, 0.5, -0.5, 0.0, 0.0, | |
| -0.5, -0.5, -0.5, 1.0, 0.0, | |
| -0.5, -0.5, -0.5, 1.0, 0.0, | |
| -0.5, -0.5, 0.5, 1.0, 1.0, | |
| -0.5, 0.5, 0.5, 0.0, 1.0, | |
| 0.5, 0.5, 0.5, 0.0, 1.0, | |
| 0.5, 0.5, -0.5, 0.0, 0.0, | |
| 0.5, -0.5, -0.5, 1.0, 0.0, | |
| 0.5, -0.5, -0.5, 1.0, 0.0, | |
| 0.5, -0.5, 0.5, 1.0, 1.0, | |
| 0.5, 0.5, 0.5, 0.0, 1.0, | |
| -0.5, -0.5, -0.5, 1.0, 0.0, | |
| 0.5, -0.5, -0.5, 0.0, 0.0, | |
| 0.5, -0.5, 0.5, 0.0, 1.0, | |
| 0.5, -0.5, 0.5, 0.0, 1.0, | |
| -0.5, -0.5, 0.5, 1.0, 1.0, | |
| -0.5, -0.5, -0.5, 1.0, 0.0, | |
| -0.5, 0.5, -0.5, 1.0, 0.0, | |
| 0.5, 0.5, -0.5, 0.0, 0.0, | |
| 0.5, 0.5, 0.5, 0.0, 1.0, | |
| 0.5, 0.5, 0.5, 0.0, 1.0, | |
| -0.5, 0.5, 0.5, 1.0, 1.0, | |
| -0.5, 0.5, -0.5, 1.0, 0.0 | |
| }; | |
| const glm::dvec3 cubePoses[] = { | |
| glm::dvec3( 0.0, 0.0, 0.0), | |
| glm::dvec3( 2.0, 5.0, -15.0), | |
| glm::dvec3(-1.5, -2.2, -2.5), | |
| glm::dvec3(-3.8, -2.0, -12.3), | |
| glm::dvec3( 2.4, -0.4, -3.5), | |
| glm::dvec3(-1.7, 3.0, -7.5), | |
| glm::dvec3( 1.3, -2.0, -2.5), | |
| glm::dvec3( 1.5, 2.0, -2.5), | |
| glm::dvec3( 1.5, 0.2, -1.5), | |
| glm::dvec3(-1.3, 1.0, -1.5) | |
| }; | |
| // Buffer data | |
| GLuint vao, vbo; | |
| glGenVertexArrays(1, &vao); | |
| glGenBuffers(1, &vbo); | |
| // Vertex Array Objext | |
| glBindVertexArray(vao); | |
| // Vertex Buffer Objext | |
| glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
| glBufferData(GL_ARRAY_BUFFER, sizeof(cubeData), cubeData, GL_STATIC_DRAW); | |
| // position attrib | |
| glVertexAttribPointer(0, 3, GL_DOUBLE, false, 5 * sizeof(GLdouble), (void *)(0)); | |
| glEnableVertexAttribArray(0); | |
| // texcoord attrib | |
| glVertexAttribPointer(1, 2, GL_DOUBLE, false, 5 * sizeof(GLdouble), (void *)(3 * sizeof(GLdouble))); | |
| glEnableVertexAttribArray(1); | |
| glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); | |
| while(!glfwWindowShouldClose(window)) { | |
| lastTime = time; | |
| time = glfwGetTime(); | |
| delta = (time - lastTime); | |
| shader.SetUniform("g_currentTime", time); | |
| shader.SetUniform("g_deltaTime", delta); | |
| GLFW_HandleKeys(window, delta); | |
| glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
| glBindAttribLocation(shader.GetProgram(), 0, "g_vertexPosition"); | |
| glBindAttribLocation(shader.GetProgram(), 1, "g_texCoord"); | |
| glActiveTexture(GL_TEXTURE0 + 0); | |
| glBindTexture(GL_TEXTURE_2D, texture); | |
| shader.SetUniform("g_texture", 0); | |
| shader.UseProgram(); | |
| glm::dmat4 view = g_camera.GetViewMatrix(); | |
| glm::dmat4 proj = glm::perspective(90.0, (640.0 / 480.0), 0.1, 100.0); | |
| shader.SetUniform("g_viewMtx", view); | |
| shader.SetUniform("g_projMtx", proj); | |
| glBindVertexArray(vao); | |
| for(int i = 1; i < sizeof(cubePoses)/sizeof(cubePoses[0]); i++) { | |
| double angle = 20.0 * i; | |
| glm::dmat4 model = glm::translate(glm::dmat4(1.0), cubePoses[i - 1]); | |
| model = glm::rotate(model, glfwGetTime() * glm::radians(angle), glm::dvec3(1.0, 0.3, 1.5)); | |
| shader.SetUniform("g_modelMtx", model); | |
| glDrawArrays(GL_TRIANGLES, 0, 36); | |
| } | |
| glBindVertexArray(0); | |
| glFlush(); | |
| glfwSwapBuffers(window); | |
| glfwPollEvents(); | |
| } | |
| glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); | |
| glfwDestroyWindow(window); | |
| glfwTerminate(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment