Results 1 to 2 of 2

Thread: Whats wrong with this code

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2005
    Location
    India
    Posts
    11

    Whats wrong with this code

    Hai

    I want to draw a background and then some 3D objects. But when i draw the 3D objects my background disappears. Please mention whats wrong with this code

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /****** Drawing Background Texture*******/

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glDisable(GL_DEPTH_TEST);
    glOrtho(0.0f,w,0.0f,h, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    glPushAttrib(GL_LIGHTING_BIT);
    glLoadIdentity();
    glShadeModel(GL_FLAT);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );

    const GLfloat fw((GLfloat) w);
    const GLfloat fh((GLfloat) h);

    glBegin(GL_QUADS);
    glNormal3f( 0.0f, 0.0f, 1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( fw, fh, 0.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(0, fh, 0.0f);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, 0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( fw, 0, 0.0f);
    glEnd();

    glDisable(GL_TEXTURE_2D);
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_MODELVIEW);
    glPopAttrib();
    /*******End Background Texture *********/

    /**********Draw 3D objects and rotation*******/

    glViewport(0 + AutoScrollPosition.X, 0 - (nRange - Height) - AutoScrollPosition.Y, nRange, nRange );

    // Reset projection matrix stack
    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

    glShadeModel(GL_FLAT);
    glDepthFunc(GL_LEQUAL);
    glEnable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

    // Establish clipping volume (left, right, bottom, top, near, far)

    glOrtho ((-nRange/2.0), (nRange/2.0), (nRange/2.0 ), -(nRange/2.0), -(nRange/2.0), (nRange/2.0));

    // Reset Model view matrix stack
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    glRotatef(60,1.0,1.0,0.0);
    glRotatef(45 * rot,0.0,1.0,0.0);

    glScalef(scale3D,scale3D,scale3D);
    DrawObject()

    Whats wrong with this code.

    Please help

    Thanks

  2. #2
    Senior Member Regular Contributor
    Join Date
    Aug 2003
    Posts
    369

    Re: Whats wrong with this code

    Well, for one, I believe glViewport has no right being there. It should be along with the projection code in a resize-reshape method. Also, the problem probably occurs when you reset your projection the second time. So the background texture is lost.
    What you need to do is
    a) create both the background and the 3d objects in one projection volume. You just need to experiment a bit to find out the proper size for the quad etc.

    or b) this is kind of tricky, but I've used it in the past to create text overlays over a 3d world. Maybe someone else remembers how this is done properly. You set the projection matrix as usual, draw your background quad, then push the matrix, change the projection again for your 3D objects, draw them, and when you're finished you pop the matrix. But as I've said this is kind of tricky to get the right order of glMatrixMode, glPush-PopMatrix etc. Good luck!

Similar Threads

  1. Replies: 2
    Last Post: 07-24-2018, 11:30 PM
  2. whats wrong with this code?
    By chrisx16x2008 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 03-12-2009, 08:50 AM
  3. whats wrong with this code?
    By jesh in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 03-01-2003, 05:04 AM
  4. Whats wrong with my code? :(
    By in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 02-17-2001, 09:24 AM
  5. Whats wrong with this bit of code?
    By Schlogenburg in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-01-2000, 01:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean