Archived

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

good class use?

This topic is 5015 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

is this a good class for making a 1 side triangle in OpenGL:
class GLTriangle
{

	public:
		
		GLTriangle();
		void CreateGLTriangle( float, float, float, float, float, float, float, float, float);
		void DisplayGLTriangle();

	private:

		float point1x;
		float point1y;
		float point1z;
		float point2x;
		float point2y;
		float point2z;
		float point3x;
		float point3y;
		float point3z;

};

void GLTriangle::DisplayGLTriangle()
{

	glBegin(GL_TRIANGLES);		
		glVertex3f( point1x, point1y, point1z);
		glVertex3f( point2x, point2y, point2z);
		glVertex3f( point3x, point3y, point3z);
	glEnd();

}

void GLTriangle::CreateGLTriangle(float a, float b, float c, float d, float e, float f, float g, float h, float i)
{
	
	point1x = a;
	point1y = b;
	point1z = c;
	point2x = d;
	point2y = e;
	point2z = f;
	point3x = g;
	point3y = h;
	point3z = i;		
	
}

GLTriangle::GLTriangle()
{
	
	point1x = 0.0;
	point1y = 0.0;
	point1z = 0.0;
	point2x = 0.0;
	point2y = 0.0;
	point2z = 0.0;
	point3x = 0.0;
	point3y = 0.0;
	point3z = 0.0;
	
}
and then I have CreateGLTriangle to make points, DisplayGLTriangle to show tha triangle and it compiles and works fine here is it in use:
      
int DrawGLScene(GLvoid)							
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer

	glLoadIdentity();

	GLTriangle test;

	glTranslatef(-1.5f,0.0f,-6.0f);
	
	test.CreateGLTriangle( 0.0, 1.0, 0.0, -1.0,-1.0, 0.0, 1.0, -1.0, 0.0);
	test.DisplayGLTriangle();
	return TRUE;							
}
now a few questions, is this a fast, good way to create 1 sided triangle, I am using the nehe tutorials and i see the they use:
      
glBegin(GL_QUADS);						
	glVertex3f(-1.0f, 1.0f, 0.0f);				
	glVertex3f( 1.0f, 1.0f, 0.0f);				
	glVertex3f( 1.0f,-1.0f, 0.0f);				
	glVertex3f(-1.0f,-1.0f, 0.0f);				
glEnd();	
{/source]


<SPAN CLASS=editedby>[edited by - 3dmodelerguy on March 25, 2004 2:32:04 PM]</SPAN>

Share this post


Link to post
Share on other sites
and in mine, I don''t have the -1.0f just -1.0 with out the f, now is that fine and just as fast or will the slow down or create problems later?

this is my first time with OpenGL so tell me if this is a good way of creating a 1 side triangle with a class for like a game engine and if there is any way to improve proformance, then pless tell me.

Thank You.

Share this post


Link to post
Share on other sites
The f is just an explicit hint that the literal is float (as opposed to double), but most compilers are smart enough to know that if you are assigning a floating point literal to a float variable, even if it doesn''t have the f qualifier, it''s still a float, not a double. Shouldn''t be a problem.

As for your triangle class, it will certainly work, but it probably isn''t the best way. Optimal performance in drawing geometry is not accomplished through immediate mode calls (sequences of glVertex() calls) using individualized variables, but rather through drawing from arrays of vertex data, which can also be compiled into vertex buffers for possibly increased performance. Even if you continue to use immediate mode calls, it would probably be best to do so from arrays of vertex data, especially the more geometry you must process.

Geometry handling in general is handled best through arrays of vertex data, rather than as individualized, separately named variables such as point1x, point1y, etc... Compress such data into an array of vertices, to facilitate loading and manipulation.

Using arrays of vertex data enables you to load, render, transform, etc... in batches using operations that handle arrays, rather than having to feed the data to the operation one variable at a time.

Vertex data represented as arrays can be handled in 2 basic formats: interleaved or non-interleaved. In an interleaved representation, each vertex is represented by an aggregate (structure or class) that contains all relevant vertex data, maybe something like:

struct Vertex
{
float x,y,z;
float r,g,b;
float u,v;
};

A single triangle, then, could be comprised of an array of Vertex:

struct Triangle
{
Vertex Points[3];
};

Then, for example, if you have to load a triangle from a file the operation become a simple iteration through the array, loading each vertex, rather than a more complex operation of loading each individual point component. Also, drawing is as simple as setting the array using glInterleavedArrays() to set the array as your source.

Non-interleaved arrays store each component of a vertex (vertex, color, texture coords, etc...) as separate arrays, and specify the component sources using glTexCoordPointer, glVertexPointer, etc... There is actually very little difference between the two methods, though there may be performance differences between cards, depending on quality of interleaved support.

Even this isn''t optimal for meshes composed of triangles, since it encapsulates a triangle into a unit, and triangles which share edge data each maintain their own local copy of shared vertex data, increasing the number of redundant transformations needing to be performed per vertex. An even better method is to maintain an array that holds all unique vertices in the entire mesh, and triangles are drawn in batches using glDrawElements() or glDrawArrays(). Individual triangles then, rather than maintaining a local copy of a vertex''s data, maintains instead an index into the list of vertices. This way, vertices can be manipulated and stored only once, but shared among many triangles.

I recommend that you should try to organize your vertex data so that it can be manipulated very rapidly in batches, large chunks at a time. Breaking it up into individual point1, point2 type variables is not the best way of doing so.

Just my opinion...


Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller

Share this post


Link to post
Share on other sites