Enviado em 19/07/2014 - 19:44h
Eu tinha criado a funcao mas deletei e esqueci de remover antes de enviar o codigo,eu coloquei o fog,eu so preciso ajustar ele , pq ta meio estranho,mas é so apertar f e dai o fog some e eu comentei 2 linhas nas funcoes do mouse , verifica se deu certo,pq comigo com ou sem funciona legal (obs: tenta nao deixar eu tela cheia,pode ser isso)
#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdbool.h>
float ang = 0.0,ang2 = 0.0,tmp_ang = 0.0,f = -5.0,f2 = 0.0,f3 = 0.0;
int r = 1,t = 1,res = 0,countr = 0,countw = 0,maxx,maxz,num = 3,in = 0,tx,ty,**m,w,h;
char res1[20];
bool fullscreen = false,fogON = true,fogAntes;
static GLint fogMode;
GLuint texid1,texid2,texid3;
char *img_read(char * name){
FILE *f = fopen(name,"rb");
if(f == NULL) exit(0);
int of;
fseek(f, 10, SEEK_SET);
fread(&of, sizeof(int), 1, f);
fseek(f, 4, SEEK_CUR);
fread(&w, sizeof(int), 1, f);
fread(&h, sizeof(int), 1, f);
fseek(f, of, SEEK_SET);
int by = ((w * 3 + 3)/4) * 4 - (w * 3 % 4);
char *tmp_m = malloc(sizeof(char) * by * h);
char *m = malloc(sizeof(char) * w * h * 3);
fread(tmp_m, sizeof(char) * by * h, 1, f);
int x,y,i;
for(y = 0; y < h; y++){
for(x = 0; x < w; x++){
for(i = 0; i < 3; i++){
m[3*(w*y + x) + i] = tmp_m[3*(w*y + x) + (2 - i)];
}
}
}
free(tmp_m);
return m;
}
GLuint loadTex(char *c){
char *wa = img_read(c);
GLuint texid;
glGenTextures(1, &texid);
glBindTexture(GL_TEXTURE_2D, texid);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, wa);
free(wa);
return texid;
}
void flo(float x1, float x2, float y1, float z1, float z2, float a){
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texid2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBegin(GL_QUADS);
glColor3f(1.0, a, a);
glTexCoord2f(0.0,0.0);
glVertex3f(x1, y1, z1);
glTexCoord2f(2.0,0.0);
glVertex3f(x2, y1, z1);
glTexCoord2f(2.0,2.0);
glVertex3f(x2, y1, z2);
glTexCoord2f(0.0,2.0);
glVertex3f(x1, y1, z2);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void [*****](float x1, float x2, float y1, float y2, float z1, float z2){
glEnable(GL_TEXTURE_2D);
if(t) {glBindTexture(GL_TEXTURE_2D, texid1);
//glBindTexture(GL_TEXTURE_2D, texid3);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(0.0,0.0);
glVertex3f(x1, y1, z1);
glTexCoord2f(4.0,0.0);
glVertex3f(x2, y1, z1);
glTexCoord2f(4.0,4.0);
glVertex3f(x2, y2, z1);
glTexCoord2f(0.0,4.0);
glVertex3f(x1, y2, z1);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(0.0,0.0);
glVertex3f(x1, y1, z2);
glTexCoord2f(4.0,0.0);
glVertex3f(x2, y1, z2);
glTexCoord2f(4.0,4.0);
glVertex3f(x2, y2, z2);
glTexCoord2f(0.0,4.0);
glVertex3f(x1, y2, z2);;
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(0.0,0.0);
glVertex3f(x1, y2, z1);
glTexCoord2f(4.0,0.0);
glVertex3f(x2, y2, z1);
glTexCoord2f(4.0,4.0);
glVertex3f(x2, y2, z2);
glTexCoord2f(0.0,4.0);
glVertex3f(x1, y2, z2);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(0.0,0.0);
glVertex3f(x1, y1, z1);
glTexCoord2f(4.0,0.0);
glVertex3f(x1, y2, z1);
glTexCoord2f(4.0,4.0);
glVertex3f(x1, y2, z2);
glTexCoord2f(0.0,4.0);
glVertex3f(x1, y1, z2);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(0.0,0.0);
glVertex3f(x2, y1, z1);
glTexCoord2f(4.0,0.0);
glVertex3f(x2, y2, z1);
glTexCoord2f(4.0,4.0);
glVertex3f(x2, y2, z2);
glTexCoord2f(0.0,4.0);
glVertex3f(x2, y1, z2);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void processMouseActiveMotion(int x, int y)
{
// glutSetCursor(GLUT_CURSOR_NONE);
// glutWarpPointer(800/2, 600/2);
if(y > 300){
ang = 0;
ang2 += .30;
if(ang2 > 40)ang2 = 40;
else if(ang2 < -40)ang2 = -40;
glutPostRedisplay();
}else if(y < 300){
ang=0;
ang2 -= .30;
if(ang2 > 40)ang2 = 40;
else if(ang2 < -40)ang2 = -40;
glutPostRedisplay();
}
}
void processMousePassiveMotion(int x, int y)
{
// glutSetCursor(GLUT_CURSOR_NONE);
// glutWarpPointer(800/2, 600/2);
if(x < 400){
ang2 = 0;
ang -= .45; // Aqui Vai Para a Esquerda <-
if(ang > 360.0) ang -= 360.0;
if(ang < 0) ang += 360.0;
glutPostRedisplay();
}else if(x > 400){
ang2 = 0;
ang += .45; // Aqui Vai Para a Direita ->
if(ang < 0.0) ang += 360.0;
if(ang > 360.0) ang -= 360.0;
glutPostRedisplay();
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
glRotatef(ang,0.0,1.0,0.0);
glRotatef(ang2,1.0,0.0,0.0);
glTranslatef(f2,f3, f);
flo(-f2 -0.05, -f2 + 0.05, -0.2, -f - 0.1, -f, 0.0);
int i, j;
for(i = 0 ; i < maxx; i++){
for(j = 0 ; j < maxz; j++){
if(m[j][i] == 1) [*****](0.4*i, 0.4*(i + 1), -0.2, 0.2, -0.4*j, -0.4*(j + 1));
else flo(0.4*i, 0.4*(i + 1), -0.2, -0.4*j, -0.4*(j + 1), 1.0);
}
}
glutSwapBuffers();
}
void teclaEspeciais(int key, int x, int y){
fullscreen = !fullscreen;
if(key == 11 && !fullscreen){
glutFullScreen();
}else if (fullscreen){
glutReshapeWindow(800, 600);
glutPositionWindow(0,0);
}
}
void keyboard(unsigned char key, int x, int y)
{
ang2 = 0;
if(key == 27) {
printf("Jogo Finalizado . . .\n\n");
exit(0);
}
else if((char)key == 'a' && r){
ang -= 1.0;
if(ang > 360.0) ang -= 360.0;
if(ang < 0) ang += 360.0;
glutPostRedisplay();
}else if((char)key == 'd' && r){
ang += 1.0;
if(ang < 0.0) ang += 360.0;
if(ang > 360.0) ang -= 360.0;
glutPostRedisplay();
}else if((char)key == 's' && r){
int x = -1*(int)((f2 + (0.1*sin(ang*(3.14/180))))/0.4);
int z = (int)((f - (0.1*cos(ang*(3.14/180))))/0.4);
if(x >= 0 && x < maxx && z >= 0 && z < maxz){
if(m[z][x] == 0)f2 += 0.1*sin(ang*(3.14/180));
if(m[z][x] == 0)f -= 0.1*cos(ang*(3.14/180));
}else{
f2 += 0.1*sin(ang*(3.14/180));
f -= 0.1*cos(ang*(3.14/180));
}
glutPostRedisplay();
}
else if((char)key == 'w' && r){
int x = -1*(int)((f2 - (0.1*sin(ang*(3.14/180))))/0.4);
int z = (int)((f + (0.1*cos(ang*(3.14/180))))/0.4);
if(x >= 0 && x < maxx && z >= 0 && z < maxz){
if(m[z][x] == 0)f2 -= 0.1*sin(ang*(3.14/180));
if(m[z][x] == 0)f += 0.1*cos(ang*(3.14/180));
}else{
f2 -= 0.1*sin(ang*(3.14/180));
f += 0.1*cos(ang*(3.14/180));
}
glutPostRedisplay();
}
else if((char)key == 'o'){
if(r == 1){
tmp_ang = ang;
ang = 0.0;
r = 0;
f3 = -10.0;
ang2 = 90.0;
glDisable(GL_LIGHTING);
glDisable(GL_FOG); // Desativa o FOG
glutPostRedisplay();
}
}else if((char)key == 'p'){
if(r == 0){
ang = tmp_ang;
r = 1;
f3 = 0.0;
ang2 = 0.0;
fogAntes = fogON;
if(fogAntes){
glClearColor(0, 0, 0, 0.5f);
glEnable(GL_LIGHTING);
glEnable(GL_FOG); // Ativa o FOG
}else{
glDisable(GL_LIGHTING);
glDisable(GL_FOG);
glClearColor(0, 191, 255, 1);
}
glutPostRedisplay();
}
}else if((char)key == 'f'){
if(fogON){
glDisable(GL_LIGHTING);
glDisable(GL_FOG); // Desativa o FOG
glClearColor(0, 191, 255, 1);
glutPostRedisplay();
fogON = false;
}else if(!fogON){
glClearColor(0, 0, 0, 0.5f);
glEnable(GL_LIGHTING);
glEnable(GL_FOG); // Ativa o FOG
glutPostRedisplay();
fogON = true;
}
}
}
void init()
{
texid1 = loadTex("1.bmp"); // Parede
texid2 = loadTex("tex05.bmp"); // Chao
glClearColor (135.0, 210.0, 235.0, 1.0);
glMatrixMode (GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
GLfloat position[] = { 0.5, 0.5, 0.5, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHTING);
float fogColor[] = {1,1,1,0};
fogMode = GL_EXP;
glClearColor(0, 0, 0, 0.5f); // We'll Clear To The Color Of The Fog ( Modified )
glFogi(GL_FOG_MODE, fogMode); // Fog Mode
glFogfv(GL_FOG_COLOR, fogColor); // Set Fog Color
glFogf(GL_FOG_DENSITY, 0.40f); // How Dense Will The Fog Be
glHint(GL_FOG_HINT, GL_FASTEST); // Fog Hint Value
glFogf(GL_FOG_START, 1.0f); // Fog Start Depth
glFogf(GL_FOG_END, 3.0f); // Fog End Depth
glEnable(GL_FOG); // Enables GL_FOG
}
void Reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w/(double)h, 0.1, 200.0);
}
int main(int argc, char** argv)
{
if(argc < 5){
printf("\nModo de Uso :\n\n./TesteJogos Altura Comprimento InicioX InicioY\n\n");
return 0;
}
maxx = atoi(argv[1]);
maxz = atoi(argv[2]);
f2 = -0.4*(2*atoi(argv[3]) +1)/2;
f =0.4*(2*atoi(argv[4]) + 1)/2;
m = malloc(sizeof(int *) * maxz);
FILE *f = fopen("ab.txt", "r");
if(f == NULL) return 0;
char c;
int i = 0;
int j = 0;
m[i] = malloc(sizeof(int) * maxx);
while((c = fgetc(f)) != EOF){
if(c == '\n'){
i++;
j = 0;
m[i] = malloc(sizeof(int) * maxx);
}else{
if(c == '0') m[i][j] = 0;
else m[i][j] = 1;
j++;
}
}
glutInit(&argc,argv);
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,600);
glutCreateWindow("Super A'MEZE'ing"); // creating the window
glutReshapeFunc(Reshape);
system("clear");
glutMotionFunc(processMouseActiveMotion);
glutPassiveMotionFunc(processMousePassiveMotion);
glutKeyboardFunc( keyboard );
glutSpecialFunc( teclaEspeciais);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}