Archived

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

good class use?

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

here is my modified code using arrays instead on single varibles, is this a good way to create a class for diplaying a triangle in OpenGL:
class GLTriangle
{

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

	private:

        float point1[ 3 ];
		float point2[ 3 ];
		float point3[ 3 ];
		double colorvertex[ 3 ];

};

void GLTriangle::SetColor( double r, double b, double g )
{
	
	colorvertex[ 0 ] = r;
	colorvertex[ 1 ] = b;
	colorvertex[ 2 ] = g;
	
}

void GLTriangle::DisplayGLTriangle()
{

	glBegin(GL_TRIANGLES);	

		glColor3f( colorvertex[ 0 ], colorvertex[ 1 ], colorvertex[ 2 ] );
		glVertex3f( point1[ 0 ], point1[ 1 ], point1[ 2 ]);

		glColor3f( colorvertex[ 0 ], colorvertex[ 1 ], colorvertex[ 2 ] );
		glVertex3f( point2[ 0 ], point2[ 1 ], point2[ 2 ]);

		glColor3f( colorvertex[ 0 ], colorvertex[ 1 ], colorvertex[ 2 ] );
		glVertex3f( point3[ 0 ], point3[ 1 ], point3[ 2 ]);

	glEnd();

}

void GLTriangle::CreateGLTriangle(float a, float b, float c, float d, float e, float f, float g, float h, float i)
{
	
	point1[ 0 ] = a;
	point1[ 1 ] = b;
	point1[ 2 ] = c;
	point2[ 0 ] = d;
	point2[ 1 ] = e;
	point2[ 2 ] = f;
	point3[ 0 ] = g;
	point3[ 1 ] = h;
	point3[ 2 ] = i;		
	
}

GLTriangle::GLTriangle()
{
	
	point1[ 0 ] = 0.0;
	point1[ 1 ] = 0.0;
	point1[ 2 ] = 0.0;
	point2[ 0 ] = 0.0;
	point2[ 1 ] = 0.0;
	point2[ 2 ] = 0.0;
	point3[ 0 ] = 0.0;
	point3[ 1 ] = 0.0;
	point3[ 2 ] = 0.0;

}
here is where this class is used
int DrawGLScene(GLvoid)								// Here''s Where We Do All The Drawing

{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		
	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.SetColor( 255, 0, 0 );
	test.DisplayGLTriangle();
	return TRUE;							
}
is this good?

Share this post


Link to post
Share on other sites
It''s not bad.

However - Triangle''s instance should be created somewhere else and the function CreateGLTriangle should be called only once (And - there should be constructor with enough parameters instead).

But as I said - there''s nothing bad with how you wrote it. C++''s advantage is that you can solve one problem in twenty different ways and all of them are correct.

Oxyd

---
- Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don''t.
- Real programmers aren''t afraid of goto

Share this post


Link to post
Share on other sites
Some hints for you:

Rather than using glColor3f you can use also glColor3fv that takes an array like this:

glColor3fv(colorvertex);

And it´s not a necessary to call glColor after every drawing command, the same color value will be valid as long as you change it to another..

And same for glVertex3f.. So your drawing function could look like this:


glBegin(GL_TRIANGLES);

glColor3fv(colorvertex);

glVertex3fv(point1);

glVertex3fv(point2);

glVertex3fv(point3);

glEnd();


and maybe having it so that you could set your triangle´s values already in constructor..

And about is that good idea to do class like that, I don´t know, if it makes your life easier then I guess yes

[edited by - Mkk on March 27, 2004 8:09:18 AM]

Share this post


Link to post
Share on other sites
Just don''t think about using that class in an actual game or engine, having each triangles vertex data stored in different places and having to call a function to display each triangle will be extremely slow on anything but simple scenes.

Share this post


Link to post
Share on other sites
If the functions get inlined, then it should be as fast as without the class.

Oxyd

---
- Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don''t.
- Real programmers aren''t afraid of goto

Share this post


Link to post
Share on other sites
if its just for learning purposes id give this a B, nice job.
now like those other guys said, write a class that can handle more than one triangle and draw all of them at once, without multiple opengl drawing or setup calls. So it would basicly be just a big array of structs or classes (without member functions)... so like the array would be of:

struct
{
float x, y, z;
color // however its represented in opengl
};

get the idea? good luck

Share this post


Link to post
Share on other sites