PDA

View Full Version : Drawing multiple models and texturing?



Squishy
02-27-2012, 09:42 AM
Hey all :)

I'm new here and while I've done some openGL stuff before, there are a couple of basic things I fail to understand still :( haha

I have a program that reads in a .obj file and stores all the object groupings together as a modelObject made up of multiple entities, where entities can be something about the model that moves independently of the other model parts(eg a tanks turret rotating at the top of the tank), and it should display each entity per modelObject when i run the draw for that MO (abbreviated because i dont wanna have to keep typing it out :P) by running a draw in the entity class, hope youre with me so far :)

what it is doing however is drawing only the turret which is the last one stored from the model, so my thought is its doing something along the lines of overwriting the last thing it drew, which is what i dont understand, uni never really explained how i should do this to let me draw multiple things :p

my other question is about texturing but thats no good until i have the model drawing eh :)

heres the code i use in display (im using JOGL, but ignore the gl. and you have plain openGL :)):

gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glPushMatrix();

//gl.glRotatef(rotate, 1.0f, 1.0f, 0.0f);

gl.glColor3f(0.0f, 0.0f, 0.0f);
one.draw(gl);
gl.glPopMatrix();

Heres the code for draw in modelObject:

public GL draw(GL gl)
{
gl.glMatrixMode(gl.GL_MODELVIEW);

for(int i = 0; i < entities.size(); i++)
{
entities.get(i).draw(gl);
}

return gl;
}

And heres the draw code for entity:

public void draw(GL gl)
{
//gl.glBindTexture(GL.GL_TEXTURE_2D, texture);

for(int i = 0; i < facets.size(); i++)
{
Vertex[] v = facets.get(i).getVerts();

//gl.glMatrixMode(gl.GL_MODELVIEW);
//gl.glPushMatrix();

gl.glColor3f(1.0f, 1.0f, 1.0f);

gl.glBegin(GL.GL_TRIANGLES);
gl.glNormal3f(v[0].nmX, v[0].nmY, v[0].nmZ);
gl.glVertex3f(v[0].posX, v[0].posY, v[0].posZ);
gl.glNormal3f(v[1].nmX, v[1].nmY, v[1].nmZ);
gl.glVertex3f(v[1].posX, v[1].posY, v[1].posZ);
gl.glNormal3f(v[2].nmX, v[2].nmY, v[2].nmZ);
gl.glVertex3f(v[2].posX, v[2].posY, v[2].posZ);
gl.glEnd();

//gl.glPopMatrix();
}
}

sorry this is so long :(, its been bugging me for a couple of days though heh :)

BionicBytes
02-27-2012, 10:43 AM
I know nothing about JOGL but from what you have written I assume it supports legacy OpenGI - i.e. the compatibility profile where all the decreciated API calls are still supported.

You code looks reasonable to a point (we won't go into optimisations/performance). What I don't see is any glTranslatef command for each part of the model - unless these are set once for the whole object.


what it is doing however is drawing only the turret which is the last one stored from the model
The problem may lie somewhere else - like how you fetch the verticies from the model object.

Squishy
02-27-2012, 10:51 AM
hey :), and jogl is just java with the openGL libraries yeah, not sure what the last of what you said meant heh

and it may not be the most optimised i know, but im gonna try and clean that up a bit when i can get this drawing

and about the translate thing, the modelObject is just one model file basically, only broken up to support object grouping from blender, and each entity draws itself and with them being one model they should all be in the right place anyway, i did originally have each model as an entity and it drew the entire model correctly, and now with the model being separated, each entity draws itself and so should still work

heres my draw code, not sure if it will help:

gl.glColor3f(1.0f, 1.0f, 1.0f);

gl.glBegin(GL.GL_TRIANGLES);
gl.glNormal3f(v[0].nmX, v[0].nmY, v[0].nmZ);
gl.glVertex3f(v[0].posX, v[0].posY, v[0].posZ);
gl.glNormal3f(v[1].nmX, v[1].nmY, v[1].nmZ);
gl.glVertex3f(v[1].posX, v[1].posY, v[1].posZ);
gl.glNormal3f(v[2].nmX, v[2].nmY, v[2].nmZ);
gl.glVertex3f(v[2].posX, v[2].posY, v[2].posZ);
gl.glEnd();

Squishy
02-27-2012, 11:12 AM
oh wait, i already posted that :S sorry haha, and ive found my model does all read in apart from one section of the tank :p but ill get on that at some point anyway, but i still cant see why it isnt drawing, do i even need the push and pop matrix calls? and what are they for?

i think i understand the majority of this but then not some basic understanding, is what i have enough? or is there something really important missing? its more the explanation im after, ive found a load of examples but they dont explain why i need to do what they say

Squishy
02-28-2012, 07:39 AM
anyone got any more ideas? ive tried removing the push and pop things and all manner of different things i thought could fix this :( it still only draws the turret, and about the translate thing bionicbytes mentioned, i will have it translate once for the entire model as their positions will always be grouped together, the only difference will be the rotation applied to the turret and any other mobile parts

Squishy
02-28-2012, 11:30 AM
ok i got that sorted now, problem was that i was clearing the array of facets which, in java will clear the list of facets stored for the previous entity read in... therefore it drew the turret like 3 times... rofl

so, anyone know where i should begin with texturing? any resources i would find useful :)