Advertisement
bnz

OpenGL

bnz
May 15th, 2011
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void windowSizeCallback(int width, int height)
  2. {
  3.     glViewport(0, 0, width, height);
  4. }
  5.  
  6. int main()
  7. {
  8.     if (!glfwInit()) {
  9.         std::cout << "Couldn't initialize GLFW\n";
  10.         return 1;
  11.     }
  12.  
  13.     if (!glfwOpenWindow(400, 300, 0, 0, 0, 0, 0, 0, GLFW_WINDOW)) {
  14.         std::cout << "Couldn't open window\n";
  15.         return 1;
  16.     }
  17.  
  18.     if (glewInit() != GLEW_OK) {
  19.         std::cout << "Couldn't initialize glew\n";
  20.         return 1;
  21.     }
  22.  
  23.     glfwSetWindowTitle("Test");
  24.     glfwSetWindowSizeCallback(windowSizeCallback);
  25.  
  26.     glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 4);
  27.     glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 0);
  28.     glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  29.  
  30.     const char* vertexShaderSource =
  31.         "#version 330\n"
  32.         "in vec4 inPosition;\n"
  33.         "in vec4 inColor;\n"
  34.         "out vec4 vertexColor;\n"
  35.         "void main()\n"
  36.         "{\n"
  37.         "vertexColor = inColor;\n"
  38.         "    gl_Position = inPosition;\n"
  39.         "}";
  40.     const char* fragmentShaderSource =
  41.         "#version 330\n"
  42.         "in vec4 vertexColor;\n"
  43.         "out vec4 fragColor;\n"
  44.         "void main()\n"
  45.         "{\n"
  46.         "    fragColor = vertexColor;\n"
  47.         "    //fragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
  48.         "}";
  49.  
  50.     GLuint vertexShader(glCreateShader(GL_VERTEX_SHADER))
  51.          , fragmentShader(glCreateShader(GL_FRAGMENT_SHADER))
  52.          , shaderProgram(glCreateProgram());
  53.  
  54.     glShaderSource(vertexShader, 1, &vertexShaderSource, 0);
  55.     glCompileShader(vertexShader);
  56.     glShaderSource(fragmentShader, 1, &fragmentShaderSource, 0);
  57.     glCompileShader(fragmentShader);
  58.     glAttachShader(shaderProgram, vertexShader);
  59.     glAttachShader(shaderProgram, fragmentShader);
  60.     glLinkProgram(shaderProgram);
  61.    
  62.     GLuint positionLoc(glGetAttribLocation(shaderProgram, "inPosition"))
  63.          , colorLoc(glGetAttribLocation(shaderProgram, "inColor"));
  64.  
  65.     float positionData[] =
  66.         { -0.5, -0.5, 0.0, 1.0
  67.         ,  0.5, -0.5, 0.0, 1.0
  68.         , -0.5,  0.5, 0.0, 1.0
  69.         ,  0.5,  0.5, 0.0, 1.0
  70.         };
  71.    
  72.     float colorData [] =
  73.         { 1.0, 1.0, 1.0, 1.0
  74.         , 1.0, 1.0, 1.0, 1.0
  75.         , 1.0, 1.0, 1.0, 1.0
  76.         , 1.0, 1.0, 1.0, 1.0
  77.         };
  78.  
  79.     GLuint vao, positionBuffer, colorBuffer;
  80.     glGenBuffers(1, &positionBuffer);
  81.     glGenBuffers(1, &colorBuffer);
  82.    
  83.     glGenVertexArrays(1, &vao);
  84.     glBindVertexArray(vao);
  85.    
  86.     glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
  87.     glBufferData(GL_ARRAY_BUFFER, sizeof(positionData), positionData, GL_STATIC_DRAW);    
  88.     glVertexAttribPointer(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, 0);
  89.     glEnableVertexAttribArray(positionLoc);
  90.    
  91.     glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
  92.     glBufferData(GL_ARRAY_BUFFER, sizeof(colorData), colorData, GL_STATIC_DRAW);
  93.     glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 0, 0);
  94.     glEnableVertexAttribArray(colorLoc);
  95.  
  96.     glBindVertexArray(0);
  97.  
  98.     // Main loop
  99.     while (true) {
  100.         glClear(GL_COLOR_BUFFER_BIT);
  101.        
  102.         glBindVertexArray(vao);
  103.         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  104.         glBindVertexArray(0);
  105.  
  106.         glfwSwapBuffers();
  107.         if (glfwGetKey(GLFW_KEY_ESC) || !glfwGetWindowParam(GLFW_OPENED)) {
  108.             break;
  109.         }
  110.     }
  111.     glfwTerminate();
  112.     return 0;
  113. }
Advertisement
RAW Paste Data Copied
Advertisement