Sign in to follow this  

Not showing polygons

This topic is 3743 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 have 2 functions in my code that saves the positions of all the polygons to binaryformat (map.fmr) and a load function that reads the coordinates of the polygons from map.fmr. The game shows the polygons if I have not used the load function. The game saves the file successfully and I think it also loads it successfully, BUT when I load the map the polygons does not appear. When I put a command to write polygon's coordinates to a file to check are they right after loading, it shows they are at the same positions as before, but for some reason the polygons are not showing on the screen. Any clues where's the problem, does it have something to do with OpenGL? PS. I'm using C++, SDL+OpenGL

Share this post


Link to post
Share on other sites
1. They should be filled and they do have color saved with the binaryformat.
2. "The normals are facing the wrong way." Hmm what do you mean with this?
3.

Btw, when I saved a boolean inside the map.fmr, after loading it all the coordinates showed -1059372 or something like that. So after that I removed the boolean from it and coordinates showed right, but not displaying.

Share this post


Link to post
Share on other sites
Test your loading system thoroughly - save some data, load it in as a copy, and compare the 2. Are they identical? If not, your problem's there.

You may want to consider sharing some details about your format. I think some data's missing or you're writing things wrongly. For example, there's no reason why a boolean would come out so strangely.

Share this post


Link to post
Share on other sites
The .fmr format is very simple, function SaveMap saves the polygon data to file "map.fmr" and LoadMap loads the data from "map.fmr".
But I'll post the code later when I get home, so maybe it'll make the bug search more easier.

Share this post


Link to post
Share on other sites
Here are the codes for SaveMap() and LoadMap()

SaveMap(char *fileName)
int CGame::SaveMap(char *fileName)
{
int i;
PolygonData pd;

FILE *pFile = fopen(fileName,"wb");

fwrite(&pd, sizeof(PolygonData), 1, pFile);

for (i=0;i<MAX_POLYGON;i++)
{
if (Polygon[i].active)
{
pd.id = i;
pd.pos1X = Polygon[i].pos1X;
pd.pos1Y = Polygon[i].pos1Y;
pd.pos2X = Polygon[i].pos2X;
pd.pos2Y = Polygon[i].pos2Y;
pd.pos3X = Polygon[i].pos3X;
pd.pos3Y = Polygon[i].pos3Y;
pd.color1red = Polygon[i].color1red;
pd.color1green = Polygon[i].color1green;
pd.color1blue = Polygon[i].color1blue;
pd.color2red = Polygon[i].color2red;
pd.color2green = Polygon[i].color2green;
pd.color2blue = Polygon[i].color2blue;
pd.color3red = Polygon[i].color3red;
pd.color3green = Polygon[i].color3green;
pd.color3blue = Polygon[i].color3blue;
}
else
{

}
}

fclose(pFile);
return 1;
}


LoadMap(char *fileName)
int CGame::LoadMap(char *fileName)
{
int i, j;
PolygonData pd;

for (i=0;i<MAX_POLYGON;i++)
Polygon[i].active = false;

cameraVX = 0;
cameraVY = 0;
cameraX = 0;
cameraY = 0;
viewX = 0;
viewY = 0;

FILE *pFile = fopen(fileName, "rb");
if (pFile==NULL)
return 0;
fread(&pd, sizeof(PolygonData), 1, pFile);

for (i=0;i<MAX_POLYGON;i++)
{
j = DrawPolygon(i,pd.pos1X,pd.pos1Y,pd.pos2X,pd.pos2Y,pd.pos3X,pd.pos3Y,
pd.color1red,pd.color1green,pd.color1blue,pd.color2red,pd.color2green,pd.color2blue,
pd.color3red,pd.color3green,pd.color3blue);
}

fclose (pFile);
return 0;
}


PolygonData
struct PolygonData
{
int id;

float pos1X;
float pos1Y;
float pos2X;
float pos2Y;
float pos3X;
float pos3Y;

float color1red;
float color1green;
float color1blue;
float color2red;
float color2green;
float color2blue;
float color3red;
float color3green;
float color3blue;
};


[Edited by - NukeCorr on September 19, 2007 8:13:29 AM]

Share this post


Link to post
Share on other sites
Neither of those functions look like they are doing what you want them to.

SaveMap just opens a file, writes out an uninitialized PolygonData to it, and then loops through all your polygons in the Polygon array, setting that PolygonData's members to values from each polygon, and then does nothing with it and just moves onto the next polygon, overwriting what you just set in the PolygonData, finally ending with closing the file. In other words, SaveMap just writes a single (garbage) PolygonData to file and does nothing else.

LoadMap tries to read a single PolygonData from a file, and then appears to draw that single polygon MAX_POLYGON times. I don't think that's what you want to do, and drawing should probably be separate from loading.

Writing/reading a class/struct directly like that isn't exactly a safe or portable method for saving/loading something. Different compiler options may even change the output. I suggest looking into proper serialization.

And since you are writing this in C++, you should actually use C++. Prefer std::string over char* and use C++ I/O streams instead of C's FILE routines.

Share this post


Link to post
Share on other sites
Woah!
Thanks for help Kalidor, this was very helpful. I'm no coding-god, so I had to try some kind of saving method :P

EDIT: I found the only problem that disappeared my polygons, it was:
	for (i=0;i<MAX_POLYGON;i++)
Polygon[i].active = false;


And if Polygon.active is false it doesn't draw them on screen, what I was thinking when I wrote that code :S

It's working normally now.

Share this post


Link to post
Share on other sites

This topic is 3743 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.

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