Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Include standard headers
- #include <stdio.h>
- #include <stdlib.h>
- // Include GLEW
- #include <GL/glew.h>
- // Include GLFW
- #include <glfw3.h>
- GLFWwindow* window;
- // Include GLM
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- using namespace glm;
- #include <common/shader.hpp>
- #include <common/texture.hpp>
- int width = 640;
- int height = 480;
- unsigned char *data;
- 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){
- for(int i=y; i<y+h; ++i){
- for(int j=x; j<x+w; ++j){
- data[3*(stride*i+j)+0] = b;
- data[3*(stride*i+j)+1] = g;
- data[3*(stride*i+j)+2] = r;
- }
- }
- }
- int main( void )
- {
- // Initialise GLFW
- if( !glfwInit() )
- {
- fprintf( stderr, "Failed to initialize GLFW\n" );
- return -1;
- }
- glfwWindowHint(GLFW_SAMPLES, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
- // Open a window and create its OpenGL context
- window = glfwCreateWindow( 1024, 768, "Tutorial 05 - Textured Cube", NULL, NULL);
- if( window == NULL ){
- fprintf( stderr, "Failed to open GLFW window.\n" );
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- // Initialize GLEW
- if (glewInit() != GLEW_OK) {
- fprintf(stderr, "Failed to initialize GLEW\n");
- return -1;
- }
- // Ensure we can capture the escape key being pressed below
- glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
- // Dark blue background
- glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
- // Enable depth test
- glEnable(GL_DEPTH_TEST);
- // Accept fragment if it closer to the camera than the former one
- glDepthFunc(GL_LESS);
- // Create and compile our GLSL program from the shaders
- GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "TextureFragmentShader.fragmentshader" );
- // Get a handle for our "MVP" uniform
- GLuint MatrixID = glGetUniformLocation(programID, "MVP");
- // Get a handle for our buffers
- GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace");
- GLuint vertexUVID = glGetAttribLocation(programID, "vertexUV");
- // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
- glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
- // Camera matrix
- glm::mat4 View = glm::lookAt(
- glm::vec3(4,3,3), // Camera is at (4,3,3), 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
- // Load the texture using any two methods
- //GLuint Texture = loadBMP_custom("uvtemplate.bmp");
- #if 0
- GLuint Texture = loadDDS("uvtemplate.DDS");
- #else
- GLuint Texture;
- glGenTextures(1, &Texture);
- // "Bind" the newly created texture : all future texture functions will modify this texture
- glBindTexture(GL_TEXTURE_2D, Texture);
- data = new unsigned char[3*width*height];
- // Give the image to OpenGL
- glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- #endif
- // Get a handle for our "myTextureSampler" uniform
- GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler");
- // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
- // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
- static const GLfloat g_vertex_buffer_data[] = {
- -1.0f,-1.0f,-1.0f,
- -1.0f,-1.0f, 1.0f,
- -1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f,-1.0f,
- -1.0f,-1.0f,-1.0f,
- -1.0f, 1.0f,-1.0f,
- 1.0f,-1.0f, 1.0f,
- -1.0f,-1.0f,-1.0f,
- 1.0f,-1.0f,-1.0f,
- 1.0f, 1.0f,-1.0f,
- 1.0f,-1.0f,-1.0f,
- -1.0f,-1.0f,-1.0f,
- -1.0f,-1.0f,-1.0f,
- -1.0f, 1.0f, 1.0f,
- -1.0f, 1.0f,-1.0f,
- 1.0f,-1.0f, 1.0f,
- -1.0f,-1.0f, 1.0f,
- -1.0f,-1.0f,-1.0f,
- -1.0f, 1.0f, 1.0f,
- -1.0f,-1.0f, 1.0f,
- 1.0f,-1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f,-1.0f,-1.0f,
- 1.0f, 1.0f,-1.0f,
- 1.0f,-1.0f,-1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f,-1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f,-1.0f,
- -1.0f, 1.0f,-1.0f,
- 1.0f, 1.0f, 1.0f,
- -1.0f, 1.0f,-1.0f,
- -1.0f, 1.0f, 1.0f,
- 1.0f, 1.0f, 1.0f,
- -1.0f, 1.0f, 1.0f,
- 1.0f,-1.0f, 1.0f
- };
- // Two UV coordinatesfor each vertex. They were created withe Blender.
- static const GLfloat g_uv_buffer_data[] = {
- 0.000f, 1.0f-0.000f,
- 0.000f, 1.0f-0.333f,
- 0.333f, 1.0f-0.333f,
- 1.000f, 1.0f-0.000f,
- 0.667f, 1.0f-0.333f,
- 1.000f, 1.0f-0.333f,
- 0.667f, 1.0f-0.333f,
- 0.333f, 1.0f-0.667f,
- 0.667f, 1.0f-0.667f,
- 1.000f, 1.0f-0.000f,
- 0.667f, 1.0f-0.000f,
- 0.667f, 1.0f-0.333f,
- 0.000f, 1.0f-0.000f,
- 0.333f, 1.0f-0.333f,
- 0.333f, 1.0f-0.000f,
- 0.667f, 1.0f-0.333f,
- 0.333f, 1.0f-0.333f,
- 0.333f, 1.0f-0.667f,
- 1.000f, 1.0f-0.667f,
- 1.000f, 1.0f-0.333f,
- 0.667f, 1.0f-0.333f,
- 0.667f, 1.0f-0.000f,
- 0.333f, 1.0f-0.333f,
- 0.667f, 1.0f-0.333f,
- 0.333f, 1.0f-0.333f,
- 0.667f, 1.0f-0.000f,
- 0.333f, 1.0f-0.000f,
- 0.000f, 1.0f-0.333f,
- 0.000f, 1.0f-0.667f,
- 0.333f, 1.0f-0.667f,
- 0.000f, 1.0f-0.333f,
- 0.333f, 1.0f-0.667f,
- 0.333f, 1.0f-0.333f,
- 0.667f, 1.0f-0.667f,
- 1.000f, 1.0f-0.667f,
- 0.667f, 1.0f-0.333f
- };
- GLuint vertexbuffer;
- glGenBuffers(1, &vertexbuffer);
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
- GLuint uvbuffer;
- glGenBuffers(1, &uvbuffer);
- glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
- int i = 0;
- do{
- // Clear the screen
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Use our shader
- glUseProgram(programID);
- // Our ModelViewProjection : multiplication of our 3 matrices
- glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
- Model = glm::rotate( Model, glm::radians(1.0f), glm::vec3(0,1,0) );
- if(i<10){
- View = glm::translate( View, glm::vec3(-0.1,0,0) );
- i++;
- }
- else if(i<20){
- View = glm::translate( View, glm::vec3(0.1,0,0) );
- i++;
- }
- else{
- i=0;
- Model = glm::rotate( Model, glm::radians(90.0f), glm::vec3(0,1,0) );
- }
- // Send our transformation to the currently bound shader,
- // in the "MVP" uniform
- glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
- // Bind our texture in Texture Unit 0
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, Texture);
- static unsigned char r;
- static unsigned char g;
- static unsigned char b;
- fill_rect(data, width, 0, 0, width, height, r, g, b);
- r += 5;
- g += 11;
- b += 17;
- // Give the image to OpenGL
- glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- // Set our "myTextureSampler" sampler to user Texture Unit 0
- glUniform1i(TextureID, 0);
- // 1rst attribute buffer : vertices
- glEnableVertexAttribArray(vertexPosition_modelspaceID);
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- glVertexAttribPointer(
- vertexPosition_modelspaceID, // The attribute we want to configure
- 3, // size
- GL_FLOAT, // type
- GL_FALSE, // normalized?
- 0, // stride
- (void*)0 // array buffer offset
- );
- // 2nd attribute buffer : UVs
- glEnableVertexAttribArray(vertexUVID);
- glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
- glVertexAttribPointer(
- vertexUVID, // The attribute we want to configure
- 2, // size : U+V => 2
- GL_FLOAT, // type
- GL_FALSE, // normalized?
- 0, // stride
- (void*)0 // array buffer offset
- );
- // Draw the triangles !
- glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles
- glDisableVertexAttribArray(vertexPosition_modelspaceID);
- glDisableVertexAttribArray(vertexUVID);
- // 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 );
- // Cleanup VBO and shader
- glDeleteBuffers(1, &vertexbuffer);
- glDeleteBuffers(1, &uvbuffer);
- glDeleteProgram(programID);
- glDeleteTextures(1, &TextureID);
- // Close OpenGL window and terminate GLFW
- glfwTerminate();
- delete[] data;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement