Jump to content
  • Advertisement
Sign in to follow this  
ShazanMiah

OpenGL (SOLVED) Change from GL_PROJECTION to GL_MODELVIEW

This topic is 3251 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

Hi, Im new to openGL and I was wondering if someone can provide a link or code to switch between GL_PROJECTION and GL_MODELVIEW. I need to do this because I need to display a loading image as a quad in GL_PROJECTION and then switch to GL_MODELVIEW to go into my 3d world. right now i am just using GL_PROJECTION. i am unsure on how to add GL_MODELVIEW and use it correctly. previous attempts didnt work. either the loading screen got zoomed in or the gamescreen wasnt showing the triangle. heres my code that works
void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glColor3f(1.0, 1.0, 1.0);

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION);	
    glLoadIdentity();
    glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0);

    // missing something here

    // set the screen state to loading screen
    ScreenState = SCREEN_LOADING;

    // load in the textures for the level
    CreateTexture(g_Texture, "Loading.bmp", TEX_LOADING);
}


my draw code is as follows
void display(void)
{
    // clear the color and depth buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // call a draw function depending on the current screen state
    switch (ScreenState)
    {
	case SCREEN_LOADING:	drawLoadingScreen();	break;

	case SCREEN_INGAME:	drawGameScreen();	break;

        default: break;		// do nothing
    }

    // swap the buffers
    glutSwapBuffers();

    glFlush();//Draw everything to the screen
    glutPostRedisplay();//Tell the program to refresh
}


my draw functions are as follows
[source ="cpp"]
// this draws the loading screen
void drawLoadingScreen()
{

    glLoadIdentity();

    // Bind the texture stored at the zero index of g_Texture[]
    glBindTexture(GL_TEXTURE_2D, g_Texture[TEX_LOADING]);

    // Display a quad texture to the screen
    glBegin(GL_QUADS);

        // Display the top left vertice
        glTexCoord2f(0.0f, 1.0f);
        glVertex3f(-10, 10, 0);

        // Display the bottom left vertice
        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(-10, -10, 0);

        // Display the bottom right vertice
        glTexCoord2f(1.0f, 0.0f);
        glVertex3f(10, -10, 0);

        // Display the top right vertice
        glTexCoord2f(1.0f, 1.0f);
        glVertex3f(10, 10, 0);

    glEnd();
}


// this draws the game screen
void drawGameScreen()
{

    glLoadIdentity();

    // modelview should be used here

    // draw a triangle, no real reason
    glBegin (GL_TRIANGLES);
        glVertex3f(0, 1, 0);
        glVertex3f(-1, 0, 0);
        glVertex3f(1, 0, 0);
    glEnd();
}


Any help would be apprecated, as ive been tryin to find something on the net for the last two days and no luck on how to add a loading / splash screen to a 3d opengl app. Thanks Shazan Miah [Edited by - ShazanMiah on October 29, 2009 9:43:57 AM]

Share this post


Link to post
Share on other sites
Advertisement
When drawing 3D, just use glMatrixMode(GL_PROJECTION), then set the 3D projection matrix (probably using gluPerspective), switch to the model-view matrix glMatrixMode(GL_MODELVIEW), and set your 3D transform matrices. Then draw your 3D geometry.

When drawing 2D, switch to GL_PROJECTION, set your ortho matrix and draw 2D.

Share this post


Link to post
Share on other sites
I'm not an OpenGL Guru, but afaik the projection matrix is only for setting the camera, while the modelview is only for rendering shapes.

therefore you should start with creating your 3D view


GL11.glViewport(0, 0, width, height);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
// creates a perspective aka 3D view
GLU.gluPerspective(45.0f, ((float)width) / ((float)height), 0.1f, 1000.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();

// render 3D stuff



after that you can create your 2D view
it might be a good idea to call GL11.glDisable(GL11.GL_DEPTH_TEST); to make sure that all 2D stuff is drawn on top of the 3D


GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glLoadIdentity();
// creates an orthogonal aka 2D view
GL11.glOrtho(0, width, 0, height, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();

// render 2D stuff



after you've rendered your 2D, you can call popMatrix, in order to restore your old perspective view.


GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPopMatrix();



and then remember to call GL11.glEnable(GL11.GL_DEPTH_TEST); to render the 3D models properly.

Just ignore the GL11 and GLU, I'm writing my games in java, s I'm using LWJGL to get the binding to OpenGL.

Share this post


Link to post
Share on other sites
Hi Guys,

Thanks for the posts.

I've decided to just keep my matrix mode as GL_MODELVIEW.

i have tried switching between both GL_PROJECTION and GL_MODELVIEW, but the views always seem to mess up.

I really dont have a clue what im doing. this is for a university project and all we've been given is a couple of demos in win32 and been told to use glut to make our game.

I would like to ask, how can i get the bounds of the window. im thinking of just streching my texture quad to the ends of the screen.

when using glOrtho(), i knew it was -10. now im using gluPerspective and i call

gluLookAt(0, 0, 6, 0, 0, 0, 0, 1, 0);

in both draw functions.

thanks

Share this post


Link to post
Share on other sites
VildNinja gave a pretty good example that will help sort out your matrix mode call problems, I would suggest starting with that call layout because you probably wont be able to ignore one matrix mode in favor of the other if you plan on doing anything worth while.
You use the projection matrix stack to modify how you want the scene projected onto the screen, you use the modelview stack to transform the objects you want to see on the screen. So think about what matrix stack you want to modify for what your trying to do and use it.
gluPerspective() will create a perspective matrix, gluOrtho() will make an orthographic view, youll want to use these with the projection matrix. Before drawing objects youll want to use the modelview matrix stack.

Share this post


Link to post
Share on other sites
Hi Guys,

Thanks for your help, i got it working.

I didn't know i had to call both projection and modelview to display 3d. i pretty much fried my brain last night. understond what i had to do this morning and now its working.

i've got a quick overview of the code below incase anyone else gets stuck like me (hopefully not).

Heres the main draw function
[source="cpp"]
void display(void)
{
// clear the color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// call a draw function depending on the current screen state
switch (ScreenState)
{
case SCREEN_LOADING: drawLoadingScreen(); break;

case SCREEN_INGAME: drawGameScreen(); break;

default: break; // do nothing
}

// swap the buffers
glutSwapBuffers();

glFlush();//Draw everything to the screen
glutPostRedisplay();//Tell the program to refresh
}




Heres the first display
[source="cpp"]
// this draws a 2d quad with texture,
// quad takes up the whole display area
void drawLoadingScreen()
{

// set the view to projection and set the viewport
glMatrixMode(GL_PROJECTION);
glOrtho(-10,10,-10,10,-10,10);
glLoadIdentity();

// code ommited ...
}




Heres the second draw function
[source="cpp"]
// this draws the game screen
// this displays a 3d world
void drawGameScreen()
{

// set the view matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// i dont really know what this does
gluPerspective(45.0f, /* FOV */
(GLfloat)SCREEN_WIDTH / (GLfloat)SCREEN_HEIGHT, /* Aspect Ratio */
.1f , /* zNear */
1000.0f); /* zFar */

// set the modelview
glMatrixMode(GL_MODELVIEW);
// Select The Modelview Matrix
glLoadIdentity();


// code ommited ...
}

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!