Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.13 KB | None | 0 0
  1. // Include standard headers
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // Include GLEW
  6. #include <GL/glew.h>
  7.  
  8. // Include GLFW
  9. #include <glfw3.h>
  10. GLFWwindow* window;
  11.  
  12. // Include GLM
  13. #include <glm/glm.hpp>
  14. #include <glm/gtc/matrix_transform.hpp>
  15. using namespace glm;
  16.  
  17. #include <common/shader.hpp>
  18. #include <common/texture.hpp>
  19.  
  20. int width = 640;
  21. int height = 480;
  22. unsigned char *data;
  23.  
  24. void fill_rect(unsigned char *data, int stride, int x, int y, int w, int h, unsigned char r, unsigned char g, unsigned char b){
  25.     for(int i=y; i<y+h; ++i){
  26.         for(int j=x; j<x+w; ++j){
  27.             data[3*(stride*i+j)+0] = b;
  28.             data[3*(stride*i+j)+1] = g;
  29.             data[3*(stride*i+j)+2] = r;
  30.         }
  31.     }
  32. }
  33.  
  34.  
  35. int main( void )
  36. {
  37.     // Initialise GLFW
  38.     if( !glfwInit() )
  39.     {
  40.         fprintf( stderr, "Failed to initialize GLFW\n" );
  41.         return -1;
  42.     }
  43.  
  44.     glfwWindowHint(GLFW_SAMPLES, 4);
  45.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  46.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
  47.  
  48.  
  49.     // Open a window and create its OpenGL context
  50.     window = glfwCreateWindow( 1024, 768, "Tutorial 05 - Textured Cube", NULL, NULL);
  51.     if( window == NULL ){
  52.         fprintf( stderr, "Failed to open GLFW window.\n" );
  53.         glfwTerminate();
  54.         return -1;
  55.     }
  56.     glfwMakeContextCurrent(window);
  57.  
  58.     // Initialize GLEW
  59.     if (glewInit() != GLEW_OK) {
  60.         fprintf(stderr, "Failed to initialize GLEW\n");
  61.         return -1;
  62.     }
  63.  
  64.     // Ensure we can capture the escape key being pressed below
  65.     glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  66.  
  67.     // Dark blue background
  68.     glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  69.  
  70.     // Enable depth test
  71.     glEnable(GL_DEPTH_TEST);
  72.     // Accept fragment if it closer to the camera than the former one
  73.     glDepthFunc(GL_LESS);
  74.  
  75.     // Create and compile our GLSL program from the shaders
  76.     GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "TextureFragmentShader.fragmentshader" );
  77.  
  78.     // Get a handle for our "MVP" uniform
  79.     GLuint MatrixID = glGetUniformLocation(programID, "MVP");
  80.  
  81.     // Get a handle for our buffers
  82.     GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
  83.     GLuint vertexUVID = glGetAttribLocation(programID, "vertexUV");
  84.  
  85.     // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
  86.     glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
  87.     // Camera matrix
  88.     glm::mat4 View       = glm::lookAt(
  89.                                 glm::vec3(4,3,3), // Camera is at (4,3,3), in World Space
  90.                                 glm::vec3(0,0,0), // and looks at the origin
  91.                                 glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
  92.                            );
  93.     // Model matrix : an identity matrix (model will be at the origin)
  94.     glm::mat4 Model      = glm::mat4(1.0f);
  95.     // Our ModelViewProjection : multiplication of our 3 matrices
  96.     glm::mat4 MVP        = Projection * View * Model; // Remember, matrix multiplication is the other way around
  97.  
  98.     // Load the texture using any two methods
  99.     //GLuint Texture = loadBMP_custom("uvtemplate.bmp");
  100. #if 0  
  101.     GLuint Texture = loadDDS("uvtemplate.DDS");
  102. #else
  103.     GLuint Texture;
  104.     glGenTextures(1, &Texture);
  105.     // "Bind" the newly created texture : all future texture functions will modify this texture
  106.     glBindTexture(GL_TEXTURE_2D, Texture);
  107.    
  108.     data = new unsigned char[3*width*height];
  109.  
  110.     // Give the image to OpenGL
  111.     glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
  112.  
  113.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  114.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  115.  
  116. #endif 
  117.     // Get a handle for our "myTextureSampler" uniform
  118.     GLuint TextureID  = glGetUniformLocation(programID, "myTextureSampler");
  119.  
  120.     // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
  121.     // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
  122.     static const GLfloat g_vertex_buffer_data[] = {
  123.         -1.0f,-1.0f,-1.0f,
  124.         -1.0f,-1.0f, 1.0f,
  125.         -1.0f, 1.0f, 1.0f,
  126.          1.0f, 1.0f,-1.0f,
  127.         -1.0f,-1.0f,-1.0f,
  128.         -1.0f, 1.0f,-1.0f,
  129.          1.0f,-1.0f, 1.0f,
  130.         -1.0f,-1.0f,-1.0f,
  131.          1.0f,-1.0f,-1.0f,
  132.          1.0f, 1.0f,-1.0f,
  133.          1.0f,-1.0f,-1.0f,
  134.         -1.0f,-1.0f,-1.0f,
  135.         -1.0f,-1.0f,-1.0f,
  136.         -1.0f, 1.0f, 1.0f,
  137.         -1.0f, 1.0f,-1.0f,
  138.          1.0f,-1.0f, 1.0f,
  139.         -1.0f,-1.0f, 1.0f,
  140.         -1.0f,-1.0f,-1.0f,
  141.         -1.0f, 1.0f, 1.0f,
  142.         -1.0f,-1.0f, 1.0f,
  143.          1.0f,-1.0f, 1.0f,
  144.          1.0f, 1.0f, 1.0f,
  145.          1.0f,-1.0f,-1.0f,
  146.          1.0f, 1.0f,-1.0f,
  147.          1.0f,-1.0f,-1.0f,
  148.          1.0f, 1.0f, 1.0f,
  149.          1.0f,-1.0f, 1.0f,
  150.          1.0f, 1.0f, 1.0f,
  151.          1.0f, 1.0f,-1.0f,
  152.         -1.0f, 1.0f,-1.0f,
  153.          1.0f, 1.0f, 1.0f,
  154.         -1.0f, 1.0f,-1.0f,
  155.         -1.0f, 1.0f, 1.0f,
  156.          1.0f, 1.0f, 1.0f,
  157.         -1.0f, 1.0f, 1.0f,
  158.          1.0f,-1.0f, 1.0f
  159.     };
  160.  
  161.     // Two UV coordinatesfor each vertex. They were created withe Blender.
  162.     static const GLfloat g_uv_buffer_data[] = {
  163.         0.000f, 1.0f-0.000f,
  164.         0.000f, 1.0f-0.333f,
  165.         0.333f, 1.0f-0.333f,
  166.         1.000f, 1.0f-0.000f,
  167.         0.667f, 1.0f-0.333f,
  168.         1.000f, 1.0f-0.333f,
  169.         0.667f, 1.0f-0.333f,
  170.         0.333f, 1.0f-0.667f,
  171.         0.667f, 1.0f-0.667f,
  172.         1.000f, 1.0f-0.000f,
  173.         0.667f, 1.0f-0.000f,
  174.         0.667f, 1.0f-0.333f,
  175.         0.000f, 1.0f-0.000f,
  176.         0.333f, 1.0f-0.333f,
  177.         0.333f, 1.0f-0.000f,
  178.         0.667f, 1.0f-0.333f,
  179.         0.333f, 1.0f-0.333f,
  180.         0.333f, 1.0f-0.667f,
  181.         1.000f, 1.0f-0.667f,
  182.         1.000f, 1.0f-0.333f,
  183.         0.667f, 1.0f-0.333f,
  184.         0.667f, 1.0f-0.000f,
  185.         0.333f, 1.0f-0.333f,
  186.         0.667f, 1.0f-0.333f,
  187.         0.333f, 1.0f-0.333f,
  188.         0.667f, 1.0f-0.000f,
  189.         0.333f, 1.0f-0.000f,
  190.         0.000f, 1.0f-0.333f,
  191.         0.000f, 1.0f-0.667f,
  192.         0.333f, 1.0f-0.667f,
  193.         0.000f, 1.0f-0.333f,
  194.         0.333f, 1.0f-0.667f,
  195.         0.333f, 1.0f-0.333f,
  196.         0.667f, 1.0f-0.667f,
  197.         1.000f, 1.0f-0.667f,
  198.         0.667f, 1.0f-0.333f
  199.     };
  200.  
  201.     GLuint vertexbuffer;
  202.     glGenBuffers(1, &vertexbuffer);
  203.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  204.     glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
  205.  
  206.     GLuint uvbuffer;
  207.     glGenBuffers(1, &uvbuffer);
  208.     glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
  209.     glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
  210.  
  211.     int i = 0;
  212.  
  213.     do{
  214.  
  215.         // Clear the screen
  216.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  217.  
  218.         // Use our shader
  219.         glUseProgram(programID);
  220.  
  221.         // Our ModelViewProjection : multiplication of our 3 matrices
  222.         glm::mat4 MVP        = Projection * View * Model; // Remember, matrix multiplication is the other way around
  223.  
  224.         Model = glm::rotate( Model, glm::radians(1.0f), glm::vec3(0,1,0) );
  225.  
  226.         if(i<10){
  227.             View = glm::translate( View, glm::vec3(-0.1,0,0) );
  228.             i++;
  229.         }
  230.         else if(i<20){
  231.             View = glm::translate( View, glm::vec3(0.1,0,0) );
  232.             i++;
  233.         }
  234.         else{
  235.             i=0;
  236.             Model = glm::rotate( Model, glm::radians(90.0f), glm::vec3(0,1,0) );
  237.         }
  238.  
  239.         // Send our transformation to the currently bound shader,
  240.         // in the "MVP" uniform
  241.         glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
  242.  
  243.         // Bind our texture in Texture Unit 0
  244.         glActiveTexture(GL_TEXTURE0);
  245.         glBindTexture(GL_TEXTURE_2D, Texture);
  246.  
  247.         static unsigned char r;
  248.         static unsigned char g;
  249.         static unsigned char b;
  250.         fill_rect(data, width, 0, 0, width, height, r, g, b);
  251.         r += 5;
  252.         g += 11;
  253.         b += 17;
  254.  
  255.         // Give the image to OpenGL
  256.         glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
  257.  
  258.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  259.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  260.  
  261.         // Set our "myTextureSampler" sampler to user Texture Unit 0
  262.         glUniform1i(TextureID, 0);
  263.  
  264.         // 1rst attribute buffer : vertices
  265.         glEnableVertexAttribArray(vertexPosition_modelspaceID);
  266.         glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  267.         glVertexAttribPointer(
  268.             vertexPosition_modelspaceID,  // The attribute we want to configure
  269.             3,                            // size
  270.             GL_FLOAT,                     // type
  271.             GL_FALSE,                     // normalized?
  272.             0,                            // stride
  273.             (void*)0                      // array buffer offset
  274.         );
  275.  
  276.         // 2nd attribute buffer : UVs
  277.         glEnableVertexAttribArray(vertexUVID);
  278.         glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
  279.         glVertexAttribPointer(
  280.             vertexUVID,                   // The attribute we want to configure
  281.             2,                            // size : U+V => 2
  282.             GL_FLOAT,                     // type
  283.             GL_FALSE,                     // normalized?
  284.             0,                            // stride
  285.             (void*)0                      // array buffer offset
  286.         );
  287.  
  288.         // Draw the triangles !
  289.         glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles
  290.  
  291.         glDisableVertexAttribArray(vertexPosition_modelspaceID);
  292.         glDisableVertexAttribArray(vertexUVID);
  293.  
  294.         // Swap buffers
  295.         glfwSwapBuffers(window);
  296.         glfwPollEvents();
  297.  
  298.     } // Check if the ESC key was pressed or the window was closed
  299.     while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
  300.            glfwWindowShouldClose(window) == 0 );
  301.  
  302.     // Cleanup VBO and shader
  303.     glDeleteBuffers(1, &vertexbuffer);
  304.     glDeleteBuffers(1, &uvbuffer);
  305.     glDeleteProgram(programID);
  306.     glDeleteTextures(1, &TextureID);
  307.  
  308.     // Close OpenGL window and terminate GLFW
  309.     glfwTerminate();
  310.  
  311.     delete[] data;
  312.  
  313.     return 0;
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement