Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #define GLEW_STATIC
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- int screenWidth = 800;
- int screenHeight = 600;
- double deltaTime = 0.0;
- bool keys[GLFW_KEY_LAST];
- bool mouseGrabbed = false;
- std::vector<glm::vec3> lines;
- struct CAMERA
- {
- glm::vec3 position = glm::vec3(0.0f, 0.0f, 10.0);
- glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0);
- glm::vec3 front = glm::vec3(0.0f, 0.0f, -1.0f);
- glm::vec3 direction = glm::normalize(position - front);
- glm::vec3 upVector = glm::vec3(0.0f, 1.0f, 0.0f);
- glm::vec3 right = glm::normalize(glm::cross(upVector, direction));
- glm::vec3 cameraUp = glm::cross(direction, right);
- }camera;
- void MakeCoordinateNet(int netSize, float yPos, float cubeSize)
- {
- for (float x = -netSize; x <= netSize; x += cubeSize)
- {
- lines.push_back(glm::vec3(x, yPos, netSize));
- lines.push_back(glm::vec3(x, yPos, -netSize));
- }
- for (float x = -netSize; x <= netSize; x += cubeSize)
- {
- lines.push_back(glm::vec3(netSize, yPos, x));
- lines.push_back(glm::vec3(-netSize, yPos, x));
- }
- }
- void CameraChanged()
- {
- std::cout << "Position: [" << camera.position.x << "," << camera.position.y << "," << camera.position.z << "]" << std::endl;
- std::cout << "rotation: [" << camera.rotation.x << "," << camera.rotation.y << "," << camera.rotation.z << "]" << std::endl;
- }
- void glfw_mouse_pos_callback(GLFWwindow* window, double xPos, double yPos)
- {
- if (!mouseGrabbed)
- {
- return;
- }
- glm::vec3 oldRotation = camera.rotation;
- static double lastX = 0.0;
- static double lastY = 0.0;
- static float yaw = camera.rotation.x;
- static float pitch = camera.rotation.y;
- static bool firstMouse = true;
- if (firstMouse)
- {
- lastX = xPos;
- lastY = yPos;
- firstMouse = false;
- }
- float xOffset = xPos - lastX;
- float yOffset = lastY - yPos;
- lastX = xPos;
- lastY = yPos;
- float sensitivity = 0.05f;
- xOffset *= sensitivity;
- yOffset *= sensitivity;
- yaw += xOffset;
- pitch += yOffset;
- if (pitch > 89.0f) pitch = 89.0f;
- if (pitch < -89.0f) pitch = -89.0f;
- camera.rotation.x = yaw;
- camera.rotation.y = pitch;
- glm::vec3 front;
- front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
- front.y = sin(glm::radians(pitch));
- front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
- camera.front = glm::normalize(front);
- if (camera.rotation.x != oldRotation.x ||
- camera.rotation.y != oldRotation.y ||
- camera.rotation.z != oldRotation.z)
- {
- CameraChanged();
- }
- }
- void key_callback(GLFWwindow* wnd, int key, int scancode, int action, int mode)
- {
- if (action == GLFW_PRESS)
- {
- switch (key)
- {
- case GLFW_KEY_ESCAPE:
- case GLFW_KEY_ENTER:
- if (mouseGrabbed)
- {
- glfwSetInputMode(wnd, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
- glfwSetCursorPos(wnd, screenWidth / 2, screenHeight / 2);
- mouseGrabbed = false;
- std::cout << "Mouse is released" << std::endl;
- }
- else
- {
- glfwSetWindowShouldClose(wnd, true);
- }
- return;
- }
- keys[key] = true;
- }
- else if (action == GLFW_RELEASE)
- {
- keys[key] = false;
- }
- }
- void callback_func_windowResize(GLFWwindow* wnd, int w, int h)
- {
- screenWidth = w;
- screenHeight = h;
- glViewport(0,0, w,h);
- std::cout << "Changed viewport to: " << w << "x" << h << std::endl;
- }
- void callback_func_MouseClicked(GLFWwindow* wnd, int button, int action, int mods)
- {
- if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
- {
- if (!mouseGrabbed)
- {
- glfwSetCursorPos(wnd, screenWidth / 2, screenHeight / 2);
- glfwSetInputMode(wnd, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
- mouseGrabbed = true;
- std::cout << "Mouse is grabbed" << std::endl;
- }
- }
- }
- void HandleMovement(float speed)
- {
- glm::vec3 oldPos = camera.position;
- float cameraSpeed = deltaTime * speed;
- if(keys[GLFW_KEY_W])
- camera.position += camera.front * cameraSpeed;
- if(keys[GLFW_KEY_S])
- camera.position -= camera.front * cameraSpeed;
- if(keys[GLFW_KEY_A])
- camera.position -= glm::normalize(glm::cross(camera.front, camera.cameraUp)) * cameraSpeed;
- if(keys[GLFW_KEY_D])
- camera.position += glm::normalize(glm::cross(camera.front, camera.cameraUp)) * cameraSpeed;
- if(keys[GLFW_KEY_C])
- camera.position.y -= cameraSpeed;
- if(keys[GLFW_KEY_SPACE])
- camera.position.y += cameraSpeed;
- if (camera.position.x != oldPos.x || camera.position.y != oldPos.y || camera.position.z != oldPos.z)
- {
- CameraChanged();
- }
- }
- int main()
- {
- if (!glfwInit())
- {
- std::cout << "glfwInit() failed!" << std::endl;
- return 1;
- }
- GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight,
- (char*)"camera test", nullptr, nullptr);
- if (!window)
- {
- glfwTerminate();
- std::cout << "glfwCreateWindow() failed!" << std::endl;
- return 1;
- }
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwSetWindowPos(window, 700, 300);
- glfwSetKeyCallback(window, key_callback);
- glfwSetMouseButtonCallback(window, callback_func_MouseClicked);
- glfwSetCursorPosCallback(window, glfw_mouse_pos_callback);
- glfwSetWindowSizeCallback(window, callback_func_windowResize);
- glfwMakeContextCurrent(window);
- glewExperimental = true;
- if (glewInit() != GLEW_OK)
- {
- glfwTerminate();
- std::cout << "glewInit() failed!" << std::endl;
- return 1;
- }
- GLchar* vertexShaderSource = (GLchar*)
- "#version 330 core\n"
- "\n"
- "layout (location = 0) in vec3 position;\n"
- "\n"
- "uniform mat4 view;\n"
- "uniform mat4 projection;\n"
- "\n"
- "void main()\n"
- "{\n"
- " gl_Position = projection * view * vec4(position, 1.0f);\n"
- "}\n";
- GLuint vertexShader;
- vertexShader = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
- glCompileShader(vertexShader);
- GLint success;
- GLchar infoLog[512];
- glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
- std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
- glfwTerminate();
- return 1;
- }
- GLchar* fragmentShaderSource = (GLchar*)
- "#version 330 core\n"
- "\n"
- "out vec4 color;\n"
- "\n"
- "void main()\n"
- "{\n"
- " color = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
- "}\n";
- GLuint fragmentShader;
- fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
- glCompileShader(fragmentShader);
- glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
- std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
- glDeleteShader(vertexShader);
- glfwTerminate();
- return 1;
- }
- GLuint shaderProgram;
- shaderProgram = glCreateProgram();
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- glLinkProgram(shaderProgram);
- glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
- if (!success)
- {
- glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
- std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
- glDeleteShader(vertexShader);
- glDeleteShader(fragmentShader);
- glfwTerminate();
- return 1;
- }
- glUseProgram(shaderProgram);
- glDeleteShader(vertexShader);
- glDeleteShader(fragmentShader);
- MakeCoordinateNet(10, -1.0f, 1.0f);
- GLuint vbo;
- glGenBuffers(1, &vbo);
- GLuint vao;
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, lines.size() * sizeof(glm::vec3), &lines[0], GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (GLvoid*)0);
- glEnableVertexAttribArray(0);
- glBindVertexArray(0);
- while (!glfwWindowShouldClose(window))
- {
- glfwPollEvents();
- deltaTime = glfwGetTime();
- glfwSetTime(0.0);
- HandleMovement(10.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glBindVertexArray(vao);
- glm::mat4 projectionMatrix = glm::perspective(45.0f, (float)screenWidth / (float)screenHeight,
- 0.1f, 1000.0f);
- GLint n = glGetUniformLocation(shaderProgram, "projection");
- glUniformMatrix4fv(n, 1, GL_FALSE, glm::value_ptr(projectionMatrix));
- glm::mat4 viewMatrix = glm::lookAt(camera.position, camera.position + camera.front, camera.cameraUp);
- n = glGetUniformLocation(shaderProgram, "view");
- glUniformMatrix4fv(n, 1, GL_FALSE, glm::value_ptr(viewMatrix));
- glDrawArrays(GL_LINES, 0, lines.size() * 2);
- glBindVertexArray(0);
- glfwSwapBuffers(window);
- }
- glDeleteProgram(shaderProgram);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement