fork download
  1. /* Copyright (C) 2011 by Eddy Luten
  2.  
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9.  
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12.  
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21.  
  22. #include "Utils.h"
  23. #define WINDOW_TITLE_PREFIX "Chapter 4"
  24.  
  25. int CurrentWidth = 800,
  26. CurrentHeight = 600,
  27. WindowHandle = 0;
  28.  
  29. unsigned FrameCount = 0;
  30.  
  31. GLuint
  32. ProjectionMatrixUniformLocation,
  33. ViewMatrixUniformLocation,
  34. ModelMatrixUniformLocation,
  35. BufferIds[3] = { 0 },
  36. ShaderIds[3] = { 0 };
  37.  
  38. Matrix
  39. ProjectionMatrix,
  40. ViewMatrix,
  41. ModelMatrix;
  42.  
  43. float CubeRotation = 0;
  44. clock_t LastTime = 0;
  45.  
  46. void Initialize(int, char*[]);
  47. void InitWindow(int, char*[]);
  48. void ResizeFunction(int, int);
  49. void RenderFunction(void);
  50. void TimerFunction(int);
  51. void IdleFunction(void);
  52. void CreateCube(void);
  53. void DestroyCube(void);
  54. void DrawCube(void);
  55.  
  56. int main(int argc, char* argv[])
  57. {
  58. Initialize(argc, argv);
  59.  
  60. glutMainLoop();
  61.  
  62. exit(EXIT_SUCCESS);
  63. }
  64.  
  65. void Initialize(int argc, char* argv[])
  66. {
  67. GLenum GlewInitResult;
  68.  
  69. InitWindow(argc, argv);
  70.  
  71. glewExperimental = GL_TRUE;
  72. GlewInitResult = glewInit();
  73.  
  74. if (GLEW_OK != GlewInitResult) {
  75. fprintf(
  76. stderr,
  77. "ERROR: %s\n",
  78. glewGetErrorString(GlewInitResult)
  79. );
  80. exit(EXIT_FAILURE);
  81. }
  82.  
  83. fprintf(
  84. stdout,
  85. "INFO: OpenGL Version: %s\n",
  86. glGetString(GL_VERSION)
  87. );
  88.  
  89. glGetError();
  90. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  91.  
  92. glEnable(GL_DEPTH_TEST);
  93. glDepthFunc(GL_LESS);
  94. ExitOnGLError("ERROR: Could not set OpenGL depth testing options");
  95.  
  96. glEnable(GL_CULL_FACE);
  97. glCullFace(GL_BACK);
  98. glFrontFace(GL_CCW);
  99. ExitOnGLError("ERROR: Could not set OpenGL culling options");
  100.  
  101. ModelMatrix = IDENTITY_MATRIX;
  102. ProjectionMatrix = IDENTITY_MATRIX;
  103. ViewMatrix = IDENTITY_MATRIX;
  104. TranslateMatrix(&ViewMatrix, 0, 0, -2);
  105.  
  106. CreateCube();
  107. }
  108.  
  109. void InitWindow(int argc, char* argv[])
  110. {
  111. glutInit(&argc, argv);
  112.  
  113. glutInitContextVersion(4, 0);
  114. glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
  115. glutInitContextProfile(GLUT_CORE_PROFILE);
  116.  
  117. glutSetOption(
  118. GLUT_ACTION_ON_WINDOW_CLOSE,
  119. GLUT_ACTION_GLUTMAINLOOP_RETURNS
  120. );
  121.  
  122. glutInitWindowSize(CurrentWidth, CurrentHeight);
  123.  
  124. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  125.  
  126. WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);
  127.  
  128. if(WindowHandle < 1) {
  129. fprintf(
  130. stderr,
  131. "ERROR: Could not create a new rendering window.\n"
  132. );
  133. exit(EXIT_FAILURE);
  134. }
  135.  
  136. glutReshapeFunc(ResizeFunction);
  137. glutDisplayFunc(RenderFunction);
  138. glutIdleFunc(IdleFunction);
  139. glutTimerFunc(0, TimerFunction, 0);
  140. glutCloseFunc(DestroyCube);
  141. }
  142.  
  143. void ResizeFunction(int Width, int Height)
  144. {
  145. CurrentWidth = Width;
  146. CurrentHeight = Height;
  147. glViewport(0, 0, CurrentWidth, CurrentHeight);
  148. ProjectionMatrix =
  149. CreateProjectionMatrix(
  150. 60,
  151. (float)CurrentWidth / CurrentHeight,
  152. 1.0f,
  153. 100.0f
  154. );
  155.  
  156. glUseProgram(ShaderIds[0]);
  157. glUniformMatrix4fv(ProjectionMatrixUniformLocation, 1, GL_FALSE, ProjectionMatrix.m);
  158. glUseProgram(0);
  159. }
  160.  
  161. void RenderFunction(void)
  162. {
  163. ++FrameCount;
  164.  
  165. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  166.  
  167. DrawCube();
  168.  
  169. glutSwapBuffers();
  170. glutPostRedisplay();
  171. }
  172.  
  173. void IdleFunction(void)
  174. {
  175. glutPostRedisplay();
  176. }
  177.  
  178. void TimerFunction(int Value)
  179. {
  180. if (0 != Value) {
  181. char* TempString = (char*)
  182. malloc(512 + strlen(WINDOW_TITLE_PREFIX));
  183.  
  184. sprintf(
  185. TempString,
  186. "%s: %d Frames Per Second @ %d x %d",
  187. WINDOW_TITLE_PREFIX,
  188. FrameCount * 4,
  189. CurrentWidth,
  190. CurrentHeight
  191. );
  192.  
  193. glutSetWindowTitle(TempString);
  194. free(TempString);
  195. }
  196.  
  197. FrameCount = 0;
  198. glutTimerFunc(250, TimerFunction, 1);
  199. }
  200.  
  201. void CreateCube()
  202. {
  203. const Vertex VERTICES[8] =
  204. {
  205. { { -.5f, -.5f, .5f, 1 }, { 0, 0, 1, 1 } },
  206. { { -.5f, .5f, .5f, 1 }, { 1, 0, 0, 1 } },
  207. { { .5f, .5f, .5f, 1 }, { 0, 1, 0, 1 } },
  208. { { .5f, -.5f, .5f, 1 }, { 1, 1, 0, 1 } },
  209. { { -.5f, -.5f, -.5f, 1 }, { 1, 1, 1, 1 } },
  210. { { -.5f, .5f, -.5f, 1 }, { 1, 0, 0, 1 } },
  211. { { .5f, .5f, -.5f, 1 }, { 1, 0, 1, 1 } },
  212. { { .5f, -.5f, -.5f, 1 }, { 0, 0, 1, 1 } }
  213. };
  214.  
  215. const GLuint INDICES[36] =
  216. {
  217. 0,2,1, 0,3,2,
  218. 4,3,0, 4,7,3,
  219. 4,1,5, 4,0,1,
  220. 3,6,2, 3,7,6,
  221. 1,6,5, 1,2,6,
  222. 7,5,6, 7,4,5
  223. };
  224.  
  225. ShaderIds[0] = glCreateProgram();
  226. ExitOnGLError("ERROR: Could not create the shader program");
  227. {
  228. ShaderIds[1] = LoadShader("SimpleShader.fragment.glsl", GL_FRAGMENT_SHADER);
  229. ShaderIds[2] = LoadShader("SimpleShader.vertex.glsl", GL_VERTEX_SHADER);
  230. glAttachShader(ShaderIds[0], ShaderIds[1]);
  231. glAttachShader(ShaderIds[0], ShaderIds[2]);
  232. }
  233. glLinkProgram(ShaderIds[0]);
  234. ExitOnGLError("ERROR: Could not link the shader program");
  235.  
  236. ModelMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ModelMatrix");
  237. ViewMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ViewMatrix");
  238. ProjectionMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ProjectionMatrix");
  239. ExitOnGLError("ERROR: Could not get shader uniform locations");
  240.  
  241. glGenVertexArrays(1, &BufferIds[0]);
  242. ExitOnGLError("ERROR: Could not generate the VAO");
  243. glBindVertexArray(BufferIds[0]);
  244. ExitOnGLError("ERROR: Could not bind the VAO");
  245.  
  246. glEnableVertexAttribArray(0);
  247. glEnableVertexAttribArray(1);
  248. ExitOnGLError("ERROR: Could not enable vertex attributes");
  249.  
  250. glGenBuffers(2, &BufferIds[1]);
  251. ExitOnGLError("ERROR: Could not generate the buffer objects");
  252.  
  253. glBindBuffer(GL_ARRAY_BUFFER, BufferIds[1]);
  254. glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW);
  255. ExitOnGLError("ERROR: Could not bind the VBO to the VAO");
  256.  
  257. glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)0);
  258. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(VERTICES[0]), (GLvoid*)sizeof(VERTICES[0].Position));
  259. ExitOnGLError("ERROR: Could not set VAO attributes");
  260.  
  261. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferIds[2]);
  262. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(INDICES), INDICES, GL_STATIC_DRAW);
  263. ExitOnGLError("ERROR: Could not bind the IBO to the VAO");
  264.  
  265. glBindVertexArray(0);
  266. }
  267.  
  268. void DestroyCube()
  269. {
  270. glDetachShader(ShaderIds[0], ShaderIds[1]);
  271. glDetachShader(ShaderIds[0], ShaderIds[2]);
  272. glDeleteShader(ShaderIds[1]);
  273. glDeleteShader(ShaderIds[2]);
  274. glDeleteProgram(ShaderIds[0]);
  275. ExitOnGLError("ERROR: Could not destroy the shaders");
  276.  
  277. glDeleteBuffers(2, &BufferIds[1]);
  278. glDeleteVertexArrays(1, &BufferIds[0]);
  279. ExitOnGLError("ERROR: Could not destroy the buffer objects");
  280. }
  281.  
  282. void DrawCube(void)
  283. {
  284. float CubeAngle;
  285. clock_t Now = clock();
  286.  
  287. if (LastTime == 0)
  288. LastTime = Now;
  289.  
  290. CubeRotation += 45.0f * ((float)(Now - LastTime) / CLOCKS_PER_SEC);
  291. CubeAngle = DegreesToRadians(CubeRotation);
  292. LastTime = Now;
  293.  
  294. ModelMatrix = IDENTITY_MATRIX;
  295. RotateAboutY(&ModelMatrix, CubeAngle);
  296. RotateAboutX(&ModelMatrix, CubeAngle);
  297.  
  298. glUseProgram(ShaderIds[0]);
  299. ExitOnGLError("ERROR: Could not use the shader program");
  300.  
  301. glUniformMatrix4fv(ModelMatrixUniformLocation, 1, GL_FALSE, ModelMatrix.m);
  302. glUniformMatrix4fv(ViewMatrixUniformLocation, 1, GL_FALSE, ViewMatrix.m);
  303. ExitOnGLError("ERROR: Could not set the shader uniforms");
  304.  
  305. glBindVertexArray(BufferIds[0]);
  306. ExitOnGLError("ERROR: Could not bind the VAO for drawing purposes");
  307.  
  308. glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (GLvoid*)0);
  309. ExitOnGLError("ERROR: Could not draw the cube");
  310.  
  311. glBindVertexArray(0);
  312. glUseProgram(0);
  313. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:22:19: fatal error: Utils.h: No such file or directory
 #include "Utils.h"
                   ^
compilation terminated.
stdout
Standard output is empty