PDA

View Full Version : terrain mapping this time, not cube, lol



nick2price
11-19-2008, 07:44 PM
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, &amp;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, &amp;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, &amp;Terrain.ID);
glDeleteTextures(1, &amp;CubeImage.ID);
}

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

init();
glutDisplayFunc(RenderScene);


glutMainLoop();
return (0);
}

nick2price
11-20-2008, 09:05 AM
can anyone see if i am doing anything wrong with my main?