Advertisement
Guest User

Untitled

a guest
Oct 15th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. // GLEW
  4. #define GLEW_STATIC
  5. #include <GL/glew.h>
  6.  
  7. // GLFW
  8. #include <GLFW/glfw3.h>
  9.  
  10.  
  11. // Function prototypes
  12. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
  13.  
  14. // Window dimensions
  15. const GLuint WIDTH = 800, HEIGHT = 600;
  16.  
  17. // Shaders
  18. const GLchar* vertexShaderSource = "#version 330 core\n"
  19. "layout (location = 0) in vec3 position;\n"
  20. "void main()\n"
  21. "{\n"
  22. "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
  23. "}\0";
  24. const GLchar* fragmentShaderSource = "#version 330 core\n"
  25. "out vec4 color;\n"
  26. "void main()\n"
  27. "{\n"
  28. "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
  29. "}\n\0";
  30.  
  31. // The MAIN function, from here we start the application and run the game loop
  32. int main()
  33. {
  34. // Init GLFW
  35. glfwInit();
  36. // Set all the required options for GLFW
  37. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  38. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  39. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  40. glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  41.  
  42. // Create a GLFWwindow object that we can use for GLFW's functions
  43. GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
  44. glfwMakeContextCurrent(window);
  45.  
  46. // Set the required callback functions
  47. glfwSetKeyCallback(window, key_callback);
  48.  
  49. // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
  50. glewExperimental = GL_TRUE;
  51. // Initialize GLEW to setup the OpenGL Function pointers
  52. glewInit();
  53.  
  54. // Define the viewport dimensions
  55. glViewport(0, 0, WIDTH, HEIGHT);
  56.  
  57.  
  58. // Build and compile our shader program
  59. // Vertex shader
  60. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  61. glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
  62. glCompileShader(vertexShader);
  63. // Check for compile time errors
  64. GLint success;
  65. GLchar infoLog[512];
  66. glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  67. if (!success)
  68. {
  69. glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  70. std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
  71. }
  72. // Fragment shader
  73. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  74. glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
  75. glCompileShader(fragmentShader);
  76. // Check for compile time errors
  77. glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
  78. if (!success)
  79. {
  80. glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
  81. std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
  82. }
  83. // Link shaders
  84. GLuint shaderProgram = glCreateProgram();
  85. glAttachShader(shaderProgram, vertexShader);
  86. glAttachShader(shaderProgram, fragmentShader);
  87. glLinkProgram(shaderProgram);
  88. // Check for linking errors
  89. glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  90. if (!success) {
  91. glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
  92. std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
  93. }
  94. glDeleteShader(vertexShader);
  95. glDeleteShader(fragmentShader);
  96.  
  97.  
  98. // Set up vertex data (and buffer(s)) and attribute pointers
  99. GLfloat * vertices;
  100. vertices = new GLfloat[12] {
  101. -0.5f, -0.5f, 0.0f, // Left
  102. 0.5f, -0.5f, 0.0f, // Right
  103. 0.0f, 0.5f, 0.0f // Top
  104. };
  105. GLuint VBO, VAO;
  106. glGenVertexArrays(1, &VAO);
  107. glGenBuffers(1, &VBO);
  108. // Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
  109. glBindVertexArray(VAO);
  110.  
  111. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  112. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  113.  
  114. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
  115. glEnableVertexAttribArray(0);
  116.  
  117. glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
  118.  
  119. glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)
  120.  
  121. // Game loop
  122. while (!glfwWindowShouldClose(window))
  123. {
  124. // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
  125. glfwPollEvents();
  126.  
  127. // Render
  128. // Clear the colorbuffer
  129. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  130. glClear(GL_COLOR_BUFFER_BIT);
  131.  
  132. // Draw our first triangle
  133. glUseProgram(shaderProgram);
  134. glBindVertexArray(VAO);
  135. glDrawArrays(GL_TRIANGLES, 0, 3);
  136. glBindVertexArray(0);
  137.  
  138. // Swap the screen buffers
  139. glfwSwapBuffers(window);
  140. }
  141. // Properly de-allocate all resources once they've outlived their purpose
  142. glDeleteVertexArrays(1, &VAO);
  143. glDeleteBuffers(1, &VBO);
  144. // Terminate GLFW, clearing any resources allocated by GLFW.
  145. glfwTerminate();
  146. return 0;
  147. }
  148.  
  149. // Is called whenever a key is pressed/released via GLFW
  150. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
  151. {
  152. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  153. glfwSetWindowShouldClose(window, GL_TRUE);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement