Advertisement
James2250

FFP/GLSL

Feb 3rd, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. unsigned int vbo;
  2. vector<GLfloat> Vertices;
  3.  
  4. void CreateTerrain()
  5. {
  6.     const int TILE_SIZE = 16;
  7.     const int MAP_WIDTH = 200;
  8.     const int MAP_HEIGHT = 200;
  9.  
  10.     for(int i = 0; i < MAP_WIDTH; i++)
  11.     {
  12.         float Per = (Perlin(1000 + (i / 50.0))*3000 + 150);    // Random Noise function (in testing)  
  13.         Per -= fmod(Per, TILE_SIZE);
  14.  
  15.         for(int j = 0; j < MAP_HEIGHT; j ++)
  16.         {
  17.             Vertices.push_back(i * TILE_SIZE);              Vertices.push_back(j * TILE_SIZE);
  18.             Vertices.push_back(i * TILE_SIZE + TILE_SIZE);  Vertices.push_back(j * TILE_SIZE);
  19.             Vertices.push_back(i * TILE_SIZE + TILE_SIZE);  Vertices.push_back(j  * TILE_SIZE + TILE_SIZE);
  20.             Vertices.push_back(i * TILE_SIZE);              Vertices.push_back(j  * TILE_SIZE + TILE_SIZE);
  21.         }
  22.     }
  23.  
  24.     glGenBuffers(1, &vbo);
  25.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  26.     glBufferData(GL_ARRAY_BUFFER, Vertices.size() * sizeof(GLfloat), &Vertices[0], GL_STATIC_DRAW);
  27. }
  28.  
  29. void Draw()
  30. {
  31.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  32.     glEnableClientState(GL_VERTEX_ARRAY );
  33.     glVertexPointer(2, GL_FLOAT, 0, 0);
  34.     glDrawArrays(GL_QUADS, 0, Vertices.size() / 2);
  35.  
  36.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  37.  
  38.     glDisableClientState(GL_VERTEX_ARRAY );
  39. }
  40.  
  41. void LoadShaders()
  42. {
  43.     Program program;
  44.  
  45.     Shader vs;  
  46.     Shader fs;
  47.  
  48.     vs.LoadFile("Vertex.vs");
  49.     fs.LoadFile("Fragment.fs");
  50.  
  51.     vs.LoadShader(GL_VERTEX_SHADER);
  52.     fs.LoadShader(GL_FRAGMENT_SHADER);
  53.  
  54.     program.CreateProgram(vs.getShader(), fs.getShader());
  55.  
  56. }
  57.  
  58. void CameraMovement()
  59. {
  60.     if(RightDown)
  61.     {
  62.         CameraX += 4;
  63.     }
  64.  
  65.     else if(LeftDown)
  66.     {
  67.         CameraX -= 4;
  68.     }
  69.  
  70.     else if(UpPressed)
  71.     {
  72.         CameraY -= 4;
  73.     }
  74.  
  75.     else if(DownPressed)
  76.     {
  77.         CameraY += 4;
  78.     }
  79.  
  80. }
  81.  
  82. int main(int argc, char *argv[])
  83. {
  84.     Init();
  85.  
  86.     int Frames = 0;
  87.     int Timer = SDL_GetTicks();
  88.  
  89.     CreateTerrain();
  90.     LoadShaders();   // No Errors returned here
  91.  
  92.     while(Running)
  93.     {
  94.         glClear(GL_COLOR_BUFFER_BIT);
  95.         CameraMovement();
  96.  
  97.         glTranslatef(-CameraX, -CameraY, 0);   // Just for testing movement, isn't causing major fps drop
  98.         glColor3f(1,0,0);
  99.         Draw();
  100.         glTranslatef(CameraX,CameraY, 0);
  101.  
  102.         EventLoop();
  103.  
  104.         if(SDL_GetTicks() - Timer > 1000)
  105.         {
  106.             cout << "FPS " << Frames << endl;
  107.             Timer = SDL_GetTicks();
  108.             Frames = 0;
  109.         }
  110.  
  111.         Frames++;
  112.         SDL_GL_SwapWindow(window);
  113.     }
  114.  
  115.     Quit();
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement