? about OpenGL

This is a discussion on ? about OpenGL within the Game Programming forums, part of the General Programming Boards category; Im making a really simple 3d walkaround in OpenGL right now, and am having some problems. I have 3 classes: ...

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738

    Question ? about OpenGL

    Im making a really simple 3d walkaround in OpenGL right now, and am having some problems.

    I have 3 classes:

    Face
    Block
    Map

    A face is simple one face of a block. A block is what it says it is. A map is, (for the moment at least), a 20x20 matrix of blocks.

    Now, here is what I am doing. My face class and block class work perfectly, but I am having problems with the map class. It loads in the map fine, but it does not display it correctly. I will show you the map class (its pretty short...just something simple for now) and then explain it:

    Code:
    struct MAP3D_DTP
    {
    	BLOCK_DTP *myBlock[20][20];
    	ifstream myLoadMap;
    	
    	MAP3D_DTP ( ) 
    	{
    		for(int x = 0; x < 20; x++)
    		{
    			for(int y = 0; y < 20; y++)
    			{
    				myBlock[x][y] = new BLOCK_DTP;
    				myBlock[x][y]->LoadFaces("block.dat");
    			}
    		}
    	}
    	
    	void LoadMap ( char *filename )
    	{
    		myLoadMap.open(filename);
    		int loadNum;
    
    		for(int x = 0; !myLoadMap.eof() && x < 20; x++)
    		{
    			for(int y = 0; !myLoadMap.eof() && y < 20; y++)
    			{
    				myLoadMap >> loadNum;
    				myBlock[x][y]->SetBlockType((BLOCKTYPE_DTP)loadNum);
    			}
    		}
    		myLoadMap.close();
    	}
    
    	void DisplayMap ( void )
    	{
    		for(int x = 0; x < 20; x++)
    		{
    			for(int y = 0; y < 20; y++)
    			{
    				myBlock[x][y]->DisplayFaces();
    				glTranslatef(1.0f, 0.0f, 0.0f);
    			}
    			glTranslatef(-20.0f, 0.0f, 1.0f);
    		}
    	}
    
    };

    As you can see, the map is stored in a 20x20 matrix of blocks. It loads the map perfectly, but does not display it correctly. You can tell by looking at the display function, that it is supposed to display a block, use glTranslatef() to move over a space, display another, etc. until we have displayed 20 blocks. It then uses glTranslatef() to go back 20 spaces on the x-axis, but move 1 space up on the z-axis and start displaying the next row of blocks, and does this until 20 rows of blocks have been displayed.

    However, for some reason it is displaying all of the blocks in the same place. I dont know why. Anybody have an idea? Thanx.
    My Website

    "Circular logic is good because it is."

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    use the variables from the for loops to translate to the right spot

    void DisplayMap ( void )
    {
    for(int x = 0; x < 20; x++)
    {
    for(int y = 0; y < 20; y++)
    {
    glLoadIdentity();
    glTranslatef(x, y, 0.0f);
    myBlock[x][y]->DisplayFaces();
    }
    }
    }

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Wouldn't it be better to do this..

    Code:
    void DisplayMap ( void )
    {
    for(int x = 0; x < 20; x++)
    {
    for(int y = 0; y < 20; y++)
    {
    glPushMatrix();
    glTranslatef(x, y, 0.0f);
    myBlock[x][y]->DisplayFaces();
    glPopMatrix();
    }
    }
    }
    So as not to disturb the matrix (I forget which one, MODELVIEW?), after the routine is done.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    what the heck do push and pop matrix do?
    My Website

    "Circular logic is good because it is."

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It pushes a copy of the current matrix on the stack. So you push the copy, do whatever transformations you want, and then pop the matrix to get back to the original one. I don't know if you are doing compound transformations (by the sounds of it, your not ).
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    using the code all of you gave me all of the blocks are still drawn in one place..

    even if they were drawn in different places, the map would still be wrong because you guys forgot a principle thing about glTranslatef(). It moves from where we last were, not from (0,0,0) every time.
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  3. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 06:57 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM

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