PDA

View Full Version : simple bug that i can't fix..



tanaka
05-20-2012, 11:27 PM
Edit: apparent near view can't be 0.0, so i fix the problem. there's another problem, my blue light wont work i'm too close to the wall. ur welcome to modify and let me know anything cool i could change.

// bf edit

hi, i just start working on this the past 2 days, on and off. my brain is very tire. i basically learning on my own this summer, so please try to be nice if you can :T

this program is a room(maze - incomplete) with a "spotlight". it works fine, it has collision detection so you can walk around perfectly, i used a 2d grid for collision.

my problem is, i can see through my cube, even when lighting is turnoff. i spend hours try to fix this but i cant. i have not apply materials color,etc yet. not sure if that matters(sorry). it also has a birdview by pressing 1.

here's keyboard:
j - look left
l - look right
i - go forward
o - back step

important - press '1' to see a birdview

//code in c++

#include <stdio.h>
#include <windows.h> // Standard header for MS Windows applications
#include <GL/gl.h> // Open Graphics Library (OpenGL) header
#include <glut.h> // The GL Utility Toolkit (GLUT) Header
#include<string>
#include<math.h>
#include<iostream>
#include<vector>

using namespace std;
//prototype
void drawBlock();
void stepUp();
void stepBack();
void turnRight();
void drawCircle(float radius);
void turnLeft();
bool collison(float x, float z);
float findDis(float x, float z, float x2, float z2);
void birdView();
void update(int value);
bool birdCam = false;
//vectors
struct point {
float xmin, zmax;
float xmax, zmin;

};
float _angle = 0;
float R = 0;

//blockcordinate
vector<point>find;

const float DEG2RAD = 3.14159/180;
string maze[20][20] = {{"x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x"},
{"x"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","x"} ,
{"x"," ","x"," ","x","x","x","x","x","x","x","x","x"," ","x","x","x","x"," ","x"},
{"x"," ","x"," ","x"," "," "," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x"," ","x","x","x"," "," "," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x"," "," "," "," "," "," "," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x"," ","x"," "," "," "," "," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x"," ","x"," "," "," "," "," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x"," ","x"," "," "," "," "," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x"," ","x"," ","x"," "," "," "," "," "," "," "," "," "," "," "," "," "," ","x"},
{"x"," ","x"," ","x"," "," "," "," "," "," "," "," "," "," "," "," "," "," ","x"},
{"x"," ","x","x","x","x","x"," "," "," "," "," "," "," "," "," "," "," "," ","x"},
{"x"," ","x"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","x"},
{"x"," ","x","x","x","x","x"," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x"," "," "," "," "," "," "," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x"," "," "," "," "," "," "," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x"," "," "," "," "," "," "," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x"," "," "," "," "," "," "," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x"," "," "," "," "," "," "," "," ","x","x"," "," "," "," "," "," "," "," ","x"},
{"x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x"}};


#define KEY_ESCAPE 27
float Angle = 0;
float PI = 3.14159265;
float radius = 1;
float r = 0.1f; // rate of moving
bool test = false;

typedef struct {
int width;
int height;
char* title;

float field_of_view_angle;
float z_near;
float z_far;
} glutWindow;

struct{
float xpos,ypos,zpos;
float xlook,ylook,zlook;

}camera;

struct{
float xpos,ypos,zpos;
float xlook,ylook,zlook;

}camera2;
glutWindow win;


void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen and Depth Buffer

glLoadIdentity();


if(birdCam)
gluLookAt(camera2.xpos, camera2.ypos, camera2.zpos, camera2.xlook, camera2.ylook, camera2.zlook, 0, 0, 1);
else
gluLookAt(camera.xpos, camera.ypos, camera.zpos, camera.xlook, camera.ylook, camera.zlook, 0, 1, 0);

float ambient[] = {0.3,0.3,0.3, 1.0f};
float diffuse[] = {0,0,1, 1.0f};
float specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
float position[] = {camera.xpos,camera.ypos,camera.zpos,1};
float lightColor[] = {0,0,1, 1.0f};

glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);

glPushMatrix();
glRotatef(Angle,0,-1,0);
GLfloat direction[]={0,0,-1};
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.0f);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 1.0f);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION,0.0);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,direction);
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,15);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT,20.0);
glPopMatrix();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

for(int i = 19; i >= 0; i--){
glPushMatrix();
glTranslatef(0,0, -i);
for(int j = 0; j < 20; j++){
if(maze[19-i][j] == "x"){
drawBlock();

if(!test){
//vectors collison
point current;
current.xmin = j;
current.zmin = -i -1;
current.xmax = j + 1;
current.zmax = -i ;
find.push_back(current);
}
}
glTranslatef(1,0,0);
}
glPopMatrix();
}


test = true;

//circle target
glPushMatrix();
glTranslatef(camera.xlook,camera.ylook,camera.zloo k);
glRotatef(-Angle,0,1,0);
drawCircle(0.005);
glPopMatrix();



glutSwapBuffers();
}



void initialize ()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
// glEnable(GL_COLOR_MATERIAL);

float ambient[] = {0.2,0.2,0.2,1};
float diffuse[] = {0,0,1};
// glMaterialfv(GL_FRONT,
// GL_EMISSION, diffuse);
// glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);

glMatrixMode(GL_PROJECTION); // select projection matrix
glViewport(0, 0, win.width, win.height); // set the viewport
glMatrixMode(GL_PROJECTION); // set matrix mode
glLoadIdentity(); // reset projection matrix
GLfloat aspect = (GLfloat) win.width / win.height;
gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far); // set up a perspective projection matrix
glMatrixMode(GL_MODELVIEW); // specify which matrix is the current matrix
glShadeModel( GL_SMOOTH );
glClearDepth( 1.0f ); // specify the clear value for the depth buffer
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); // specify implementation-specific hints
// glClearColor(0.0, 0.0, 0.0, 1.0); // specify clear values for the color buffers
glColor3f(0.0, 0.0f, 1.0f);


}


void keyboards ( unsigned char key, int mousePositionX, int mousePositionY )
{
switch ( key )
{
case KEY_ESCAPE:
exit ( 0 );
break;
case 'i':
stepUp();
break;
case 'o':
stepBack();
break;
case 'j':
turnLeft();
break;
case 'l':
turnRight();
break;
case '1':
birdCam = !birdCam;
break;

case'r':
R += 01;
break;

default:
break;
}
}


int main(int argc, char **argv)
{
// set window values
win.width = 640;
win.height = 480;
win.title = "OpenGL/GLUT Example. Visit http://openglsamples.sf.net ";
win.field_of_view_angle = 45;
win.z_near = 0;
win.z_far = 100;
birdView();

camera.xpos = 10;
camera.ypos = 0.5;
camera.zpos = -10;
camera.xlook = (float) (camera.xpos +radius*sin(Angle*PI/180));
camera.ylook = 0.5;
camera.zlook = (float) (camera.zpos +radius*-cos(Angle*PI/180));

// initialize and run program
glutInit(&argc, argv); // GLUT initialization
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); // Display Mode
glutInitWindowSize(win.width,win.height); // set window size
glutCreateWindow(win.title); // create Window
glutDisplayFunc(display); // register Display Function
glutIdleFunc( display ); // register Idle Function
glutKeyboardFunc( keyboards ); // register Keyboard Handler
initialize();
glutMainLoop(); // run GLUT mainloop
return 0;

}

void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

void drawBlock(){


glBegin(GL_QUADS);/* f1: front */

glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f,0.0f,0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1,0.0f,0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1,1,0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f,1,0.0f);

//right

glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1,0.0,0.0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1,1,0.0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1,1,-1);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1,0.0f,-1);


//back

glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0,0,-1);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1,0,-1);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1,1,-1);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0,1,-1);

//left

glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f,0.0f,0.0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.0f,1,0.0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.0f,1,-1);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f,0.0f,-1);




glEnd();


}

void stepUp(){

//look ahead to stop going through wall
// r is speed, declared global
// this will be use in the next if statement, to see if step forward will produce collison or not
float fxpos = (float) (camera.xpos + 13*r* sin(Angle*PI/180));
float fzpos = (float) (camera.zpos + 13*r*-cos(Angle*PI/180));

// if collision is false advance
if(!collison(fxpos, fzpos)){
cout<<"no col"<<endl;
camera.xpos = (float) (camera.xpos + r* sin(Angle*PI/180));
camera.zpos = (float) (camera.zpos + r*-cos(Angle*PI/180));
camera.xlook = (float) (camera.xpos +(radius)*sin(Angle*PI/180));
camera.zlook = (float) (camera.zpos +(radius)*-cos(Angle*PI/180));
}
else
cout<<"collision"<<endl;
}

void stepBack(){

float fxpos = (float) (camera.xpos - 10*r* sin(Angle*PI/180));
float fzpos = (float) (camera.zpos - 10*r*-cos(Angle*PI/180));

if(!collison(fxpos, fzpos)){
camera.xpos = (float) (camera.xpos - r* sin(Angle*PI/180));
camera.zpos = (float) (camera.zpos - r*- cos(Angle*PI/180));
camera.xlook = (float) (camera.xpos +(radius)*sin(Angle*PI/180));
camera.zlook = (float) (camera.zpos +(radius)*-cos(Angle*PI/180));
}
}

void turnRight(){

if(Angle >360)
Angle = 0;
Angle += 2;
camera.xlook = (float) (camera.xpos +radius*sin(Angle*PI/180));
camera.zlook = (float) (camera.zpos +radius*-cos(Angle*PI/180));

}

void turnLeft(){

if(Angle >360)
Angle = 0;

Angle -= 2;
camera.xlook = (float) (camera.xpos +radius*sin(Angle*PI/180));
camera.zlook = (float) (camera.zpos +radius*-cos(Angle*PI/180));



}

void drawCircle(float radius)
{
glBegin(GL_LINE_LOOP);

for (int i=0; i < 360; i+= 20)
{
float degInRad = i*DEG2RAD;
glNormal3f(-camera.xlook - camera.xpos, -camera.ylook - camera.ypos, -camera.zlook - camera.zpos);
glVertex3f(cos(degInRad)*radius,sin(degInRad)*radi us,0);
}
glEnd();
}

bool collison(float xpos, float zpos){


vector<point>::iterator i;
for(i = find.begin(); i < find.end(); i++){


if((xpos >= i-> xmin) && (xpos <= i-> xmax)
&& (zpos >= i-> zmin) && (zpos <= i-> zmax))

return true;
}

return false;
}


float findDis(float x, float z, float x2, float z2){

return sqrt(pow(x2-x,2) + pow(z2-z,2));

}

void birdView(){

camera2.xpos = 10;
camera2.ypos = 30;
camera2.zpos = -10;
camera2.xlook = 10;
camera2.ylook = 0;
camera2.zlook = -10;

}

tonyo_au
05-21-2012, 07:49 PM
I haven't check your code fully but if you mean by "see through my cube" is that from some angles a wall disappears; thisw is usually caused by culling. That is the "back" of as triangle is not rendered. Try using glDisable(GL_CULL_FACE)