Archived

This topic is now archived and is closed to further replies.

Vendayan

3D Object Classes

Recommended Posts

I''m pretty new to 3D game programming and i was wondering if anyone could give me a basic example of a 3D object class? I really dont need to know all the code in any methds, I just need to have something to give me an idea of how i need to store my objects in model space and worldspace. Please give me as much help as possible, I''m at a real loss here. ~Vendayan

Share this post


Link to post
Share on other sites
Well Im actually trying to learn how to make a 3D engine from the ground up, although I understand most of what needs to be done. Im simply wondering what would be the best way to make the class for objects, and a type of hierarchy for them as well. I know the DirectX functions well enough and Ive read shelves of books full about them, but for some reason I still cant see the best way to set up the class. I also have a good feeling that it may be all that I need to know to actually start some serious work on my app.

~Vendayan

Share this post


Link to post
Share on other sites
Hi,

I think that small static objects aren''t any problem, because you can just load them (Of course, you need to know which file format you want to implement), store them in a vertex array (or vertex buffer, in DX terms) and push them onto you graphics board.

Do you want to use some sorting-techniques such as BSP-Trees, Octrees, etc., or do you want to draw some simple objects in the beginning? Thats a bit more complex...

Share this post


Link to post
Share on other sites
Huh...?

Ok im somewhat new to this, If I have an example of how I should store my primitives in the form of an object I can very easily find my own efficient way of rendering them, I simply need an idea of a class that would let me store me store all the information and some of the methods I will need.

For instance I''m not entirely sure if I should use a VB for each primitive or each object, and for that matter if I should really use them at all. I would also like to include a function for calculating bounding spheres, tht would be called whenever the object would animate itself or be scaled / stretched etc. I am also not entirely sure how I should go about storing objects in model space and moving them into world space.

Like I said, I''m at a complete loss here

~Vendayan

Share this post


Link to post
Share on other sites
That didnt really help much, I looked at the code they had but it was nothing like what im looking for. I am just looking for something that would tell me how to set up the vertex buffers for the objects and how to use them.

I have also been wondering if anyone could answer this simple question for me - If you are defining primitives by indexes then how do you give each vertex all the different texture coordinates it will need for the different primitives that it makes.

~Vendayan

Share this post


Link to post
Share on other sites
The way that I am implementing all of my objects is through a standard Base "Object" class, that includes a Update() function, a Render() function, SendMessage(), etc, as well as it''s own message queue, and other info (what it is, a handle to itself in my object cache, etc). Then, I subclass the base object for each other object. So, we get a CTexture, CVertexBuffer, CSegment (for audio), etc. You could also get "Unit", or "ParticleSystem", etc. An added benefit is that I can store every object in the game as an "Object", and gain in storage efficiency(only have to look in one place to find something).

If you have a vertex, and it shares texture coordinates with other vertices, then you should index them. If it doesnt, you better not index them, or your texture will get funky =P.

Z.

Share this post


Link to post
Share on other sites
This is a very early version of my basic object class. Hopefully you will be able to get an idea out of this.

  
class imObject
{
public:
imObject ();
imObject (float x, float y, float z);
~imObject ();

HRESULT InitGeometry (LPDIRECT3DDEVICE8 pDevice, imLWO &Loader, char *File);

void Translate (float x, float y, float z);
void Translate (D3DXVECTOR3 Offset);
void Rotate (float x, float y, float z);
void Rotate (D3DXVECTOR3 Angle);
void Render (LPDIRECT3DDEVICE8 pDevice);

void ResetTransforms (void);

void SetVelocity (D3DXVECTOR3 NewVelocity); // Linear velocity

D3DXVECTOR3 GetVelocity (void);
void SetAngVelocity (D3DXVECTOR3 NewAngVelocity); // Rotational velocity (in radians?)

D3DXVECTOR3 GetAngVelocity (void);

D3DXVECTOR3 GetPosition (void);

void SetFriction (float NewFriction);
float GetFriction (void);

private:
LPDIRECT3DVERTEXBUFFER8 pVB;
LPDIRECT3DINDEXBUFFER8 pIB;
int NumVertexes, NumFaces;

PositionStruct CurrentPosition;

D3DXVECTOR3 Velocity;
D3DXVECTOR3 AngVelocity;
float Friction;

D3DXMATRIX matTransform;

void DoWorld (LPDIRECT3DDEVICE8 pDevice);
};


This class is going to be changing heaps soon to fit my new object file format and object types. But it will work for a basic object, in fact it does work.

Never eat anything bigger than your own head.

Share this post


Link to post
Share on other sites
okay, i think i know what you want. first, you know each object is made up of vertices. each set of 3 verts makes a poly. that means, you'd have to have your renderer draw each poly of that object. so your class object would first have an array of verts

float *verts[3] x,y,z

i use the * cuz you don't know how many verts the object will have. once you know, you can call "new" to make that many number of vertices. and then you can reference each vertex[3] by your standard array referencing ...

but then you need to know how to draw the triangles that are made up of these verts, so you have an array of polys

int *polys[3]

this is also dynamically allocated. so, each poly (set of 3 ints) would contain an INDEX to the vertex array. that way, when you want to render each triangle, you just loop through each set of 3 vertices via the polys array.

next, you have texture indices. if you had N textures, each poly would have a texture id (one of the N textures). but then, you'd need to know how to map that particular texture onto that poly. which leads to texture coords.

each poly has a set of 3 texture coords, one for each vertex.

float *texcoords[3][2]

this is dynamically allocated to however many polys you got. the reason why each is only a set of 2 coords instead of 3 is because you're taking the x,y value of your bitmap image.

these are the basic class members of your object class. hope it helped and i hope i didnt' waste my time.

a2k



Edited by - a2k on October 20, 2001 1:59:41 AM

Share this post


Link to post
Share on other sites
Thanks guys this is all very helpful, and has got me started, but if anyone has anything else to add please do so. I still dont completely understand how it is that indexing the verts will give me all the texture coordinates I will need per vert. For instance all verts on a cube would need 3 sets of TexCoords am I not right? But if I try to make 3 different verts I am simply wasting space.

~Vendayan

Share this post


Link to post
Share on other sites
no, all verts on a cube would need 2 coords, an x and y referencing where on your bitmap image that vertex is "located"

you know that 3 verts make a poly which is in essence, a plane, which can be mapped onto a 2D surface, and this surface is, you guessed it, the bitmap.

so for a cube would have 8 verts, but 12 polys (cuz each side of the cube consists of 2 polys). if one side of the cube used vertices 0,1,2,3 in your array, then the corresponding poly indices would be something like 0,1,2 for the first face, and 2, 3, 0 for the second face, which makes a quad.

a2k

Share this post


Link to post
Share on other sites
Say for instance that I wanted to render a cube with each face having its own texture applied to it, how would I use 1 set of texture coords then? Each vert would need 3 different sets right, but if i were to do that then im right back at using 24 verts as i would normally without indexing them.

~Vendayan

Share this post


Link to post
Share on other sites