Advertisement
SashaKarmanov

Untitled

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