PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/first_glut.cpp

https://github.com/superBatbrat/LearningOGL
C++ | 282 lines | 112 code | 52 blank | 118 comment | 7 complexity | a093c29a2010ca6ec272b09f35efacbe MD5 | raw file
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. //For OGL 3.2
  5. #define GL3_PROTOTYPES 1
  6. #include "GL3/gl3.h"
  7. #include "GL/glut.h"
  8. #include "GL/freeglut_ext.h"
  9. //Freeglut pulls these in anyway
  10. //#include <GL/gl.h>
  11. //#include <GL/glu.h>
  12. //#define DEBUG 1
  13. void init()
  14. {
  15. /* select clearing (background) color */
  16. glClearColor(0.0, 0.0, 0.0, 0.0);
  17. /* initialize viewing values */
  18. /*
  19. glMatrixMode(GL_PROJECTION);
  20. glLoadIdentity();
  21. glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
  22. */
  23. }
  24. /*
  25. * Reads the contents of a file and stores them in a c_string.
  26. * The c_string is allocated dynamically. If
  27. * there is any error at any stage, a null c_string is returned.
  28. * Input:
  29. * The full path to a file (including the filename) as
  30. * a std::string. Note that it is assumed that the file
  31. * is assumed to be plaintext. Also, if the filename alone
  32. * is specified, it assumes that the file is in the current
  33. * directory.
  34. * Returns:
  35. * A c style string with the contents of the shader's file.
  36. * A null string.
  37. */
  38. GLchar* file_to_char_pointer(std::string path_to_file)
  39. {
  40. //TODO: Add exception handling.
  41. //TODO: Make this function nicer, so as to use glShaderSource fully.
  42. GLchar* shader_source_code = NULL;
  43. unsigned int file_size;
  44. //Open the file.
  45. std::ifstream shader_file(path_to_file.c_str());
  46. //Read in all the lines if the file is open.
  47. if(shader_file.is_open())
  48. {
  49. //Calculate the file's size in bytes
  50. //Move the get pointer to the end
  51. shader_file.seekg(0, std::ios::end);
  52. //The position of the get pointer gives the number of bytes
  53. file_size = shader_file.tellg();
  54. if(file_size != -1)
  55. {
  56. /* Allocate enough space in the char array to hold
  57. * the file's contents.
  58. */
  59. //TODO: Check this code.
  60. shader_source_code = new GLchar[file_size];
  61. //Send the get pointer back to the beginning.
  62. shader_file.seekg(0, std::ios::beg);
  63. //Read the file into the source code buffer.
  64. int i = 0; //Gives the position in the buffer to write the
  65. //next character to.
  66. while(!shader_file.eof())
  67. {
  68. /* TODO: See if the other ways to read data are
  69. * better.
  70. */
  71. /* Get the next character and write it to
  72. * the buffer.
  73. */
  74. shader_file.get(shader_source_code[i]);
  75. //Step to te next position in the buffer.
  76. i++;
  77. }
  78. }
  79. }
  80. //Close the file.
  81. shader_file.close();
  82. #ifdef DEBUG
  83. std::cout << shader_source_code;
  84. #endif
  85. //Return the said pointer.
  86. return shader_source_code;
  87. }
  88. void display(void)
  89. {
  90. /* clear all pixels */
  91. glClear(GL_COLOR_BUFFER_BIT);
  92. //OLD OGL CODE
  93. /* draw white polygon (rectangle) with corners at
  94. * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
  95. */
  96. //glColor3f(1.0, 1.0, 1.0);
  97. //GLfloat vertex_buffer[] = {0.25, 0.25, 0.0, 0.75, 0.25, 0.0, 0.75, 0.75, 0.0,\
  98. // 0.25, 0.75, 0.0};
  99. //FFP
  100. /*
  101. glBegin(GL_POLYGON);
  102. glVertex3f(0.25, 0.25, 0.0);
  103. glVertex3f(0.75, 0.25, 0.0);
  104. glVertex3f(0.75, 0.75, 0.0);
  105. glVertex3f(0.25, 0.75, 0.0);
  106. glEnd();
  107. */
  108. //Using vertex arrays
  109. /*
  110. glEnableClientState(GL_VERTEX_ARRAY);
  111. glVertexPointer(3, GL_FLOAT, 0, vertex_buffer);
  112. glDrawArrays(GL_QUADS, 0, 4);
  113. glDisableClientState(GL_VERTEX_ARRAY);
  114. */
  115. //Draw code for OGL 3.2
  116. //TODO Hope this works
  117. glDrawElements(GL_TRIANGLES, 6,\
  118. GL_UNSIGNED_INT, 0);
  119. glFlush();
  120. }
  121. int main(int argc, char** argv)
  122. {
  123. //The order of initialisation may be wrong. TODO check.
  124. //For OGL3.2
  125. glutInitContextVersion (3, 2);
  126. glutInitContextProfile(GLUT_CORE_PROFILE);
  127. //Usual glut code
  128. glutInit(&argc, argv);
  129. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  130. #ifdef DEBUG
  131. //TODO: Check if freeglut is at least at version 2.60 (?)
  132. std::cout<< "Glut version:" << glutGet(GLUT_VERSION) << std::endl;
  133. #endif
  134. glutInitWindowSize(250, 250);
  135. glutInitWindowPosition(100, 100);
  136. glutCreateWindow("Hello");
  137. init();
  138. GLfloat square[] = {0.25, 0.25, 0.0, 0.75, 0.25, 0.0, 0.75, 0.75, 0.0,\
  139. 0.25, 0.75, 0.0};
  140. //Indices assume GL_TRIANGLES TODO-- check!
  141. GLuint square_indices[] = {0, 3, 2, 2, 1, 0};
  142. //Using VBO's and VAO's
  143. //Generate the name for a Vertex Array
  144. GLuint vao_name;
  145. glGenVertexArrays(1, &vao_name);
  146. //Bind this vao as the currently used object
  147. glBindVertexArray(vao_name);
  148. //Create the names two vertex buffer object
  149. GLuint vbo_names[2];
  150. glGenBuffers(2, vbo_names);
  151. //Bind the first vbo and indicate that it contains vertex data
  152. glBindBuffer(GL_ARRAY_BUFFER, vbo_names[0]);
  153. /* TODO: See if there is a better spec for vbo access
  154. * than GL_STATIC_DRAW
  155. */
  156. //Copy the vertex data from the square array to this buffer
  157. glBufferData(GL_ARRAY_BUFFER, sizeof(GL_FLOAT) * 12, square,\
  158. GL_STATIC_DRAW);
  159. /* Specify that the vertex data is going into vertex attribute zero.
  160. * in the vertex attribute array. Enable the corresponding attribute
  161. * in the array.
  162. */
  163. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  164. glEnableVertexAttribArray(0);
  165. //Bind the second vbo as being an active buffer
  166. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_names[1]);
  167. //Copy the index data to the second vbo
  168. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * 6, \
  169. square_indices, GL_STATIC_DRAW);
  170. /* Specify that the index data is going into vertex attribute one
  171. * n the vertex attribute array. Enable the corresponding attribute
  172. * in the array.
  173. */
  174. glVertexAttribPointer(1, 1, GL_INT, GL_FALSE, 0, 0);
  175. glEnableVertexAttribArray(1);
  176. //Shaders!
  177. GLchar *vertex_source, *fragment_source;
  178. GLuint vertex_shader, fragment_shader;
  179. GLuint shader_program;
  180. //Obtain the source for the shader programs.
  181. vertex_source = file_to_char_pointer("first_vertex_shader.glsl");
  182. fragment_source = file_to_char_pointer("first_fragment_shader.glsl");
  183. if((vertex_source != NULL) && (fragment_source != NULL))
  184. {
  185. //Obtain the name for the shader program.
  186. shader_program = glCreateProgram();
  187. //Obtain the names of the shaders
  188. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  189. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  190. //Set the source code of the shaders.
  191. //Fix this unsafe casting.
  192. glShaderSource(vertex_shader, 1, (const GLchar**)&vertex_source, NULL);
  193. //Strings \0 terminated
  194. glShaderSource(fragment_shader, 1, (const GLchar**) &fragment_source, NULL);
  195. //Compile the shaders
  196. glCompileShader(vertex_shader);
  197. glCompileShader(fragment_shader);
  198. //Attach the shaders to the program
  199. /* TODO: Figure out these lines carefully. Is the order of attaching
  200. * shaders important?
  201. */
  202. glAttachShader(shader_program, vertex_shader);
  203. glAttachShader(shader_program, fragment_shader);
  204. //Link the program
  205. glLinkProgram(shader_program);
  206. //Bind parameters to the shader.
  207. glBindAttribLocation(shader_program, 0, "in_position");
  208. //Set this program as the active shader program
  209. glUseProgram(shader_program);
  210. }
  211. glutDisplayFunc(display);
  212. glutMainLoop();
  213. //Do cleanup.
  214. /*
  215. * CAUTION: Assumes that all the things that are cleaned up exist in the first place. This may not be true.
  216. */
  217. glUseProgram(0);
  218. glDetachShader(shader_program, vertex_shader);
  219. glDetachShader(shader_program, fragment_shader);
  220. glDeleteProgram(shader_program);
  221. glDeleteShader(vertex_shader);
  222. glDeleteShader(fragment_shader);
  223. glDisableVertexAttribArray(0);
  224. glDisableVertexAttribArray(1);
  225. glDeleteBuffers(2, vbo_names);
  226. glDeleteVertexArrays(1, &vao_name);
  227. delete vertex_source;
  228. delete fragment_source;
  229. return 0;
  230. }