Jump to content
  • Advertisement
Sign in to follow this  
danne89

OpenGL I lost my geometry

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

Hi! See what you can make out of this:
#include "terrain.h"
#include <SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>

TerrainBlock::TerrainBlock(const char* path)
{
	char data[5][5];

	FILE* f = fopen(path, "r");
	if (f == NULL)
	{
		printf("Error opening %s\n", path);
		exit(1);
	}
	if (fread(data, 1, 25, f) != 25)
	{
		printf("Coulndt read 25 elements\n");
		exit(1);
	}

	displayList = glGenLists(5);
	for (int base = 0; base < 4; base++)
	{
		printf("displaylist = %d\n", displayList + base);
		glNewList(displayList + base, GL_COMPILE);
		glBegin(GL_TRIANGLE_STRIP);
	
		printf("%d, %d, %d\n", base + 0, data[base + 0][0], 0);
		printf("%d, %d, %d\n", base + 1, data[base + 1][0], 0);
		
		printf("%d, %d, %d\n", base + 0, data[base + 0][1], 1);
		
		printf("%d, %d, %d\n", base + 1, data[base + 1][1], 1);
		
		printf("%d, %d, %d\n", base + 0, data[base + 0][2], 2);
		
		printf("%d, %d, %d\n", base + 1, data[base + 1][2], 2);
		
		printf("%d, %d, %d\n", base + 0, data[base + 0][3], 3);
		
		printf("%d, %d, %d\n", base + 1, data[base + 1][3], 3);
		
		printf("%d, %d, %d\n", base + 0, data[base + 0][4], 4);
		
		printf("%d, %d, %d\n", base + 1, data[base + 1][4], 4);
		
		glColor3f(1.0, 1.0, 1.0);
	
		glVertex3d(base + 0, data[base + 0][0], 0);
		glVertex3d(base + 1, data[base + 1][0], 0);
	
		glVertex3d(base + 0, data[base + 0][1], 1);
	
		glVertex3d(base + 1, data[base + 1][1], 1);
	
		glVertex3d(base + 0, data[base + 0][2], 2);
	
		glVertex3d(base + 1, data[base + 1][2], 2);
	
		glVertex3d(base + 0, data[base + 0][3], 3);

		glVertex3d(base + 1, data[base + 1][3], 3);
	
		glVertex3d(base + 0, data[base + 0][4], 4);
	
		glVertex3d(base + 1, data[base + 1][4], 4);
	
		glEnd();
		glEndList();
	}
}

void TerrainBlock::render()
{
	for (int i = 0; i < 5; i++)
		glCallList(displayList + i);
}


int main(int argc, char** argv)
{
	if(SDL_Init(SDL_INIT_VIDEO) == -1)
    {
        return 1;    
    }
    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE |SDL_OPENGL | SDL_DOUBLEBUF);
    if(screen == NULL)
    {
        return 1;    
    }
	SDL_WM_SetCaption("Hej", "Hej");
	
	TerrainBlock* tb = new TerrainBlock("test.dat");

	int fps = 0, ff = 0;
	float lastframe = 0, thisframe = 0;
	bool running = true;
	SDL_Event event;
	while (running)
	{
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();	
		
		tb->render();
		SDL_GL_SwapBuffers();
		
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
			{
				case SDL_KEYDOWN:
					switch(event.key.keysym.sym)
					{
						case SDLK_q:
						case SDLK_ESCAPE:
							running = false;
						break;
					}
				break;
				case SDL_QUIT:
					running = false;
				break;
			}
		}

		thisframe = SDL_GetTicks();     
		ff++;
		if((thisframe - lastframe) > 1000)
		{
			fps = ff;
			ff = 0;
			lastframe = thisframe;
		}
	}
	
	delete tb;
	
	SDL_Quit();
	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!