Jump to content
  • Advertisement
Sign in to follow this  
nick2price

OpenGL terrain mapping

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

I am doing a program in C++ using opengl and glut libraries. I am trying to produce a terrain. I have produced the code but the window is just black. I dont think i might have linked up my methods very well, or done my main correctly. Would be so greatful if anyone could offer some advice if they know opengl. cheers

#include "stdafx.h"
#include<windows.h>
#include "glut.h"
#include <stdlib.h>
#include <stdio.h>
#include"CTGALoader.h"
#include"CCamera.h"


// Function prototypes...
bool InitializeGL();
void RenderScene();
void Shutdown();

int width=800;
int height=600;


CTGALoader Terrain;        // Terrain texture image.
CTGALoader CubeImage;      // Cube's texture image.

CCamera Camera;            // Camera object used to move around the 3D scene.

void init(void)
{
	glViewport(0, 0, width, height); // resets the viewport to new dimensions.
	glMatrixMode(GL_PROJECTION);     // Sets the projection matrix.
	glLoadIdentity();                // Reset the modelview matrix.


	gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 1000.0f);

	glMatrixMode(GL_MODELVIEW);      // Sets the projection matrix.
	glLoadIdentity();                // Reset the modelview matrix.
}

bool InitializeGL()
{
   glClearColor(0.5f, 0.5f, 1.0f, 1.0f);  // Clear the screen to the specified color.

   glShadeModel(GL_SMOOTH);               // Smooth shading in our scenes.
   glEnable(GL_DEPTH_TEST);               // Enable desth testing for hidden surface removal.
   glEnable(GL_TEXTURE_2D);               // Enable texture mapping.

   // Load the terrain texture.   
   if(!Terrain.LoadTGA("ground.tga")) return false;
   glGenTextures(1, &Terrain.ID);
   
   glBindTexture(GL_TEXTURE_2D, Terrain.ID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

   int type = Terrain.type == 3 ? GL_RGB : GL_RGBA;
   gluBuild2DMipmaps(GL_TEXTURE_2D, type, Terrain.imageWidth, Terrain.imageHeight,
                     type, GL_UNSIGNED_BYTE, Terrain.image);

   // Load the texture that will be displayed on the screen.
   if(!CubeImage.LoadTGA("Cube.tga")) return false;
   glGenTextures(1, &CubeImage.ID);
   
   glBindTexture(GL_TEXTURE_2D, CubeImage.ID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

   type = CubeImage.type == 3 ? GL_RGB : GL_RGBA;
   gluBuild2DMipmaps(GL_TEXTURE_2D, type, CubeImage.imageWidth, CubeImage.imageHeight,
                     type, GL_UNSIGNED_BYTE, CubeImage.image);


   Camera.SetCamera(0.0f, 0.2f, -2.0f, 0.0f, 0.2f, 0.0f, 0.0f, 1.0f, 0.0f);

   // If all went well we return true.
   return true;
}


void RenderScene()
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   // Clears the screen.
   glLoadIdentity();                   //Reset modelview matrix for new frame.

   gluLookAt(Camera.xPos, Camera.yPos, Camera.zPos,
             Camera.xView, Camera.yView, Camera.zView,
             Camera.xUp, Camera.yUp, Camera.zUp);


   if(GetKeyState(VK_UP) & 0x80) Camera.MoveCamera(UP);
   if(GetKeyState(VK_DOWN) & 0x80) Camera.MoveCamera(DOWN);


   if(GetKeyState(VK_LEFT) & 0x80)
      Camera.RotateCamera(LEFT, 0.0f, 1.0f, 0.0f);  // Rotate the camera left.

   if(GetKeyState(VK_RIGHT) & 0x80)
      Camera.RotateCamera(RIGHT, 0.0f, 1.0f, 0.0f);  // Rotate the camera right.

 
   // Draw the cube.
   glBindTexture(GL_TEXTURE_2D, CubeImage.ID);

   // Front face.
   glBegin(GL_QUADS);
      glTexCoord2f(0.0, 0.0); glVertex3f(0.3f, 0.0f, 0.0f);
      glTexCoord2f(1.0, 0.0); glVertex3f(-0.2f, 0.0f, 0.0f);
      glTexCoord2f(1.0, 1.0); glVertex3f(-0.2f, 0.5f, 0.0f);
      glTexCoord2f(0.0, 1.0); glVertex3f(0.3f, 0.5f, 0.0f);
   glEnd();

   // Back face.
   glBegin(GL_QUADS);
      glTexCoord2f(0.0, 0.0); glVertex3f(0.3f, 0.0f, -0.5f);
      glTexCoord2f(1.0, 0.0); glVertex3f(-0.2f, 0.0f, -0.5f);
      glTexCoord2f(1.0, 1.0); glVertex3f(-0.2f, 0.5f, -0.5f);
      glTexCoord2f(0.0, 1.0); glVertex3f(0.3f, 0.5f, -0.5f);
   glEnd();

   // Left face.
   glBegin(GL_QUADS);
      glTexCoord2f(0.0, 0.0); glVertex3f(-0.2f, 0.0f, -0.5f);
      glTexCoord2f(0.0, 1.0); glVertex3f(-0.2f, 0.5f, -0.5f);
      glTexCoord2f(1.0, 1.0); glVertex3f(-0.2f, 0.5f, 0.0f);
      glTexCoord2f(1.0, 0.0); glVertex3f(-0.2f, 0.0f, 0.0f);
   glEnd();

   // Right face.
   glBegin(GL_QUADS);
      glTexCoord2f(0.0, 0.0); glVertex3f(0.3f, 0.0f, -0.5f);
      glTexCoord2f(0.0, 1.0); glVertex3f(0.3f, 0.5f, -0.5f);
      glTexCoord2f(1.0, 1.0); glVertex3f(0.3f, 0.5f, 0.0f);
      glTexCoord2f(1.0, 0.0); glVertex3f(0.3f, 0.0f, 0.0f);
   glEnd();

   // Top face.
   glBegin(GL_QUADS);
      glTexCoord2f(0.0, 0.0); glVertex3f(-0.2f, 0.5f, 0.0f);
      glTexCoord2f(0.0, 1.0); glVertex3f(-0.2f, 0.5f, -0.5f);
      glTexCoord2f(1.0, 1.0); glVertex3f(0.3f, 0.5f, -0.5f);
      glTexCoord2f(1.0, 0.0); glVertex3f(0.3f, 0.5f, 0.0f);
   glEnd();

   // Bottom face.
   glBegin(GL_QUADS);
      glTexCoord2f(0.0, 0.0); glVertex3f(-0.2f, 0.0f, 0.0f);
      glTexCoord2f(0.0, 1.0); glVertex3f(-0.2f, 0.0f, -0.5f);
      glTexCoord2f(1.0, 1.0); glVertex3f(0.3f, 0.0f, -0.5f);
      glTexCoord2f(1.0, 0.0); glVertex3f(0.3f, 0.0f, 0.0f);
   glEnd();
   
   int x = 0, z = 0;
   
   glBindTexture(GL_TEXTURE_2D, Terrain.ID);
   for(int i = 0; i < 10; i++)
      {
		 
         for(int j = 0; j < 10; j++)
            {
			
               glBegin(GL_QUADS);
                  glTexCoord2f(0.0, 0.0); glVertex3f(15.0f + x, -0.01f, 15.0f + z);
                  glTexCoord2f(1.0, 0.0); glVertex3f(10.0f + x, -0.01f, 15.0f + z);
                  glTexCoord2f(1.0, 1.0); glVertex3f(10.0f + x, -0.01f, 10.0f + z);
                  glTexCoord2f(0.0, 1.0); glVertex3f(15.0f + x, -0.01f, 10.0f + z);
               glEnd();

               x += -5;
            }
         x = 0;
         z += -5;
      }
 
   glutSwapBuffers();
}


void Shutdown()
{
   Terrain.FreeImage();
   CubeImage.FreeImage();
   glDeleteTextures(1, &Terrain.ID);
   glDeleteTextures(1, &CubeImage.ID);
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_DOUBLE | GLUT_RGB);
   glutInitWindowSize (width, height); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
 
   init();
   glutDisplayFunc(RenderScene);
   

   glutMainLoop();
   return (0);
}

Share this post


Link to post
Share on other sites
Advertisement
Havent tested the code but you are probably drawing the grid but not seeing it.
Try moving your camera around and see if the 10x10 grid lies somewhere.Also disable back face culling and depth test until you get something on screen you can enable them later on.
You can also simplify your for loops.Instead of using seperate variables like x,z you can just use the fors iterator.
for(int i=0; i<50; i+=5) etc..

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!