Skip to main content
GameDev.net gamedev.net
🔒 Locked

creating separate objects

Started by drawrer Jan 2, 2007 at 5:34 PM 5 replies 750+ views
Original Post
drawrer
drawrer
I wrote a C program on a mac years ago that creates hundreds unique objects which will be saved as one object in different formats e.g. VRML so they can be used in games etc. I decided to use opengl so that the program would be cross platform. I have been adapting Glutbasics from apples developer site. This uses GL_TRIANGLE_STRIP to draw one object. As it is, when used with my program all of my objects are obviously joined together. What I need is to be able to create the objects as separate entities, which is my immediate problem, so they can be individually selected and the geometry modified. I have looked at Display List in the opengl manuals but can't see how it fits my needs, can someone please point me in the right direction.
Eitsch
Eitsch
how do you render your objects?

// first object
glBegin..
glEnd..

// second object
glBegin..
glEnd...

and maybe you should try GL_TRIANGLES.
drawrer
drawrer
I'm very new to opengl so it may be easier for you to cast your eyes over the code.

http://developer.apple.com/samplecode/GLUTBasics/index.html

the following is from the above which creates and draws one object. It works perfectly for what I need except I need it for a number of objects which are created on the fly. I think I need to know how to rewrite the *polyList = glGenLists (1) routine to draw more than one object. I realize that a complete rewrite of the whole procedure may be necessary.

at the moment what I'm doing is putting my data into vertexPos[index]. Using GL_TRIANGLES won't allow individual object selection which is what I eventually want.

// delete existing list
if (*polyList)
glDeleteLists (*polyList, 1);
if (*lineList)
glDeleteLists (*lineList, 1);
if (*pointList)
glDeleteLists (*pointList, 1);
*polyList = *lineList = *pointList = 0;

if (surface == kCube) // build the standard color cube (disregard color, subdivisions, and xyRatio)
BuildCube (polyList, lineList, pointList, colorScheme);
else {
// build buffers
vertexPos = (recVec*) malloc ((maxI) * (maxJ) * sizeof (recVec));
if (vertexNormal)
free (vertexNormal);
vertexNormal = (recVec*) malloc ((maxI) * (maxJ) * sizeof (recVec));
if (vertexColor)
free (vertexColor);
vertexColor = (recColor*) malloc ((maxI) * (maxJ) * sizeof (recColor));
if (vertexTexCoord)
free (vertexTexCoord);
vertexTexCoord = (recTexCoord*) malloc ((maxI) * (maxJ) * sizeof (recTexCoord));
if (!vertexPos || !vertexNormal || !vertexColor || !vertexTexCoord)
return;

// build surface
for (i = 0; i < maxI; i++) {
for (j = 0; j < maxJ; j++) {
index = i * maxJ + j;
u = -PI + (i % maxI) * TWOPI / maxI;
v = -PI + (j % maxJ) * TWOPI / maxJ;
vertexPos[index] = Eval(u,v, surface);
p1 = Eval(u + delta, v, surface);
p2 = Eval(u, v + delta, surface);
vertexNormal[index] = CalcNormal(vertexPos[index],p1,p2);
vertexColor[index] = getColor(u, -PI, PI, colorScheme);
vertexTexCoord[index].s = (float) i * 5.0f / (float) maxI;
vertexTexCoord[index].t = (float) j * 1.0f/ (float) maxJ;
}
}

*polyList = glGenLists (1);
glNewList(*polyList, GL_COMPILE);
for (i=0; i< maxI; i++) {
glBegin(GL_TRIANGLE_STRIP);
for (j = 0; j <= maxJ; j++) {
index = (i % maxI) * maxJ + (j % maxJ);
glColor3fv (&vertexColor[index].r);
glNormal3fv (&vertexNormal[index].x);
glTexCoord2fv (&vertexTexCoord[index].s);
glVertex3fv (&vertexPos[index].x);

index = ((i + 1) % maxI) * maxJ + (j % maxJ);
glColor3fv (&vertexColor[index].r);
glNormal3fv (&vertexNormal[index].x);
glTexCoord2fv (&vertexTexCoord[index].s);
glVertex3fv (&vertexPos[index].x);
// index = ((i - 1) % maxI) * maxJ + (j % maxJ);
}
glEnd ();
}
glEndList ();
Rasmadrak
Rasmadrak
everything you put inside a displaylist will be drawn when you call this list.

I'd simply do this:

Setup displaylist
new list
draw first object
end list
setup display list
new list
draw second object
end list.

then either create another displaylist which calls the other lists, or call them separatly.

If you created the first program, I'm pretty sure that this would be a walk in the park - unless the triangles in VRML is a trianglesoup?
"Game Maker For Life, probably never professional thou." =)
drawrer
drawrer
I didn't create it, I'm certainly not that familiar with opengl

Thanks I shall try your suggestions and will no doubt get back to you when I get stuck.
zedzeek
zedzeek
u say on the fly
display lists are for things that are static eg the ground, a static tree etc.
if u wanna change something every frame dont use a list
also that code uses a lot of mem allocations whiich aint optimal
anyways ignore what i just said

// easy method
in a preprocessing phase setup the objects in identity space calc the verts + save them in a displaylist

during runtime use
glPushMatrix();
glMultMatrix( orientation );
glCallList(..);
glPopMatrix();

easy + fast
drawrer
drawrer
thanks I'll let you know how I get on

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.