Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. #include "window.h"
  2.  
  3. void triangle()
  4. {
  5. GLuint VAO;
  6. glGenVertexArrays(1, &VAO);
  7. glBindVertexArray(VAO);
  8.  
  9. static const GLfloat triangle[] = {
  10. -1.0f, -1.0f, 0.0f,
  11. 1.0f, -1.0f, 0.0f,
  12. 0.0f, 1.0f, 0.0f,
  13. };
  14.  
  15. GLuint VBO;
  16. glGenBuffers(1, &VBO);
  17. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  18. glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
  19.  
  20. glEnableVertexAttribArray(0);
  21. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  22. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  23. glDrawArrays(GL_TRIANGLES, 0, 3);
  24. glDisableVertexAttribArray(0);
  25. }
  26. GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path) {
  27. // Create the shaders
  28. GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  29. GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  30.  
  31. // Read the Vertex Shader code from the file
  32. std::string VertexShaderCode;
  33. std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
  34. if (VertexShaderStream.is_open()) {
  35. std::string Line = "";
  36. while (getline(VertexShaderStream, Line))
  37. VertexShaderCode += "\n" + Line;
  38. VertexShaderStream.close();
  39. }
  40. else {
  41. printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
  42. getchar();
  43. return 0;
  44. }
  45. // Read the Fragment Shader code from the file
  46. std::string FragmentShaderCode;
  47. std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
  48. if (FragmentShaderStream.is_open()) {
  49. std::string Line = "";
  50. while (getline(FragmentShaderStream, Line))
  51. FragmentShaderCode += "\n" + Line;
  52. FragmentShaderStream.close();
  53. }
  54.  
  55. GLint Result = GL_FALSE;
  56. int InfoLogLength;
  57.  
  58.  
  59. // Compile Vertex Shader
  60. printf("Compiling shader : %s\n", vertex_file_path);
  61. char const * VertexSourcePointer = VertexShaderCode.c_str();
  62. glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
  63. glCompileShader(VertexShaderID);
  64.  
  65. // Check Vertex Shader
  66. glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
  67. glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  68. if (InfoLogLength > 0) {
  69. std::vector<char> VertexShaderErrorMessage(InfoLogLength + 1);
  70. glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
  71. printf("%s\n", &VertexShaderErrorMessage[0]);
  72. }
  73.  
  74.  
  75.  
  76. // Compile Fragment Shader
  77. printf("Compiling shader : %s\n", fragment_file_path);
  78. char const * FragmentSourcePointer = FragmentShaderCode.c_str();
  79. glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
  80. glCompileShader(FragmentShaderID);
  81.  
  82. // Check Fragment Shader
  83. glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
  84. glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  85. if (InfoLogLength > 0) {
  86. std::vector<char> FragmentShaderErrorMessage(InfoLogLength + 1);
  87. glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
  88. printf("%s\n", &FragmentShaderErrorMessage[0]);
  89. }
  90.  
  91.  
  92.  
  93. // Link the program
  94. printf("Linking program\n");
  95. GLuint ProgramID = glCreateProgram();
  96. glAttachShader(ProgramID, VertexShaderID);
  97. glAttachShader(ProgramID, FragmentShaderID);
  98. glLinkProgram(ProgramID);
  99.  
  100. // Check the program
  101. glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
  102. glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
  103. if (InfoLogLength > 0) {
  104. std::vector<char> ProgramErrorMessage(InfoLogLength + 1);
  105. glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
  106. printf("%s\n", &ProgramErrorMessage[0]);
  107. }
  108.  
  109.  
  110. glDetachShader(ProgramID, VertexShaderID);
  111. glDetachShader(ProgramID, FragmentShaderID);
  112.  
  113. glDeleteShader(VertexShaderID);
  114. glDeleteShader(FragmentShaderID);
  115. return ProgramID;
  116. }
  117. GLuint programID = LoadShaders("SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader");
  118. int main()
  119. {
  120. Window window(900, 500, "Title");
  121. triangle();
  122. GLuint programID = LoadShaders("SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader");
  123. while (!window.closed())
  124. {
  125. window.update();
  126. }
  127. glfwTerminate();
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. #pragma once
  148.  
  149. #include <iostream>
  150. #include <GL\glew.h>
  151. #include <GLFW\glfw3.h>
  152.  
  153. using std::string;
  154. using std::cout;
  155.  
  156. class Window
  157. {
  158. public:
  159. Window(int width, int height, string title);
  160. void update();
  161. bool closed();
  162. GLFWwindow *window;
  163. private:
  164. int privWidth, privHeight;
  165. string privTitle;
  166. bool init();
  167. };
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. #include "window.h"
  200.  
  201. Window::Window(int width, int height, string title)
  202. {
  203. privWidth = width;
  204. privHeight = height;
  205. privTitle = title;
  206. init();
  207. }
  208.  
  209. bool Window::init(){
  210. if (!glfwInit())
  211. {
  212. cout << "Failed to Initialize GLFW!\n";
  213. glfwTerminate();
  214. return false;
  215. }
  216. else
  217. {
  218. cout << "Initialized GLFW!\n";
  219. }
  220. window = glfwCreateWindow(privWidth, privHeight, privTitle.c_str(), nullptr, nullptr);
  221. if (!window)
  222. {
  223. cout << "Failed to Create a Window!\n";
  224. glfwTerminate();
  225. return false;
  226. }
  227. else
  228. {
  229. cout << "Created a Window!";
  230. }
  231. glfwMakeContextCurrent(window);
  232. return true;
  233. }
  234.  
  235. bool Window::closed()
  236. {
  237. return (glfwWindowShouldClose(window));
  238. }
  239.  
  240. void Window::update()
  241. {
  242. glfwPollEvents();
  243. glfwSwapBuffers(window);
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement