Advertisement
bnz

OpenGL - second version

bnz
May 17th, 2011
29
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None
  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. glEnableVertexAttribArray(positionLoc);
  87. glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
  88. glBufferData(GL_ARRAY_BUFFER, sizeof(positionData), positionData, GL_STATIC_DRAW);
  89. glVertexAttribPointer(positionLoc, 4, GL_FLOAT, GL_FALSE, 0, 0);
  90. glBindBuffer(GL_ARRAY_BUFFER, 0);
  91.  
  92. glEnableVertexAttribArray(colorLoc);
  93. glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
  94. glBufferData(GL_ARRAY_BUFFER, sizeof(colorData), colorData, GL_STATIC_DRAW);
  95. glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 0, 0);
  96. glBindBuffer(GL_ARRAY_BUFFER, 0);
  97.  
  98. //glBindVertexArray(0);
  99. glClearColor(0.5, 0.5, 0.5, 1.0);
  100.  
  101. // Main loop
  102. while (true) {
  103. glClear(GL_COLOR_BUFFER_BIT);
  104.  
  105. //glBindVertexArray(vao);
  106. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  107. //glBindVertexArray(0);
  108.  
  109. glfwSwapBuffers();
  110. if (glfwGetKey(GLFW_KEY_ESC) || !glfwGetWindowParam(GLFW_OPENED)) {
  111. break;
  112. }
  113. }
  114. glfwTerminate();
  115. return 0;
  116. }
Advertisement
RAW Paste Data Copied
Advertisement