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 <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <GL/glew.h>
  26. #include <GL/freeglut.h>
  27. #define WINDOW_TITLE_PREFIX "Chapter 2"
  28.  
  29. int CurrentWidth = 800,
  30. CurrentHeight = 600,
  31. WindowHandle = 0;
  32.  
  33. unsigned FrameCount = 0;
  34.  
  35. GLuint
  36. VertexShaderId,
  37. FragmentShaderId,
  38. ProgramId,
  39. VaoId,
  40. VboId,
  41. ColorBufferId;
  42.  
  43. const GLchar* VertexShader =
  44. {
  45. "#version 330\n"\
  46.  
  47. "layout(location=0) in vec4 in_Position;\n"\
  48. "layout(location=1) in vec4 in_Color;\n"\
  49. "out vec4 ex_Color;\n"\
  50.  
  51. "int main()\n"\
  52. "{\n"\
  53. " gl_Position = in_Position;\n"\
  54. " ex_Color = in_Color;\n"\
  55. "}\n"
  56. };
  57.  
  58. const GLchar* FragmentShader =
  59. {
  60. "#version 330\n"\
  61.  
  62. "in vec4 ex_Color;\n"\
  63. "out vec4 out_Color;\n"\
  64.  
  65. "void main(void)\n"\
  66. "{\n"\
  67. " out_Color = ex_Color;\n"\
  68. "}\n"
  69. };
  70.  
  71. void Initialize(int, char*[]);
  72. void InitWindow(int, char*[]);
  73. void ResizeFunction(int, int);
  74. void RenderFunction(void);
  75. void TimerFunction(int);
  76. void IdleFunction(void);
  77. void Cleanup(void);
  78. void CreateVBO(void);
  79. void DestroyVBO(void);
  80. void CreateShaders(void);
  81. void DestroyShaders(void);
  82.  
  83. int main(int argc, char* argv[])
  84. {
  85. Initialize(argc, argv);
  86.  
  87. glutMainLoop();
  88.  
  89. exit(EXIT_SUCCESS);
  90. }
  91.  
  92. void Initialize(int argc, char* argv[])
  93. {
  94. GLenum GlewInitResult;
  95.  
  96. InitWindow(argc, argv);
  97.  
  98. glewExperimental = GL_TRUE;
  99. GlewInitResult = glewInit();
  100.  
  101. if (GLEW_OK != GlewInitResult) {
  102. fprintf(
  103. stderr,
  104. "ERROR: %s\n",
  105. glewGetErrorString(GlewInitResult)
  106. );
  107. exit(EXIT_FAILURE);
  108. }
  109.  
  110. fprintf(
  111. stdout,
  112. "INFO: OpenGL Version: %s\n",
  113. glGetString(GL_VERSION)
  114. );
  115.  
  116. CreateShaders();
  117. CreateVBO();
  118.  
  119. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  120. }
  121.  
  122. void InitWindow(int argc, char* argv[])
  123. {
  124. glutInit(&argc, argv);
  125.  
  126. glutSetOption(
  127. GLUT_ACTION_ON_WINDOW_CLOSE,
  128. GLUT_ACTION_GLUTMAINLOOP_RETURNS
  129. );
  130.  
  131. glutInitWindowSize(CurrentWidth, CurrentHeight);
  132.  
  133. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  134.  
  135. WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);
  136.  
  137. if(WindowHandle < 1) {
  138. fprintf(
  139. stderr,
  140. "ERROR: Could not create a new rendering window.\n"
  141. );
  142. exit(EXIT_FAILURE);
  143. }
  144.  
  145. glutReshapeFunc(ResizeFunction);
  146. glutDisplayFunc(RenderFunction);
  147. glutIdleFunc(IdleFunction);
  148. glutTimerFunc(0, TimerFunction, 0);
  149. glutCloseFunc(Cleanup);
  150. }
  151.  
  152. void ResizeFunction(int Width, int Height)
  153. {
  154. CurrentWidth = Width;
  155. CurrentHeight = Height;
  156. glViewport(0, 0, CurrentWidth, CurrentHeight);
  157. }
  158.  
  159. void RenderFunction(void)
  160. {
  161. ++FrameCount;
  162.  
  163. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  164.  
  165. glDrawArrays(GL_TRIANGLES, 0, 3);
  166.  
  167. glutSwapBuffers();
  168. glutPostRedisplay();
  169. }
  170.  
  171. void IdleFunction(void)
  172. {
  173. glutPostRedisplay();
  174. }
  175.  
  176. void TimerFunction(int Value)
  177. {
  178. if (0 != Value) {
  179. char* TempString = (char*)
  180. malloc(512 + strlen(WINDOW_TITLE_PREFIX));
  181.  
  182. sprintf(
  183. TempString,
  184. "%s: %d Frames Per Second @ %d x %d",
  185. WINDOW_TITLE_PREFIX,
  186. FrameCount * 4,
  187. CurrentWidth,
  188. CurrentHeight
  189. );
  190.  
  191. glutSetWindowTitle(TempString);
  192. free(TempString);
  193. }
  194.  
  195. FrameCount = 0;
  196. glutTimerFunc(250, TimerFunction, 1);
  197. }
  198.  
  199. void Cleanup(void)
  200. {
  201. DestroyShaders();
  202. DestroyVBO();
  203. }
  204.  
  205. void CreateVBO(void)
  206. {
  207. GLfloat Vertices[] = {
  208. -0.8f, -0.8f, 0.0f, 1.0f,
  209. 0.0f, 0.8f, 0.0f, 1.0f,
  210. 0.8f, -0.8f, 0.0f, 1.0f
  211. };
  212.  
  213. GLfloat Colors[] = {
  214. 1.0f, 0.0f, 0.0f, 1.0f,
  215. 0.0f, 1.0f, 0.0f, 1.0f,
  216. 0.0f, 0.0f, 1.0f, 1.0f
  217. };
  218.  
  219. GLenum ErrorCheckValue = glGetError();
  220.  
  221. glGenVertexArrays(1, &VaoId);
  222. glBindVertexArray(VaoId);
  223.  
  224. glGenBuffers(1, &VboId);
  225. glBindBuffer(GL_ARRAY_BUFFER, VboId);
  226. glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
  227. glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
  228. glEnableVertexAttribArray(0);
  229.  
  230. glGenBuffers(1, &ColorBufferId);
  231. glBindBuffer(GL_ARRAY_BUFFER, ColorBufferId);
  232. glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
  233. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
  234. glEnableVertexAttribArray(1);
  235.  
  236. ErrorCheckValue = glGetError();
  237. if (ErrorCheckValue != GL_NO_ERROR)
  238. {
  239. fprintf(
  240. stderr,
  241. "ERROR: Could not create a VBO: %s \n",
  242. gluErrorString(ErrorCheckValue)
  243. );
  244.  
  245. exit(-1);
  246. }
  247. }
  248.  
  249. void DestroyVBO(void)
  250. {
  251. GLenum ErrorCheckValue = glGetError();
  252.  
  253. glDisableVertexAttribArray(1);
  254. glDisableVertexAttribArray(0);
  255.  
  256. glBindBuffer(GL_ARRAY_BUFFER, 0);
  257.  
  258. glDeleteBuffers(1, &ColorBufferId);
  259. glDeleteBuffers(1, &VboId);
  260.  
  261. glBindVertexArray(0);
  262. glDeleteVertexArrays(1, &VaoId);
  263.  
  264. ErrorCheckValue = glGetError();
  265. if (ErrorCheckValue != GL_NO_ERROR)
  266. {
  267. fprintf(
  268. stderr,
  269. "ERROR: Could not destroy the VBO: %s \n",
  270. gluErrorString(ErrorCheckValue)
  271. );
  272.  
  273. exit(-1);
  274. }
  275. }
  276.  
  277. void CreateShaders(void)
  278. {
  279. GLenum ErrorCheckValue = glGetError();
  280.  
  281. VertexShaderId = glCreateShader(GL_VERTEX_SHADER);
  282. glShaderSource(VertexShaderId, 1, &VertexShader, NULL);
  283. glCompileShader(VertexShaderId);
  284.  
  285. FragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
  286. glShaderSource(FragmentShaderId, 1, &FragmentShader, NULL);
  287. glCompileShader(FragmentShaderId);
  288.  
  289. ProgramId = glCreateProgram();
  290. glAttachShader(ProgramId, VertexShaderId);
  291. glAttachShader(ProgramId, FragmentShaderId);
  292. glLinkProgram(ProgramId);
  293. glUseProgram(ProgramId);
  294.  
  295. ErrorCheckValue = glGetError();
  296. if (false && ErrorCheckValue != GL_NO_ERROR)
  297. {
  298. fprintf(
  299. stderr,
  300. "ERROR: Could not create the shaders: %s \n",
  301. gluErrorString(ErrorCheckValue)
  302. );
  303.  
  304. exit(-1);
  305. }
  306. }
  307.  
  308. void DestroyShaders(void)
  309. {
  310. GLenum ErrorCheckValue = glGetError();
  311.  
  312. glUseProgram(0);
  313.  
  314. glDetachShader(ProgramId, VertexShaderId);
  315. glDetachShader(ProgramId, FragmentShaderId);
  316.  
  317. glDeleteShader(FragmentShaderId);
  318. glDeleteShader(VertexShaderId);
  319.  
  320. glDeleteProgram(ProgramId);
  321.  
  322. ErrorCheckValue = glGetError();
  323. if (ErrorCheckValue != GL_NO_ERROR)
  324. {
  325. fprintf(
  326. stderr,
  327. "ERROR: Could not destroy the shaders: %s \n",
  328. gluErrorString(ErrorCheckValue)
  329. );
  330.  
  331. exit(-1);
  332. }
  333. }
  334.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:25:21: error: GL/glew.h: No such file or directory
prog.cpp:26:25: error: GL/freeglut.h: No such file or directory
prog.cpp:35: error: ‘GLuint’ does not name a type
prog.cpp:43: error: expected initializer before ‘*’ token
prog.cpp:58: error: expected initializer before ‘*’ token
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:87: error: ‘glutMainLoop’ was not declared in this scope
prog.cpp: In function ‘void Initialize(int, char**)’:
prog.cpp:94: error: ‘GLenum’ was not declared in this scope
prog.cpp:94: error: expected `;' before ‘GlewInitResult’
prog.cpp:98: error: ‘glewExperimental’ was not declared in this scope
prog.cpp:98: error: ‘GL_TRUE’ was not declared in this scope
prog.cpp:99: error: ‘GlewInitResult’ was not declared in this scope
prog.cpp:99: error: ‘glewInit’ was not declared in this scope
prog.cpp:101: error: ‘GLEW_OK’ was not declared in this scope
prog.cpp:105: error: ‘glewGetErrorString’ was not declared in this scope
prog.cpp:113: error: ‘GL_VERSION’ was not declared in this scope
prog.cpp:113: error: ‘glGetString’ was not declared in this scope
prog.cpp:119: error: ‘glClearColor’ was not declared in this scope
prog.cpp: In function ‘void InitWindow(int, char**)’:
prog.cpp:124: error: ‘glutInit’ was not declared in this scope
prog.cpp:127: error: ‘GLUT_ACTION_ON_WINDOW_CLOSE’ was not declared in this scope
prog.cpp:128: error: ‘GLUT_ACTION_GLUTMAINLOOP_RETURNS’ was not declared in this scope
prog.cpp:129: error: ‘glutSetOption’ was not declared in this scope
prog.cpp:131: error: ‘glutInitWindowSize’ was not declared in this scope
prog.cpp:133: error: ‘GLUT_DEPTH’ was not declared in this scope
prog.cpp:133: error: ‘GLUT_DOUBLE’ was not declared in this scope
prog.cpp:133: error: ‘GLUT_RGBA’ was not declared in this scope
prog.cpp:133: error: ‘glutInitDisplayMode’ was not declared in this scope
prog.cpp:135: error: ‘glutCreateWindow’ was not declared in this scope
prog.cpp:145: error: ‘glutReshapeFunc’ was not declared in this scope
prog.cpp:146: error: ‘glutDisplayFunc’ was not declared in this scope
prog.cpp:147: error: ‘glutIdleFunc’ was not declared in this scope
prog.cpp:148: error: ‘glutTimerFunc’ was not declared in this scope
prog.cpp:149: error: ‘glutCloseFunc’ was not declared in this scope
prog.cpp: In function ‘void ResizeFunction(int, int)’:
prog.cpp:156: error: ‘glViewport’ was not declared in this scope
prog.cpp: In function ‘void RenderFunction()’:
prog.cpp:163: error: ‘GL_COLOR_BUFFER_BIT’ was not declared in this scope
prog.cpp:163: error: ‘GL_DEPTH_BUFFER_BIT’ was not declared in this scope
prog.cpp:163: error: ‘glClear’ was not declared in this scope
prog.cpp:165: error: ‘GL_TRIANGLES’ was not declared in this scope
prog.cpp:165: error: ‘glDrawArrays’ was not declared in this scope
prog.cpp:167: error: ‘glutSwapBuffers’ was not declared in this scope
prog.cpp:168: error: ‘glutPostRedisplay’ was not declared in this scope
prog.cpp: In function ‘void IdleFunction()’:
prog.cpp:173: error: ‘glutPostRedisplay’ was not declared in this scope
prog.cpp: In function ‘void TimerFunction(int)’:
prog.cpp:191: error: ‘glutSetWindowTitle’ was not declared in this scope
prog.cpp:196: error: ‘glutTimerFunc’ was not declared in this scope
prog.cpp: In function ‘void CreateVBO()’:
prog.cpp:207: error: ‘GLfloat’ was not declared in this scope
prog.cpp:207: error: expected `;' before ‘Vertices’
prog.cpp:213: error: expected `;' before ‘Colors’
prog.cpp:219: error: ‘GLenum’ was not declared in this scope
prog.cpp:219: error: expected `;' before ‘ErrorCheckValue’
prog.cpp:221: error: ‘VaoId’ was not declared in this scope
prog.cpp:221: error: ‘glGenVertexArrays’ was not declared in this scope
prog.cpp:222: error: ‘glBindVertexArray’ was not declared in this scope
prog.cpp:224: error: ‘VboId’ was not declared in this scope
prog.cpp:224: error: ‘glGenBuffers’ was not declared in this scope
prog.cpp:225: error: ‘GL_ARRAY_BUFFER’ was not declared in this scope
prog.cpp:225: error: ‘glBindBuffer’ was not declared in this scope
prog.cpp:226: error: ‘Vertices’ was not declared in this scope
prog.cpp:226: error: ‘GL_STATIC_DRAW’ was not declared in this scope
prog.cpp:226: error: ‘glBufferData’ was not declared in this scope
prog.cpp:227: error: ‘GL_FLOAT’ was not declared in this scope
prog.cpp:227: error: ‘GL_FALSE’ was not declared in this scope
prog.cpp:227: error: ‘glVertexAttribPointer’ was not declared in this scope
prog.cpp:228: error: ‘glEnableVertexAttribArray’ was not declared in this scope
prog.cpp:230: error: ‘ColorBufferId’ was not declared in this scope
prog.cpp:232: error: ‘Colors’ was not declared in this scope
prog.cpp:236: error: ‘ErrorCheckValue’ was not declared in this scope
prog.cpp:236: error: ‘glGetError’ was not declared in this scope
prog.cpp:237: error: ‘GL_NO_ERROR’ was not declared in this scope
prog.cpp:242: error: ‘gluErrorString’ was not declared in this scope
prog.cpp: In function ‘void DestroyVBO()’:
prog.cpp:251: error: ‘GLenum’ was not declared in this scope
prog.cpp:251: error: expected `;' before ‘ErrorCheckValue’
prog.cpp:253: error: ‘glDisableVertexAttribArray’ was not declared in this scope
prog.cpp:256: error: ‘GL_ARRAY_BUFFER’ was not declared in this scope
prog.cpp:256: error: ‘glBindBuffer’ was not declared in this scope
prog.cpp:258: error: ‘ColorBufferId’ was not declared in this scope
prog.cpp:258: error: ‘glDeleteBuffers’ was not declared in this scope
prog.cpp:259: error: ‘VboId’ was not declared in this scope
prog.cpp:261: error: ‘glBindVertexArray’ was not declared in this scope
prog.cpp:262: error: ‘VaoId’ was not declared in this scope
prog.cpp:262: error: ‘glDeleteVertexArrays’ was not declared in this scope
prog.cpp:264: error: ‘ErrorCheckValue’ was not declared in this scope
prog.cpp:264: error: ‘glGetError’ was not declared in this scope
prog.cpp:265: error: ‘GL_NO_ERROR’ was not declared in this scope
prog.cpp:270: error: ‘gluErrorString’ was not declared in this scope
prog.cpp: In function ‘void CreateShaders()’:
prog.cpp:279: error: ‘GLenum’ was not declared in this scope
prog.cpp:279: error: expected `;' before ‘ErrorCheckValue’
prog.cpp:281: error: ‘VertexShaderId’ was not declared in this scope
prog.cpp:281: error: ‘GL_VERTEX_SHADER’ was not declared in this scope
prog.cpp:281: error: ‘glCreateShader’ was not declared in this scope
prog.cpp:282: error: ‘VertexShader’ was not declared in this scope
prog.cpp:282: error: ‘glShaderSource’ was not declared in this scope
prog.cpp:283: error: ‘glCompileShader’ was not declared in this scope
prog.cpp:285: error: ‘FragmentShaderId’ was not declared in this scope
prog.cpp:285: error: ‘GL_FRAGMENT_SHADER’ was not declared in this scope
prog.cpp:286: error: ‘FragmentShader’ was not declared in this scope
prog.cpp:289: error: ‘ProgramId’ was not declared in this scope
prog.cpp:289: error: ‘glCreateProgram’ was not declared in this scope
prog.cpp:290: error: ‘glAttachShader’ was not declared in this scope
prog.cpp:292: error: ‘glLinkProgram’ was not declared in this scope
prog.cpp:293: error: ‘glUseProgram’ was not declared in this scope
prog.cpp:295: error: ‘ErrorCheckValue’ was not declared in this scope
prog.cpp:295: error: ‘glGetError’ was not declared in this scope
prog.cpp:296: error: ‘GL_NO_ERROR’ was not declared in this scope
prog.cpp:301: error: ‘gluErrorString’ was not declared in this scope
prog.cpp: In function ‘void DestroyShaders()’:
prog.cpp:310: error: ‘GLenum’ was not declared in this scope
prog.cpp:310: error: expected `;' before ‘ErrorCheckValue’
prog.cpp:312: error: ‘glUseProgram’ was not declared in this scope
prog.cpp:314: error: ‘ProgramId’ was not declared in this scope
prog.cpp:314: error: ‘VertexShaderId’ was not declared in this scope
prog.cpp:314: error: ‘glDetachShader’ was not declared in this scope
prog.cpp:315: error: ‘FragmentShaderId’ was not declared in this scope
prog.cpp:317: error: ‘glDeleteShader’ was not declared in this scope
prog.cpp:320: error: ‘glDeleteProgram’ was not declared in this scope
prog.cpp:322: error: ‘ErrorCheckValue’ was not declared in this scope
prog.cpp:322: error: ‘glGetError’ was not declared in this scope
prog.cpp:323: error: ‘GL_NO_ERROR’ was not declared in this scope
prog.cpp:328: error: ‘gluErrorString’ was not declared in this scope
stdout
Standard output is empty