Sign in to follow this  
Impen

OpenGL beginners question

Recommended Posts

Hi I have started with NeHe's tutorials on OpenGL and after the first five lessons I decided to do a small experiment on my own. I want to make a very simple "3D-landscape", essentially a symetric 5x5 tile large space with different height values for the Y coordinates. I have managed to draw the tiles side by side (I think) but the height values look completely wrong. I have no idea what I'm doing wrong. Here's my source:
#include <windows.h>
#include <stdio.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <iostream>

void makeTerrain(char* terrain, int size);
void feedTerrain(char* terrain, int size);
static void render(void);
void setColor(float height);

GLfloat angle = 0.0f;

using namespace std;

int p_size = 5;
char p_terrain[5 * 5];

/**
 * Initilizes everything
 */
int main(int argc, char **argv) 
{	
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutCreateWindow("OpenGL Lab");
	glutDisplayFunc(render);
	glutIdleFunc(render);
	
	glMatrixMode(GL_PROJECTION); //Set state "change perspective"
	gluPerspective(	45, //view angle
			1.0, //aspect ratio
		        1.0, //near clip
			200.0 ); //far clip
					
	glMatrixMode(GL_MODELVIEW);
	
	char* terrain = p_terrain;
	int size = p_size;
	makeTerrain(terrain, size);
	
	glutMainLoop();
	
	return 0;
}

/**
 * My render loop
 */
static void render(void) 
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	glLoadIdentity();
	
	glTranslatef(-2.5f, -5.0f, -20.0f);
	
	glPushMatrix();
	glRotatef(angle, 0.0f, 1.0f, 0.0f);
	
	char* terrain = p_terrain;
	int size = p_size;
	
	glBegin(GL_QUADS);
		feedTerrain(terrain, size);
	glEnd();
	
	angle += 0.1f;
	
	glPopMatrix();

	glutSwapBuffers();
}

/**
 * Fill the terrain with Y values
 */
void makeTerrain(char* terrain, int size)
{
			 //1, 2, 3, 4, 5,  This is how the grid should
			 //0, 1, 2, 3, 4,  look like.
			 //0, 0, 1, 2, 3,
			 //0, 0, 0, 1, 1,
			 //0, 0, 0, 0, 0

			 terrain[0 * 0] = 1;
			 terrain[0 * 1] = 2;
			 terrain[0 * 2] = 3;
			 terrain[0 * 3] = 4;
			 terrain[0 * 4] = 5;
			 
			 terrain[1 * 0] = 0;
			 terrain[1 * 1] = 1;
			 terrain[1 * 2] = 2;
			 terrain[1 * 3] = 3;
			 terrain[1 * 4] = 4;
			 
			 terrain[2 * 0] = 0;
			 terrain[2 * 1] = 0;
			 terrain[2 * 2] = 1;
			 terrain[2 * 3] = 2;
			 terrain[2 * 4] = 3;
			 
			 terrain[3 * 0] = 0;
			 terrain[3 * 1] = 0;
			 terrain[3 * 2] = 0;
			 terrain[3 * 3] = 1;
			 terrain[3 * 4] = 1;
			 
			 terrain[4 * 0] = 0;
			 terrain[4 * 1] = 0;
			 terrain[4 * 2] = 0;
			 terrain[4 * 3] = 0;
			 terrain[4 * 4] = 0;
}

/**
 * Draws the "terrain" to openGL.  
 */
void feedTerrain(char* terrain, int size)
{
	int i;
	for(i = 0; i < size-1; i++) 
	{
		int j;
		for(j = 0; j < size-1; j++)
		{
			float x = (float) j;
			float y1 = terrain[(i*size) + j]; //Top left
			float y2 = terrain[(i*size) + (j+1)]; //Top right
			float y3 = terrain[((i+1)*size) + (j+1)]; //bottom right
			float y4 = terrain[((i+1)*size) + j]; //Bottom left
			float z = (float) i; 

			setColor(y1);
			glVertex3f(x, y1, z); 		//Top left
			setColor(y2);
			glVertex3f(x+1, y2, z); 	//Top right
			setColor(y3);
			glVertex3f(x+1, y3, z+1); 	//Bottom right
			setColor(y4);
			glVertex3f(x, y4, z+1); 	//Bottom left
		}
	}
} 

/**
 * Determines the color for a vertex based on its height value.
 */
void setColor(float height)
{
			if(height > 4) {
				glColor3f(1.0f, 0.0f, 0.0f);
			} else if(height > 3) {
				glColor3f(0.0f, 1.0f, 0.0f);
			} else if(height > 2) {
				glColor3f(0.0, 1.0f, 0.0f);
			} else if(height > 1) {
				glColor3f(0.0f, 0.0f, 1.0f);
			} else {
				glColor3f(0.0f, 0.0f, 1.0f);
			}
}
Thanks in advance!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this