Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <array>
- #include <cstring>
- #include <glad/glad.h>
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- #include <iostream>
- #include <glm/glm.hpp>
- #include <iterator>
- #include <vector>
- #include "camera.hpp"
- #include "glm/ext/matrix_float4x4.hpp"
- #include "glm/ext/matrix_transform.hpp"
- #include "glm/ext/vector_float3.hpp"
- #include "glm/trigonometric.hpp"
- #include "glm/ext/matrix_clip_space.hpp"
- #include "shader.hpp"
- #include "cubes.hpp"
- #include "triangles.hpp"
- void framebuffer_change_callback(GLFWwindow* window, int width, int height);
- void cursor_position_callback(GLFWwindow* window, double xpos, double ypos);
- Camera camera = Camera(glm::vec3(0.0f,0.0f,1.0f), glm::vec3(0.0f,0.0f,0.0f), 80.0f);
- float mouse_xpos = 0.0f;
- float mouse_ypos = 0.0f;
- int main(){
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- GLFWwindow* window = glfwCreateWindow(600, 400, "Test", nullptr, nullptr);
- glfwMakeContextCurrent(window);
- glfwSetFramebufferSizeCallback(window, framebuffer_change_callback);
- glfwSetCursorPosCallback(window, cursor_position_callback);
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
- std::cout << "Error starting Opengl" << std::endl;
- return -1;
- }
- float vertices[] = {
- 0.0f, -0.5f, -0.5,
- 0.5f, -0.5f, 0.0f,
- 0.0f, -0.5f, 0.5f,
- -0.5f, -0.5f, 0.0f,
- 0.0f, 0.5f, -0.5f,
- 0.5f, 0.5f, 0.0f,
- 0.0f, 0.5f, 0.5f,
- -0.5f, 0.5f, 0.0f,
- -0.5f, 0.0f, -0.5f,
- 0.5f, 0.0f, -0.5f,
- 0.5f, 0.0f, 0.5f,
- -0.5f, 0.0f, 0.5f,
- };
- unsigned int VBO, VAO, EBO;
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
- glBindVertexArray(VAO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
- Cube cube = Cube(glm::vec3(0.0,0.0,0.0));
- cube.values[0] = 1.0;
- cube.values[1] = 1.0;
- int edgeIndex = edgeTable[cube.getTriangleIndex(0.5)];
- // need to put indices in here
- int indices[16];
- //segfault
- memcpy(indices, triTable[edgeIndex], 16);
- // load ebo here
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
- Shader shader = Shader("res/shaders/shader.vs", "res/shaders/shader.fs");
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
- //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- glEnable(GL_DEPTH_TEST);
- //glEnable(GL_CULL_FACE);
- //glCullFace(GL_BACK);
- while(!glfwWindowShouldClose(window)){
- float camera_speed = 0.03f;
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS){
- camera.position -= camera.front * camera_speed;
- camera.target -= camera.front * camera_speed;
- }
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS){
- camera.position += camera.front * camera_speed;
- camera.target += camera.front * camera_speed;
- }
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS){
- camera.position -= camera.right * camera_speed;
- camera.target -= camera.right * camera_speed;
- }
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS){
- camera.position += camera.right * camera_speed;
- camera.target += camera.right * camera_speed;
- }
- glClearColor(0.0f,0.0f,0.0f,1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- int width, height;
- glfwGetWindowSize(window, &width, &height);
- glm::mat4 projection = glm::perspective(glm::radians(camera.fov), (float)width / (float)height, 0.1f, 100.0f);
- shader.setMat4("projection", projection);
- shader.setMat4("view", camera.view);
- shader.setMat4("model", glm::mat4(1.0f));
- glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
- shader.use();
- camera.update_camera_transform();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- return 0;
- }
- void framebuffer_change_callback(GLFWwindow* window, int width, int height){
- glViewport(0,0,width,height);
- }
- void cursor_position_callback(GLFWwindow* window, double xpos, double ypos){
- float x_delta = mouse_xpos - xpos;
- float y_delta = mouse_ypos - ypos;
- mouse_ypos = ypos;
- mouse_xpos = xpos;
- camera.target -= (camera.right * x_delta * 0.01f);
- camera.target += (camera.up * y_delta * 0.01f);
- }
Advertisement
Add Comment
Please, Sign In to add comment