Jump to content
  • Advertisement
Sign in to follow this  
portable

View Frustum Vertices / Corners

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hei! I want to render the view frustum I created with glFrustum. Thus I'd need all the vertices which define it in world space. My idea was to use the CameraToWorld-Matrix (the inverse of the GL_MODELVIEW_MATRIX in my case) and multiply it with the vertices defined by glFrustum. But this did not work! Does anybody know why? Thanks in advance, Phil

Share this post


Link to post
Share on other sites
Advertisement
Can you post your code? I can think of a couple places where things could be going wrong, but I couldn't say for sure without seeing it.

Share this post


Link to post
Share on other sites
O.k., here we go. The matrix inversion should be correct. I'm using the one provided on the INTEL-Web-Site.


void multMatrixVector( float mat[ 16 ], float vec[ 4 ], float* res )
{
res[ 0 ] = mat[ 0 ] * vec[ 0 ] + mat[ 1 ] * vec[ 1 ] + mat[ 2 ] * vec[ 2 ] + mat[ 3 ] * vec[ 3 ];
res[ 1 ] = mat[ 4 ] * vec[ 0 ] + mat[ 5 ] * vec[ 1 ] + mat[ 6 ] * vec[ 2 ] + mat[ 7 ] * vec[ 3 ];
res[ 2 ] = mat[ 8 ] * vec[ 0 ] + mat[ 9 ] * vec[ 1 ] + mat[ 10 ] * vec[ 2 ] + mat[ 11 ] * vec[ 3 ];
res[ 3 ] = mat[ 12 ] * vec[ 0 ] + mat[ 13 ] * vec[ 1 ] + mat[ 14 ] * vec[ 2 ] + mat[ 15 ] * vec[ 3 ];
}

static void draw_screen( void )
{
static GLint vertices[ 24 ] = { -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, 1, 1,
1, -1, -1, 1, -1, 1, 1, 1, -1, 1, 1, 1 };

static GLuint indices[] = { 0, 4, 6, 0, 6, 2, 3, 7, 5, 3, 5, 1, 2, 3, 1, 2, 1, 0,
4, 5, 7, 4, 7, 6, 3, 2, 7, 2, 6, 7, 0, 1, 4, 1, 5, 4 };

glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_INT, 0, vertices );

glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glTranslatef( 0.0f, 0.0f, sinf( SDL_GetTicks() / 250.0f ) * 8.0f );
glColor3f( 1, 0, 0 );
glDrawElements( GL_TRIANGLES, 36, GL_UNSIGNED_INT, indices );
glPopMatrix();

glPushMatrix();
glTranslatef( 0.0f, 0.0f, 10.0f );
glColor3f( 0, 1, 0 );
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_INT, indices );
glPopMatrix();

glDisableClientState( GL_VERTEX_ARRAY );
}

void draw_camera()
{
float mat[ 16 ];
float inv[ 16 ];

glGetFloatv( GL_MODELVIEW_MATRIX, mat );
invertMatrix( mat, inv );

float c[ 8 ][ 4 ] = { { -2, 2, 1, 1 },
{ 2, 2, 1, 1 },
{ 2, -2, 1, 1 },
{ -2, -2, 1, 1 },
{ -2, 2, 100, 1 },
{ 2, 2, 100, 1 },
{ 2, -2, 100, 1 },
{ -2, -2, 100, 1 } };

int i;
float p[ 8 ][ 4 ];
for ( i = 0; i < 8; i++ )
multMatrixVector( inv, c[ i ], p[ i ] );

glColor3f( 1, 0, 0 );
glBegin( GL_LINES );
glVertex3fv( p[ 0 ] ); glVertex3fv( p[ 1 ] );
glVertex3fv( p[ 1 ] ); glVertex3fv( p[ 2 ] );
glVertex3fv( p[ 2 ] ); glVertex3fv( p[ 3 ] );
glVertex3fv( p[ 3 ] ); glVertex3fv( p[ 0 ] );
glVertex3fv( p[ 4 ] ); glVertex3fv( p[ 5 ] );
glVertex3fv( p[ 5 ] ); glVertex3fv( p[ 6 ] );
glVertex3fv( p[ 6 ] ); glVertex3fv( p[ 7 ] );
glVertex3fv( p[ 7 ] ); glVertex3fv( p[ 4 ] );
glVertex3fv( p[ 0 ] ); glVertex3fv( p[ 4 ] );
glVertex3fv( p[ 1 ] ); glVertex3fv( p[ 5 ] );
glVertex3fv( p[ 2 ] ); glVertex3fv( p[ 6 ] );
glVertex3fv( p[ 3 ] ); glVertex3fv( p[ 7 ] );
glEnd();
}

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

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glViewport( 0, 0, 320, 480 );
gluLookAt( 0, 0, -20, 0, 0, 0, 0, 1, 0 );
draw_screen();

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glViewport( 320, 0, 320, 480 );
gluLookAt( 20, 20, -20, 0, 0, 0, 0, 1, 0 );
draw_screen();
draw_camera();

SDL_GL_SwapBuffers( );
}

Share this post


Link to post
Share on other sites
After a quick look, my first guess is that the matrix indices in the matrix-vector mult function are the transpose of what they should be. You might just comment out the current version and try it with transposed indices, and see if that fixes the problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!