Advertisement

openGL subclasses draw themselves??

Started by April 03, 2002 12:08 AM
4 comments, last by Palidine 22 years, 5 months ago
OK, i like OO design. i also have read many times that people doing openGL in an OO way like to have each object draw itself. i can''t get it to work i know i''m doing something dumb. but anyway my assumption of the "draw yourself" functionality looks like this for a triangle:

void GameTriangle::Draw() {

	glPushMatrix();
	
	glTranslatef(position.x, position.y, position.z);

	glBegin (GL_TRIANGLES);								// This is our BEGIN to draw

		glColor3ub(255, 0, 0);							// Make the top vertex RED
		glVertex3f(point1.x, point1.y, point1.z);					// Here is the top point of the triangle

		glColor3ub(0, 255, 0);							// Make the left vertex GREEN
		glVertex3f(point1.x, point1.y, point1.z);			// Here is the right point of the triangle

		glColor3ub(0, 0, 255);							// Make the right vertex BLUE
		glVertex3f(point1.x, point1.y, point1.z);			// Here is the left point of the triangle
	
	glEnd();

	glPopMatrix();
 
this won''t compile b/c i''m not including the following in my triangle object .h file: #include <gl\gl.h> #include <gl\glu.h> the problem is that when i try to include these i get all kinds of elemet X already defined errors b/c those .h files don''t do the #ifndef trick to avoid double defining something. now my sense of this is that i have no idea how to implement a self.Draw() function. if you can''t actually make calls directly to gl from within an object, how do you get an object to draw itself? the solution for all of this that i''m currently using is to iterate through my objects in a RenderScene() function in main.cpp and for each object iterate through it''s triangles to draw it. but then there is no need for an object.Draw() function. Since everyone seems to talk about self.Draw() functions, that again leads me to think i''m doing something wrong. is there a better / more customary way to do OO type self.Draw() stuff? -me
To solve your problem with the multiple OpenGL header file includes, have your triangle class in its own header file with its own source file. Then have the OpenGL headers included only in that source file. That should compile ok, since it won''t be included in different spots.

That''s how I have that version of my test engine set up, at least.
Advertisement
Yeah, my triangle class IS in it''s own header file and .cpp file. which is definitely wierd. i see what you are saying, and now i''m even more confused about why it''s not working. i wonder if there is some problem with some switches in MSVC++. i''m used to command line compiling, so i''m sure there might be some funk lying around. damn IDEs making things "easy"

any other suggestions out there?

-me
Does it work to put your preprocessor stuff in your .cpp file(hack?)
is,
#ifndef MY_HEADER_H#include <my_header.h>#endifint Main() 

etc?
Or is that considered bad karma?
hrm,

hadn''t tried that. i''ll give it a go when i get home. tks.

though i don''t see why that would help since the header file itself is wrapped in an #ifndef ---- #endif. that should be the same thing.

damn, i knew i should have ftp''d my damn code to work i''m thinking now that i just messed up the #ifndef statement b/c that would pretty much explain everything.

i bet a bizillion dollars i did something like:

#ifndef _GAMETRIANGLE
#define _GAMETRIANGL


#endif

or some other silly typo like that so it''s re including everything every time the GameTriangle.h file is #included

thanks.

-me

thanks to all for help. turns out i forgot to include windows.h somewhere. it WAS a stupid problem.

anyway you all got me looking in the right place. txs

-me

This topic is closed to new replies.

Advertisement