Thread: hmm....terrain wont display

  1. #1
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790

    Terrain Display Problem

    This has been bugging me for a couple weeks, so I finally decided its time to get some input on it. Ok, before i show the code, TERRAIN is and will be my main terrain loading and displaying class, it mainly right now is using GameTutorials base loading from RAW heightmap tutorials code and this problem happened even before i made it into a class.
    :
    Code:
    
    
    #include "main.h"
    
    
    
    
    int TERRAIN::Height(BYTE *pHeightMap, int X, int Y)
    {
    	
    
    	int x = X % MAP_SIZE;					// Error check our x value
    	int y = Y % MAP_SIZE;					// Error check our y value
    
    	if(!pHeightMap) return 0;				// Make sure our data is valid
    
    	
    
    	return pHeightMap[x + (y * MAP_SIZE)];	// Index into our height array and return the height
    }
    
    
    void TERRAIN::SetTextureCoord(float x, float z)
    {
    	
    
    	// Give OpenGL the current texture coordinate for our height map
    	glTexCoord2f(   (float)x / (float)MAP_SIZE,	
    				  - (float)z / (float)MAP_SIZE	);
    }
    
    
    
    
    void TERRAIN::RenderHeightMap(BYTE pHeightMap[])
    {
    	int X = 0, Y = 0;						// Create some variables to walk the array with.
    	int x, y, z;							// Create some variables for readability
    	float fColor = 0.0f;					// Create a variable to hold our color of the polygon
    
    	if(!pHeightMap) return;					// Make sure our height data is valid
    glEnable(GL_TEXTURE_2D);
    	glBindTexture(GL_TEXTURE_2D,g_Texture[0]);
    	glBegin( GL_QUADS );					// Render Quads
    
    
    	for ( X = 0; X < MAP_SIZE; X += STEP_SIZE )
    		for ( Y = 0; Y < MAP_SIZE; Y += STEP_SIZE )
    		{
    			// Get the (X, Y, Z) value for the bottom left vertex		
    			x = X;							
    			y = Height(pHeightMap, X, Y );	
    			z = Y;							
    
    			// Set the color value of the current vertice
    			
    			SetTextureCoord(float(x), float(z));	glVertex3i(x, y, z);			// Send this vertex to OpenGL to be rendered (integer points are faster)
    
    			// Get the (X, Y, Z) value for the top left vertex		
    			x = X;										
    			y = Height(pHeightMap, X, Y + STEP_SIZE );  
    			z = Y + STEP_SIZE ;							
    			
    			
    			SetTextureCoord(float(x), float(z)); glVertex3i(x, y, z);			// Send this vertex to OpenGL to be rendered
    
    			// Get the (X, Y, Z) value for the top right vertex		
    			x = X + STEP_SIZE; 
    			y = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE ); 
    			z = Y + STEP_SIZE ;
    
    			
    			
    			SetTextureCoord(float(x), float(z)); glVertex3i(x, y, z);			// Send this vertex to OpenGL to be rendered
    
    			// Get the (X, Y, Z) value for the bottom right vertex		
    			x = X + STEP_SIZE; 
    			y = Height(pHeightMap, X + STEP_SIZE, Y ); 
    			z = Y;
    
    			
    
    			SetTextureCoord(float(x), float(z)); glVertex3i(x, y, z);			// Send this vertex to OpenGL to be rendered
    		}
    	glEnd();
    
    	glDisable(GL_TEXTURE_2D);
    
    	// Reset the color
    	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    }
    
    
    
    
    void TERRAIN::LoadRawFile(LPSTR strName, int nSize, BYTE *pHeightMap)
    {
    	FILE *pFile = NULL;
    
    	// Let's open the file in Read/Binary mode.
    	pFile = fopen( strName, "rb" );
    
    	// Check to see if we found the file and could open it
    	if ( pFile == NULL )	
    	{
    		// Display our error message and stop the function
    		MessageBox(NULL, "Can't find the height map!", "Error", MB_OK);
    		return;
    	}
    
    	// Here we load the .raw file into our pHeightMap data array.
    	// We are only reading in '1', and the size is the (width * height)
    	fread( pHeightMap, 1, nSize, pFile );
    
    	// After we read the data, it's a good idea to check if everything read fine.
    	int result = ferror( pFile );
    
    	// Check if we received an error.
    	if (result)
    	{
    		MessageBox(NULL, "Can't get data!", "Error", MB_OK);
    	}
    
    	// Close the file.
    	fclose(pFile);
    }
    whats wrong with this code, when i call the function to load the raw file, i dont get any error popup, and no errors at all when i run it, it just wont display. I wonder what i am missing....
    Last edited by EvBladeRunnervE; 05-25-2003 at 07:45 PM.

  2. #2
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    anyone?

  3. #3
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Are you using double buffering mode? If so, are you swapping the buffers after you have drawn the scene? If you are using single buffering, are you making a call to glFlush()?

    Do you have lighting enabled? if so, have you tried rendering the scene with lighting disabled (could be an eroneously set light).

    I suppose the other thing to check is that the camera is set up correctly. Make sure you are looking down the correct axis, ensure that your near and far values (passed to the glFrustum or gluPerspective or glOrtho) are set up correctly. You may be drawing the terrain outside of the clipping volume.

    Do you have any other rendering code that you can show? I can see from this method in the terrain class you are just rendering the terrain, but there is no bufferswap or viewing transformations etc.. so perhaps show the rest of the code if possible.

    There is also no need to disable the 2D texturing at the end of the drawing routine that I can see. It is probably easier to just set that up in your initialisation code and leave it enabled, unless there is some pressing reason to do otherwise.

  4. #4
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    1) double buffering
    2) no lighting
    3) camera works fine with everything else
    4) i narrowed it down to what i showed you, all my other classes, and rendering code displays fine.

    i just dont get why it doesnt work.......

  5. #5
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    bingo, figured it out, i spent 5 minutes looking at it under a new light, i have it set to load up a Raw file based upon a defines MAP_SIZE, which i set to 1024, however,t hat is only the height, i forgot to have the width, which is MAP_SIZE * MAP_SIZE, so i was only getting half my map loaded....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  2. Continous LOD Terrain Source... Released!
    By Perspective in forum Game Programming
    Replies: 13
    Last Post: 04-17-2006, 11:21 PM
  3. New terrain engine
    By VirtualAce in forum Game Programming
    Replies: 16
    Last Post: 03-16-2006, 02:47 AM
  4. Drawing only what you can see (terrain)
    By Eber Kain in forum Game Programming
    Replies: 8
    Last Post: 07-05-2004, 12:19 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM