Need help with drawing Multiple models?

This is a discussion on Need help with drawing Multiple models? within the Game Programming forums, part of the General Programming Boards category; Hey guys, I've started learning OpenGL through Nehe's tutorial. I was playing around with the code of tutorial # 31 ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    26

    Need help with drawing Multiple models?

    Hey guys,

    I've started learning OpenGL through Nehe's tutorial. I was playing around with the code of tutorial # 31 which basically loads and draws a model. I modfied the code and drew a chessboard and i'm having 2 problems. First, i tried to draw multiple models but every time when i run the program only one of the Models is drawn. It seems the second Models doesn't apper or maybe something else which i don't know how to solve.
    Second, I can't draw my chess piece on the chess board. The chess piece model is always drawn behind the chess board.
    Here is the code for reference:
    Code:
    pModel -> drawChessBoard();	// Draw The chess board	
    	// Draw The Models	
    	glTranslatef(-270.0f, -50.0f, -640.0f);		
    	bPawn -> draw();
    	glTranslatef(-350.0f, -50.0f, -640.0f);		
    	wPawn -> draw();

    I'm beginner please someone help me? I would really apperciate. Thanks in advance

    Regards,

    Salu

  2. #2

    Join Date
    May 2005
    Posts
    1,041
    Try this, tell me if it works. Otherwise post more code/information (the model's drawing routine would be nice).

    Code:
    glPushMatrix();
    pModel -> drawChessBoard();	// Draw The chess board	
    glPopMatrix();
    	// Draw The Models	
    glPushMatrix();
    	glTranslatef(-270.0f, -50.0f, -640.0f);		
    	bPawn -> draw();
    glPopMatrix();
    
    glPushMatrix();
    	glTranslatef(-350.0f, -50.0f, -640.0f);		
    	wPawn -> draw();
    glPopMatrix();
    I'm not immature, I'm refined in the opposite direction.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    26

    Smile

    Thanks for the reply now i can draw multiple models but my 2nd problem is still not solved . I still can't draw the model(s) on the chess board. They are always drawn behind the board. Any other ideas? I would really Apperciate the help.

    BobMcGee123 bro if you need the code to how to draw a model check below website tutorial # 31: http://nehe.gamedev.net/

  4. #4

    Join Date
    May 2005
    Posts
    1,041
    Post a screenshot of what you mean by behind the chessboard, I think you just need to alter the coordinates.
    I'm not immature, I'm refined in the opposite direction.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    26
    Here are: the screen shot and the code of how the chessboard is drawn, the coordinates and other stuff. In the screen shot there are 2 models the brown we can see but the blue one is drawn behind the chessboard. I'm keeping it's one part outside so you can see what do i mean?

    Code:
     
    void Model:: initChessBoardBStripes(GLfloat x, GLfloat y) {
    	
       glLoadIdentity();	
       glPushMatrix();
    		
           glTranslatef(-4.0f + x, -4.0f + y, 0.0f);		
           texture[0] = LoadGLTexture("Data/black.bmp" );	
    		
           glBegin(GL_TRIANGLE_STRIP);
    	glTexCoord2f(0.0f, 0.0f);glVertex3f(0.0f, 0.0f, -12.0f);
    	glTexCoord2f(1.0f, 0.0f);glVertex3f(1.0f, 0.0f, -12.0f);
    	glTexCoord2f(0.0f, 1.0f);glVertex3f(0.0f, 1.0f, -12.0f);
    	glTexCoord2f(1.0f, 1.0f);glVertex3f(1.0f, 1.0f, -12.0f);
            glEnd();
    			
       glPopMatrix( );	
    }
    
    void Model:: initChessBoardWStripes(GLfloat x, GLfloat y) {
    	
       glLoadIdentity();	
       glPushMatrix();
       
              glTranslatef(-3.0f + x, -4.0f + y, 0.0f);		
              texture[0] = LoadGLTexture("Data/white.bmp" );
    		
              glBegin(GL_TRIANGLE_STRIP);
    	glTexCoord2f(0.0f, 0.0f);glVertex3f(0.0f, 0.0f, -12.0f);
    	glTexCoord2f(1.0f, 0.0f);glVertex3f(1.0f, 0.0f, -12.0f);
    	glTexCoord2f(0.0f, 1.0f);glVertex3f(0.0f, 1.0f, -12.0f);
    	glTexCoord2f(1.0f, 1.0f);glVertex3f(1.0f, 1.0f, -12.0f);
               glEnd();
    			
       glPopMatrix();	
    }
    
    void Model:: drawChessBoard() {
    		
       for (int i = 1; i < 65; i++) {
    	 if (i <= 8) {
    	    if (i % 2 == 0)		
    		initChessBoardWStripes(0.0f + i - 2, 0.0f);
    	    else
    		initChessBoardBStripes(0.0f + i - 1, 0.0f);
    
    	} else if (i > 8 && i <= 16) {									
    	    if (i % 2 == 0)		
    		initChessBoardBStripes(1.0f + i - 10, 1.0f);
     	    else
    		initChessBoardWStripes(-1.0f + i - 9, 1.0f);
    
    	}else if (i > 16 && i <= 24) {									
    	    if (i % 2 == 0)		
    		initChessBoardWStripes(0.0f + i - 18, 2.0f);
    	    else
    		initChessBoardBStripes(0.0f + i - 17, 2.0f);
    
    	} else if (i > 24 && i <= 32) {											
    	    if (i % 2 == 0)		
    		initChessBoardBStripes(1.0f + i - 26, 3.0f);
    	   else
    	               initChessBoardWStripes(-1.0f + i - 25, 3.0f);
    
    	}else if (i > 32 && i <= 40) {									
    	   if (i % 2 == 0)		
    		initChessBoardWStripes(0.0f + i - 34, 4.0f);
    	   else
    		initChessBoardBStripes(0.0f + i - 33, 4.0f);
    
    	}else if (i > 40 && i <= 48) {									
    	   if (i % 2 == 0)		
    		initChessBoardBStripes(1.0f + i - 42, 5.0f);
    	   else
    	                initChessBoardWStripes(-1.0f + i - 41, 5.0f);
    
    	}else if (i > 48 && i <= 56) {									
    	   if (i % 2 == 0)		
    		initChessBoardWStripes(0.0f + i - 50, 6.0f);
    	   else
    		initChessBoardBStripes(0.0f + i - 49, 6.0f);
    
    	}else if (i > 56 && i <= 64) {									
    	    if (i % 2 == 0)		
    		initChessBoardBStripes(1.0f + i - 58, 7.0f);
    	    else
    		initChessBoardWStripes(-1.0f + i - 57, 7.0f);
    	}		
        }	
    }
    Thanks in advance!!
    Attached Images Attached Images  

  6. #6
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    I dunno what order you are drawing the pieces in, but try enabling depth testing.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    26
    Quote Originally Posted by Jeremy G
    I dunno what order you are drawing the pieces in, but try enabling depth testing.
    I've already tried that it didn't work. Here is the code Jermey in the main class where everything is drawn.

    Code:
    int DrawGLScene(GLvoid)  {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			// Clear The Screen And The Depth Buffer
            glLoadIdentity();	// Reset The Modelview Matrix
    		
    
           glPushMatrix();
    	pModel -> drawChessBoard();	// Draw The chess board	
           glPopMatrix();
    	
           // Draw The Models	
           glPushMatrix();
    	glTranslatef(-220.0f, -50.0f, -640.0f);		
    	bPawn -> draw();
           glPopMatrix();
    
           glPushMatrix();
    	glTranslatef(-330.0f, -50.0f, -640.0f);		
    	wPawn -> draw();
           glPopMatrix();
    	
    return TRUE;	// Keep Going
    }
    Any other ideas guys pleas help me?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Draw the board first thing.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    26
    Thanks guys i've figured out the porblem. Actually it wasn't that hard. The depth of the board was far less than the depth of the chess piece. so it was always drawn behind the chess board but i've solved it. Thanks though!!

  10. #10

    Join Date
    May 2005
    Posts
    1,041
    Yep it was a coordinate problem !
    I'm not immature, I'm refined in the opposite direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Slow drawing code
    By tjpanda in forum Windows Programming
    Replies: 5
    Last Post: 05-09-2008, 05:09 PM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21