Teen Programmers Unite  
 

 

Return to forum top

How to set up an array to store 3D data

Posted by CodeRed [send private reply] at February 05, 2002, 10:14:56 PM

Don't think I'm an idiot, I had this working perfectly, until I wanted to include texture data in my data files. What I had originally was this:

float polyData[MAX_TRIANGLES][3][6]; //triangle | vertex | coordinate(3)/Color(3)

--------------------------------------------------------
But now I want to store a texture ID with each triangle,
but not with each vertice. I did this at first:
--------------------------------------------------------

float polyData[MAX_TRIANGLES][3][4]; //triangle | vertex | coordinate(3)/Texture ID

-----------------------------------------------------------
but then realized that I was allocating space for a texture
ID for each vertice, when I only need one for each triangle
Here is a sample data file and description of each piece of
data:
-----------------------------------------------------------

//Number of textures used by object
4

//Texture filename and ID to associate it with
Data/Textures/CJH.bmp 0
Data/Textures/CJH2.bmp 1
Data/Textures/CJH3.bmp 2
Data/Textures/CJH4.bmp 3

//Polygon data. Each group of three represents a coordinate
// (distance from center) (x,y,z) and the final value on the
// line is the texture ID for the triangle (each line is
// one triangle)
0 1 0 -1 -1 -1 -1 -1 1 1
0 1 0 1 -1 -1 1 -1 1 2
0 1 0 1 -1 1 -1 -1 1 3
0 1 0 -1 -1 -1 -1 -1 1 4

----------------------------
This is my loading function:
----------------------------

void init()
{
numTriangles = 0;
bool end = false;
int currentTri=0, textureID;
float x,y,z;
char *filename;

ifstream getData;
getData.open(filename);

getData >> numTextures;

for(int i=0; i<numTextures; i++)
{
getData >> filename;
getData >> textureID;

LoadGLTexture(filename, textureID);
}

while(1)
{
for(int currentVert=0; currentVert<3; currentVert++)
{
x=y=z=0;
textureID=0;
getData >> x; getData >> y; getData >> z; getData >> textureID;

if(!(x==0 && y==0 && z==0 && textureID))
{
polyData[currentTri][currentVert][0]=x;
polyData[currentTri][currentVert][1]=y;
polyData[currentTri][currentVert][2]=z;
polyData[currentTri][currentVert][3]=textureID;
}
else end = true;
}

if(end==true || currentTri==MAX_TRIANGLES)break;
else {currentTri++;numTriangles++;}
}
}

-----------------------
Now my drawing function
-----------------------

void draw() //Draw poly *WORKING*
{
glLoadIdentity();

glTranslatef(x,y,z);
glRotatef(rotationX,1,0,0);
glRotatef(rotationY,0,1,0);
glRotatef(rotationZ,0,0,1);
glBegin(GL_TRIANGLES);
for(int currentTri=0;currentTri<numTriangles;currentTri++)
{
glBindTexture(GL_TEXTURE_2D, polyData[currentTri][0][3]);
glVertex3f(polyData[currentTri][0][0],polyData[currentTri][0][1],polyData[currentTri][0][2]);
glVertex3f(polyData[currentTri][1][0],polyData[currentTri][1][1],polyData[currentTri][1][2]);
glVertex3f(polyData[currentTri][2][0],polyData[currentTri][2][1],polyData[currentTri][2][2]);
}
glEnd();

rotationX+=rotationSpeedX;
rotationY+=rotationSpeedY;
rotationZ+=rotationSpeedZ;
}

------------------------------------------------------------
See, even though I am allocating more space than I need in the array, I don't use the extra values and it should still work, but it doesn't, I get a black screen. If anyone can give me a better way to arrange my array or figure out what is wrong with it currently I would appreciate it

Posted by sphinX [send private reply] at February 06, 2002, 03:42:04 AM

would a struct not make a lot more sense (ie make it easier to work) in this instance?

Posted by CodeRed [send private reply] at February 06, 2002, 08:47:05 AM

No, because that is just a small part of a large class

Posted by Psion [send private reply] at February 06, 2002, 04:05:21 PM

structured types still make more sense than ad hoc schemes in pretty much all cases.

Posted by CodeRed [send private reply] at February 06, 2002, 06:43:42 PM

What do you mean by structured types? Is a class not a structured type? Are any of you even familiar with C++ or OGL?

Posted by CodeRed [send private reply] at February 06, 2002, 06:44:38 PM

...or do you mean to replace the array with a struct?

Posted by buzgub [send private reply] at February 06, 2002, 11:16:54 PM

An array of structs makes much more sense (to me, at least) than an array of arrays in this case.

Posted by gian [send private reply] at February 07, 2002, 12:22:27 AM

Amen to arrays of structs.

"Are any of you even familiar with C++ or OGL?"
No, every single one of us have been bullshitting ever since you arrived here. We all actually use QBasic, except for Psion, because he's really l337 and uses VB. You're so smart Hollman, oh, how I wish I could ever have such a firm grasp on C++ as you do... oh wait let me go and read your tutorial...
OH LOOK! As it turns out, I _DO_ have as firm a grasp on C++ as you do (despite whatever snide comments Psion may produce).

Posted by CodeRed [send private reply] at February 07, 2002, 09:48:18 AM

I thought you meant replacing the array with a struct, which made no sense, but now I understand, thanks for the help

Posted by CodeRed [send private reply] at February 07, 2002, 09:48:29 AM

I thought you meant replacing the class with a struct, which made no sense, but now I understand, thanks for the help

Posted by CodeRed [send private reply] at February 07, 2002, 09:48:58 AM

CLASS, You really should enable message editing

Posted by Psion [send private reply] at February 07, 2002, 01:09:21 PM

CodeRed, it's ironic that you say that, when it was your abuse of editing for non-moderators that forced me to remove it. You reap what you sow. Even if I were to allow it at this point, I'd feel obligated to make sure that at least _you_ couldn't edit messages.

Posted by gian [send private reply] at February 07, 2002, 09:01:35 PM

* gian claps for Psion :-)

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.