Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Include standard headers
- #include <stdio.h>
- #include <stdlib.h>
- #include <vector>
- #include <fstream>
- #include <string>
- #include <iostream>
- #include <sstream>
- // Include GLEW
- #include <GL/glew.h>
- // Include GLFW
- #include <GL/glfw.h>
- // Include GLM
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- using namespace glm;
- #include <common/shader.hpp>
- #include <common/texture.hpp>
- #include <common/controls.hpp>
- using namespace std;
- struct mycolor
- {
- mycolor() : r(0.0), g(0.0), b(0.0) {}
- GLfloat r,g,b;
- };
- int main( void )
- {
- // Initialise GLFW
- if( !glfwInit() )
- {
- fprintf( stderr, "Failed to initialize GLFW\n" );
- return -1;
- }
- glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
- glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
- glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
- glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- // Open a window and create its OpenGL context
- if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
- {
- 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;
- }
- // Initialize GLEW
- glewExperimental = GL_TRUE; // Recommended fix for access violation
- if (glewInit() != GLEW_OK) {
- fprintf(stderr, "Failed to initialize GLEW\n");
- return -1;
- }
- glfwSetWindowTitle( "Tutorial 06" );
- // Ensure we can capture the escape key being pressed below
- glfwEnable( GLFW_STICKY_KEYS );
- glfwSetMousePos(1024/2, 768/2);
- // Background
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- // Enable depth test
- //glEnable(GL_DEPTH_TEST);
- // Accept fragment if it closer to the camera than the former one
- //glDepthFunc(GL_LESS);
- // Cull triangles which normal is not towards the camera
- glEnable(GL_CULL_FACE);
- glEnable(GL_BLEND);
- glBlendFunc(GL_ONE, GL_ONE);
- GLuint VertexArrayID;
- glGenVertexArrays(1, &VertexArrayID);
- glBindVertexArray(VertexArrayID);
- // Create and compile our GLSL program from the shaders
- GLuint programID = LoadShaders( "C:\\OpenGL\\Sandbox\\OpenGLSandbox\\TransformVertexShader.vertexshader", "C:\\OpenGL\\Sandbox\\OpenGLSandbox\\ColorFragmentShader.fragmentshader" );
- // Get a handle for our "MVP" uniform
- GLuint MatrixID = glGetUniformLocation(programID, "MVP");
- vector<GLfloat> vertex_buffer_data;
- vector<mycolor> color_buffer_data;
- ifstream ifp("c:\\sopg1.input.txt");
- string strLine;
- getline(ifp, strLine); // get rid of size
- while (getline(ifp, strLine))
- {
- cout << strLine << endl;
- string strToken;
- stringstream ss(strLine);
- getline(ss, strToken, ' ');
- mycolor col;
- if (strToken == "R")
- {
- col.r = 1.0;
- }
- else
- {
- col.b = 1.0;
- }
- color_buffer_data.push_back(col);
- color_buffer_data.push_back(col);
- color_buffer_data.push_back(col);
- color_buffer_data.push_back(col);
- color_buffer_data.push_back(col);
- color_buffer_data.push_back(col);
- GLfloat l, t, w, h;
- getline(ss, strToken, ' ');
- l = atoi(strToken.c_str()) / 1000.0;
- l -= 2.2;
- getline(ss, strToken, ' ');
- t = atoi(strToken.c_str()) / 1000.0;
- t -= 2.2;
- getline(ss, strToken, ' ');
- w = atoi(strToken.c_str()) / 1000.0;
- getline(ss, strToken, ' ');
- h = atoi(strToken.c_str()) / 1000.0;
- vertex_buffer_data.push_back(l);
- vertex_buffer_data.push_back(t);
- vertex_buffer_data.push_back(0.0);
- vertex_buffer_data.push_back(l);
- vertex_buffer_data.push_back(t - h);
- vertex_buffer_data.push_back(0.0);
- vertex_buffer_data.push_back(l + w);
- vertex_buffer_data.push_back(t - h);
- vertex_buffer_data.push_back(0.0);
- vertex_buffer_data.push_back(l);
- vertex_buffer_data.push_back(t);
- vertex_buffer_data.push_back(0.0);
- vertex_buffer_data.push_back(l + w);
- vertex_buffer_data.push_back(t - h);
- vertex_buffer_data.push_back(0.0);
- vertex_buffer_data.push_back(l + w);
- vertex_buffer_data.push_back(t);
- vertex_buffer_data.push_back(0.0);
- }
- GLuint vertexbuffer;
- glGenBuffers(1, &vertexbuffer);
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- glBufferData(GL_ARRAY_BUFFER, vertex_buffer_data.size() * sizeof(GL_FLOAT), &vertex_buffer_data[0], GL_STATIC_DRAW);
- GLuint colorBuffer;
- glGenBuffers(1, &colorBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
- glBufferData(GL_ARRAY_BUFFER, color_buffer_data.size() * sizeof(mycolor), &color_buffer_data[0], GL_STATIC_DRAW);
- do{
- // Clear the screen
- glClear(GL_COLOR_BUFFER_BIT);
- // Use our shader
- glUseProgram(programID);
- // Compute the MVP matrix from keyboard and mouse input
- computeMatricesFromInputs();
- glm::mat4 ProjectionMatrix = getProjectionMatrix();
- glm::mat4 ViewMatrix = getViewMatrix();
- glm::mat4 ModelMatrix = glm::mat4(1.0);
- glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
- // Send our transformation to the currently bound shader,
- // in the "MVP" uniform
- glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
- // 1rst attribute buffer : vertices
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
- glVertexAttribPointer(
- 0, // attribute. No particular reason for 0, but must match the layout in the shader.
- 3, // size
- GL_FLOAT, // type
- GL_FALSE, // normalized?
- 0, // stride
- (void*)0 // array buffer offset
- );
- // Colors
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
- glVertexAttribPointer(
- 1,
- 3,
- GL_FLOAT,
- GL_FALSE,
- 0,
- (void *)0);
- // Draw the triangle !
- glDrawArrays(GL_TRIANGLES, 0, vertex_buffer_data.size());
- glDisableVertexAttribArray(0);
- glDisableVertexAttribArray(1);
- // Swap buffers
- glfwSwapBuffers();
- } // Check if the ESC key was pressed or the window was closed
- while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
- glfwGetWindowParam( GLFW_OPENED ) );
- // Cleanup VBO and shader
- glDeleteBuffers(1, &vertexbuffer);
- glDeleteProgram(programID);
- // Close OpenGL window and terminate GLFW
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement