Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.59 KB | None | 0 0
  1. #include <GL/glew.h>
  2.  
  3. #include <GLFW/glfw3.h>
  4. #define GLM_FORCE_RADIANS
  5. #include <glm/glm.hpp>
  6. #include <glm/gtc/matrix_transform.hpp>
  7. #include <glm/gtc/type_ptr.hpp>
  8.  
  9. #include <vector>
  10. #include <fstream>
  11. #include <cstdio>
  12.  
  13. using namespace glm;
  14.  
  15. GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){
  16.  
  17.     // Create the shaders
  18.     GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  19.     GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  20.  
  21.     // Read the Vertex Shader code from the file
  22.     std::string VertexShaderCode;
  23.     std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
  24.     if(VertexShaderStream.is_open())
  25.     {
  26.         std::string Line = "";
  27.         while(getline(VertexShaderStream, Line))
  28.             VertexShaderCode += "\n" + Line;
  29.         VertexShaderStream.close();
  30.     }
  31.  
  32.     // Read the Fragment Shader code from the file
  33.     std::string FragmentShaderCode;
  34.     std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
  35.     if(FragmentShaderStream.is_open()){
  36.         std::string Line = "";
  37.         while(getline(FragmentShaderStream, Line))
  38.             FragmentShaderCode += "\n" + Line;
  39.         FragmentShaderStream.close();
  40.     }
  41.  
  42.     GLint Result = GL_FALSE;
  43.     int InfoLogLength;
  44.  
  45.     // Compile Vertex Shader
  46.     printf("Compiling shader : %s\n", vertex_file_path);
  47.     char const * VertexSourcePointer = VertexShaderCode.c_str();
  48.     glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
  49.     glCompileShader(VertexShaderID);
  50.  
  51.     // Check Vertex Shader
  52.     glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
  53.     glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  54.     std::vector<char> VertexShaderErrorMessage(InfoLogLength);
  55.     glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
  56.     fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
  57.  
  58.     // Compile Fragment Shader
  59.     printf("Compiling shader : %s\n", fragment_file_path);
  60.     char const * FragmentSourcePointer = FragmentShaderCode.c_str();
  61.     glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
  62.     glCompileShader(FragmentShaderID);
  63.  
  64.     // Check Fragment Shader
  65.     glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
  66.     glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  67.     std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
  68.     glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
  69.     fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);
  70.  
  71.     // Link the program
  72.     fprintf(stdout, "Linking program\n");
  73.     GLuint ProgramID = glCreateProgram();
  74.     glAttachShader(ProgramID, VertexShaderID);
  75.     glAttachShader(ProgramID, FragmentShaderID);
  76.     glLinkProgram(ProgramID);
  77.  
  78.     // Check the program
  79.     glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
  80.     glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  81.     std::vector<char> ProgramErrorMessage( max(InfoLogLength, int(1)) );
  82.     glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
  83.     fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
  84.  
  85.     glDeleteShader(VertexShaderID);
  86.     glDeleteShader(FragmentShaderID);
  87.  
  88.     return ProgramID;
  89. }
  90.  
  91. GLFWwindow* window;
  92.  
  93. struct Camera {
  94.   mat4 view, proj, viewProj;
  95.   float fieldOfView, aspectRatio, nearPlaneDistance, farPlaneDistance;
  96.   vec3 position, direction, upDir, rightDir;
  97.  
  98.   Camera() {
  99.     fieldOfView = 45.0f;
  100.     aspectRatio = 1024.0/768.0;
  101.     nearPlaneDistance = 0.1f;
  102.     farPlaneDistance = 100.0f;
  103.     position = vec3(0.0f, 0.0f, 10.0f);
  104.     direction = vec3(0.0f, 0.0f, -1.0f);
  105.     upDir = vec3(0.0f, 1.0f, 0.0f);
  106.     rightDir = vec3(1.0f, 0.0f, 0.0f);
  107.     updateProj();
  108.     updateView();
  109.     glfwGetCursorPos(window, &lastX, &lastY);
  110.   }
  111.  
  112.   void updateProj() {
  113.     proj = perspective(fieldOfView, aspectRatio, nearPlaneDistance, farPlaneDistance);
  114.   }
  115.  
  116.   void updateView() {
  117.     vec3 target = position + direction;
  118.     view = lookAt(position, target, upDir);
  119.     viewProj = proj * view;
  120.   }
  121.  
  122.   void update(double elapsedTime) {
  123.     vec2 mov(0.0f, 0.0f);
  124.     if (glfwGetKey(window, GLFW_KEY_W))
  125.       mov.y =1.0;
  126.     if (glfwGetKey(window, GLFW_KEY_S))
  127.       mov.y =-1.0;
  128.     if (glfwGetKey(window, GLFW_KEY_A))
  129.       mov.x =-1.0;
  130.     if (glfwGetKey(window, GLFW_KEY_D))
  131.       mov.x =1.0;
  132.     mov *= 10.0 * elapsedTime;
  133.     position += (rightDir * mov.x + direction * mov.y);
  134.    
  135.     vec2 rot(0.0, 0.0);
  136.     double x, y;
  137.     glfwGetCursorPos(window, &x, &y);
  138.     if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)) {
  139.       rot.x = static_cast<float>(lastX - x)*3.0f;
  140.       rot.y = static_cast<float>(lastY - y)*3.0f;
  141.     }
  142.     lastX = x;
  143.     lastY = y;
  144.     rot *= elapsedTime;
  145.     applayRotation(rotate(rotate(mat4(), rot.y, rightDir), rot.x, vec3(0.0f, 1.0f, 0.0f)));
  146.    
  147.     updateView();
  148.   }
  149.  
  150.   void applayRotation(mat4 transform) {
  151.     direction = (vec3)normalize(transform * vec4(direction, 0.0f));
  152.     upDir = (vec3)normalize(transform * vec4(upDir, 0.0f));
  153.     rightDir = cross(direction, upDir);
  154.     upDir = cross(rightDir, direction);
  155.   }
  156.  
  157.   double lastX, lastY;
  158. };
  159.  
  160. int main() {
  161.   if (!glfwInit())
  162.     return 1;
  163.  
  164.   glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
  165.   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
  166.   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  167.   glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
  168.   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
  169.  
  170.   window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
  171.   if (!window) {
  172.     glfwTerminate();
  173.     return 2;
  174.   }
  175.  
  176.   glfwMakeContextCurrent(window);
  177.   glewExperimental=true; // Needed in core profile
  178.   if (glewInit() != GLEW_OK)
  179.     return 3;
  180.   glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  181.  
  182.   glEnable(GL_DEPTH_TEST);
  183.   glDepthFunc(GL_LESS);
  184.  
  185.   const GLuint prog = LoadShaders("test.vert", "test.frag");
  186.   const GLint posLoc = glGetAttribLocation(prog, "pos");
  187.   const GLint colorLoc = glGetAttribLocation(prog, "color");
  188.   const GLint worldViewProjLoc = glGetUniformLocation(prog, "worldViewProj");
  189.  
  190.   const double cube[] = {
  191.     -1.0, +1.0, -1.0, 1.0,
  192.     +1.0, +1.0, -1.0, 1.0,
  193.     +1.0, +1.0, +1.0, 1.0,
  194.     -1.0, +1.0, +1.0, 1.0,
  195.     -1.0, -1.0, +1.0, 1.0,
  196.     +1.0, -1.0, +1.0, 1.0,
  197.     +1.0, -1.0, -1.0, 1.0,
  198.     -1.0, -1.0, -1.0, 1.0,
  199.   };
  200.  
  201.   const GLuint cubeIndices[] = {
  202.       0, 2, 1,
  203.       0, 3, 2,
  204.  
  205.       4, 6, 5,
  206.       4, 7, 6,
  207.  
  208.       3, 5, 2,
  209.       3, 4, 5,
  210.  
  211.       2, 6, 1,
  212.       2, 5, 6,
  213.  
  214.       1, 6, 7,
  215.       1, 7, 0,
  216.  
  217.       0, 4, 3,
  218.       0, 7, 4
  219.   };
  220.  
  221.   const double colors[] = {
  222.     0.1, 1.0, 0.1, 1.0,
  223.     1.0, 1.0, 0.1, 1.0,
  224.     1.0, 1.0, 1.0, 1.0,
  225.     0.1, 1.0, 1.0, 1.0,
  226.     0.1, 0.1, 1.0, 1.0,
  227.     1.0, 0.1, 1.0, 1.0,
  228.     1.0, 0.1, 0.1, 1.0,
  229.     0.1, 0.1, 0.1, 1.0
  230.   };
  231.  
  232.  
  233.   GLuint vertexArrayObject;
  234.   glGenVertexArrays(1, &vertexArrayObject);
  235.   glBindVertexArray(vertexArrayObject);
  236.  
  237.   GLuint vertexBuffer;
  238.   glGenBuffers(1, &vertexBuffer);
  239.   glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  240.   glBufferData(GL_ARRAY_BUFFER, sizeof(cube) + sizeof(colors), nullptr, GL_STATIC_DRAW);
  241.   glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(cube), cube);
  242.   glBufferSubData(GL_ARRAY_BUFFER, sizeof(cube), sizeof(colors), colors);
  243.  
  244.   glVertexAttribPointer(posLoc, 4, GL_DOUBLE, GL_FALSE, 0, nullptr);
  245.   glEnableVertexAttribArray(posLoc);
  246.   glVertexAttribPointer(colorLoc, 4, GL_DOUBLE, GL_FALSE, 0, (void*)sizeof(cube));
  247.   glEnableVertexAttribArray(colorLoc);
  248.  
  249.   GLuint indexBuffer;
  250.   glGenBuffers(1, &indexBuffer);
  251.   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  252.   glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeIndices), cubeIndices, GL_STATIC_DRAW);
  253.  
  254.   glBindVertexArray(0);
  255.  
  256.  
  257.   Camera camera;
  258.   glEnable(GL_CULL_FACE);
  259.   glFrontFace(GL_CCW);
  260.  
  261.   while ((glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS) && !glfwWindowShouldClose(window)) {
  262.     camera.update(1.0/60.0);
  263.    
  264.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  265.    
  266.     glBindVertexArray(vertexArrayObject);
  267.     glUseProgram(prog);
  268.    
  269.     glUniformMatrix4fv(worldViewProjLoc, 1, GL_FALSE, glm::value_ptr(camera.viewProj));
  270.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
  271.        
  272.     glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
  273.     glBindVertexArray(0);
  274.    
  275.     glfwSwapBuffers(window);
  276.     glfwPollEvents();    
  277.   }
  278.   glfwDestroyWindow(window);
  279.   glfwTerminate();
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement