Sign in to follow this  
logank9

Problems with floats

Recommended Posts

main.cpp
#include <iostream>
#include <stdlib.h>
#include <string>

//include OpenGL header files
#include <GL/glut.h>

//include custom header files
// faster system for bidimensional dynamic arrays
#include "biarray.h"

#include <iostream>
#include <stdlib.h> //Needed for "exit" function

//LEVEL EDIT VARS
//size of tiles
float tilesize = 1.0;
//xyz coordinates top-left of the map will be
float maporiginx = 3.0;
float maporiginy = 3.0;
float maporiginz = 100.0;
//2d array of the map
biarray map(100,100);

//when a key is pressed
void handleKeypress(unsigned char key, int x, int y)
{
	switch (key) {
		case 27: // ESCAPE
			exit(0); //exit
	}
}

//rendering
void initRendering() {
	//draw objects based on depth
	glEnable(GL_DEPTH_TEST);
}

//when the window is resized
void handleResize(int w, int h) {
	//convert xyz coordinates to pixel values
	glViewport(0, 0, w, h);
	
	glMatrixMode(GL_PROJECTION); //start setting camera perspective
	
	//camera perspective
	glLoadIdentity(); //reset camera
	gluPerspective(45.0, //The camera angle
				   (double)w / (double)h, //The width-to-height ratio
				   1.0, //how close something is before it's not rendered
				   200.0); //how far something is before it's not rendered
}

/****************************
THE DRAW SCENE FUNCTION

THIS IS THE FUNCTION I

AM HAVING PROBLEMS WITH
****************************/

void drawScene()
{
	float xcoord = 0;
	float ycoord = 0;
	float zcoord = 0;
	
	//Clear information from last draw
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
	glLoadIdentity(); //Reset the drawing perspective
	
	int mpwidth = map.Width();
	int mpheight = map.Height();
	for(int i=0;i<mpwidth;i++)
	{
		xcoord = maporiginx + (tilesize*i);
		ycoord = maporiginy;
		zcoord = maporiginz + map.Get(xcoord,ycoord);
		std::cout << xcoord << ", " << ycoord << ", " << zcoord << std::endl;
		/*********************/
		/* top left triangle */
		glBegin(GL_TRIANGLES);
		glVertex3f((xcoord), (ycoord), (zcoord));
		glVertex3f((xcoord+tilesize), (ycoord), (zcoord));
		glVertex3f((xcoord), (ycoord+tilesize), (zcoord));
		glEnd();
		/*************************/
		/* bottom right triangle */
		glBegin(GL_TRIANGLES);
		glVertex3f((xcoord+tilesize), (ycoord), (zcoord));
		glVertex3f((xcoord+tilesize), (ycoord+tilesize), (zcoord));
		glVertex3f((xcoord), (ycoord+tilesize), (zcoord));
		glEnd();
		
		for(int b=0;b<mpheight;b++)
		{
			xcoord = maporiginx + (tilesize*i);
			ycoord = maporiginy + (tilesize*b);
			zcoord = maporiginz + map.Get(xcoord,ycoord);
			/*********************/
			/* top left triangle */
			glBegin(GL_TRIANGLES);
			glVertex3f((xcoord), (ycoord), (zcoord));
			std::cout << xcoord << ", " << ycoord << ", " << zcoord << std::endl;
			glVertex3f((xcoord+tilesize), (ycoord), (zcoord));
			std::cout << xcoord+tilesize << ", " << ycoord << ", " << zcoord << std::endl;
			glVertex3f((xcoord), (ycoord+tilesize), (zcoord));
			std::cout << xcoord << ", " << ycoord+tilesize << ", " << zcoord << std::endl;
			glEnd();
			/*************************/
			/* bottom right triangle */
			glBegin(GL_TRIANGLES);
			glVertex3f((xcoord+tilesize), (ycoord), (zcoord));
			glVertex3f((xcoord+tilesize), (ycoord+tilesize), (zcoord));
			glVertex3f((xcoord), (ycoord+tilesize), (zcoord));
			glEnd();
		}
	}
	
	glutSwapBuffers(); //send 3d scene to the screen
}

int main(int argc, char** argv)
{
	
	//Initialize GLUT
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowSize(800, 600); //Set the window size
	
	//Create the window
	glutCreateWindow("Map Editor");
	initRendering(); //Initialize rendering
	
	//Set handler functions for drawing, keypresses, and window resizes
	glutDisplayFunc(drawScene);
	glutKeyboardFunc(handleKeypress);
	glutReshapeFunc(handleResize);
	
	glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
	return 0; //This line is never reached
}









biarray.h
#include <iostream>

/*
 * biarray class was written by
 * M_D_Kodar after about 5
 * failed attempts from me
 */

class biarray
{
	public:
		biarray(int rowsl, int colsl)
		{
			//create an array
			Create(rowsl, colsl);
		};
		
		~biarray()
		{
			//delete the array
			Delete();
		};
		
		void Clear()
		{
			//clear array
			for(int a=0;a<rows;a++)
			{
				for(int b=0;b<cols;b++)
				{
					array[a][b] = 0;
				}
			}
		};
		
		void Resize(int rowsl, int colsl)
		{
			//resize array
			Delete();
			Create(rowsl, colsl);
		};
		
		void Set(int x, int y, int val)
		{
			//set x,y coordinate in the 2d array to a value
			array[x][y] = val;
		};
		
		int Get(int x, int y)
		{
			//get x,y coordinate in the 2d array
			return array[x][y];
		};
		
		int Width()
		{
			return cols;
		};
		
		int Height()
		{
			return rows;
		}
		
	private:
		void Create(int rowsl, int colsl)
		{
			array = (int**)malloc(sizeof(int*) * rowsl);
			for(int i=0;i<rowsl;i++)
			{
				array[i] = (int*)malloc(sizeof(int) * colsl);
			}
			rows = rowsl;
			cols = colsl;
		};
		
		void Delete()
		{
			for(int i=0;i<rows;i++)
			{
				free(array[i]);
			}
		};
		
		int **array;
		int rows,cols;
};









I'm not sure if you will need the 'biarray.h' file, but just in case, there ya go. Alright, so I am trying to make a map editor, and I am having trouble with looping through the map array to load the terrain it gets up to line 96, but then there is a segmentation fault. I'm pretty much a newbie to this stuff, so if someone could explain what that is if I'm wrong: I think it means you try to allocate something to a memory address which does not exist. I don't see any possible way I could be doing this here. It's output is that the for loop continues loops right after i == 96, but the xcoord, etc. stay the same, which is odd, because if I do tilesize*i right after it w/o setting it to a variable or anything, it works, which makes me think it's something wrong with the "float" data type, so I decided to try "int", and just divide by 10 when I set the vertex positions. This didn't work either. Could someone please tell me what I'm doing wrong with my code? And before someone asks: I did try defining xcoord, ycoord, and zcoord variables before the for loop in the function, so yeah. Sorry for the long post, and sorry again if I'm doing something really stupid here! Edit: Sorry if the #include's got messed up, dunno what happened. [Edited by - logank9 on July 25, 2009 2:28:53 PM]

Share this post


Link to post
Share on other sites
Your drawScene() function uses the statement
for(float i=0;i<=mpwidth;i++).
You should NOT use floating point variables as loop counters. Someone who is a c++ guru prolly can explain better than me, but I think it is bad.

Instead discretize your floating point number span and map integer loop count variable to the discrete steps.

Share this post


Link to post
Share on other sites
Alright, I updated my post, now that I look back on it, I'm wondering why I used a float for either of them, an int would have worked just as well.

However, even after I changed the 2 values to int's it still outputs the exact same thing.

Edit:

Also, thanks for the tip, never knew about that :D

Share this post


Link to post
Share on other sites
A few things that struck me as odd by are probably not helpful:

You're printing out maporiginx, maporiginy, and maporiginz instead of xcoord, ycoord, and zcoord.

You're doing a one-dimensional loop, which will print one line of tiles. But xcoord and ycoord keep increasing, so you're making a diagonal line of triangles. You'd need a double loop to make a 2D map out of triangles.

From what I recall of GL_TRIANGLES, you specify vertices and it makes a chain of triangles (so the first two verts do nothing, then the third makes a triangle, the fourth makes a triangle, etc.). So with the code you're describing here, you should be outputting some one-dimensional "triangles", because you have a sequence that puts all three verts on a line:

(x1, y1 + tileSize), (x2, y2), (x2 + tileSize, y2)

where x2 == x1 + tileSize and y2 == y1 + tileSize

Share this post


Link to post
Share on other sites
Yeah, I was just trying to get it pretty much to display something on the screen. I just fixed update the draw scene function, but can you help me on what you are saying?

I thought glVertex3f() took the paramaters x, y, z ?

x is left and right
y is up and down
z is farther away from/closer to the camera/eye

Am I correct on all these?

If so, how am I making a 1D triangle? I set all the points to a different xyz coordinate.

And also, I thought glBegin(GL_TRIANGLES) made it so every 3 vertices you declared it connected them together and made a triangle. please correct me if I am wrong. And also, please tell me how I am making a 1D triangle. I do not understand that part.

Thanks!

Share this post


Link to post
Share on other sites
Bump

I'm still having problems with this if anyone can help. I finally figured out what the segmentation fault was, though:

I outputted the xyz every time I created a vertex, and what I found was:

the left->right coordinates worked fine, but the up and down coordinates were off by a few..

So if I had a 5x5 array, instead it does:

1 2 3 4 5
----------
X X X X X | 1
X X X X X | 2
X X X X X | 3
X X X X X | 4
X X X X X | 5
X X X X X | 6
X X X X X | 7

So I thought it was a problem with the biarray map's y coordinate, but I outputted that, and it was exactly what I set it to.

And so, 'since the program is trying to access an xy coordinate that isn't in the array, it causes a segmentation fault.

these are the last 6 lines of of when I output it:


99, 101, 117
100, 101, 117
99, 102, 117
99, 102, 1.97042e+09
100, 102, 1.97042e+09
99, 103, 1.97042e+09






Everything before that is completely correct. I just don't understand why the first 3 line's z coordinates are 117. I understand the last 3, are memory addresses or something.

Could someone explain what's happening here? I've updated all of my code.

Thanks!

Edit:

I tried subtracting 3 from the y coordinate, but it still gives me a segmentation fault. Atleast it doesn't give me those weird numbers, so I guess that's good. Not sure what else could be causing the segmentation fault.

[Edited by - logank9 on July 25, 2009 3:27:30 PM]

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