Advertisement
Guest User

main.cpp

a guest
Oct 21st, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.15 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <assert.h>
  6. #include <string.h>
  7. #include "teapot.h"
  8. #include <glm/glm.hpp>
  9.  
  10. int gl_width = 800;
  11. int gl_height = 800;
  12.  
  13. std::string readShaderFile(std::string filename) {
  14.     std::ifstream ifile(filename);
  15.     std::string text;
  16.    
  17.     while( ifile.good() ) {
  18.         std::string line;
  19.         std::getline(ifile, line);
  20.         text.append(line + "\n");
  21.     }
  22.    
  23.     text += '\0';
  24.    
  25.     return text;
  26. }
  27.  
  28. void printShaderStatus(GLuint shader) {
  29.     GLint status;
  30.     glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
  31.    
  32.     if (status == GL_FALSE) {
  33.         GLint infoLogLength;
  34.         glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
  35.        
  36.         GLchar *strInfoLog = (char *)malloc(infoLogLength + 1);
  37.         glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);
  38.        
  39.         fprintf(stderr, "Compilation error in shader: %s\n", strInfoLog);
  40.         free(strInfoLog);
  41.        
  42.         glDeleteShader(shader);
  43.     }
  44. }
  45.  
  46. int main() {
  47.     /* pointer to an OS window created by GLFW */
  48.     GLFWwindow *window = NULL;
  49.     /* pointers to strings that GL will return, giving use the version being run */
  50.     const GLubyte *renderer;
  51.     const GLubyte *version;
  52.     GLuint shader_programme;
  53.     GLuint vao;
  54.    
  55.     /* start GL context and O/S window using the GLFW helper library */
  56.     if (!glfwInit()) {
  57.         fprintf (stderr, "ERROR: could not start GLFW3\n");
  58.         return 1;
  59.     }
  60.    
  61.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  62.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  63.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  64.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  65.    
  66.     /* ask GLFW library to create an OS window */
  67.     window = glfwCreateWindow(gl_width, gl_height, "Assignment 2", NULL, NULL);
  68.     if (!window) {
  69.         fprintf (stderr, "ERROR: opening OS window\n");
  70.         return 1;
  71.     }
  72.     glfwMakeContextCurrent(window);
  73.    
  74.     glewExperimental = GL_TRUE;
  75.     glewInit();
  76.    
  77.     /* get version info */
  78.     renderer = glGetString(GL_RENDERER); /* get renderer string */
  79.     version = glGetString(GL_VERSION); /* version as a string */
  80.     printf("Renderer: %s\n", renderer);
  81.     printf("OpenGL version supported %s\n", version);
  82.    
  83.     //
  84.     // Set up vertex buffers and vertex array object
  85.     // --------------------------------------------------------------------------
  86.     {
  87.         GLuint points_vbo, normals_vbo;
  88.         glGenBuffers(1, &points_vbo);
  89.         glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
  90.         // copy our points from the header file into our VBO on graphics hardware
  91.         glBufferData(GL_ARRAY_BUFFER, sizeof (teapot_vertex_points),
  92.                      teapot_vertex_points, GL_STATIC_DRAW);
  93.         // and grab the normals
  94.         glGenBuffers(1, &normals_vbo);
  95.         glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
  96.         glBufferData(GL_ARRAY_BUFFER, sizeof (teapot_normals),
  97.                      teapot_normals, GL_STATIC_DRAW);
  98.        
  99.         glGenVertexArrays(1, &vao);
  100.         glBindVertexArray(vao);
  101.         glEnableVertexAttribArray(0);
  102.         glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
  103.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  104.         glEnableVertexAttribArray(1);
  105.         glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
  106.         glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  107.     }
  108.    
  109.     //
  110.     // Load shaders from files
  111.     // --------------------------------------------------------------------------
  112.     {
  113.         std::string vertex_shader_str = readShaderFile("/Users/mattdonnelly/Documents/College/4th Year/Computer Graphics/Assignment 2/Assignment 2/teapot.vert");
  114.         GLchar const *vertex_shader_source = vertex_shader_str.c_str();
  115.  
  116.         std::string fragment_shader_str = readShaderFile("/Users/mattdonnelly/Documents/College/4th Year/Computer Graphics/Assignment 2/Assignment 2/teapot.frag");
  117.         GLchar const *fragment_shader_source = fragment_shader_str.c_str();
  118.  
  119.         GLuint vs, fs;
  120.         vs = glCreateShader(GL_VERTEX_SHADER);
  121.         fs = glCreateShader(GL_FRAGMENT_SHADER);
  122.         glShaderSource(vs, 1, &vertex_shader_source, NULL);
  123.         glShaderSource(fs, 1, &fragment_shader_source, NULL);
  124.        
  125.         glCompileShader(vs);
  126.         printShaderStatus(vs);
  127.        
  128.         glCompileShader(fs);
  129.         printShaderStatus(fs);
  130.        
  131.         shader_programme = glCreateProgram ();
  132.         glAttachShader(shader_programme, fs);
  133.         glAttachShader(shader_programme, vs);
  134.         glLinkProgram (shader_programme);
  135.     }
  136.    
  137.     //
  138.     // Create some matrices
  139.     // --------------------------------------------------------------------------
  140.     // a model matrix
  141.     // can you see what affine transformation this does?
  142.     float M[] = {
  143.         0.05f, 0.0f,  0.0f,  0.0f,
  144.         0.0f,  0.05f, 0.0f,  0.0f,
  145.         0.0f,  0.0f,  0.05f, 0.0f,
  146.         0.0f,  0.0f,  0.0f,  1.0f
  147.     };
  148.     // location of "M" in vertex shader
  149.     int M_loc = glGetUniformLocation (shader_programme, "M");
  150.     assert(M_loc > -1);
  151.    
  152.     // send matrix values to shader immediately
  153.     glUseProgram(shader_programme);
  154.     glUniformMatrix4fv(M_loc, 1, GL_FALSE, M);
  155.    
  156.     //
  157.     // Start rendering
  158.     // --------------------------------------------------------------------------
  159.     // tell GL to only draw onto a pixel if the fragment is closer to the viewer
  160.     glEnable(GL_DEPTH_TEST); // enable depth-testing
  161.     glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"
  162.     glClearColor(0.5, 0.5, 0.5, 1.0);
  163.    
  164.     while (!glfwWindowShouldClose (window)) {
  165.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  166.        
  167.         glUseProgram(shader_programme);
  168.         glBindVertexArray(vao);
  169.        
  170.         glViewport(0, 0, gl_width, gl_height);
  171.         glDrawArrays(GL_TRIANGLES, 0, teapot_vertex_count);
  172.        
  173.         /* this just updates window events and keyboard input events (not used yet) */
  174.         glfwPollEvents();
  175.         glfwSwapBuffers(window);
  176.     }
  177.    
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement