Advertisement
Guest User

Untitled

a guest
Jul 4th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.12 KB | None | 0 0
  1. // Include standard headers
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <vector>
  5. #include <fstream>
  6. #include <string>
  7. #include <iostream>
  8. #include <sstream>
  9.  
  10. // Include GLEW
  11. #include <GL/glew.h>
  12.  
  13. // Include GLFW
  14. #include <GL/glfw.h>
  15.  
  16. // Include GLM
  17. #include <glm/glm.hpp>
  18. #include <glm/gtc/matrix_transform.hpp>
  19. using namespace glm;
  20.  
  21. #include <common/shader.hpp>
  22. #include <common/texture.hpp>
  23. #include <common/controls.hpp>
  24.  
  25. using namespace std;
  26.  
  27. struct mycolor
  28. {
  29.     mycolor() : r(0.0), g(0.0), b(0.0) {}
  30.     GLfloat r,g,b;
  31. };
  32.  
  33. int main( void )
  34. {
  35.     // Initialise GLFW
  36.     if( !glfwInit() )
  37.     {
  38.         fprintf( stderr, "Failed to initialize GLFW\n" );
  39.         return -1;
  40.     }
  41.  
  42.     glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
  43.     glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
  44.     glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
  45.     glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  46.  
  47.     // Open a window and create its OpenGL context
  48.     if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
  49.     {
  50.         fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
  51.         glfwTerminate();
  52.         return -1;
  53.     }
  54.  
  55.     // Initialize GLEW
  56.     glewExperimental = GL_TRUE; // Recommended fix for access violation
  57.     if (glewInit() != GLEW_OK) {
  58.         fprintf(stderr, "Failed to initialize GLEW\n");
  59.         return -1;
  60.     }
  61.  
  62.     glfwSetWindowTitle( "Tutorial 06" );
  63.  
  64.     // Ensure we can capture the escape key being pressed below
  65.     glfwEnable( GLFW_STICKY_KEYS );
  66.     glfwSetMousePos(1024/2, 768/2);
  67.  
  68.     // Background
  69.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  70.  
  71.     // Enable depth test
  72.     //glEnable(GL_DEPTH_TEST);
  73.     // Accept fragment if it closer to the camera than the former one
  74.     //glDepthFunc(GL_LESS);
  75.  
  76.     // Cull triangles which normal is not towards the camera
  77.     glEnable(GL_CULL_FACE);
  78.  
  79.     glEnable(GL_BLEND);
  80.     glBlendFunc(GL_ONE, GL_ONE);
  81.  
  82.     GLuint VertexArrayID;
  83.     glGenVertexArrays(1, &VertexArrayID);
  84.     glBindVertexArray(VertexArrayID);
  85.  
  86.     // Create and compile our GLSL program from the shaders
  87.     GLuint programID = LoadShaders( "C:\\OpenGL\\Sandbox\\OpenGLSandbox\\TransformVertexShader.vertexshader", "C:\\OpenGL\\Sandbox\\OpenGLSandbox\\ColorFragmentShader.fragmentshader" );
  88.  
  89.     // Get a handle for our "MVP" uniform
  90.     GLuint MatrixID = glGetUniformLocation(programID, "MVP");
  91.  
  92.     vector<GLfloat> vertex_buffer_data;
  93.     vector<mycolor> color_buffer_data;
  94.  
  95.    
  96.     ifstream ifp("c:\\sopg1.input.txt");
  97.  
  98.     string strLine;
  99.     getline(ifp, strLine); // get rid of size
  100.     while (getline(ifp, strLine))
  101.     {
  102.         cout << strLine << endl;
  103.         string strToken;
  104.         stringstream ss(strLine);
  105.         getline(ss, strToken, ' ');
  106.         mycolor col;
  107.         if (strToken == "R")
  108.         {
  109.             col.r = 1.0;
  110.         }
  111.         else
  112.         {
  113.             col.b = 1.0;
  114.         }
  115.         color_buffer_data.push_back(col);
  116.         color_buffer_data.push_back(col);
  117.         color_buffer_data.push_back(col);
  118.         color_buffer_data.push_back(col);
  119.         color_buffer_data.push_back(col);
  120.         color_buffer_data.push_back(col);
  121.  
  122.  
  123.         GLfloat l, t, w, h;
  124.  
  125.         getline(ss, strToken, ' ');
  126.         l = atoi(strToken.c_str()) / 1000.0;
  127.         l -= 2.2;
  128.  
  129.         getline(ss, strToken, ' ');
  130.         t = atoi(strToken.c_str()) / 1000.0;
  131.         t -= 2.2;
  132.  
  133.         getline(ss, strToken, ' ');
  134.         w = atoi(strToken.c_str()) / 1000.0;
  135.  
  136.         getline(ss, strToken, ' ');
  137.         h = atoi(strToken.c_str()) / 1000.0;
  138.  
  139.         vertex_buffer_data.push_back(l);
  140.         vertex_buffer_data.push_back(t);
  141.         vertex_buffer_data.push_back(0.0);
  142.  
  143.         vertex_buffer_data.push_back(l);
  144.         vertex_buffer_data.push_back(t - h);
  145.         vertex_buffer_data.push_back(0.0);
  146.  
  147.         vertex_buffer_data.push_back(l + w);
  148.         vertex_buffer_data.push_back(t - h);
  149.         vertex_buffer_data.push_back(0.0);
  150.  
  151.         vertex_buffer_data.push_back(l);
  152.         vertex_buffer_data.push_back(t);
  153.         vertex_buffer_data.push_back(0.0);
  154.  
  155.         vertex_buffer_data.push_back(l + w);
  156.         vertex_buffer_data.push_back(t - h);
  157.         vertex_buffer_data.push_back(0.0);
  158.  
  159.         vertex_buffer_data.push_back(l + w);
  160.         vertex_buffer_data.push_back(t);
  161.         vertex_buffer_data.push_back(0.0);
  162.     }
  163.  
  164.     GLuint vertexbuffer;
  165.     glGenBuffers(1, &vertexbuffer);
  166.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  167.     glBufferData(GL_ARRAY_BUFFER, vertex_buffer_data.size() * sizeof(GL_FLOAT), &vertex_buffer_data[0], GL_STATIC_DRAW);
  168.  
  169.     GLuint colorBuffer;
  170.     glGenBuffers(1, &colorBuffer);
  171.     glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
  172.     glBufferData(GL_ARRAY_BUFFER, color_buffer_data.size() * sizeof(mycolor), &color_buffer_data[0], GL_STATIC_DRAW);
  173.  
  174.     do{
  175.  
  176.         // Clear the screen
  177.         glClear(GL_COLOR_BUFFER_BIT);
  178.  
  179.         // Use our shader
  180.         glUseProgram(programID);
  181.  
  182.         // Compute the MVP matrix from keyboard and mouse input
  183.         computeMatricesFromInputs();
  184.         glm::mat4 ProjectionMatrix = getProjectionMatrix();
  185.         glm::mat4 ViewMatrix = getViewMatrix();
  186.         glm::mat4 ModelMatrix = glm::mat4(1.0);
  187.         glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
  188.  
  189.         // Send our transformation to the currently bound shader,
  190.         // in the "MVP" uniform
  191.         glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
  192.  
  193.         // 1rst attribute buffer : vertices
  194.         glEnableVertexAttribArray(0);
  195.         glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  196.         glVertexAttribPointer(
  197.             0,                  // attribute. No particular reason for 0, but must match the layout in the shader.
  198.             3,                  // size
  199.             GL_FLOAT,           // type
  200.             GL_FALSE,           // normalized?
  201.             0,                  // stride
  202.             (void*)0            // array buffer offset
  203.         );
  204.  
  205.         // Colors
  206.         glEnableVertexAttribArray(1);
  207.         glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
  208.         glVertexAttribPointer(
  209.             1,
  210.             3,
  211.             GL_FLOAT,
  212.             GL_FALSE,
  213.             0,
  214.             (void *)0);
  215.  
  216.  
  217.         // Draw the triangle !
  218.         glDrawArrays(GL_TRIANGLES, 0, vertex_buffer_data.size());
  219.  
  220.         glDisableVertexAttribArray(0);
  221.         glDisableVertexAttribArray(1);
  222.  
  223.         // Swap buffers
  224.         glfwSwapBuffers();
  225.  
  226.     } // Check if the ESC key was pressed or the window was closed
  227.     while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
  228.            glfwGetWindowParam( GLFW_OPENED ) );
  229.  
  230.     // Cleanup VBO and shader
  231.     glDeleteBuffers(1, &vertexbuffer);
  232.     glDeleteProgram(programID);
  233.  
  234.     // Close OpenGL window and terminate GLFW
  235.     glfwTerminate();
  236.  
  237.     return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement