Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include "shader.h"
- using point3 = glm::vec3;
- using color = glm::vec3;
- float deltaT = 0, lastF = 0;
- std::vector<point3> control_points;
- std::vector<point3> curve_points;
- void process_keyboard(GLFWwindow* w);
- glm::vec3 cameraPos = glm::vec3(0.f,0.f,10.f);
- glm::vec3 cameraFront = glm::vec3(-0.4f,0.f,1.f);
- glm::vec3 lightPos = glm::vec3(2.f, -1.f, 5.f);
- color lightColor = color(1.f);
- color objectColor = color(0.071, 0.682, 0.82);
- glm::vec3 cameraUp = glm::vec3(0,1,0);
- glm::mat4 model = glm::mat4(1.f);
- glm::mat4 proj = glm::perspective(glm::radians(45.f), 16.f/9.f, 0.1f, 100.f);
- glm::mat4 view = glm::lookAt(cameraPos, cameraPos - cameraFront, glm::vec3(0.0, 1.0, 0.0));
- float floor_data[] = {
- //straznja
- -1.0f, -1.0f, -1.0f, 0.f, 0.f, -1.f, 0.0f, 0.0f, //dl
- 1.0f, -1.0f, -1.0f, 0.f, 0.f, -1.f, 1.0f, 0.0f, //dd
- 1.0f, 1.0f, -1.0f, 0.f, 0.f, -1.f, 1.0f, 1.0f, //gd
- -1.0f, 1.0f, -1.0f, 0.f, 0.f, -1.f, 0.0f, 1.0f, //gl
- //lijeva
- -1.0f, -1.0f, -1.0f, -1.f, 0.f, 0.f, 0.0f, 0.0f, //ds
- -1.0f, -1.0f, 1.0f, -1.f, 0.f, 0.f, 1.0f, 0.0f, //dp
- -1.0f, 1.0f, 1.0f, -1.f, 0.f, 0.f, 1.0f, 1.0f, //gp
- -1.0f, 1.0f, -1.0f, -1.f, 0.f, 0.f, 0.0f, 1.0f, //gs
- //desna
- 1.0f, -1.0f, -1.0f, 1.f, 0.f, 0.f, 0.0f, 0.0f, //ds
- 1.0f, -1.0f, 1.0f, 1.f, 0.f, 0.f, 1.0f, 0.0f, //dp
- 1.0f, 1.0f, 1.0f, 1.f, 0.f, 0.f, 1.0f, 1.0f, //gp
- 1.0f, 1.0f, -1.0f, 1.f, 0.f, 0.f, 0.0f, 1.0f, //gs
- //gornja
- -1.0f, 1.0f, -1.0f, 0.f, 1.f, 0.f, 0.0f, 0.0f, //sl
- 1.0f, 1.0f, -1.0f, 0.f, 1.f, 0.f, 1.0f, 0.0f, //sd
- 1.0f, 1.0f, 1.0f, 0.f, 1.f, 0.f, 1.0f, 1.0f, //pd
- -1.0f, 1.0f, 1.0f, 0.f, 1.f, 0.f, 0.0f, 1.0f, //pl
- //donja
- -1.0f, -1.0f, -1.0f, 0.f, -1.f, 0.f, 0.0f, 0.0f, //sl
- 1.0f, -1.0f, -1.0f, 0.f, -1.f, 0.f, 1.0f, 0.0f, //sd
- 1.0f, -1.0f, 1.0f, 0.f, -1.f, 0.f, 1.0f, 1.0f, //pd
- -1.0f, -1.0f, 1.0f, 0.f, -1.f, 0.f, 0.0f, 1.0f //pl
- };
- int floor_indices[] = {
- //straznja
- 0, 1, 2,
- 2, 3, 0,
- //lijeva
- 4, 5, 6,
- 6, 7, 4,
- //desna
- 8, 9, 10,
- 10, 11, 8,
- //gornja
- 12, 13, 14,
- 14, 15, 12,
- //donja
- 16, 17, 18,
- 18, 19, 16
- };
- int main()
- {
- if( !glfwInit() )
- {
- std::cerr << "GLFW nije inicijaliziran\n";
- getchar();
- return -1;
- }
- glfwWindowHint(GLFW_SAMPLES, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // samo za MacOS
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- GLFWwindow* window = glfwCreateWindow( 1024, 768, "prog", nullptr, nullptr);
- if( window == nullptr )
- {
- std::cerr << "Nije prošao init glfw prozora. Npr. neki Intel GPU nisu 3.3 kompatibilni.\n";
- getchar();
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- if (glewInit() != GLEW_OK)
- {
- std::cerr << "GLEW nije inicijaliziran\n";
- getchar();
- glfwTerminate();
- return -1;
- }
- glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
- glEnable(GL_DEPTH_TEST);
- glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
- glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
- unsigned int VAO, VBO, EBO;
- glGenVertexArrays(1, &VAO);
- glBindVertexArray(VAO);
- glGenBuffers(1, &VBO);
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(floor_data), floor_data, GL_STATIC_DRAW);
- glGenBuffers(1, &EBO);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(floor_indices), floor_indices, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, false, 8*sizeof(float), (void*)0);
- glVertexAttribPointer(1, 3, GL_FLOAT, false, 8*sizeof(float), (void*)(3 * sizeof(float)));
- glVertexAttribPointer(2, 2, GL_FLOAT, false, 8*sizeof(float), (void*)(6 * sizeof(float)));
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glEnableVertexAttribArray(2);
- std::vector<ShaderInfo> shaders = {
- {GL_VERTEX_SHADER, "shaders/vert.glsl"},
- {GL_FRAGMENT_SHADER, "shaders/sphere_frag.glsl"}
- };
- Shader shader(shaders);
- glEnable(GL_DEPTH_TEST);
- glDisable(GL_CULL_FACE);
- do {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- float currentF = glfwGetTime();
- deltaT = currentF - lastF;
- lastF = currentF;
- process_keyboard(window);
- view = glm::lookAt(cameraPos, cameraPos - cameraFront, glm::vec3(0.0, 1.0, 0.0));
- glCullFace(GL_BACK);
- shader.use();
- shader.setMat4("model", model);
- shader.setMat4("proj", proj);
- shader.setMat4("view", view);
- shader.setVec3("objectColor", objectColor);
- shader.setVec3("lightColor", lightColor);
- shader.setVec3("viewPos", cameraPos);
- shader.setVec3("lightPos", lightPos);
- glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, 30, GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- glDisableVertexAttribArray(0);
- glDisableVertexAttribArray(1);
- glDisableVertexAttribArray(2);
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
- glfwWindowShouldClose(window) == 0 );
- glfwTerminate();
- return 0;
- }
- void process_keyboard(GLFWwindow* w)
- {
- float camSpeed = 4 * deltaT;
- if(glfwGetKey(w,GLFW_KEY_W) == GLFW_PRESS)
- {
- cameraPos -= cameraFront * camSpeed;
- }
- else if(glfwGetKey(w,GLFW_KEY_S) == GLFW_PRESS)
- {
- cameraPos += cameraFront * camSpeed;
- }
- else if(glfwGetKey(w,GLFW_KEY_A) == GLFW_PRESS)
- {
- cameraPos += glm::cross(cameraFront, cameraUp) * camSpeed;
- }
- else if(glfwGetKey(w,GLFW_KEY_D) == GLFW_PRESS)
- {
- cameraPos -= glm::cross(cameraFront, cameraUp) * camSpeed;
- }
- else if(glfwGetKey(w,GLFW_KEY_Q) == GLFW_PRESS)
- {
- cameraFront = glm::rotate(glm::mat4(1.f), std::acos(std::cos(deltaT)), cameraUp) * glm::vec4(cameraFront, 1.f);
- }
- else if(glfwGetKey(w,GLFW_KEY_E) == GLFW_PRESS)
- {
- cameraFront = glm::rotate(glm::mat4(1.f), -std::acos(std::cos(deltaT)), cameraUp) * glm::vec4(cameraFront, 1.f);
- }
- if(glfwGetKey(w,GLFW_KEY_R) == GLFW_PRESS)
- {
- cameraPos += glm::vec3(0,1,0) * camSpeed;
- }
- if(glfwGetKey(w,GLFW_KEY_T) == GLFW_PRESS)
- {
- cameraPos -= glm::vec3(0,1,0) * camSpeed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement