Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. #include "glew.h"
  2. #include "glfw3.h"
  3. #include "glm\glm.hpp"
  4. #include "glm\gtc\matrix_transform.hpp"
  5. #include "glm\gtx\transform.hpp"
  6. #include <vector>
  7. #include <string>
  8. #include <fstream>
  9.  
  10. GLuint CreateShader(GLenum a_eShaderType, const char *a_strShaderFile)
  11. {
  12. std::string strShaderCode;
  13. //open shader file
  14. std::ifstream shaderStream(a_strShaderFile);
  15. //if that worked ok, load file line by line
  16. if(shaderStream.is_open())
  17. {
  18. std::string Line = "";
  19. while(std::getline(shaderStream, Line))
  20. {
  21. strShaderCode += "\n" + Line;
  22. }
  23. shaderStream.close();
  24. }
  25.  
  26. //convert to cstring
  27. char const *szShaderSourcePointer = strShaderCode.c_str();
  28.  
  29. //create shader ID
  30. GLuint uiShader = glCreateShader(a_eShaderType);
  31. //load source code
  32. glShaderSource(uiShader, 1, &szShaderSourcePointer, NULL);
  33.  
  34. //compile shader
  35. glCompileShader(uiShader);
  36.  
  37. //check for compilation errors and output them
  38. GLint iStatus;
  39. glGetShaderiv(uiShader, GL_COMPILE_STATUS, &iStatus);
  40. if (iStatus == GL_FALSE)
  41. {
  42. GLint infoLogLength;
  43. glGetShaderiv(uiShader, GL_INFO_LOG_LENGTH, &infoLogLength);
  44.  
  45. GLchar *strInfoLog = new GLchar[infoLogLength + 1];
  46. glGetShaderInfoLog(uiShader, infoLogLength, NULL, strInfoLog);
  47.  
  48. const char *strShaderType = NULL;
  49. switch(a_eShaderType)
  50. {
  51. case GL_VERTEX_SHADER: strShaderType = "vertex"; break;
  52. case GL_FRAGMENT_SHADER: strShaderType = "fragment"; break;
  53. }
  54.  
  55. fprintf(stderr, "Compile failure in %s shader:\n%s\n", strShaderType, strInfoLog);
  56. delete[] strInfoLog;
  57. }
  58.  
  59. return uiShader;
  60. }
  61.  
  62. GLuint CreateProgram(const char *a_vertex, const char *a_frag)
  63. {
  64. std::vector<GLuint> shaderList;
  65.  
  66. shaderList.push_back(CreateShader(GL_VERTEX_SHADER, a_vertex));
  67. shaderList.push_back(CreateShader(GL_FRAGMENT_SHADER, a_frag));
  68.  
  69. //create shader program ID
  70. GLuint uiProgram = glCreateProgram();
  71.  
  72. //attach shaders
  73. for(auto shader = shaderList.begin(); shader != shaderList.end(); shader++)
  74. glAttachShader(uiProgram, *shader);
  75.  
  76. //link program
  77. glLinkProgram(uiProgram);
  78.  
  79. //check for link errors and output them
  80. GLint status;
  81. glGetProgramiv (uiProgram, GL_LINK_STATUS, &status);
  82. if (status == GL_FALSE)
  83. {
  84. GLint infoLogLength;
  85. glGetProgramiv(uiProgram, GL_INFO_LOG_LENGTH, &infoLogLength);
  86.  
  87. GLchar *strInfoLog = new GLchar[infoLogLength + 1];
  88. glGetProgramInfoLog(uiProgram, infoLogLength, NULL, strInfoLog);
  89. fprintf(stderr, "Linker failure: %s\n", strInfoLog);
  90. delete[] strInfoLog;
  91. }
  92.  
  93. for(auto shader = shaderList.begin(); shader != shaderList.end(); shader++)
  94. {
  95. glDetachShader(uiProgram, *shader);
  96. glDeleteShader(*shader);
  97. }
  98.  
  99. return uiProgram;
  100. }
  101.  
  102. int main()
  103. {
  104. //Initialise GLFW
  105. if(!glfwInit())
  106. {
  107. return -1;
  108. }
  109.  
  110. //create a windowed mode window and it's OpenGL context
  111. GLFWwindow* window;
  112. window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
  113. if(!window)
  114. {
  115. glfwTerminate();
  116. return -1;
  117. }
  118.  
  119. //make the window's context current
  120. glfwMakeContextCurrent(window);
  121.  
  122. float points[] =
  123. {
  124. 0.0f , 0.5f , 0.0f,
  125. 0.5f , -0.5f , 0.0f,
  126. -0.5f , -0.5f , 1.0f,
  127. 0.0f , 0.5f , 0.0f,
  128. -0.5f , 0.5f , 0.0f,
  129. 0.5f , 0.5f , 1.0f,
  130. 0.0f , 0.5f , 0.0f,
  131. 0.5f , -0.5f , 0.0f,
  132. -0.5f , -0.5f , 1.0f,
  133. 0.0f , 0.5f , 0.0f,
  134. -0.5f , 0.5f , 0.0f,
  135. 0.5f , 0.5f , 1.0f,
  136. };
  137. float colours[] =
  138. {
  139. 1.0f , 0.0f , 0.0f,
  140. 0.0f , 1.0f , 0.0f,
  141. 0.0f , 0.0f , 1.0f,
  142. 0.0f , 0.0f , 1.0f,
  143. 0.0f , 1.0f , 0.0f,
  144. 1.0f , 0.0f , 0.0f,
  145. 1.0f , 0.0f , 0.0f,
  146. 0.0f , 1.0f , 0.0f,
  147. 0.0f , 0.0f , 1.0f,
  148. 0.0f , 0.0f , 1.0f,
  149. 0.0f , 1.0f , 0.0f,
  150. 1.0f , 0.0f , 0.0f
  151. };
  152.  
  153. // start GLEW
  154. if (glewInit() != GLEW_OK)
  155. {
  156. // OpenGL didn't start-up! shutdown GLFW and return an error code
  157. glfwTerminate();
  158. return -1;
  159. };
  160. //………
  161. GLuint shaderProgram = CreateProgram("./shaders/simple.vert", "./shaders/simple.frag");
  162. glUseProgram(shaderProgram);
  163.  
  164. GLuint MatrixID = glGetUniformLocation(shaderProgram, "MVP");
  165.  
  166. glm::mat4 Projection = glm::perspective(35.0f, 4.0f / 3.0f, 0.1f, 100.0f);
  167. glm::mat4 View = glm::lookAt(
  168. glm::vec3(4,2,2), // Camera location
  169. glm::vec3(0,0,0), // and looks at the origin
  170. glm::vec3(0,-1,0) // Head is up (set to 0,-1,0 to look upside-down)
  171. );
  172.  
  173. glm::mat4 myMatrix = glm::translate(glm::mat4(-2.f),glm::vec3(0.f));
  174. glm::mat4 Model = glm::mat4(1.f);
  175. Model= myMatrix * Model;
  176. glm::mat4 MVP = Projection * View * Model;
  177.  
  178. glm::mat4 myMatrix2 = glm::translate(glm::mat4(2.f),glm::vec3(0.f));
  179. glm::mat4 Model2 = glm::mat4(1.f);
  180. Model2= myMatrix2 * Model2;
  181. glm::mat4 MVP2 = Projection * View * Model2;
  182.  
  183. glm::mat4 myMatrix3 = glm::translate(glm::mat4(4.f),glm::vec3(0.f));
  184. glm::mat4 Model3 = glm::mat4(1.f);
  185. Model3= myMatrix3 * Model3;
  186. glm::mat4 MVP3 = Projection * View * Model3;
  187.  
  188. glm::mat4 myMatrix4 = glm::translate(glm::mat4(8.f),glm::vec3(0.f));
  189. glm::mat4 Model4 = glm::mat4(1.f);
  190. Model4= myMatrix4 * Model4;
  191. glm::mat4 MVP4 = Projection * View * Model4;
  192.  
  193. glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
  194. glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP2[0][0]);
  195. glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP3[0][0]);
  196. glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP4[0][0]);
  197.  
  198. GLuint points_vbo = 0;
  199. glGenBuffers (1, &points_vbo);
  200. glBindBuffer (GL_ARRAY_BUFFER, points_vbo);
  201. glBufferData (GL_ARRAY_BUFFER, 18 * sizeof (float), points, GL_STATIC_DRAW);
  202.  
  203. GLuint colours_vbo = 0;
  204. glGenBuffers (1, &colours_vbo);
  205. glBindBuffer (GL_ARRAY_BUFFER, colours_vbo);
  206. glBufferData (GL_ARRAY_BUFFER, 18 * sizeof (float), colours, GL_STATIC_DRAW);
  207.  
  208. GLuint vao = 0;
  209. glGenVertexArrays (1, &vao);
  210. glBindVertexArray (vao);
  211. glBindBuffer (GL_ARRAY_BUFFER, points_vbo);
  212. glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  213. glBindBuffer (GL_ARRAY_BUFFER, colours_vbo);
  214. glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  215.  
  216. glEnableVertexAttribArray (0);
  217. glEnableVertexAttribArray (1);
  218.  
  219.  
  220. while (!glfwWindowShouldClose(window))
  221. {
  222. //loop until the user closes the window
  223. while(!glfwWindowShouldClose(window))
  224. {
  225. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  226.  
  227. glUseProgram (shaderProgram);
  228.  
  229. glBindVertexArray (vao);
  230.  
  231. //draw code goes here
  232. glDrawArrays(GL_TRIANGLES,0,12*3);
  233.  
  234. glfwSwapBuffers(window);
  235.  
  236. //poll for and process events
  237. glfwPollEvents();
  238.  
  239. if (glfwGetKey (window, GLFW_KEY_ESCAPE))
  240. glfwSetWindowShouldClose (window, 1);
  241. }
  242.  
  243. glfwTerminate();
  244. return 0;
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement