Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #ifndef GLU
- #define GLU
- #define DRAW_WIREFRAME
- //#include <GL/gl.h>
- //#include <GL/glu.h>
- #include <GL/glew.h>
- #include <gl/freeglut.h>
- #include <glfw3.h>
- GLFWwindow* window;
- // include opencv2
- #include <opencv2/core.hpp>
- #include <opencv2/imgproc.hpp>
- #include <opencv2/highgui.hpp>
- using namespace cv;
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- using namespace glm;
- #include <vector>
- #include <cmath>
- #include "common/shader.hpp"
- // your framework of choice here
- extern const int WIN_WIDTH = 800;
- extern const int WIN_HEIGHT = 600;
- class SolidSphere
- {
- protected:
- std::vector<GLfloat> vertices;
- std::vector<GLfloat> normals;
- std::vector<GLfloat> texcoords;
- std::vector<GLushort> indices;
- public:
- SolidSphere(float radius, unsigned int rings, unsigned int sectors)
- {
- float const R = 1. / (float)(rings - 1);
- float const S = 1. / (float)(sectors - 1);
- int r, s;
- vertices.resize(rings * sectors * 3);
- normals.resize(rings * sectors * 3);
- texcoords.resize(rings * sectors * 2);
- std::vector<GLfloat>::iterator v = vertices.begin();
- std::vector<GLfloat>::iterator n = normals.begin();
- std::vector<GLfloat>::iterator t = texcoords.begin();
- for (r = 0; r < rings; r++) for (s = 0; s < sectors; s++) {
- float const y = sin(-M_PI_2 + M_PI * r * R);
- float const x = cos(2 * M_PI * s * S) * sin(M_PI * r * R);
- float const z = sin(2 * M_PI * s * S) * sin(M_PI * r * R);
- *t++ = s*S;
- *t++ = r*R;
- *v++ = x * radius;
- *v++ = y * radius;
- *v++ = z * radius;
- *n++ = x;
- *n++ = y;
- *n++ = z;
- }
- indices.resize(rings * sectors * 4);
- std::vector<GLushort>::iterator i = indices.begin();
- for (r = 0; r < rings - 1; r++) for (s = 0; s < sectors - 1; s++) {
- *i++ = r * sectors + s;
- *i++ = r * sectors + (s + 1);
- *i++ = (r + 1) * sectors + (s + 1);
- *i++ = (r + 1) * sectors + s;
- }
- }
- void draw(GLfloat x, GLfloat y, GLfloat z)
- {
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- glTranslatef(x, y, z);
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_NORMAL_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glVertexPointer(0, GL_FLOAT, 0, &vertices[0]);
- glNormalPointer(GL_FLOAT, 0, &normals[0]);
- glTexCoordPointer(1, GL_FLOAT, 0, &texcoords[0]);
- glDrawElements(GL_QUADS, indices.size(), GL_UNSIGNED_SHORT, &indices[0]);
- glPopMatrix();
- }
- };
- SolidSphere sphere(10, 12, 24);
- void display()
- {
- int const win_width = WIN_WIDTH; // retrieve window dimensions from
- int const win_height = WIN_HEIGHT; // framework of choice here
- float const win_aspect = (float)win_width / (float)win_height;
- glViewport(0, 0, win_width, win_height);
- //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45, win_aspect, 1, 10);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- #ifdef DRAW_WIREFRAME
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- #endif
- sphere.draw(0, 0, -5);
- }
- GLuint matToTexture(cv::Mat &mat, GLenum minFilter, GLenum magFilter, GLenum wrapFilter)
- {
- // Generate a number for our textureID's unique handle
- GLuint textureID;
- glGenTextures(1, &textureID);
- // Bind to our texture handle
- glBindTexture(GL_TEXTURE_2D, textureID);
- // Catch silly-mistake texture interpolation method for magnification
- if (magFilter == GL_LINEAR_MIPMAP_LINEAR ||
- magFilter == GL_LINEAR_MIPMAP_NEAREST ||
- magFilter == GL_NEAREST_MIPMAP_LINEAR ||
- magFilter == GL_NEAREST_MIPMAP_NEAREST)
- {
- fprintf(stderr, "You can't use MIPMAPs for magnification - setting filter to GL_LINEAR\n");
- magFilter = GL_LINEAR;
- }
- // Set texture interpolation methods for minification and magnification
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
- // Set texture clamping method
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapFilter);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapFilter);
- // Set incoming texture format to:
- // GL_BGR for CV_CAP_OPENNI_BGR_IMAGE,
- // GL_LUMINANCE for CV_CAP_OPENNI_DISPARITY_MAP,
- // Work out other mappings as required ( there's a list in comments in main() )
- GLenum inputColourFormat = GL_BGR;
- if (mat.channels() == 1)
- {
- inputColourFormat = GL_LUMINANCE;
- }
- // Create the texture
- glTexImage2D(GL_TEXTURE_2D, // Type of texture
- 0, // Pyramid level (for mip-mapping) - 0 is the top level
- GL_RGB, // Internal colour format to convert to
- mat.cols, // Image width i.e. 640 for Kinect in standard mode
- mat.rows, // Image height i.e. 480 for Kinect in standard mode
- 0, // Border width in pixels (can either be 1 or 0)
- inputColourFormat, // Input image format (i.e. GL_RGB, GL_RGBA, GL_BGR etc.)
- GL_UNSIGNED_BYTE, // Image data type
- mat.ptr() // The actual image data itself
- );
- // If we're using mipmaps then generate them. Note: This requires OpenGL 3.0 or higher
- if (minFilter == GL_LINEAR_MIPMAP_LINEAR ||
- minFilter == GL_LINEAR_MIPMAP_NEAREST ||
- minFilter == GL_NEAREST_MIPMAP_LINEAR ||
- minFilter == GL_NEAREST_MIPMAP_NEAREST)
- {
- glGenerateMipmap(GL_TEXTURE_2D);
- }
- return textureID;
- }
- int main(int argc, char *argv[])
- {
- if (!glfwInit())
- {
- fprintf(stderr, "Failed to initialize GLFW\n");
- return -1;
- }
- glfwWindowHint(GLFW_SAMPLES, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- window = glfwCreateWindow(WIN_WIDTH, WIN_HEIGHT, "Tutorial 05 - Textured Cube", NULL, NULL);
- if (window == NULL) {
- 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");
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- glewExperimental = true;
- if (glewInit() != GLEW_OK) {
- fprintf(stderr, "Failed to initialize GLEW\n");
- return -1;
- }
- glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glDisable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- GLuint programID = LoadShaders("shaders/TransformVertexShader.vertexshader", "shaders/TextureFragmentShader.fragmentshader");
- Mat img = imread("resources/mnt.jpg");
- if (img.empty()) {
- fprintf(stderr, "empty texture!\n");
- return -1;
- }
- GLuint TextureID = matToTexture(img, GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST, GL_CLAMP);
- GLuint MatrixID = glGetUniformLocation(programID, "proj_inv"); // "MVP"
- float ratio = (float)((float)WIN_WIDTH / WIN_HEIGHT);
- glm::mat4 Projection = glm::perspective(45.0f, ratio, 0.1f, 10.0f);
- //glm::mat4 invProjection = glm::inverse(Projection);
- // Camera matrix
- glm::mat4 View = glm::lookAt(
- glm::vec3(0, 0, 0), // Camera is at (0,0,0), in World Space
- glm::vec3(0, 0, 0), // and looks at the origin
- glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
- );
- // Model matrix : an identity matrix (model will be at the origin)
- glm::mat4 Model = glm::mat4(1.0f);
- // Our ModelViewProjection : multiplication of our 3 matrices
- glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
- do {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glUseProgram(programID);
- glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
- // Bind our texture in Texture Unit 0
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, TextureID);
- // Set our "myTextureSampler" sampler to user Texture Unit 0
- glUniform1i(TextureID, 0);
- // Clear the screen
- display();
- // Swap buffers
- glfwSwapBuffers(window);
- glfwPollEvents();
- } // Check if the ESC key was pressed or the window was closed
- while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
- glfwWindowShouldClose(window) == 0);
- display();
- return 0;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement