PDA

View Full Version : 3D Rendring



Pushkar
12-17-2010, 12:07 PM
Hi, I was trying to convert my 2D code into 3D and I am facing a problem in that.

The particle simulation that I have shows only particles that are in the lane z=0 and if I change the z co-ordinates, they become invisible.

Here is my code for rendering. What could be the problem ?

************************************************** ****

void initializeOpenGLandGLUT( int argc, char** argv )
{
// Initialize GLUT
glutInit(&argc,argv);
// glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(g_display_controller.getWindowW idth(),g_display_controller.getWindowHeight());
glutCreateWindow("Forty One Sixty Seven Sim");

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glShadeModel(GL_SMOOTH);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutIdleFunc(idle);

// Initialize OpenGL
reshape(g_display_controller.getWindowWidth(),g_di splay_controller.getWindowHeight());
glClearColor(g_bgcolor.r, g_bgcolor.g, g_bgcolor.b, 0.0);

}



// TODO: Move these functions to scene renderer?
void setOrthographicProjection()
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glOrtho(0, g_display_controller.getWindowWidth(), 0, g_display_controller.getWindowHeight(), 0, 512);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

assert( renderingutils::checkGLErrors() );
}

void renderBitmapString( float x, float y, float z, void *font, std::string s )
{
glRasterPos3f(x, y, z);
for( std::string::iterator i = s.begin(); i != s.end(); ++i )
{
char c = *i;
glutBitmapCharacter(font, c);
}

assert( renderingutils::checkGLErrors() );
}

void drawHUD()
{
setOrthographicProjection();
glColor3f(1.0-g_bgcolor.r,1.0-g_bgcolor.g,1.0-g_bgcolor.b);
renderBitmapString( 4, g_display_controller.getWindowHeight()-20, 0.0, GLUT_BITMAP_HELVETICA_18, stringutils::convertToString(g_current_step*g_dt) );
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

assert( renderingutils::checkGLErrors() );
}

void display()
{
// glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up viewing transformation, looking down -Z axis
glLoadIdentity();
gluLookAt(0, 0, -9, 0, 0, -1, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
g_scene_renderer->renderScene();

drawHUD();

glutSwapBuffers();

assert( renderingutils::checkGLErrors() );
}


void TwoDimensionalDisplayController::reshape( int w, int h )
{
assert( renderingutils::checkGLErrors() );
// Record the new width and height
m_window_width = w;
m_window_height = h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the coordinate system to achieve the desired zoom level, center
double ratio = (double)h/(double)w;
// gluOrtho2D(m_center_x-m_scale_factor/ratio,m_center_x+m_scale_factor/ratio,m_center_y-m_scale_factor,m_center_y+m_scale_factor);
gluPerspective(65.0, (float)w / h, 1, 1000);

// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Render the scene
glutPostRedisplay();
assert( renderingutils::checkGLErrors() );
}

************************************************** *

Thanks a lot.

Dark Photon
12-17-2010, 04:54 PM
Hi, I was trying to convert my 2D code into 3D and I am facing a problem in that.

The particle simulation that I have shows only particles that are in the lane z=0 and if I change the z co-ordinates, they become invisible.

Here is my code for rendering. What could be the problem ?
...

glOrtho(0, g_display_controller.getWindowWidth(), 0, g_display_controller.getWindowHeight(), 0, 512);
...
gluPerspective(65.0, (float)w / h, 1, 1000);

Couple things. Do you realize that the 0,512 and 1,1000 are the negative values of eye-space Z. That is, z_eye = -2 should pass both of these, but 2 should pass neither.

Also, seems odd to me that when you flip to "render HUD" mode, you neither disable DEPTH_TEST nor clear the depth buffer. You don't want your HUD to depth test against the scene's old Z-buffer, do you?