Advertisement
Ramy1989

VBO

Nov 11th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <OpenGL/OpenGL.h>
  2. #include <GLUT/glut.h>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. const float width= 800, height=600;
  9. vector<GLfloat> data;
  10. GLuint buffer;
  11.  
  12. void init(void)
  13. {
  14.     glEnable(GL_DEPTH_TEST);
  15.     glViewport(0,0,width,height);
  16.     glMatrixMode(GL_PROJECTION);
  17.     glLoadIdentity();
  18.     gluPerspective(45.0, width/height , 1.0, 1000.0);
  19.     glMatrixMode(GL_MODELVIEW);
  20.     glLoadIdentity();
  21.     gluLookAt(0.0, 0.0, 100.0, width/2, height/2, 0.0, 0.0, 1.0, 0.0);
  22.    
  23.     data= vector<GLfloat> {
  24.         width/2+20,height/2+20,0.0,
  25.         width/2+20,height/2-20,0.0,
  26.         width/2-20, height/2-20,0.0,
  27.         width/2-20, height/2+20,0.0
  28.     };
  29.     glGenBuffers(1, &buffer);
  30.     glBindBuffer(GL_ARRAY_BUFFER, buffer);
  31.     glBufferData(GL_ARRAY_BUFFER, 12*sizeof(GLfloat), data.data(), GL_STATIC_DRAW);
  32.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  33. }
  34.  
  35. void display(void)
  36. {
  37.     glClearColor(1.0, 1.0, 1.0, 1.0);
  38.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  39.    
  40.     glColor3f(1, 0, 0);
  41.     glBindBuffer(GL_ARRAY_BUFFER, buffer);
  42.     glEnableClientState(GL_VERTEX_ARRAY);
  43.     glVertexPointer(3, GL_FLOAT, 0, data.data());
  44.     glDrawArrays(GL_QUADS, 0, 4);
  45.     glDisableClientState(GL_VERTEX_ARRAY);
  46.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  47.    
  48.     glutSwapBuffers();
  49. }
  50.  
  51. int main(int argc, char** argv)
  52. {
  53.     glutInit(&argc, argv);
  54.     glutInitWindowPosition(100, 100);
  55.     glutInitWindowSize(width,height);
  56.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
  57.     glutCreateWindow("Interaction");
  58.     glutDisplayFunc(display);
  59.     init();
  60.     glutMainLoop();
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement