Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <cstdlib>
- int VELOCIDADE_MAX = 40;
- int a = 0 ;
- float k = -1.0;
- /** mota variaveis **/
- int d = 0;
- float b = -1 ;
- int c = 0 ;
- int bolaavir = 3;
- float x = 0,y= 0,z=-1 ;
- int trans = 3.0 ;
- int flag =1;
- int vetor[4];
- float pos[4];
- /* Create checkerboard texture */
- #define checkImageWidth 64
- #define checkImageHeight 64
- static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
- static GLuint texName;
- float mov1, mov2;
- float t1=0, t2=-3.2, t3=-6.4, t4=-9.6, t5=-12.8, t6=-16, t7 = -19.4;
- float distancia_objecto_1 = -18.0; // variavel usada por 5 objetos!
- float velocidade=400;
- void gerar()
- {
- pos[0]= -30 ;
- pos[1]= -30 ;
- pos[2]= -30;
- pos[3]= -30 ;
- pos[4]= -30 ;
- if(flag==0)
- {
- pos[0]= -2 ;
- pos[1]= -1.5 ;
- pos[2]= -1;
- pos[3]= -0.5 ;
- pos[4]= 0 ;
- vetor[0]= 0 ;
- vetor[1]= 0 ;
- vetor[2]= 0;
- vetor[3]= 0 ;
- vetor[4]= 0 ;
- }
- else{
- vetor[0]= rand() %5 + 1;
- if(vetor[0]==1)
- pos[0]= -2.0;
- else if (vetor[0]==2)
- pos[0]= -1.5;
- else if (vetor[0]==3)
- pos[0]= -1.0;
- else if (vetor[0]==4)
- pos[0]= -0.5;
- else if (vetor[0]==5)
- pos[0]= 0.0;
- vetor[1]= rand() %5 + 1;
- if(vetor[0]!= vetor[1])
- {
- if(vetor[1]==1)
- pos[1]= -2.0;
- else if (vetor[1]==2)
- pos[1]= -1.5;
- else if (vetor[1]==3)
- pos[1]= -1.0;
- else if (vetor[1]==4)
- pos[1]= -0.5;
- else if (vetor[1]==5)
- pos[1]= 0.0;
- }
- vetor[2]= rand() %5 + 1;
- if(vetor[0]!= vetor[2] && vetor[1]!= vetor[2])
- {
- if(vetor[2]==1)
- pos[2]= -2.0;
- else if (vetor[2]==2)
- pos[2]= -1.5;
- else if (vetor[2]==3)
- pos[2]= -1.0;
- else if (vetor[2]==4)
- pos[2]= -0.5;
- else if (vetor[2]==5)
- pos[2]= 0.0;
- }
- vetor[3]= rand() %5 + 1;
- if(vetor[0]!= vetor[3] && vetor[1]!= vetor[3] && vetor[2]!= vetor[3])
- {
- if(vetor[3]==1)
- pos[3]= -2.0;
- else if (vetor[3]==2)
- pos[3]= -1.5;
- else if (vetor[3]==3)
- pos[3]= -1.0;
- else if (vetor[3]==4)
- pos[3]= -0.5;
- else if (vetor[3]==5)
- pos[3]= 0.0;
- }
- printf("pos1 = %d\n pos2 %d\n pos3 %d\n pos4 %d\n",pos[0],pos[1],pos[2],pos[3]);
- }
- }
- void teclado (int key, int x, int y){
- if(key == GLUT_KEY_RIGHT && (b<0))
- b=b+0.5;
- if(key == GLUT_KEY_LEFT && (b >-2))
- b=b-0.5;
- glutPostRedisplay();
- }
- void desenha_padroes (int a){
- glPushMatrix();
- glRotatef(90, 1 ,0 ,0);
- glTranslatef(0, a, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- }
- void movimento_do_chao (int value){
- velocidade=velocidade-10;
- if(velocidade<50)
- velocidade=VELOCIDADE_MAX;
- t1 = t1 + 0.2;
- t2 = t2 + 0.2;
- t3 = t3 + 0.2;
- t4 = t4 + 0.2;
- t5 = t5 + 0.2;
- t6 = t6 + 0.2;
- t7 = t7 + 0.2;
- distancia_objecto_1 = distancia_objecto_1 + 0.2;
- if(t1>=4.0)
- t1=-18.6;
- if(t2>=4.0)
- t2=-18.6;
- if(t3>=4.0)
- t3=-18.6;
- if(t4>=4.0)
- t4=-18.6;
- if(t5>=4.0)
- t5=-18.6;
- if(t6>=4.0)
- t6=-18.6;
- if(t7>=4.0)
- t7=-18.6;
- if(distancia_objecto_1>4.0){
- distancia_objecto_1=-18.0;
- gerar();
- flag++;
- }
- glutPostRedisplay();
- glutTimerFunc(velocidade, movimento_do_chao, 1);
- }
- void desenho() // desenho das bolas
- {
- /** JOGADOR **/
- glPushMatrix();
- glTranslated(b, -0.34, 2.62);// glTranslated(-1,-0.34, 2.62);
- glRotated(1,1,1,1);
- glutSolidSphere (0.12, 20, 10);
- glPopMatrix();
- /** FIM DO JOGADOR **/
- glPushMatrix();
- glTranslated(pos[0], -0.34, distancia_objecto_1);
- glRotated(1,1,1,1);
- glutSolidSphere (0.18, 20, 10);
- glPopMatrix();
- /** um cone FEIO **/
- glPushMatrix();
- glTranslated(pos[1], -0.4 , distancia_objecto_1);
- glRotated(90,-10,1,1);
- glutSolidCone(0.2 , 0.2 ,20 , 10);
- glPopMatrix();
- /** um cubo **/
- glPushMatrix();
- glTranslated(pos[2], -0.34, distancia_objecto_1);
- glutSolidCube(0.2);
- glPopMatrix();
- /** Donut **/
- glPushMatrix();
- glTranslated(pos[3], -0.33, distancia_objecto_1);
- glutSolidTorus(0.08 , 0.17, 4 , 20);
- glPopMatrix();
- }
- void makeCheckImage(void)
- {
- int i, j, c;
- for (i = 0; i < checkImageHeight; i++) {
- for (j = 0; j < checkImageWidth; j++) {
- c = ((((i&0x8)==0)^((j&0x8))==0))*255;
- checkImage[i][j][0] = (GLubyte) c;
- checkImage[i][j][1] = (GLubyte) c;
- checkImage[i][j][2] = (GLubyte) c;
- checkImage[i][j][3] = (GLubyte) 255;
- }
- }
- }
- void init(void)
- {
- GLfloat mat_amb[] = { 0.1, 1, 0.8, 1.0 };
- GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat mat_shininess[] = { 100.0 };
- GLfloat light_position[] = {0.0, -0.1, -5.0, 0.0 };
- glClearColor (0.0, 0.0, 0.0, 0.0);
- glShadeModel(GL_SMOOTH);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT,
- mat_amb);
- makeCheckImage();
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glGenTextures(1, &texName);
- glBindTexture(GL_TEXTURE_2D, texName);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
- GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
- GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
- checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
- checkImage);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_DEPTH_TEST);
- }
- void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glEnable(GL_TEXTURE_2D);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
- glBindTexture(GL_TEXTURE_2D, texName);
- /** chao 1 **/
- glPushMatrix();
- glRotatef(90, 1 ,0 ,0);
- glTranslatef(0, t1, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** parede 1 direita **/
- glPushMatrix();
- glRotatef(90, 0 ,1 ,0);
- glTranslatef(-t1, 1, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** parede 1 esquerda **/
- glPushMatrix();
- glRotatef(90, 0 , -1 ,0);
- glTranslatef(t1, 0.77, 2.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** chao 2 **/
- glPushMatrix();
- glRotatef(90, 1 ,0 ,0);
- glTranslatef(0, t2, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- // desenha_padroes(t2);
- /** parede 2 direita **/
- glPushMatrix();
- glRotatef(90, 0 ,1 ,0);
- glTranslatef(-t2, 1, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** parede 2 esquerda **/
- glPushMatrix();
- glRotatef(90, 0 , -1 ,0);
- glTranslatef(t2, 0.77, 2.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** chao 3 **/
- glPushMatrix();
- glRotatef(90, 1 ,0 ,0);
- glTranslatef(0, t3, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- // desenha_padroes(t3);
- /** parede 3 direita **/
- glPushMatrix();
- glRotatef(90, 0 ,1 ,0);
- glTranslatef(-t3, 1, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** parede 3 esquerda **/
- glPushMatrix();
- glRotatef(90, 0 , -1 ,0);
- glTranslatef(t3, 0.77, 2.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** chao 4 **/
- glPushMatrix();
- glRotatef(90, 1 ,0 ,0);
- glTranslatef(0, t4, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- // desenha_padroes(t4);
- /** parede 4 direita **/
- glPushMatrix();
- glRotatef(90, 0 ,1 ,0);
- glTranslatef(-t4, 1, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** parede 4 esquerda **/
- glPushMatrix();
- glRotatef(90, 0 , -1 ,0);
- glTranslatef(t4, 0.77, 2.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** chao 5 **/
- glPushMatrix();
- glRotatef(90, 1 ,0 ,0);
- glTranslatef(0, t5, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** parede 5 direita **/
- glPushMatrix();
- glRotatef(90, 0 ,1 ,0);
- glTranslatef(-t5, 1, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** parede 5 esquerda **/
- glPushMatrix();
- glRotatef(90, 0 , -1 ,0);
- glTranslatef(t5, 0.77, 2.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** chao 6 **/
- glPushMatrix();
- glRotatef(90, 1 ,0 ,0);
- glTranslatef(0, t6, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** parede 6 direita **/
- glPushMatrix();
- glRotatef(90, 0 ,1 ,0);
- glTranslatef(-t6, 1, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** parede 6 esquerda **/
- glPushMatrix();
- glRotatef(90, 0 , -1 ,0);
- glTranslatef(t6, 0.77, 2.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** chao 7 **/
- glPushMatrix();
- glRotatef(90, 1 ,0 ,0);
- glTranslatef(0, t7, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** parede 7 direita **/
- glPushMatrix();
- glRotatef(90, 0 ,1 ,0);
- glTranslatef(-t7, 1, 0);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** parede 7 esquerda **/
- glPushMatrix();
- glRotatef(90, 0 , -1 ,0);
- glTranslatef(t7, 0.77, 2.6);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0, 0.0); glVertex3f(-2.6, -1.6, 0.6);
- glTexCoord2f(0.0, 1.0); glVertex3f(-2.6, 1.6, 0.6);
- glTexCoord2f(1.0, 1.0); glVertex3f(0.6, 1.6, 0.6);
- glTexCoord2f(1.0, 0.0); glVertex3f(0.6, -1.6, 0.6);
- glEnd();
- glPopMatrix();
- /** fim dos desenhos **/
- desenho();
- glFlush();
- glDisable(GL_TEXTURE_2D);
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(1.0, 0.0, -3.6);
- }
- int main(int argc, char** argv)
- {
- pos[0]= -2 ;
- pos[1]= -1.5 ;
- pos[2]= -1;
- pos[3]= -0.5 ;
- pos[4]= 0 ;
- flag++;
- vetor[0]= 0 ;
- vetor[1]= 0 ;
- vetor[2]= 0;
- vetor[3]= 0 ;
- vetor[4]= 0 ;
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(600, 480);
- glutInitWindowPosition(150, 150);
- glutCreateWindow(argv[0]);
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutTimerFunc(1, movimento_do_chao, 1);
- glutSpecialFunc(teclado);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement