PDA

View Full Version : Why do my objects keep disappearing?



04-09-2002, 01:13 PM
I've been working on this project for a while and have recently encountered a few problems. I decided to implement object selection. I've only just implemented a very basic picking system, but un doing so I redesigned the structure of my code.

The problem that has now come up is that you can only view the scene when you're moving around, using the cursor keys, but as soon as you stop moving the objects all disappear.

I don't know if I'm missing something really stupid, which is probably the case, or if there is something drastically wrong. I've included some of my code below.

I'd really appreciate any help or notes from anyone.

Cheers

Mike

void orient(float ang)
{

lx = sin(ang);
lz = -cos(ang);
glLoadIdentity();
gluLookAt(jx, jy, jz,
jx + lx,jy + ly,jz + lz,
0.0f,1.0f,0.0f);
}


void moveFlat(int i)
{

jx = jx + i*(lx)*0.1;
jz = jz + i*(lz)*0.1;
glLoadIdentity();
gluLookAt(jx, jy, jz,
jx + lx,jy + ly,jz + lz,
0.0f,1.0f,0.0f);
}

void moveVertically(int i)
{
jy = jy + i*(0.1);
glLoadIdentity();
gluLookAt(jx, jy, jz,
jx + lx,jy + ly,jz + lz,
0.0f,1.0f,0.0f);
}

void renderScene()
{
drawGO();
drawDO();

}

void setupLight()
{
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, GlobalLightAmbient) ;
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0) ;


glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light
glEnable(GL_LIGHT1); // Enable Light One

glEnable(GL_LIGHTING);
}

void setupCamera(int c_pickmode,int c_x,int c_y)
{

GLint viewport[4];

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (c_pickmode == TRUE) {
glGetIntegerv(GL_VIEWPORT,viewport); // Get the viewport bounds
gluPickMatrix(c_x,viewport[3]-c_y,3.0,3.0,viewport);
}

gluPerspective(70.0, // Field of view
1.0, // aspect ratio
0.1,1000.0);
glLightfv(GL_LIGHT0, GL_AMBIENT, GlobalLightAmbient);
glMatrixMode(GL_MODELVIEW);
if (deltaMove) {
moveFlat(deltaMove);
}
if (deltaAngle) {
angle += deltaAngle;
orient(angle);
}
if (deltaVert) {
moveVertically(deltaVert);
}
}

void CreateEnvironment(void)
{
glClearColor(0.0f, 0.0f, 0.7f, 0.5f); //Background
glEnable(GL_DEPTH_TEST) ;
glEnable(GL_NORMALIZE) ;
glShadeModel(GL_SMOOTH) ;
glDisable(GL_CULL_FACE) ;
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
setupCamera(FALSE,0,0);
setupLight();
renderScene();
glPopMatrix();

glutSwapBuffers();
}

void HandleIdle(void)
{

glutPostRedisplay();
}

void HandleVisibility(int visible)
{
if (visible == GLUT_VISIBLE)
glutIdleFunc(HandleIdle);
else
glutIdleFunc(NULL);
}

void pressKey(int key, int x, int y)
{

switch (key)
{
case GLUT_KEY_LEFT : deltaAngle = -0.01f;break;
case GLUT_KEY_RIGHT : deltaAngle = 0.01f;break;
case GLUT_KEY_UP : deltaMove = 1;break;
case GLUT_KEY_DOWN : deltaMove = -1;break;
case GLUT_KEY_PAGE_UP : deltaVert = 1;break;
case GLUT_KEY_PAGE_DOWN: deltaVert = -1;break;

}
}

void releaseKey(int key, int x, int y)
{

switch (key)
{
case GLUT_KEY_LEFT :
case GLUT_KEY_RIGHT : deltaAngle = 0.0f;break;
case GLUT_KEY_UP :
case GLUT_KEY_DOWN : deltaMove = 0;break;
case GLUT_KEY_PAGE_UP :
case GLUT_KEY_PAGE_DOWN : deltaVert = 0;break;
}
}

void processNormalKeys(unsigned char key, int x, int y)
{

if (key == 27)
exit(0);
}

void mouse(int button, int state, int x, int y)
{

int maxselect = 100,nhits = 0;
GLuint selectlist[100];


if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {


glSelectBuffer(maxselect,selectlist);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);

glPushMatrix();
setupCamera(TRUE,x,y);
renderScene();
glPopMatrix();

nhits = glRenderMode(GL_RENDER);

if (nhits < 0){

}

if (nhits > 0) {
/*
Process the hits
*/
ShowWindow(aboutwin,SW_SHOW);
}
}

}

int WINAPI WinMain( HINSTANCE hInstance, // Instance
HINSTANCE hPrevInstance, // Previous Instance
LPSTR lpCmdLine, // Command Line Parameters
int nCmdShow) // Window Show State
{

glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,360);
glutCreateWindow("PIT Version 1.0");


glutDisplayFunc(display);
glutVisibilityFunc(HandleVisibility);


glutIgnoreKeyRepeat(1);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(releaseKey);
glutMouseFunc(mouse);

CreateEnvironment();

glutMainLoop();


return 0;
}

zeckensack
04-09-2002, 01:39 PM
Without movement keys being depressed, all your IFs at the end of setupCamera will fail. Try taking the glLoadIdentity and gluLookAt calls (they are all identical) out of the movement handlers and put them at the end of setupCamera. Might help, maybe not.

PS: edit your post to enclose your code into CODE tags. You can see how it's done when you try to quote me.




nice fixed font size
proper indentation too!


[This message has been edited by zeckensack (edited 04-09-2002).]