Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #define GLM_FORCE_RADIANS
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <vector>
- #include <fstream>
- #include <cstdio>
- using namespace glm;
- GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){
- // Create the shaders
- GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
- GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
- // Read the Vertex Shader code from the file
- std::string VertexShaderCode;
- std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
- if(VertexShaderStream.is_open())
- {
- std::string Line = "";
- while(getline(VertexShaderStream, Line))
- VertexShaderCode += "\n" + Line;
- VertexShaderStream.close();
- }
- // Read the Fragment Shader code from the file
- std::string FragmentShaderCode;
- std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
- if(FragmentShaderStream.is_open()){
- std::string Line = "";
- while(getline(FragmentShaderStream, Line))
- FragmentShaderCode += "\n" + Line;
- FragmentShaderStream.close();
- }
- GLint Result = GL_FALSE;
- int InfoLogLength;
- // Compile Vertex Shader
- printf("Compiling shader : %s\n", vertex_file_path);
- char const * VertexSourcePointer = VertexShaderCode.c_str();
- glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
- glCompileShader(VertexShaderID);
- // Check Vertex Shader
- glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
- glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
- std::vector<char> VertexShaderErrorMessage(InfoLogLength);
- glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
- fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
- // Compile Fragment Shader
- printf("Compiling shader : %s\n", fragment_file_path);
- char const * FragmentSourcePointer = FragmentShaderCode.c_str();
- glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
- glCompileShader(FragmentShaderID);
- // Check Fragment Shader
- glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
- glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
- std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
- glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
- fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);
- // Link the program
- fprintf(stdout, "Linking program\n");
- GLuint ProgramID = glCreateProgram();
- glAttachShader(ProgramID, VertexShaderID);
- glAttachShader(ProgramID, FragmentShaderID);
- glLinkProgram(ProgramID);
- // Check the program
- glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
- glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
- std::vector<char> ProgramErrorMessage( max(InfoLogLength, int(1)) );
- glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
- fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
- glDeleteShader(VertexShaderID);
- glDeleteShader(FragmentShaderID);
- return ProgramID;
- }
- GLFWwindow* window;
- struct Camera {
- mat4 view, proj, viewProj;
- float fieldOfView, aspectRatio, nearPlaneDistance, farPlaneDistance;
- vec3 position, direction, upDir, rightDir;
- Camera() {
- fieldOfView = 45.0f;
- aspectRatio = 1024.0/768.0;
- nearPlaneDistance = 0.1f;
- farPlaneDistance = 100.0f;
- position = vec3(0.0f, 0.0f, 10.0f);
- direction = vec3(0.0f, 0.0f, -1.0f);
- upDir = vec3(0.0f, 1.0f, 0.0f);
- rightDir = vec3(1.0f, 0.0f, 0.0f);
- updateProj();
- updateView();
- glfwGetCursorPos(window, &lastX, &lastY);
- }
- void updateProj() {
- proj = perspective(fieldOfView, aspectRatio, nearPlaneDistance, farPlaneDistance);
- }
- void updateView() {
- vec3 target = position + direction;
- view = lookAt(position, target, upDir);
- viewProj = proj * view;
- }
- void update(double elapsedTime) {
- vec2 mov(0.0f, 0.0f);
- if (glfwGetKey(window, GLFW_KEY_W))
- mov.y =1.0;
- if (glfwGetKey(window, GLFW_KEY_S))
- mov.y =-1.0;
- if (glfwGetKey(window, GLFW_KEY_A))
- mov.x =-1.0;
- if (glfwGetKey(window, GLFW_KEY_D))
- mov.x =1.0;
- mov *= 10.0 * elapsedTime;
- position += (rightDir * mov.x + direction * mov.y);
- vec2 rot(0.0, 0.0);
- double x, y;
- glfwGetCursorPos(window, &x, &y);
- if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)) {
- rot.x = static_cast<float>(lastX - x)*3.0f;
- rot.y = static_cast<float>(lastY - y)*3.0f;
- }
- lastX = x;
- lastY = y;
- rot *= elapsedTime;
- applayRotation(rotate(rotate(mat4(), rot.y, rightDir), rot.x, vec3(0.0f, 1.0f, 0.0f)));
- updateView();
- }
- void applayRotation(mat4 transform) {
- direction = (vec3)normalize(transform * vec4(direction, 0.0f));
- upDir = (vec3)normalize(transform * vec4(upDir, 0.0f));
- rightDir = cross(direction, upDir);
- upDir = cross(rightDir, direction);
- }
- double lastX, lastY;
- };
- int main() {
- if (!glfwInit())
- return 1;
- glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
- window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
- if (!window) {
- glfwTerminate();
- return 2;
- }
- glfwMakeContextCurrent(window);
- glewExperimental=true; // Needed in core profile
- if (glewInit() != GLEW_OK)
- return 3;
- glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- const GLuint prog = LoadShaders("test.vert", "test.frag");
- const GLint posLoc = glGetAttribLocation(prog, "pos");
- const GLint colorLoc = glGetAttribLocation(prog, "color");
- const GLint worldViewProjLoc = glGetUniformLocation(prog, "worldViewProj");
- const double cube[] = {
- -1.0, +1.0, -1.0, 1.0,
- +1.0, +1.0, -1.0, 1.0,
- +1.0, +1.0, +1.0, 1.0,
- -1.0, +1.0, +1.0, 1.0,
- -1.0, -1.0, +1.0, 1.0,
- +1.0, -1.0, +1.0, 1.0,
- +1.0, -1.0, -1.0, 1.0,
- -1.0, -1.0, -1.0, 1.0,
- };
- const GLuint cubeIndices[] = {
- 0, 2, 1,
- 0, 3, 2,
- 4, 6, 5,
- 4, 7, 6,
- 3, 5, 2,
- 3, 4, 5,
- 2, 6, 1,
- 2, 5, 6,
- 1, 6, 7,
- 1, 7, 0,
- 0, 4, 3,
- 0, 7, 4
- };
- const double colors[] = {
- 0.1, 1.0, 0.1, 1.0,
- 1.0, 1.0, 0.1, 1.0,
- 1.0, 1.0, 1.0, 1.0,
- 0.1, 1.0, 1.0, 1.0,
- 0.1, 0.1, 1.0, 1.0,
- 1.0, 0.1, 1.0, 1.0,
- 1.0, 0.1, 0.1, 1.0,
- 0.1, 0.1, 0.1, 1.0
- };
- GLuint vertexArrayObject;
- glGenVertexArrays(1, &vertexArrayObject);
- glBindVertexArray(vertexArrayObject);
- GLuint vertexBuffer;
- glGenBuffers(1, &vertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(cube) + sizeof(colors), nullptr, GL_STATIC_DRAW);
- glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(cube), cube);
- glBufferSubData(GL_ARRAY_BUFFER, sizeof(cube), sizeof(colors), colors);
- glVertexAttribPointer(posLoc, 4, GL_DOUBLE, GL_FALSE, 0, nullptr);
- glEnableVertexAttribArray(posLoc);
- glVertexAttribPointer(colorLoc, 4, GL_DOUBLE, GL_FALSE, 0, (void*)sizeof(cube));
- glEnableVertexAttribArray(colorLoc);
- GLuint indexBuffer;
- glGenBuffers(1, &indexBuffer);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeIndices), cubeIndices, GL_STATIC_DRAW);
- glBindVertexArray(0);
- Camera camera;
- glEnable(GL_CULL_FACE);
- glFrontFace(GL_CCW);
- while ((glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS) && !glfwWindowShouldClose(window)) {
- camera.update(1.0/60.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glBindVertexArray(vertexArrayObject);
- glUseProgram(prog);
- glUniformMatrix4fv(worldViewProjLoc, 1, GL_FALSE, glm::value_ptr(camera.viewProj));
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
- glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement