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 <stdio.h>
- #include <SOIL.h>
- #define DEGREES_PER_PIXEL 0.6f
- #define ZOOM_FACTOR 0.04f
- #define UNITS_PER_PIXEL 0.1f
- typedef struct {
- bool leftButton;
- bool rightButton;
- int x;
- int y;
- } MouseState;
- MouseState mouseState = { false, false, 0, 0 };
- GLuint pista_tex; GLuint fire_tex;
- GLfloat zoom = 90;
- GLfloat eye[] = { 0, 0, 20 };
- GLfloat at[] = { 0, 0, 0 };
- GLUquadric* pista; GLUquadric* roda; GLUquadric* fire;
- float angle = 0;
- float rotacao_nave = 10;
- float xRotate = 0, yRotate = 0;
- float movPista1 = 0;
- bool movimentando = false;
- bool gameOverBool = false;
- int profundidade_eixo = -20;
- float moveCarro = 0;
- int pontos = 0;
- int borgSize = 1;
- float randX = 0;
- float borgSpeed = 0;
- float moveYBorg = 0;
- float roadSpeed = 0;
- bool colisao = false;
- static GLuint LoadItens(char* filename ){
- GLuint tex;
- tex = SOIL_load_OGL_texture(filename, SOIL_LOAD_AUTO,
- SOIL_CREATE_NEW_ID,
- SOIL_FLAG_INVERT_Y );
- if( 0 == tex )
- {
- printf( "erro do SOIL: '%s'\n", SOIL_last_result() );
- }
- // seta os parametros para a textura
- glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_DECAL);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- return tex;
- }
- void MouseMove(int x, int y) {
- /* Calculate the displacement in movement */
- int xDelta = mouseState.x - x;
- int yDelta = mouseState.y - y;
- /* Commit the mouse position */
- mouseState.x = x;
- mouseState.y = y;
- /* If left button is down, rotate when mouse is moved */
- if (mouseState.leftButton) {
- xRotate -= xDelta * DEGREES_PER_PIXEL;
- yRotate -= yDelta * DEGREES_PER_PIXEL;
- }
- glutPostRedisplay();
- }
- void draw(void)
- {
- //printf("%f\n", zoom);
- glClearColor(0, 0, 0, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(eye[0], eye[1], eye[2], at[0], at[1], at[2], 0, 1, 0);
- /* Rotate the scene in the x and y directions */
- glRotatef(xRotate, 0, 1, 0);
- glRotatef(yRotate, 1, 0, 0);
- //carro
- glPushMatrix();
- //glShadeModel(GL_SMOOTH);
- glEnable(GL_COLOR_MATERIAL);
- //glClearColor(0, 0, 15, 1);
- //glColor3ub(255, 200, 0);
- glColor3f(0.7,0.7,0.7);
- glTranslatef(moveCarro,-13,1.7);
- glScaled(0.8,1.5,0.4);
- glutSolidCube(3);
- //glDisable(GL_COLOR_MATERIAL);
- glPopMatrix();
- /*
- // parte de cima do carro
- glPushMatrix();
- glColor3f(0,0,1);
- glTranslatef(moveCarro,-13,2.5);
- glScaled(0.8,1,0.4);
- glutSolidCube(2);
- glPopMatrix();
- // cilindro
- glPushMatrix();
- glColor3f(0,0,1);
- glTranslatef(moveCarro,-13,2.5);
- glScaled(0.8,1,0.4);
- gluCylinder(pista, 10, 10, 10, 20, 1);
- glPopMatrix();
- */
- glColor3f(1,1,1);
- glPushMatrix();
- glTranslatef(-10, movPista1+23,profundidade_eixo-21);
- glRotatef(90, 0, 1, 0);
- glRotatef(45, 0,0, 1);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, pista_tex);
- gluQuadricDrawStyle(pista, GLU_FILL);
- glBindTexture(GL_TEXTURE_2D, pista_tex);
- gluQuadricTexture(pista, GL_TRUE);
- gluQuadricNormals(pista, GLU_SMOOTH);
- gluCylinder(pista, 60, 60, 20, 4, 1);
- glDisable(GL_TEXTURE_2D);
- glPopMatrix();
- /*
- //pista2
- glPushMatrix();
- glTranslatef(-10, movPista1+42.5,profundidade_eixo);
- glRotatef(90, 0, 1, 0);
- glRotatef(45, 0,0, 1);
- //glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, pista_tex);
- gluQuadricDrawStyle(pista, GLU_FILL);
- glBindTexture(GL_TEXTURE_2D, pista_tex);
- gluQuadricTexture(pista, GL_TRUE);
- gluQuadricNormals(pista, GLU_SMOOTH);
- gluCylinder(pista, 30, 30, 20, 4, 1);
- glDisable(GL_TEXTURE_2D);
- glPopMatrix();
- */
- // carro inimigo?
- glPushMatrix();
- glTranslatef(randX, moveYBorg, 1.7);
- //glShadeModel(GL_SMOOTH);
- glEnable(GL_COLOR_MATERIAL);
- //glClearColor(0, 0, 15, 1);
- //glColor3ub(255, 200, 0);
- glColor3f(0.7,0.7,0.7);
- //glTranslatef(moveCarro,-13,1.7);
- glScaled(0.8,1.5,0.4);
- glutSolidCube(3);
- //glDisable(GL_COLOR_MATERIAL);
- //glRotatef(angle, 0, 1, 0);
- //glRotatef(angle*2, 0,0, 1);
- glPopMatrix();
- if (gameOverBool) {
- // explosao1
- glPushMatrix();
- glTranslatef(moveCarro, -12, 1.7);
- glRotatef(-angle*10, 1, 0, 0);
- glRotatef(-angle*2, 0, 1, 0);
- glRotatef(-angle*7, 0, 0, 1);
- //glRotatef(45, 0,0, 1);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, fire_tex);
- gluQuadricDrawStyle(fire, GLU_FILL);
- glBindTexture(GL_TEXTURE_2D, fire_tex);
- gluQuadricTexture(fire, GL_TRUE);
- gluQuadricNormals(fire, GLU_SMOOTH);
- gluSphere(fire, 3, 32, 16);
- glDisable(GL_TEXTURE_2D);
- glPopMatrix();
- }
- glutSwapBuffers();
- }
- void rotation(int value){
- if (!gameOverBool) {
- movPista1-= 1;
- borgSpeed-= (1+roadSpeed);
- moveYBorg = borgSpeed + 20;
- if (movPista1<=-43){
- movPista1=0;
- }
- if (borgSpeed<=-43) {
- borgSpeed = 15;
- randX = rand() % 18 + (-8);
- if (roadSpeed < 3) {
- roadSpeed+=0.05;
- }
- }
- if (moveYBorg <= -11 && moveYBorg >= -12) {
- if (!((randX >= moveCarro+2.5) || (randX <= moveCarro-2.5))) {
- printf("colidiu\n");
- gameOverBool = true;
- }
- }
- }
- angle += 5;
- glutPostRedisplay();
- glutTimerFunc(50, rotation, 1);
- }
- void resize(int w, int h)
- {
- if (!h)
- h = 1;
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(zoom, 1.0*w/h, 0.1, 100.0);
- }
- void init(void)
- {
- /* Initialize the camera position */
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(eye[0], eye[1], eye[2], at[0], at[1], at[2], 0, 1, 0);
- glEnable(GL_DEPTH_TEST);
- //make_tex();
- pista = gluNewQuadric();
- roda = gluNewQuadric();
- fire = gluNewQuadric();
- glEnable(GL_TEXTURE_2D);
- pista_tex = LoadItens("D:\\street.jpg");
- fire_tex = LoadItens("D:\\fire.jpg");
- }
- void mouse(int button, int state, int x, int y)
- {
- if ((button == 3) || (button == 4))
- {
- // 3 pra cima, 4 pra baixo
- if (button == 3){
- eye[0] = (1 - ZOOM_FACTOR) * eye[0] + at[0] * ZOOM_FACTOR;
- eye[1] = (1 - ZOOM_FACTOR) * eye[1] + at[1] * ZOOM_FACTOR;
- eye[2] = (1 - ZOOM_FACTOR) * eye[2] + at[2] * ZOOM_FACTOR;
- }
- if (button == 4){
- eye[0] = (1 + ZOOM_FACTOR) * eye[0] - at[0] * ZOOM_FACTOR;
- eye[1] = (1 + ZOOM_FACTOR) * eye[1] - at[1] * ZOOM_FACTOR;
- eye[2] = (1 + ZOOM_FACTOR) * eye[2] - at[2] * ZOOM_FACTOR;
- }
- if (state == GLUT_UP) return; // S pra evitar GLUT_UP redundante pq a roda do mouse considerada botao
- }else{ // eventos normais button
- //printf("Botao %s em %d %d\n", (state == GLUT_DOWN) ? "Clicado" : "Largado", x, y);
- }
- if (button == GLUT_LEFT_BUTTON) {
- if (state == GLUT_DOWN)
- mouseState.leftButton = true;
- else
- mouseState.leftButton = false;
- }
- if (button == GLUT_RIGHT_BUTTON) {
- if (state == GLUT_DOWN)
- mouseState.rightButton = true;
- else
- mouseState.rightButton = false;
- }
- // update the mouse position, so we can track the mouse move
- mouseState.x = x;
- mouseState.y = y;
- }
- void ControlaTeclado(unsigned char key, int x, int y){
- switch(key){
- //DIMINUI A VELOCIDADE
- case ',':
- eye[1] = eye[1] - UNITS_PER_PIXEL;
- at[1] = at[1] - UNITS_PER_PIXEL;
- break;
- case 'e':
- eye[1] = eye[1] + UNITS_PER_PIXEL;
- at[1] = at[1] + UNITS_PER_PIXEL;
- break;
- case 'i':
- eye[0] = eye[0] + UNITS_PER_PIXEL;
- at[0] = at[0] + UNITS_PER_PIXEL;
- break;
- case 'a':
- eye[0] = eye[0] - UNITS_PER_PIXEL;
- at[0] = at[0] - UNITS_PER_PIXEL;
- case 'A':
- //velocidade += 1;
- //printf("%f\n", velocidade);
- break;
- //AUMENTA A VELOCIDADE
- case 'D':
- //velocidade -= 1;
- //printf("%f\n", velocidade);
- break;
- case ' ':
- //bomb = true;
- //boom = false;
- //fire = false;
- //posxbomb = posX;
- //posybomb = posY;
- break;
- }
- glutPostRedisplay();
- }
- void SpecialKey(int key, int x, int y) {
- switch (key) {
- case GLUT_KEY_LEFT:
- if(moveCarro>=-8.0 && !gameOverBool){
- moveCarro--;
- colisao = true;
- }
- break;
- case GLUT_KEY_RIGHT:
- if(moveCarro<=8.0 && !gameOverBool){
- moveCarro++;
- colisao = true;
- };
- break;
- case GLUT_KEY_UP:
- break;
- case GLUT_KEY_DOWN:
- break;
- case GLUT_KEY_END: /* zoom out */
- //eye[0] = (1 + ZOOM_FACTOR) * eye[0] - at[0] * ZOOM_FACTOR;
- //eye[1] = (1 + ZOOM_FACTOR) * eye[1] - at[1] * ZOOM_FACTOR;
- //eye[2] = (1 + ZOOM_FACTOR) * eye[2] - at[2] * ZOOM_FACTOR;
- break;
- case GLUT_KEY_HOME: /* zoom in */
- //eye[0] = (1 - ZOOM_FACTOR) * eye[0] + at[0] * ZOOM_FACTOR;
- //eye[1] = (1 - ZOOM_FACTOR) * eye[1] + at[1] * ZOOM_FACTOR;
- //eye[2] = (1 - ZOOM_FACTOR) * eye[2] + at[2] * ZOOM_FACTOR;
- break;
- }
- glutPostRedisplay();
- }
- void SpecialKeyUp(int key, int x, int y) {
- switch (key) {
- case GLUT_KEY_LEFT:
- //movimentando = false;
- break;
- case GLUT_KEY_RIGHT:
- //movimentando = false;
- break;
- case GLUT_KEY_UP:
- movimentando = false;
- break;
- case GLUT_KEY_DOWN:
- movimentando = false;
- break;
- }
- glutPostRedisplay();
- }
- void VerificaRotacaoNave(int teste){
- if (movimentando == false){
- if (rotacao_nave > 10){
- while (rotacao_nave > 10){
- rotacao_nave+=1;
- }
- }else{
- while (rotacao_nave < 10){
- rotacao_nave-=1;
- }
- }
- }
- glutPostRedisplay();
- glutTimerFunc(10, VerificaRotacaoNave, 1);
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutInitWindowSize(640, 512);
- glutCreateWindow("Test");
- glutDisplayFunc(draw);
- glutReshapeFunc(resize);
- glutTimerFunc(50, rotation, 1);
- glutMouseFunc(mouse);
- glutMotionFunc(MouseMove);
- glutKeyboardFunc(ControlaTeclado);
- glutSpecialFunc(SpecialKey);
- glutSpecialUpFunc(SpecialKeyUp);
- glutTimerFunc(10, VerificaRotacaoNave, 1);
- init();
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement