Advertisement
Guest User

Untitled

a guest
Oct 8th, 2013
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. /* $Id: ex2-1_white_square.cpp, v1.3 2011/09/05$
  2. *
  3. * Author: Christopher Dyken, <[email protected]>
  4. * Reviewed by: Bartlomiej Siwek, <[email protected]>
  5. *
  6. * Distributed under the GNU GPL.
  7. */
  8.  
  9. #include <iostream>
  10. #include <GL/glew.h>
  11. #include <GL/freeglut.h>
  12. #include <glm/glm.hpp>
  13.  
  14. #define BUFFER_OFFSET(i) ((char *)NULL + (i))
  15.  
  16. // Vertex structure
  17. struct Vertex {
  18. Vertex(float x, float y)
  19. : position(x, y) {
  20. }
  21.  
  22. Vertex(const glm::vec2 &p)
  23. : position(p) {
  24. }
  25.  
  26. glm::vec2 position;
  27. };
  28.  
  29. // Buffer indentifiers
  30. GLuint verticesVertexBufferObjectId;
  31.  
  32. void myInit() {
  33. // Geometry - vertices and indices
  34. Vertex vertices[4] = {
  35. Vertex(-0.5f, 0.5f),
  36. Vertex( 0.5f, 0.5f),
  37. Vertex( 0.5f, -0.5f),
  38. Vertex(-0.5f, -0.5f),
  39. };
  40.  
  41. // Setup buffer objects
  42. glGenBuffers(1, &verticesVertexBufferObjectId);
  43.  
  44. // Fill the VBO's with data
  45. glBindBuffer(GL_ARRAY_BUFFER, verticesVertexBufferObjectId);
  46. glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
  47. glVertexPointer(2, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(0));
  48.  
  49. // Deactivate VBO
  50. glBindBuffer(GL_ARRAY_BUFFER, 0);
  51.  
  52. // Setup a clear color
  53. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  54.  
  55. // Setup a projection
  56. glMatrixMode(GL_PROJECTION);
  57. glLoadIdentity();
  58. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  59.  
  60. // Setup a view
  61. glMatrixMode(GL_MODELVIEW);
  62. glLoadIdentity();
  63.  
  64.  
  65. }
  66.  
  67. void myReshape(int w, int h) {
  68. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  69.  
  70. }
  71.  
  72. void myDisplay() {
  73. // Clear the backgound
  74. glClear(GL_COLOR_BUFFER_BIT);
  75.  
  76. // Bind VBO's
  77. glBindBuffer(GL_ARRAY_BUFFER, verticesVertexBufferObjectId);
  78.  
  79. // Draw
  80. glColor3f(1.0f, 1.0f, 1.0f);
  81. glEnableClientState(GL_VERTEX_ARRAY);
  82. glDrawArrays(GL_QUADS, 0, 4);
  83.  
  84. // Unbind VBO's
  85. glBindBuffer(GL_ARRAY_BUFFER, 0);
  86.  
  87. // Switch the buffer
  88. glFlush();
  89. glutSwapBuffers();
  90.  
  91.  
  92. }
  93.  
  94. void myShutdown() {
  95. glDeleteBuffers(1, &verticesVertexBufferObjectId);
  96. }
  97.  
  98. int main(int argc, char **argv) {
  99. // Initialization of GLUT
  100. glutInit(&argc, argv);
  101. glutInitContextVersion(3, 1);
  102. glutInitContextFlags(GLUT_DEBUG);
  103. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  104. glutInitWindowSize(512, 512);
  105. glutCreateWindow( __FILE__ );
  106.  
  107. // Init GLEW
  108. GLenum err = glewInit();
  109. if (GLEW_OK != err) {
  110. std::cerr << "Error: " << glewGetErrorString(err) << std::endl;
  111. return 1;
  112. }
  113.  
  114. if(!GLEW_VERSION_3_1) {
  115. std::cerr << "Driver does not support OpenGL 3.1" << std::endl;
  116. return 1;
  117. }
  118.  
  119. // Attach handlers
  120. glutDisplayFunc(myDisplay);
  121. glutReshapeFunc(myReshape);
  122.  
  123. // A nasty trick to get a shutdown handler
  124. atexit(myShutdown);
  125.  
  126. // Application specific initialization
  127. myInit();
  128.  
  129. // Run the GLUT main loop
  130. glutMainLoop();
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement