Advertisement
thecplusplusguy

OpenGL tutorial 23 - VBO 1

Jul 30th, 2012
1,843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. #include <iostream>
  3. #include <SDL/SDL.h>
  4. #include <SDL/SDL_image.h>
  5. #include "GLee.h"   //copy GLee.h and GLee.c into your project
  6. #include <GL/gl.h>
  7. #include <GL/glu.h>
  8. #include "camera.h" //this doesn't changed at all
  9. #include <vector>
  10.  
  11. camera cam;
  12.  
  13. /* //this is for mip-mapping, it's not important here
  14. unsigned int loadTexture(const char* filename,bool generate=false)
  15. {
  16.     unsigned int num;
  17.     glGenTextures(1,&num);
  18.     SDL_Surface* img=IMG_Load(filename);
  19.     if(img==NULL)
  20.     {
  21.         std::cout << "img was not loaded" << std::endl;
  22.         return -1;
  23.     }
  24.     SDL_PixelFormat form={NULL,32,4,0,0,0,0,0,0,0,0,0xff000000,0x00ff0000,0x0000ff00,0x000000ff,0,255};
  25.     SDL_Surface* img2=SDL_ConvertSurface(img,&form,SDL_SWSURFACE);
  26.     if(img2==NULL)
  27.     {
  28.         std::cout << "img2 was not loaded" << std::endl;
  29.         return -1;     
  30.     }
  31.     glBindTexture(GL_TEXTURE_2D,num);      
  32.    
  33.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  34.    
  35.     glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,img2->w,img2->h,0,GL_RGBA,GL_UNSIGNED_INT_8_8_8_8,img2->pixels);
  36.     if(generate)
  37.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  38.     else
  39.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  40.        
  41.     if(generate)
  42.         glGenerateMipmap(GL_TEXTURE_2D);
  43.        
  44.     SDL_FreeSurface(img);
  45.     SDL_FreeSurface(img2);
  46.     return num;
  47. }*/
  48.  
  49. unsigned int vbo,ind;   //indicis
  50. float f[]={0,2,-4,1,0,0,-2,-2,-4,0,1,0,2,-2,-4,0,0,1};  //vertex array (3 component vertex, 3 component color)
  51. unsigned int indarr[]={0,1,2};  //index array
  52.  
  53. void init()
  54. {
  55.     glClearColor(0,0,0,1);
  56.     glMatrixMode(GL_PROJECTION);
  57.         glLoadIdentity();
  58.         gluPerspective(50,640.0/480.0,1,1000);
  59.     glMatrixMode(GL_MODELVIEW);
  60.     glEnable(GL_DEPTH_TEST);
  61.    
  62.     glGenBuffers(1,&vbo);   //generate an index for the vertexbuffer
  63.     glGenBuffers(1,&ind);   //and the indices
  64.     glBindBuffer(GL_ARRAY_BUFFER,vbo);  //use vbo as ARRAY_BUFFER
  65.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ind);  //use ind as ELEMENT_ARRAY_BUFFER (index-array)
  66.     glBufferData(GL_ARRAY_BUFFER,sizeof(f),f,GL_STATIC_DRAW);//fill up the array with vertex and color-data
  67.     glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indarr),indarr,GL_STATIC_DRAW);//this one with indicis
  68. }
  69.  
  70.  
  71. void display()
  72. {
  73.     glLoadIdentity();
  74.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  75.     cam.Control();
  76.     cam.UpdateCamera();
  77.  
  78.     glBindBuffer(GL_ARRAY_BUFFER,vbo);  //use this VBO
  79.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ind);  //and index
  80.     glEnableClientState(GL_VERTEX_ARRAY);   //we use vertex and
  81.     glEnableClientState(GL_COLOR_ARRAY);    //color data
  82.     //the vertex has 3 component (x,y,z),it has the type float, 3 float is the vertex + 3 float is the color/6*sizeof(float), and the first
  83.     //coordinate start at the begginning of the VBO
  84.     glVertexPointer(3,GL_FLOAT,6*sizeof(float),0); 
  85.     //same thing
  86.     glColorPointer(3,GL_FLOAT,6*sizeof(float),(void*)(3*sizeof(float)));
  87.     glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,0);       //draw it
  88.     glDisableClientState(GL_COLOR_ARRAY);   //disable everything
  89.     glDisableClientState(GL_VERTEX_ARRAY);
  90.     glBindBuffer(GL_ARRAY_BUFFER,0);   
  91.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
  92.    
  93. }
  94.  
  95. int main()
  96. {
  97.     SDL_Init(SDL_INIT_EVERYTHING);
  98.     SDL_SetVideoMode(640,480,32,SDL_OPENGL);
  99.     Uint32 start;
  100.     SDL_Event event;
  101.     bool running=true;
  102.     init();
  103.     bool b=false;
  104.     while(running)
  105.     {
  106.         start=SDL_GetTicks();
  107.         while(SDL_PollEvent(&event))
  108.         {
  109.             switch(event.type)
  110.             {
  111.                 case SDL_QUIT:
  112.                     running=false;
  113.                     break;
  114.                 case SDL_KEYDOWN:
  115.                     switch(event.key.keysym.sym)
  116.                     {
  117.                         case SDLK_ESCAPE:
  118.                             running=false;
  119.                             break;
  120.                     }
  121.                     break;
  122.                 case SDL_MOUSEBUTTONDOWN:
  123.                     cam.mouseIn(true);
  124.                     break;
  125.                    
  126.             }
  127.         }
  128.         display();
  129.         SDL_GL_SwapBuffers();
  130.         if(1000.0/30>SDL_GetTicks()-start)
  131.             SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  132.     }
  133.     glDeleteBuffers(1,&ind);    //delete it
  134.     glDeleteBuffers(1,&vbo);
  135.     SDL_Quit();
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement