Last active
August 29, 2015 14:03
-
-
Save mfilipelino/bf65ee406028e863e75a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <GL/glut.h> | |
#include <stdio.h> | |
#include <math.h> | |
// constantes | |
#define COORD_TEXTURA_PLANO 1.0 | |
#define COORD_TEXTURA_AVIAO 1.0 | |
#define COR_DO_PLANO 0.52,0.52,0.78,1.0 | |
#define TEXTURA_DO_PLANO "montanhas.rgb" | |
#define TEXTURA_DO_AVIAO "camuflagem.rgb" | |
#define PI 3.1415 | |
GLUquadricObj *params; | |
// janela | |
GLint WIDTH = 800; | |
GLint HEIGHT = 600; | |
// TG's | |
GLfloat escala; | |
GLfloat ang_x = 0.0; | |
GLfloat ang_y = 0.0; | |
GLfloat ang_z; | |
GLfloat anguloz = 0.0; | |
GLfloat translacao; | |
// plano | |
GLint plano_x = 105; | |
GLint plano_z = 105; | |
GLuint textura_plano; | |
GLuint textura_aviao; | |
int wire = 0; | |
GLUquadricObj *quadric; | |
typedef struct { | |
GLfloat x; | |
GLfloat y; | |
GLfloat z; | |
} Vertice; | |
GLfloat angle, fAspect; | |
GLfloat cat_ang = 0; | |
GLfloat dog_ang = 0; | |
int pespectiva = 1; | |
Vertice cat_ant; | |
Vertice dog_ant; | |
Vertice cat; | |
Vertice dog; | |
GLfloat perna_state[2][4][2]; | |
int cat_presente = 1; | |
int dog_presente = 0; | |
int cat_state = 0; | |
int dog_state = 0; | |
int perna_state_esq; | |
int perna_state_dir; | |
GLfloat frames_perna[4][3]; | |
int state_perna_esq = 0; | |
int state_perna_dir = 0; | |
void cat_inicializa(GLfloat x, GLfloat y, GLfloat z) { | |
cat_state = 0; | |
cat.x = x; | |
cat.y = y; | |
cat.z = z; | |
cat_presente = 1; | |
} | |
void desenhaArvore() { | |
glPushMatrix(); | |
glColor3f(0.54, 0.4, 0.3); | |
glTranslatef(0, -3.2, 0); | |
glScaled(2.0, 2.0, 2.0); | |
glRotatef(-90, 1, 0, 0); | |
gluCylinder(params, 0.2, 0.2, 2, 15, 2); | |
glTranslatef(0, 0, 2); | |
glColor3f(0.14, 0.42, 0.13); | |
gluCylinder(params, 0.8, 0.0, 2, 15, 2); | |
glPopMatrix(); | |
} | |
void dog_inicializa(GLfloat x, GLfloat y, GLfloat z) { | |
dog.x = x; | |
dog.y = y; | |
dog.z = z; | |
dog_presente = 1; | |
dog_state = 1; | |
cat_state = 1; | |
} | |
void inicializa_tgs() { | |
escala = 1.3; | |
ang_x = 0; | |
ang_y = 0; | |
anguloz = 0; | |
translacao = 0; | |
} | |
void desenhaCubo(GLdouble width, GLdouble height, GLdouble depth) { | |
glPushMatrix(); | |
glScalef(width, height, depth); | |
if( wire == 1) | |
glutWireCube(1.0); | |
else | |
glutSolidCube(1.0); | |
glPopMatrix(); | |
} | |
void desenhaCabecaDog() { | |
glPushMatrix(); | |
glTranslated(1.0, 0.0, 0.0); | |
glRotated(-90, 0.0, 1.0, 0.0); | |
glPushMatrix(); | |
glColor3f(0.3, 0.3, 0.3); | |
glTranslated(0.6, 1.0, 0.0); | |
glRotated(65, 1.0, 0.0, 0.0); | |
glTranslated(-0.6, 0.0, 0.0); | |
//pescoco | |
quadric = gluNewQuadric(); | |
gluCylinder(quadric, 0.2, 0.2, 1.0, 12, 3); | |
glPushMatrix(); | |
glTranslated(0.0, 0.0, -0.7); | |
//glPushMatrix(); | |
//cabeca | |
glColor3f(0.2, 0.2, 0.2); | |
glPushMatrix(); | |
//glRotated(90, 0.0, 1.0, 1.0); | |
if( wire == 1) | |
glutWireCube(1.5); | |
else | |
glutSolidCube(1.5); | |
glColor3f(0.5, 0.5, 0.5); | |
glTranslatef(0, -0.8, 0); | |
if( wire == 1) | |
glutSolidCube(0.6); | |
else | |
glutWireCube(0.6); | |
glPopMatrix(); | |
//glTranslatef(-0.53,0.40,0);XX | |
//orelha | |
glPushMatrix(); | |
glColor3f(1, 1, 1); | |
glTranslatef(-0.8, 0.3, -0.19); | |
if( wire == 1) | |
glutWireCube(0.3); | |
else | |
glutSolidCube(0.3); | |
glScalef(1.0, 1.0, 1.0); | |
glTranslatef(1.6, 0, 0); | |
if( wire == 1) | |
glutWireCube(0.3); | |
else | |
glutSolidCube(0.3); | |
glPopMatrix(); | |
glPopMatrix(); | |
glPopMatrix(); | |
glPopMatrix(); | |
} | |
void desenhaPerna_esq(GLfloat x, GLfloat y, GLfloat z) { | |
int pro = 1.0; | |
glPushMatrix(); | |
glTranslated(x, y, z); | |
glRotated(frames_perna[state_perna_esq][0], 0.0, 0.0, 1); | |
desenhaCubo(pro * 0.5, pro * 1.0, pro * 0.5); | |
glPushMatrix(); | |
glTranslated(0, pro * -0.5, 0); | |
glRotated(frames_perna[state_perna_esq][1], 0.0, 0.0, 1.0); | |
glTranslated(0, pro * 0.5, 0); | |
glTranslated(0, pro * -1.25, 0); | |
desenhaCubo(pro * 0.5, pro * 1.5, pro * 0.5); | |
glPushMatrix(); | |
glTranslated(0, pro * -0.5, 0); | |
glRotated(frames_perna[state_perna_esq][2], 0.0, 0.0, 1.0); | |
glTranslated(0, pro * 0.5, 0); | |
glTranslated(0, pro * -1.5, 0); | |
desenhaCubo(pro * 0.5, pro * 1.5, pro * 0.5); | |
glPopMatrix(); | |
glPopMatrix(); | |
glPopMatrix(); | |
} | |
void desenhaPerna_dir(GLfloat x, GLfloat y, GLfloat z){ | |
int pro = 1.0; | |
glPushMatrix(); | |
glTranslated(x, y, z); | |
glRotated(frames_perna[state_perna_dir][0], 0.0, 0.0, 1); | |
desenhaCubo(pro * 0.5, pro * 1.0, pro * 0.5); | |
glPushMatrix(); | |
glTranslated(0, pro * -0.5, 0); | |
glRotated(frames_perna[state_perna_dir][1], 0.0, 0.0, 1.0); | |
glTranslated(0, pro * 0.5, 0); | |
glTranslated(0, pro * -1.25, 0); | |
desenhaCubo(pro * 0.5, pro * 1.5, pro * 0.5); | |
glPushMatrix(); | |
glTranslated(0, pro * -0.5, 0); | |
glRotated(frames_perna[state_perna_dir][2], 0.0, 0.0, 1.0); | |
glTranslated(0, pro * 0.5, 0); | |
glTranslated(0, pro * -1.5, 0); | |
desenhaCubo(pro * 0.5, pro * 1.5, pro * 0.5); | |
glPopMatrix(); | |
glPopMatrix(); | |
glPopMatrix(); | |
} | |
void desenhaCorpoDog() { | |
glPushMatrix(); | |
glRotated(-90, 0.0, 1.0, 0.0); | |
glPushMatrix(); | |
//CORPO | |
glColor3f(0.0, 0.0, 0.0); | |
glTranslatef(0, 0, 2.05); | |
glScalef(1.5, 1.0, 4.1); | |
if( wire == 1) | |
glutWireCube(1.4); | |
else | |
glutSolidCube(1.4); | |
glPopMatrix(); | |
//glTranslatef(-0.45,0.7,0.0); | |
glPushMatrix(); | |
//RABO | |
glTranslatef(0, 1, 4.5); | |
glColor3f(0.5, 0.4, 0.3); | |
glScalef(0.3, 1.0, 0.3); | |
if( wire == 1) | |
glutWireCube(1); | |
else | |
glutSolidCube(1); | |
glPopMatrix(); | |
glPopMatrix(); | |
} | |
void desenhaDog() { | |
glPushMatrix(); | |
glColor3f(1.0f, 1.0f, 0.0f); | |
glTranslated(dog.x, dog.y, dog.z); | |
glRotated(dog_ang, 0, 1.0, 0); | |
// corpo | |
desenhaCorpoDog(); | |
// pernas | |
desenhaPerna_esq(0, 0, 0.6); | |
desenhaPerna_dir(0, 0, -0.6); | |
desenhaPerna_esq(-4.5, 0, 0.6); | |
desenhaPerna_dir(-4.5, 0, -0.6); | |
// cabeca | |
desenhaCabecaDog(); | |
glPopMatrix(); | |
} | |
void gato_orelha() { | |
glPushMatrix(); | |
glColor3f(0.7, 0.7, 0.7); | |
glScalef(1.0, 5.0, 1.0); | |
glTranslatef(0.0, 0.0, 0.01); | |
if( wire == 1) | |
glutWireCone(0.1, 0.1, 10.0, 10.0); | |
else | |
glutSolidCone(0.1, 0.1, 10.0, 10.0); | |
//glutSolidCube(0.1); | |
glTranslatef(0.0, 0.0, 0.37); | |
if( wire == 1) | |
glutWireCone(0.1, 0.1, 10.0, 10.0); | |
else | |
glutSolidCone(0.1, 0.1, 10.0, 10.0); | |
//glutSolidCube(0.1); | |
glPopMatrix(); | |
} | |
void gato_cabeca() { | |
glPushMatrix(); | |
glTranslatef(-0.1, 0, 0); | |
glColor3f(0.5, 0.5, 0.5); | |
if( wire == 1) | |
glutWireSphere(0.4, 12, 12); | |
else | |
glutSolidSphere(0.4, 12, 12); | |
glColor3f(0.7, 0.7, 0.6); | |
glTranslatef(0.2, -0.1, 0.0); | |
if( wire == 1 ) | |
glutWireSphere(0.15, 12, 12); | |
else | |
glutSolidSphere(0.15, 12, 12); | |
glTranslatef(-0.37, 0.40, -0.2); | |
gato_orelha(); | |
glPopMatrix(); | |
} | |
void gato_pescoco() { | |
glColor3f(0.7, 0.7, 0.7); | |
if( wire == 1) | |
glutWireCube(0.3); | |
else | |
glutSolidCube(0.3); | |
} | |
// dog’s upper body in group | |
void gato_CabePesc() { | |
glPushMatrix(); | |
glTranslatef(0, -0.1, 0.1); | |
gato_pescoco(); | |
glTranslatef(0.3, 0.0, 0); | |
gato_cabeca(); | |
glPopMatrix(); | |
} | |
void gato_perna1_a() { | |
glPushMatrix(); | |
glColor3f(0.6, 0.6, 0.6); | |
if( wire == 1) | |
glutWireCube(0.3); | |
else | |
glutSolidCube(0.3); | |
glPopMatrix(); | |
} | |
void gato_perna1_b() { | |
glPushMatrix(); | |
glColor3f(0.7, 0.7, 0.7); | |
if( wire == 1) | |
glutWireCube(0.3); | |
else | |
glutSolidCube(0.3); | |
glPopMatrix(); | |
} | |
//———————— | |
void gato_perna1() { | |
glPushMatrix(); | |
glScalef(1.0, 0.5, 1.0); | |
glTranslatef(0.87, 0.0, 0.02); | |
glScalef(1.0, 3.0, 1.0); | |
glRotated(perna_state[cat_state][perna_state_esq][0], 0.0, 0.0, 1.0); | |
gato_perna1_a(); | |
glScalef(1.0, 1.0, 1.0); | |
glTranslatef(0.0, -0.3, 0.0); | |
glRotated(perna_state[cat_state][perna_state_esq][1], 0.0, 0.0, 1.0); | |
gato_perna1_b(); | |
glPopMatrix(); | |
} | |
//———- | |
void gato_perna2_a() { | |
glPushMatrix(); | |
glColor3f(0.6, 0.6, 0.6); | |
if( wire == 1) | |
glutWireCube(0.3); | |
else | |
glutSolidCube(0.3); | |
glPopMatrix(); | |
} | |
void gato_perna2_b() { | |
glPushMatrix(); | |
glColor3f(0.7, 0.7, 0.7); | |
if( wire == 1) | |
glutWireCube(0.3); | |
else | |
glutSolidCube(0.3); | |
glPopMatrix(); | |
} | |
//————————————– | |
void gato_perna2() { | |
glPushMatrix(); | |
glScalef(1.0, 0.5, 1.0); | |
glTranslatef(0.9, 0.0, -0.58); | |
glScalef(1.0, 3.0, 1.0); | |
glRotated(perna_state[cat_state][perna_state_esq][0], 0.0, 0.0, 1.0); | |
gato_perna2_a(); | |
//glScalef(1.0,1.0,1.0); | |
glTranslatef(0.0, -0.3, 0.0); | |
glRotated(perna_state[cat_state][perna_state_esq][1], 0.0, 0.0, 1.0); | |
gato_perna2_b(); | |
glPopMatrix(); | |
} | |
//———- | |
void gato_perna3_a() { | |
glPushMatrix(); | |
glColor3f(0.6, 0.6, 0.6); | |
if( wire == 1) | |
glutWireCube(0.3); | |
else | |
glutSolidCube(0.3); | |
glPopMatrix(); | |
} | |
void gato_perna3_b() { | |
glPushMatrix(); | |
glColor3f(0.7, 0.7, 0.7); | |
if( wire == 1) | |
glutWireCube(0.3); | |
else | |
glutSolidCube(0.3); | |
glPopMatrix(); | |
} | |
//————————————– | |
void gato_perna3() { | |
glPushMatrix(); | |
glScalef(1.0, 0.5, 1.0); | |
glTranslatef(-.5, 0.0, -0.58); | |
glScalef(1.0, 3.0, 1.0); | |
glRotated(perna_state[cat_state][perna_state_dir][0], 0.0, 0.0, 1.0); | |
gato_perna3_a(); | |
//glScalef(1.0,1.0,1.0); | |
glTranslatef(0.0, -0.3, 0.0); | |
glRotated(perna_state[cat_state][perna_state_dir][1], 0.0, 0.0, 1.0); | |
gato_perna3_b(); | |
glPopMatrix(); | |
} | |
//———- | |
void gato_perna4_a() { | |
glPushMatrix(); | |
glColor3f(0.6, 0.6, 0.6); | |
if( wire == 1) | |
glutWireCube(0.3); | |
else | |
glutSolidCube(0.3); | |
glPopMatrix(); | |
} | |
void gato_perna4_b() { | |
glPushMatrix(); | |
glColor3f(0.7, 0.7, 0.7); | |
if( wire == 1) | |
glutWireCube(0.3); | |
else | |
glutSolidCube(0.3); | |
glPopMatrix(); | |
} | |
//————————————– | |
void gato_perna4() { | |
glPushMatrix(); | |
glScalef(1.0, 0.5, 1.0); | |
glTranslatef(-0.5, 0.0, 0.02); | |
glScalef(1.0, 3.0, 1.0); | |
glRotated(perna_state[cat_state][perna_state_dir][0], 0.0, 0.0, 1.0); | |
gato_perna4_a(); | |
glTranslatef(0.0, -0.3, 0.0); | |
glRotated(perna_state[cat_state][perna_state_dir][1], 0.0, 0.0, 1.0); | |
gato_perna4_b(); | |
glPopMatrix(); | |
} | |
//—————————————————– | |
//dog’s tail | |
void gato_rabo() { | |
glPushMatrix(); | |
glTranslatef(0.03, 0.25, 0); | |
glColor3f(0.7, 0.7, 0.7); | |
glScalef(1.0, 10.0, 1.0); | |
glRotated(45, 0.0, 1.0, 0.0); | |
if( wire == 1) | |
glutWireCone(0.1, 0.1, 6, 6); | |
else | |
glutSolidCone(0.1, 0.1, 6, 6); | |
glPopMatrix(); | |
} | |
//—————————————————– | |
//dog’s body with tail | |
void gato_corpo() { | |
glPushMatrix(); | |
glColor3f(0.8, 0.8, 0.7); | |
glScalef(1.4, 0.6, 0.6); | |
if( wire == 1) | |
glutWireCube(1.0); | |
else | |
glutSolidCube(1.0); | |
glTranslatef(-0.45, 0.7, 0.0); | |
gato_rabo(); | |
glPopMatrix(); | |
} | |
//——————————————————– | |
// dog’s middle section with legs | |
void gato_pernas() { | |
gato_perna1(); | |
gato_perna2(); | |
gato_perna3(); | |
gato_perna4(); | |
} | |
//——————————————————– | |
// complete dog body and body together | |
void gato_comb_pernacorpo() { | |
glPushMatrix(); | |
glTranslatef(1.0, 1.0, 1.0); | |
glScalef(1.0, 1.0, 1.0); | |
gato_corpo(); | |
glPopMatrix(); | |
glPushMatrix(); | |
glTranslatef(0.85, 0.5, 1.2); | |
glScalef(0.6, 0.6, 0.6); | |
gato_pernas(); | |
glPopMatrix(); | |
} | |
//——————————————————– | |
// complete dog legs, head together | |
void gato_comb_pernacorpocabeca() { | |
glPushMatrix(); | |
glTranslatef(1.0, 1.0, 1.0); | |
glScalef(1.0, 1.0, 1.0); | |
gato_comb_pernacorpo(); | |
glPopMatrix(); | |
glPushMatrix(); | |
glTranslatef(2.8, 2.2, 1.8); | |
glScalef(1.5, 1.0, 1.5); | |
gato_CabePesc(); | |
glPopMatrix(); | |
} | |
//——————————————————– | |
// complete dog together | |
void gato_completo() { | |
glPushMatrix(); | |
glTranslatef(cat.x, cat.y, cat.z); | |
glRotated(cat_ang, 0.0, 1.0, 0.0); | |
gato_comb_pernacorpocabeca(); | |
glPopMatrix(); | |
} | |
// Luz ativa: branca | |
int luz = 0; | |
// Posições das fontes de luz | |
GLfloat posLuz[3][4] = { { 80.0, 40.0, 40.0, 1.0 }, { -80.0, 40.0, 40.0, 1.0 }, | |
{ 0.0, 40.0, 40.0, 1.0 } }; | |
// Cores difusas | |
GLfloat luzDifusa[3][4] = { { 0.0, 0.3, 0.0, 1.0 }, { 0.3, 0.0, 0.0, 1.0 }, { | |
0.0, 0.0, 0.3, 1.0 } }; | |
// Cores especulares | |
GLfloat luzEspecular[3][4] = { { 0.0, 0.8, 0.0, 1.0 }, { 0.8, 0.0, 0.0, 0.0 }, { | |
0.0, 0.0, 0.8, 0.0 } }; | |
// Função responsável pela especificação dos parâmetros de iluminação | |
void DefineIluminacao(void) { | |
int i; | |
GLfloat luzAmbiente[4] = { 0.2, 0.2, 0.2, 1.0 }; | |
// Capacidade de brilho do material | |
GLfloat especularidade[4] = { 0.5, 0.5, 0.5, 1.0 }; | |
GLint especMaterial = 90; | |
// Define a refletância do material | |
glMaterialfv(GL_FRONT, GL_SPECULAR, especularidade); | |
// Define a concentração do brilho | |
glMateriali(GL_FRONT, GL_SHININESS, especMaterial); | |
// Ativa o uso da luz ambiente | |
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, luzAmbiente); | |
// Define os parâmetros das fontes de luz | |
for (i = 0; i < 3; ++i) { | |
glLightfv(GL_LIGHT0 + i, GL_AMBIENT, luzAmbiente); | |
glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, luzDifusa[i]); | |
glLightfv(GL_LIGHT0 + i, GL_SPECULAR, luzEspecular[i]); | |
glLightfv(GL_LIGHT0 + i, GL_POSITION, posLuz[i]); | |
} | |
} | |
void desenha_plano() { | |
glPushMatrix(); | |
glColor3f(0.05f, 0.25f, 0.05f); | |
glTranslatef(0, -3.2, 0); | |
desenhaCubo(plano_x, 0.10, plano_z); | |
glPopMatrix(); | |
} | |
void Desenha(void) { | |
int i; | |
int j; | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glEnable(GL_DEPTH_TEST); | |
DefineIluminacao(); | |
glPushMatrix(); | |
glTranslated(0, translacao, 0); | |
glScaled(escala, escala, escala); | |
glRotated(ang_x, 1, 0, 0); | |
glRotated(ang_y, 0, 1, 0); | |
glRotated(anguloz, 0, 0, 1); | |
desenha_plano(); | |
if (cat_presente == 1) | |
gato_completo(); | |
if (dog_presente == 1) | |
desenhaDog(); | |
for (i = -5; i < 5; i++) | |
for (j = -5; j < 5; j++) { | |
glPushMatrix(); | |
glTranslatef(i * 10.0, 0.0, j * 10.0); | |
desenhaArvore(); | |
glPopMatrix(); | |
} | |
glPopMatrix(); | |
glutSwapBuffers(); | |
} | |
void EspecificaParametrosVisualizacao(void) { | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
if( pespectiva == 0) | |
glOrtho(-50, 50, -50, 50, -300.0, 300.0); | |
else | |
gluPerspective(45, fAspect, 0.5, 500); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
gluLookAt(0, 60, 150, 0, 0, 0, 0, 1, 0); | |
} | |
void inicializa_pernas() { | |
state_perna_esq = 0; | |
state_perna_dir = 2; | |
frames_perna[0][1] = 15.0; | |
frames_perna[0][2] = -15.0; | |
frames_perna[0][3] = 0.0; | |
frames_perna[1][0] = 30.0; | |
frames_perna[1][1] = -15.0; | |
frames_perna[1][2] = -15.0; | |
frames_perna[2][0] = 30.0; | |
frames_perna[2][1] = -30.0; | |
frames_perna[2][2] = 0.0; | |
frames_perna[3][0] = 0.0; | |
frames_perna[3][1] = -15.0; | |
frames_perna[3][2] = 0.0; | |
frames_perna[4][0] = 0.0; | |
frames_perna[4][1] = -30.0; | |
frames_perna[4][2] = 0.0; | |
perna_state_esq = 0; | |
perna_state_dir = 4; | |
perna_state[1][0][0] = 30; | |
perna_state[1][1][0] = 45; | |
perna_state[1][2][0] = 60; | |
perna_state[1][3][0] = 45; | |
perna_state[1][4][0] = 30; | |
perna_state[1][5][0] = 0; | |
perna_state[1][0][1] = 30; | |
perna_state[1][1][1] = 45; | |
perna_state[1][2][1] = 60; | |
perna_state[1][3][1] = 45; | |
perna_state[1][4][1] = 30; | |
perna_state[1][5][1] = 0; | |
perna_state[0][0][1] = 0; | |
perna_state[0][1][1] = 0; | |
perna_state[0][2][1] = 0; | |
perna_state[0][3][1] = 0; | |
perna_state[0][4][1] = 0; | |
perna_state[0][5][1] = 0; | |
perna_state[1][0][0] = 0; | |
perna_state[1][1][0] = 0; | |
perna_state[1][2][0] = 0; | |
perna_state[1][3][0] = 0; | |
perna_state[1][4][0] = 0; | |
perna_state[1][5][0] = 0; | |
} | |
void cat_move() { | |
Vertice r; | |
float dist; | |
float numerador; | |
float denominador; | |
if (cat_state > 0) { | |
r.x = cat.x - dog.x; | |
r.z = cat.z - dog.z; | |
numerador = 1.0 * r.x + 0.0 * r.z; | |
denominador = sqrt(pow((float) 0.0, 2) + pow((float) 1.0, 2)) | |
* sqrt(pow((float) r.z, 2) + pow((float) r.x, 2)); | |
numerador /= denominador; | |
cat_ang = acos(numerador) * 180 / PI; | |
dist = sqrt(pow((float) r.z, 2) + pow((float) r.x, 2)); | |
r.x *= (float) 1 / dist; | |
r.z *= (float) 1 / dist; | |
if (cat.x > 50 && cat.x < -50 || cat.z < -50 && cat.z > 50) { | |
cat_state = 0; | |
} | |
else if( cat.x > 50 || cat.x < -50 ){ | |
cat.z += r.z; | |
} else if( cat.z < -50 || cat.z > 50 ){ | |
cat.x += r.x; | |
}else{ | |
cat.x += r.x; | |
cat.z += r.z; | |
} | |
} | |
} | |
void dog_move() { | |
Vertice r; | |
float dist; | |
float numerador; | |
float denominador; | |
if (dog_state > 0) { | |
r.x = cat.x - dog.x; | |
r.z = cat.z - dog.z; | |
numerador = 1.0 * r.x + 0.0 * r.z; | |
denominador = sqrt(pow((float) 0.0, 2) + pow((float) 1.0, 2)) | |
* sqrt(pow((float) r.z, 2) + pow((float) r.x, 2)); | |
numerador /= denominador; | |
dog_ang = acos(numerador) * 180 / PI; | |
dist = sqrt(pow((float) r.z, 2) + pow((float) r.x, 2)); | |
r.x *= (float) 1 / dist; | |
r.z *= (float) 1 / dist; | |
dog.x += r.x * 1.3; | |
dog.z += r.z * 1.3; | |
dist = sqrt(pow(cat.x - dog.x, 2) + pow(cat.z - dog.z, 2)); | |
if (dist < 2) { | |
cat_state = 0; | |
cat_presente = 0; | |
dog_state = 0; | |
} | |
} | |
} | |
void AlteraTamanhoJanela(GLsizei w, GLsizei h) { | |
if (h == 0) | |
h = 1; | |
glViewport(0, 0, w, h); | |
fAspect = (GLfloat) w / (GLfloat) h; | |
EspecificaParametrosVisualizacao(); | |
} | |
void TeclasEspeciais(int key, int x, int y) { | |
glutPostRedisplay(); | |
} | |
void GerenciaMouse(int button, int state, int x, int y) { | |
if(state==GLUT_DOWN){ | |
printf("%d %d\n", x, y); | |
cat_inicializa(x / 100, -4.2 , y / 100); | |
dog_state = 1; | |
cat_state = 1; | |
} | |
glutPostRedisplay(); | |
} | |
void Inicializa(void) { | |
inicializa_tgs(); | |
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // cor de fundo | |
EspecificaParametrosVisualizacao(); | |
angle = 45; | |
cat_inicializa(0.0, -4.2, 0.0); | |
inicializa_pernas(); | |
params = gluNewQuadric(); | |
} | |
void Teclado(unsigned char key, int x, int y) { | |
switch (key) { | |
case 'S': | |
escala += 0.1; | |
break; | |
case 's': | |
if (escala == 0.1) | |
escala = 0.1; | |
else | |
escala -= 0.1; | |
break; | |
case 'X': | |
ang_x = (ang_x + 5); | |
break; | |
case 'x': | |
ang_x = (ang_x - 5); | |
break; | |
case 'Y': | |
ang_y = (ang_y + 5); | |
break; | |
case 'y': | |
ang_y = (ang_y - 5); | |
break; | |
case 'Z': | |
anguloz = (anguloz + 5); | |
break; | |
case 'z': | |
anguloz = (anguloz - 5); | |
break; | |
case 'T': | |
translacao += 0.1; | |
break; | |
case 't': | |
translacao -= 0.1; | |
break; | |
case 'L': | |
Inicializa(); | |
break; | |
case 'C': | |
dog_inicializa(40.0, 1, 5); | |
break; | |
case 'P': | |
if( pespectiva == 1) | |
pespectiva = 0; | |
else | |
pespectiva = 1; | |
EspecificaParametrosVisualizacao(); | |
break; | |
case 'A': | |
if( wire == 1) | |
wire = 0; | |
else | |
wire = 1; | |
} | |
glutPostRedisplay(); | |
} | |
void Anima(int value) { | |
if( state_perna_esq > 3) | |
state_perna_esq = 0; | |
else | |
state_perna_esq++; | |
if( state_perna_dir > 3) | |
state_perna_dir = 0; | |
else | |
state_perna_dir++; | |
if (perna_state_esq > 5) { | |
perna_state_esq = 0; | |
} else | |
perna_state_esq++; | |
if (perna_state_dir > 5) { | |
perna_state_dir = 0; | |
} else | |
perna_state_dir++; | |
if (cat_presente) | |
cat_move(); | |
if (dog_presente) | |
dog_move(); | |
glutPostRedisplay(); | |
glutTimerFunc(300, Anima, 1); | |
} | |
int main(int argc, char* argv[]) { | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_SINGLE | GLUT_DEPTH | GLUT_RGB); | |
glutInitWindowPosition(5, 5); | |
glutInitWindowSize(WIDTH, HEIGHT); | |
glutCreateWindow("CATDOG"); | |
glutDisplayFunc(Desenha); | |
glutReshapeFunc(AlteraTamanhoJanela); | |
glutSpecialFunc(TeclasEspeciais); | |
glutMouseFunc(GerenciaMouse); | |
glutTimerFunc(60, Anima, 1); | |
glutKeyboardFunc(Teclado); | |
Inicializa(); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment