ShermanUA

Application

Apr 13th, 2020
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include <sstream>
  8.  
  9. /* Create struct for our shaders */
  10. struct ShaderProgramSource
  11. {
  12. std::string VertexSource;
  13. std::string FragmentSource;
  14. };
  15.  
  16. /* Create function for parse our file with shaders */
  17. static ShaderProgramSource ParseShader(const std::string& filepath)
  18. {
  19. std::ifstream stream(filepath); // Open our file
  20.  
  21. /* Create enum class for understanding the current state of the data */
  22. enum class ShaderType
  23. {
  24. NONE = -1, VERTEX = 0, FRAGMENT = 1
  25. };
  26.  
  27. /* Create variable and array to store information */
  28. std::string line;
  29. std::stringstream ss[2];
  30. ShaderType type = ShaderType::NONE;
  31. /* Read and process our file */
  32. while (getline(stream, line))
  33. {
  34. if (line.find("#shader") != std::string::npos)
  35. {
  36. if (line.find("vertex") != std::string::npos)
  37. type = ShaderType::VERTEX;
  38. else if (line.find("fragment") != std::string::npos)
  39. type = ShaderType::FRAGMENT;
  40. }
  41. else
  42. {
  43. ss[(int)type] << line << '\n';
  44. }
  45. }
  46.  
  47. return { ss[0].str(), ss[1].str() };
  48. }
  49.  
  50. /* Function for compiling shader */
  51. static unsigned int CompileShader(unsigned int type, const std::string& source)
  52. {
  53. /* Creates a shader object */
  54. unsigned int id = glCreateShader(type);
  55. const char* src = source.c_str();
  56. glShaderSource(id, 1, &src, nullptr); // Replaces the source code in a shader object
  57. glCompileShader(id); // Compiles a shader object
  58.  
  59. /* Check for success compiles */
  60. int result;
  61. glGetShaderiv(id, GL_COMPILE_STATUS, &result);
  62. if (result == GL_FALSE)
  63. {
  64. int lenght;
  65. glGetShaderiv(id, GL_INFO_LOG_LENGTH, &lenght);
  66. char* message = (char*)alloca(lenght * sizeof(char));
  67. glGetShaderInfoLog(id, lenght, &lenght, message); // Return error information
  68. std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") <<
  69. std::endl; // Print information about error
  70. std::cout << message << std::endl;
  71. glDeleteShader(id); // Delete shader
  72. return 0;
  73. }
  74.  
  75. return id;
  76. }
  77.  
  78. /* Function for creating shader */
  79. static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
  80. {
  81. unsigned int program = glCreateProgram(); // Creates a program object
  82.  
  83. /* Compiles shaders */
  84. unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
  85. unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
  86.  
  87. /* Attaches a shader objects to a program object */
  88. glAttachShader(program, vs);
  89. glAttachShader(program, fs);
  90. glLinkProgram(program);
  91. glValidateProgram(program);
  92.  
  93. /* Delete unnecessary data */
  94. glDeleteShader(vs);
  95. glDeleteShader(fs);
  96.  
  97. return program;
  98. }
  99.  
  100. int main(void)
  101. {
  102. GLFWwindow* window;
  103.  
  104. /* Initialize the library GLFW */
  105. if (!glfwInit())
  106. return -1;
  107.  
  108. /* Creating a windowed mode window and its OpenGL context */
  109. window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
  110. if (!window)
  111. {
  112. glfwTerminate();
  113. return -1;
  114. }
  115.  
  116. /* Make the window's context current */
  117. glfwMakeContextCurrent(window);
  118.  
  119. /* Initialize the library GLEW */
  120. if (glewInit() != GLEW_OK)
  121. std::cout << "Error!" << std::endl;
  122.  
  123. /* Print version OpenGl to console */
  124. std::cout << glGetString(GL_VERSION);
  125.  
  126. /* Create vertex positions */
  127. float positions[] = {
  128. 0.0f, 0.0f, // 0
  129. 0.25f, -0.5f, // 1
  130. 0.5f, 0.0f, // 2
  131. 0.25f, 0.5f, // 3
  132. -0.25f, 0.5f, // 4
  133. -0.5f, 0.0f, // 5
  134. -0.25f, -0.5f // 6
  135. };
  136.  
  137. /* Creating vertex sequence */
  138. unsigned int indices[] = {
  139. 0, 1, 2,
  140. 0, 2, 3,
  141. 0, 3, 4,
  142. 0, 4, 5,
  143. 0, 5, 6,
  144. 0, 6, 1
  145. };
  146.  
  147.  
  148. /* Allocate memory and give data to buffer */
  149. unsigned int buffer;
  150. glGenBuffers(1, &buffer);
  151. glBindBuffer(GL_ARRAY_BUFFER, buffer);
  152. glBufferData(GL_ARRAY_BUFFER, 6 * 7 * sizeof(float), positions, GL_STATIC_DRAW);
  153.  
  154. /* Define an array of generic vertex attribute data */
  155. glEnableVertexAttribArray(0);
  156. glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
  157.  
  158. /* Creating our index buffer and allocate memory for him */
  159. unsigned int ibo;
  160. glGenBuffers(1, &ibo);
  161. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  162. glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * 7 * sizeof(unsigned int), indices, GL_STATIC_DRAW);
  163.  
  164. /* Parse our file with shaders */
  165. ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");
  166.  
  167. /* Creating shader using our function */
  168. unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
  169.  
  170. glUseProgram(shader); // Installs a program object as part of current rendering state
  171.  
  172. /* Loop until the user closes the window */
  173. while (!glfwWindowShouldClose(window))
  174. {
  175. /* Render here */
  176. glClear(GL_COLOR_BUFFER_BIT);
  177.  
  178. /* Draw triangle */
  179. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
  180.  
  181. /* Swap front and back buffers */
  182. glfwSwapBuffers(window);
  183.  
  184. /* Poll for and process events */
  185. glfwPollEvents();
  186. }
  187.  
  188. /* Delete program */
  189. glDeleteProgram(shader);
  190.  
  191. glfwTerminate();
  192. return 0;
  193. }
  194.  
  195.  
  196.  
  197.  
  198. file Basic.shader
  199.  
  200.  
  201. #shader vertex
  202. #version 330 core
  203.  
  204. layout(location = 0) in vec4 position;
  205.  
  206. void main()
  207. {
  208. gl_Position = position;
  209. };
  210.  
  211.  
  212. #shader fragment
  213. #version 330 core
  214.  
  215. layout(location = 0) out vec4 color;
  216.  
  217. void main()
  218. {
  219. color = vec4(0.2, 0.3, 0.8, 1.0);
  220. };
Advertisement
Add Comment
Please, Sign In to add comment