Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.02 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #ifndef GLU
  3. #define GLU
  4. #define DRAW_WIREFRAME
  5. //#include <GL/gl.h>
  6. //#include <GL/glu.h>
  7. #include <GL/glew.h>
  8. #include <gl/freeglut.h>
  9. #include <glfw3.h>
  10. GLFWwindow* window;
  11.  
  12. // include opencv2
  13. #include <opencv2/core.hpp>
  14. #include <opencv2/imgproc.hpp>
  15. #include <opencv2/highgui.hpp>
  16. using namespace cv;
  17.  
  18.  
  19. #include <glm/glm.hpp>
  20. #include <glm/gtc/matrix_transform.hpp>
  21. using namespace glm;
  22.  
  23. #include <vector>
  24. #include <cmath>
  25.  
  26. #include "common/shader.hpp"
  27.  
  28. // your framework of choice here
  29. extern const int WIN_WIDTH = 800;
  30. extern const int WIN_HEIGHT = 600;
  31.  
  32. class SolidSphere
  33. {
  34. protected:
  35.     std::vector<GLfloat> vertices;
  36.     std::vector<GLfloat> normals;
  37.     std::vector<GLfloat> texcoords;
  38.     std::vector<GLushort> indices;
  39.  
  40. public:
  41.     SolidSphere(float radius, unsigned int rings, unsigned int sectors)
  42.     {
  43.         float const R = 1. / (float)(rings - 1);
  44.         float const S = 1. / (float)(sectors - 1);
  45.         int r, s;
  46.  
  47.         vertices.resize(rings * sectors * 3);
  48.         normals.resize(rings * sectors * 3);
  49.         texcoords.resize(rings * sectors * 2);
  50.         std::vector<GLfloat>::iterator v = vertices.begin();
  51.         std::vector<GLfloat>::iterator n = normals.begin();
  52.         std::vector<GLfloat>::iterator t = texcoords.begin();
  53.         for (r = 0; r < rings; r++) for (s = 0; s < sectors; s++) {
  54.             float const y = sin(-M_PI_2 + M_PI * r * R);
  55.             float const x = cos(2 * M_PI * s * S) * sin(M_PI * r * R);
  56.             float const z = sin(2 * M_PI * s * S) * sin(M_PI * r * R);
  57.  
  58.             *t++ = s*S;
  59.             *t++ = r*R;
  60.  
  61.             *v++ = x * radius;
  62.             *v++ = y * radius;
  63.             *v++ = z * radius;
  64.  
  65.             *n++ = x;
  66.             *n++ = y;
  67.             *n++ = z;
  68.         }
  69.  
  70.         indices.resize(rings * sectors * 4);
  71.         std::vector<GLushort>::iterator i = indices.begin();
  72.         for (r = 0; r < rings - 1; r++) for (s = 0; s < sectors - 1; s++) {
  73.             *i++ = r * sectors + s;
  74.             *i++ = r * sectors + (s + 1);
  75.             *i++ = (r + 1) * sectors + (s + 1);
  76.             *i++ = (r + 1) * sectors + s;
  77.         }
  78.     }
  79.  
  80.     void draw(GLfloat x, GLfloat y, GLfloat z)
  81.     {
  82.         glMatrixMode(GL_MODELVIEW);
  83.         glPushMatrix();
  84.         glTranslatef(x, y, z);
  85.  
  86.         glEnableClientState(GL_VERTEX_ARRAY);
  87.         glEnableClientState(GL_NORMAL_ARRAY);
  88.         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  89.  
  90.         glVertexPointer(0, GL_FLOAT, 0, &vertices[0]);
  91.         glNormalPointer(GL_FLOAT, 0, &normals[0]);
  92.         glTexCoordPointer(1, GL_FLOAT, 0, &texcoords[0]);
  93.         glDrawElements(GL_QUADS, indices.size(), GL_UNSIGNED_SHORT, &indices[0]);
  94.         glPopMatrix();
  95.     }
  96. };
  97.  
  98. SolidSphere sphere(10, 12, 24);
  99.  
  100. void display()
  101. {
  102.     int const win_width = WIN_WIDTH; // retrieve window dimensions from
  103.     int const win_height = WIN_HEIGHT; // framework of choice here
  104.     float const win_aspect = (float)win_width / (float)win_height;
  105.  
  106.     glViewport(0, 0, win_width, win_height);
  107.  
  108.     //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  109.  
  110.     glMatrixMode(GL_PROJECTION);
  111.     glLoadIdentity();
  112.     gluPerspective(45, win_aspect, 1, 10);
  113.  
  114.     glMatrixMode(GL_MODELVIEW);
  115.     glLoadIdentity();
  116.  
  117. #ifdef DRAW_WIREFRAME
  118.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  119. #endif
  120.     sphere.draw(0, 0, -5);
  121. }
  122.  
  123. GLuint matToTexture(cv::Mat &mat, GLenum minFilter, GLenum magFilter, GLenum wrapFilter)
  124. {
  125.     // Generate a number for our textureID's unique handle
  126.     GLuint textureID;
  127.     glGenTextures(1, &textureID);
  128.  
  129.     // Bind to our texture handle
  130.     glBindTexture(GL_TEXTURE_2D, textureID);
  131.  
  132.     // Catch silly-mistake texture interpolation method for magnification
  133.     if (magFilter == GL_LINEAR_MIPMAP_LINEAR ||
  134.         magFilter == GL_LINEAR_MIPMAP_NEAREST ||
  135.         magFilter == GL_NEAREST_MIPMAP_LINEAR ||
  136.         magFilter == GL_NEAREST_MIPMAP_NEAREST)
  137.     {
  138.         fprintf(stderr, "You can't use MIPMAPs for magnification - setting filter to GL_LINEAR\n");
  139.         magFilter = GL_LINEAR;
  140.     }
  141.  
  142.     // Set texture interpolation methods for minification and magnification
  143.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  144.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  145.  
  146.     // Set texture clamping method
  147.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapFilter);
  148.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapFilter);
  149.  
  150.     // Set incoming texture format to:
  151.     // GL_BGR       for CV_CAP_OPENNI_BGR_IMAGE,
  152.     // GL_LUMINANCE for CV_CAP_OPENNI_DISPARITY_MAP,
  153.     // Work out other mappings as required ( there's a list in comments in main() )
  154.     GLenum inputColourFormat = GL_BGR;
  155.     if (mat.channels() == 1)
  156.     {
  157.         inputColourFormat = GL_LUMINANCE;
  158.     }
  159.  
  160.     // Create the texture
  161.     glTexImage2D(GL_TEXTURE_2D,     // Type of texture
  162.         0,                 // Pyramid level (for mip-mapping) - 0 is the top level
  163.         GL_RGB,            // Internal colour format to convert to
  164.         mat.cols,          // Image width  i.e. 640 for Kinect in standard mode
  165.         mat.rows,          // Image height i.e. 480 for Kinect in standard mode
  166.         0,                 // Border width in pixels (can either be 1 or 0)
  167.         inputColourFormat, // Input image format (i.e. GL_RGB, GL_RGBA, GL_BGR etc.)
  168.         GL_UNSIGNED_BYTE,  // Image data type
  169.         mat.ptr()          // The actual image data itself
  170.         );
  171.  
  172.     // If we're using mipmaps then generate them. Note: This requires OpenGL 3.0 or higher
  173.     if (minFilter == GL_LINEAR_MIPMAP_LINEAR ||
  174.         minFilter == GL_LINEAR_MIPMAP_NEAREST ||
  175.         minFilter == GL_NEAREST_MIPMAP_LINEAR ||
  176.         minFilter == GL_NEAREST_MIPMAP_NEAREST)
  177.     {
  178.         glGenerateMipmap(GL_TEXTURE_2D);
  179.     }
  180.  
  181.     return textureID;
  182. }
  183.  
  184.  
  185.  
  186.  
  187. int main(int argc, char *argv[])
  188. {
  189.     if (!glfwInit())
  190.     {
  191.         fprintf(stderr, "Failed to initialize GLFW\n");
  192.         return -1;
  193.     }
  194.  
  195.     glfwWindowHint(GLFW_SAMPLES, 4);
  196.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  197.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  198.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  199.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  200.  
  201.     window = glfwCreateWindow(WIN_WIDTH, WIN_HEIGHT, "Tutorial 05 - Textured Cube", NULL, NULL);
  202.     if (window == NULL) {
  203.         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");
  204.         glfwTerminate();
  205.         return -1;
  206.     }
  207.     glfwMakeContextCurrent(window);
  208.  
  209.     glewExperimental = true;
  210.     if (glewInit() != GLEW_OK) {
  211.         fprintf(stderr, "Failed to initialize GLEW\n");
  212.         return -1;
  213.     }
  214.     glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  215.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  216.     glDisable(GL_DEPTH_TEST);
  217.     glDepthFunc(GL_LESS);
  218.  
  219.  
  220.     GLuint programID = LoadShaders("shaders/TransformVertexShader.vertexshader", "shaders/TextureFragmentShader.fragmentshader");
  221.  
  222.     Mat img = imread("resources/mnt.jpg");
  223.     if (img.empty()) {
  224.         fprintf(stderr, "empty texture!\n");
  225.         return -1;
  226.     }
  227.     GLuint TextureID = matToTexture(img, GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST, GL_CLAMP);
  228.     GLuint MatrixID = glGetUniformLocation(programID, "proj_inv"); // "MVP"
  229.     float ratio = (float)((float)WIN_WIDTH / WIN_HEIGHT);
  230.     glm::mat4 Projection = glm::perspective(45.0f, ratio, 0.1f, 10.0f);
  231.     //glm::mat4 invProjection = glm::inverse(Projection);
  232.  
  233.     // Camera matrix
  234.     glm::mat4 View = glm::lookAt(
  235.     glm::vec3(0, 0, 0), // Camera is at (0,0,0), in World Space
  236.     glm::vec3(0, 0, 0), // and looks at the origin
  237.     glm::vec3(0, 1, 0)  // Head is up (set to 0,-1,0 to look upside-down)
  238.     );
  239.     // Model matrix : an identity matrix (model will be at the origin)
  240.     glm::mat4 Model = glm::mat4(1.0f);
  241.     // Our ModelViewProjection : multiplication of our 3 matrices
  242.     glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
  243.  
  244.     do {
  245.  
  246.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  247.         glUseProgram(programID);
  248.         glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
  249.         // Bind our texture in Texture Unit 0
  250.         glActiveTexture(GL_TEXTURE0);
  251.         glBindTexture(GL_TEXTURE_2D, TextureID);
  252.         // Set our "myTextureSampler" sampler to user Texture Unit 0
  253.         glUniform1i(TextureID, 0);
  254.  
  255.         // Clear the screen
  256.         display();
  257.  
  258.         // Swap buffers
  259.         glfwSwapBuffers(window);
  260.         glfwPollEvents();
  261.  
  262.     } // Check if the ESC key was pressed or the window was closed
  263.     while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
  264.         glfwWindowShouldClose(window) == 0);
  265.     display();
  266.     return 0;
  267. }
  268.  
  269. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement