Advertisement
TalesM

Z-Buffer Quebrado parte 2

Jan 5th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include "MainGameSteps.h"
  2. #include "SDL.h"
  3. #include "SDL_opengl.h"
  4. #include "GameWindow.h"
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstdlib>
  8. using namespace std;
  9.  
  10.  
  11. MainGameSteps::MainGameSteps() : exit(false) {}
  12.  
  13. void MainGameSteps::setup()
  14. {
  15.     degreesToRotateX = degreesToRotateY = degreesToRotateZ = 0;
  16.     camX = camY = 0;
  17.     camZ = 5;
  18.     glEnable(GL_BLEND);
  19.     glEnable(GL_CULL_FACE);
  20.     glEnable(GL_DEPTH_TEST);
  21.     glEnable(GL_POLYGON_SMOOTH);
  22. //  glDepthFunc(GL_ALWAYS);
  23.     glDepthRange(100,-100);
  24.     glCullFace(GL_BACK);
  25.     if(!glIsEnabled(GL_DEPTH_TEST))
  26.         std::exit(-1);
  27. }
  28.  
  29. void MainGameSteps::processEvents(const SDL_Event& event)
  30. {
  31.     switch (event.type)
  32.     {
  33.     case SDL_QUIT:
  34.         exit = true;
  35.         break;
  36.     case SDL_KEYDOWN:
  37.         switch (event.key.keysym.sym)
  38.         {
  39.         case SDLK_F4:
  40.             if (event.key.keysym.sym == SDLK_F4
  41.                     && (event.key.keysym.mod & KMOD_ALT))
  42.                 exit = true;
  43.             break;
  44.         case SDLK_SPACE:
  45.             degreesToRotateX = degreesToRotateY = degreesToRotateZ = 0;
  46.             break;
  47.         case SDLK_1:
  48.             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  49.             break;
  50.         case SDLK_2:
  51.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  52.             break;
  53.         case SDLK_3:
  54.             glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  55.             break;
  56.         default:
  57.             break;
  58.         }
  59.         break;
  60.     }
  61. }
  62.  
  63. void MainGameSteps::processLogics()
  64. {
  65.     //Distância para girar (em graus) =
  66.     //velocidade (0.180f) * tempo (ticks)
  67.     float distance = 0.180f * GAMEWINDOW.getTicks();
  68.     degreesToRotateY += distance;
  69. }
  70.  
  71. #define CUBE_SIZE_2 .5
  72.  
  73. static void drawCube()
  74. {
  75.     glBegin(GL_TRIANGLES);
  76.         //Frente
  77.         glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
  78.         glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
  79.         glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
  80.         glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
  81.         glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
  82.         glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
  83.  
  84.         //Verso
  85.         glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
  86.         glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
  87.         glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
  88.         glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
  89.         glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
  90.         glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
  91.  
  92. //          //Direita
  93.         glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
  94.         glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
  95.         glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
  96.         glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
  97.         glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
  98.         glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
  99.  
  100. //          //Esquerda
  101.         glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
  102.         glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
  103.         glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
  104.         glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
  105.         glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
  106.         glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
  107.  
  108. //          //Baixo
  109.         glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
  110.         glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
  111.         glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
  112.         glVertex3f(CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
  113.         glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, CUBE_SIZE_2);
  114.         glVertex3f(-CUBE_SIZE_2, -CUBE_SIZE_2, -CUBE_SIZE_2);
  115.  
  116. //          //Abaixo
  117.         glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
  118.         glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
  119.         glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
  120.         glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, -CUBE_SIZE_2);
  121.         glVertex3f(-CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
  122.         glVertex3f(CUBE_SIZE_2, CUBE_SIZE_2, CUBE_SIZE_2);
  123.  
  124.     glEnd();
  125. }
  126.  
  127. void MainGameSteps::draw() const
  128. {
  129.     glMatrixMode(GL_PROJECTION);
  130.        glLoadIdentity();  //Resetamos a projeção atual
  131.        gluPerspective(60, GAMEWINDOW.getRatio(), 1, 15000);
  132.     glMatrixMode(GL_MODELVIEW);
  133.     glPushMatrix();
  134.         //Limpa a tela
  135.         gluLookAt(camX, camY, camZ, 0, 0, 0, 0, 1, 0);
  136.         glClearColor(0.0, 0.0, 0.0, 0.0);
  137. //        glClearDepth(1.0);
  138.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  139. //        glTranslatef(0,0, -250);
  140.  
  141. //        //Gira o triângulo
  142. //        glRotatef(degreesToRotateZ, 0,0,1);
  143. //        glRotatef(degreesToRotateX, 1,0,0);
  144. //        glRotatef(degreesToRotateY, 0,1,0);
  145.  
  146.         //Sun
  147.         glColor3f(1, 1, .8f);
  148.         drawCube();
  149.         glPushMatrix();
  150.             //Planet
  151.             glRotatef(degreesToRotateY, 0,1,0);
  152.             glColor3f(0, .2, .8f);
  153.             glTranslatef(0, 0, -4);
  154.             glScalef(.4f, .4f, .4f);
  155.             glRotatef(degreesToRotateY*2, 0,1,0);
  156.             drawCube();
  157.         glPopMatrix();
  158.     glPopMatrix();
  159. }
  160.  
  161. bool MainGameSteps::ended()
  162. {
  163.     return exit;
  164. }
  165.  
  166. void MainGameSteps::teardown() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement