Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GL/gl.h>
  3. #include <GL/glut.h>
  4. #include <glm.hpp>
  5. #include <gtc/matrix_transform.hpp>
  6. #include <cstdio>
  7. #include <fstream>
  8.  
  9. GLuint vaoHandle;
  10. GLuint programHandle;
  11. GLuint positionBufferHandle;
  12. GLuint colorBufferHandle;
  13.  
  14. GLuint aPositionLocation;
  15. GLuint aColorLocation;
  16. GLuint uModelMatrixLocation;
  17. GLuint uViewMatrixLocation;
  18. GLuint uProjectionMatrixLocation;
  19.  
  20. glm::mat4 modelMatrix;
  21. glm::mat4 viewMatrix;
  22. glm::mat4 projectionMatrix;
  23.  
  24. void setupRC();
  25.  
  26. void initVBO();
  27. void freeVBO();
  28.  
  29. void initShader();
  30. void freeShader();
  31. void shaderLog(GLuint shader);
  32.  
  33. void setModelMatrix();
  34. void setViewMatrix();
  35.  
  36. void checkError();
  37.  
  38. void changeSize(int w, int h);
  39. void renderScene();
  40.  
  41. std::string getFileContent(const char* fileName);
  42.  
  43. int main(int argc, char** argv)
  44. {
  45. glutInit(&argc, argv);
  46. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  47. glutInitWindowSize(500, 500);
  48. glutCreateWindow("GLEW Example");
  49.  
  50. GLenum glew_status = glewInit();
  51. if (glew_status != GLEW_OK)
  52. {
  53. printf("Error: \"%s\"\n", glewGetErrorString(glew_status));
  54. return 1;
  55. }
  56.  
  57. if (!GLEW_VERSION_2_0)
  58. {
  59. printf("No support for OpenGL 2.0 found!\n");
  60. return 1;
  61. }
  62.  
  63. setupRC();
  64. initShader();
  65. initVBO();
  66.  
  67.  
  68. glutReshapeFunc(changeSize);
  69. glutDisplayFunc(renderScene);
  70. glutMainLoop();
  71.  
  72. freeShader();
  73. freeVBO();
  74. printf("End of program...\n");
  75. return 0;
  76. }
  77.  
  78. void setupRC()
  79. {
  80. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  81. }
  82.  
  83. void initVBO()
  84. {
  85. float positionData[] =
  86. {
  87. -0.8f, -0.8f,
  88. 0.8f, -0.8f,
  89. 0.0f, 0.8f
  90. };
  91. float colorData[] =
  92. {
  93. 1.0f, 0.0f, 0.0f,
  94. 0.0f, 1.0f, 0.0f,
  95. 0.0f, 0.0f, 1.0f
  96. };
  97.  
  98. glGenVertexArrays(1, &vaoHandle);
  99. glBindVertexArray(vaoHandle);
  100. glEnableVertexAttribArray(aPositionLocation);
  101. glEnableVertexAttribArray(aColorLocation);
  102. checkError();
  103.  
  104. glGenBuffers(1, &positionBufferHandle);
  105. glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
  106. glBufferData(GL_ARRAY_BUFFER, sizeof(positionData), positionData, GL_STATIC_DRAW);
  107.  
  108. glVertexAttribPointer(aPositionLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
  109. checkError();
  110.  
  111. glGenBuffers(1, &colorBufferHandle);
  112. glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
  113. glBufferData(GL_ARRAY_BUFFER, sizeof(colorData), colorData, GL_STATIC_DRAW);
  114.  
  115. glVertexAttribPointer(aColorLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
  116. checkError();
  117.  
  118. glBindVertexArray(vaoHandle);
  119. }
  120.  
  121. void freeVBO()
  122. {
  123. glBindBuffer(GL_ARRAY_BUFFER, 0);
  124. glDeleteBuffers(1, &positionBufferHandle);
  125. glDeleteBuffers(1, &colorBufferHandle);
  126. }
  127.  
  128. void initShader()
  129. {
  130. std::string vertexShaderCode = getFileContent("./simple.vsh");
  131. const char* cStrVertexShaderCode = vertexShaderCode.c_str();
  132.  
  133. std::string fragmentShaderCode = getFileContent("./simple.fsh");
  134. const char* cStrFragmentShaderCode = fragmentShaderCode.c_str();
  135.  
  136. GLuint vShader, fShader;
  137.  
  138. vShader = glCreateShader(GL_VERTEX_SHADER);
  139. glShaderSource(vShader, 1, &cStrVertexShaderCode, 0);
  140. glCompileShader(vShader);
  141. printf("Vertex Shader Log:\n");
  142. shaderLog(vShader);
  143. printf ("End of vertex shader log.\n");
  144.  
  145. fShader = glCreateShader(GL_FRAGMENT_SHADER);
  146. glShaderSource(fShader, 1, &cStrFragmentShaderCode, 0);
  147. glCompileShader(fShader);
  148. printf("Fragment Shader Log:\n");
  149. shaderLog(fShader);
  150. printf("End of fragment shader log.\n");
  151.  
  152. programHandle = glCreateProgram();
  153. glAttachShader(programHandle, vShader);
  154. glAttachShader(programHandle, fShader);
  155. glLinkProgram(programHandle);
  156.  
  157. glDeleteShader(vShader);
  158. glDeleteShader(fShader);
  159.  
  160. int link_ok;
  161. glGetProgramiv(programHandle, GL_LINK_STATUS, &link_ok);
  162. if (!link_ok)
  163. {
  164. printf("Error attach shaders!\n");
  165. return;
  166. }
  167.  
  168. const char* aPositionName = "a_Position";
  169. aPositionLocation = glGetAttribLocation(programHandle, aPositionName);
  170. if (aPositionLocation == -1)
  171. {
  172. printf("Could not find attrib \"%s\"\n", aPositionName);
  173. return;
  174. }
  175. checkError();
  176.  
  177. const char* aColorName = "a_Color";
  178. aColorLocation = glGetAttribLocation(programHandle, aColorName);
  179. if (aColorLocation == -1)
  180. {
  181. printf("Could not find attrib \"%s\"\n", aColorName);
  182. return;
  183. }
  184. checkError();
  185.  
  186. const char* uModelMatrixName = "u_ModelMatrix";
  187. const char* uViewMatrixName = "u_ViewMatrix";
  188. const char* uProjectionMatrixName = "u_ProjectionMatrix";
  189.  
  190. uModelMatrixLocation = glGetUniformLocation(programHandle, uModelMatrixName);
  191. uViewMatrixLocation = glGetUniformLocation(programHandle, uViewMatrixName);
  192. uProjectionMatrixLocation = glGetUniformLocation(programHandle, uProjectionMatrixName);
  193.  
  194. if (uModelMatrixLocation == -1)
  195. {
  196. printf("Could not find uniform \"%s\"\n", uModelMatrixName);
  197. return;
  198. }
  199.  
  200. if (uViewMatrixLocation == -1)
  201. {
  202. printf("Could not find uniform \"%s\"\n", uViewMatrixName);
  203. return;
  204. }
  205.  
  206. if (uProjectionMatrixLocation == -1)
  207. {
  208. printf("Could not find uniform \"%s\"\n", uProjectionMatrixName);
  209. return;
  210. }
  211.  
  212. setViewMatrix();
  213. setModelMatrix();
  214.  
  215. glUseProgram(programHandle);
  216. }
  217.  
  218. void freeShader()
  219. {
  220. glUseProgram(0);
  221. glDeleteProgram(programHandle);
  222. }
  223.  
  224. void shaderLog(GLuint shader)
  225. {
  226. int infoLogLen = 0;
  227. int charsWritten = 0;
  228. char *infoLog;
  229.  
  230. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLen);
  231.  
  232. if (infoLogLen > 1)
  233. {
  234. infoLog = new char[infoLogLen];
  235. if (!infoLog)
  236. {
  237. printf("Error: \"Could not allocate InfoLog buffer\"\n");
  238. exit(1);
  239. }
  240. glGetShaderInfoLog(shader, infoLogLen, &charsWritten, infoLog);
  241. printf("InfoLog: \"%s\"\n", infoLog);
  242. delete[] infoLog;
  243. }
  244. }
  245.  
  246. void setModelMatrix()
  247. {
  248. modelMatrix = glm::mat4(1.0f);
  249. }
  250.  
  251. void setViewMatrix()
  252. {
  253. static glm::vec3 eye(0.0f, 0.0f, 1.5f);
  254. static glm::vec3 look(0.0f, 0.0f, 0.0f);
  255. static glm::vec3 up(0.0f, 1.0f, 0.0f);
  256. viewMatrix = glm::lookAt(eye, look, up);
  257. }
  258.  
  259. void checkError()
  260. {
  261. GLenum errCode = glGetError();
  262. if (errCode != GL_NO_ERROR)
  263. {
  264. printf("OpenGL error: \"%s\"\n", gluErrorString(errCode));
  265. }
  266. }
  267.  
  268. void changeSize(int w, int h)
  269. {
  270. if (w == 0) w = 1;
  271. if (h == 0) h = 1;
  272.  
  273. float left, right, bottom, top, nearWall = 1.0f, farWall = 10.0f;
  274. if (w > h)
  275. {
  276. right = static_cast<float>(w) / h;
  277. left = -right;
  278. top = 1.0f;
  279. bottom = -1.0f;
  280. }
  281. else
  282. {
  283. right = 1.0f;
  284. left = -1.0f;
  285. top = static_cast<float>(h) / w;
  286. bottom = -top;
  287. }
  288.  
  289. projectionMatrix = glm::frustum(left, right, bottom, top, nearWall, farWall);
  290. }
  291.  
  292. void renderScene()
  293. {
  294. glClear(GL_COLOR_BUFFER_BIT);
  295.  
  296. glUniformMatrix4fv(uModelMatrixLocation, 1, GL_FALSE, &modelMatrix[0][0]);
  297. glUniformMatrix4fv(uViewMatrixLocation, 1, GL_FALSE, &viewMatrix[0][0]);
  298. glUniformMatrix4fv(uProjectionMatrixLocation, 1, GL_FALSE, &projectionMatrix[0][0]);
  299.  
  300. glDrawArrays(GL_TRIANGLES, 0, 2 * sizeof(float));
  301.  
  302. checkError();
  303. glutSwapBuffers();
  304. }
  305.  
  306. std::string getFileContent(const char* fileName)
  307. {
  308. std::ifstream stream(fileName);
  309. std::string content(std::istreambuf_iterator<char>(stream), (std::istreambuf_iterator<char>()));
  310. stream.close();
  311. return content;
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement