PDA

View Full Version : problem w/ OpenGL and class based displaying



teflon_dragon
03-28-2002, 06:38 AM
I'm trying to take NeHe's tuts and write my own classes based on them. I'm working on a class for a Font and for a Flyer (the player in my little game). Everything compiles, but neither my font nor my flyer is displayed on the screen.

//Here's the flyer class:
class CFlyer {
public:
CFlyer();

GLvoid Draw();

GLvoid SetPos( GLfloat xpos, GLfloat ypos, GLfloat zpos );

private:
XYZType pos;
RGBType color;
GLuint flyer;
};

CFlyer::CFlyer() {
flyer = glGenLists(1);

glNewList( flyer, GL_COMPILE );

glBegin(GL_TRIANGLES);
// Front
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
// Right
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
// Back
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
// Left
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd();

glEndList();

pos.x = 0.0f;
pos.y = 0.0f;
pos.z = -10.0f; // so its into the screen

color.red = 1.0f;
color.green = 0.5f;
color.blue = 0.5f;
}

GLvoid CFlyer::SetPos( GLfloat xpos, GLfloat ypos, GLfloat zpos ) {
pos.x = xpos;
pos.y = ypos;
pos.z = zpos;
}

GLvoid CFlyer: :Draw() {

glTranslatef( pos.x, pos.y, pos.z );

glCallList( flyer );

glLoadIdentity();
}

Here's my render loop:
int DrawGLScene(GLvoid) {
glClear GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)glLoadIden tity();

myFlyer.Draw();

return TRUE;

}
Yeah, pretty simple (right now the flyer is just a bottomless pyramid, lol). Eventually I want it to be a physics affected player controllable flying machine, but it has to display first! lol

Now if I just skip the flyer class and it's corresponding display list all together, and just move the:
glBegin(GL_TRIANGLES)
draw my pyramid
glEnd()
directly into the render loop it works just fine.
Anybody see a gross error in my code? I know all the OpenGL stuff in my engine.h is solid, it's just when I use classes in this manner they won't display on the screen.
Thanks in advance!

- = TEFLON DRAGON = -

[This message has been edited by teflon_dragon (edited 03-28-2002).]

zeckensack
03-28-2002, 06:46 AM
You're probably not finished with setting up your rendering context before the constructor of the class is called. The display list will never get compiled, so it won't do anything when you call it.

Two solutions:
1)Don't declare instances, use pointers and initialize them later.Instead of defining Flyer my_flyer;, use Flyer* my_flyer; somewhere and do a my_flyer=new Flyer after your context is ready.

2)Give additional gl_init() and gl_destroy() member functions to your class and take everything gl related out of the constructor/destructor. The you can manually call these after initializing OpenGL. Kinda defeats the purpose of OOP but it works.

teflon_dragon
03-28-2002, 07:03 AM
Hey many thanks! I understand some more now. Going w/ option 1 creates a new prob:

// heres my global CFlyer pointer
CFlyer *myFlyer;

//after my RC and all is set up I do this
myFlyer = new CFlyer;

//in my Render() I then do this
myFlyer.Draw();

and VC++ gives me this:
C:\Documents and Settings\Brian\Desktop\Flyer\driver.cpp(158) : error C2228: left of '.Draw' must have class/struct/union type

What am I doing wrong?

- = TEFLON DRAGON = -

03-28-2002, 07:11 AM
Change your myFlyer.Draw();

to

myFlyer->Draw();

zeckensack
03-28-2002, 07:13 AM
Originally posted by teflon_dragon:
myFlyer.Draw();

and VC++ gives me this:
C:\Documents and Settings\Brian\Desktop\Flyer\driver.cpp(158) : error C2228: left of '.Draw' must have class/struct/union type

What am I doing wrong?

- = TEFLON DRAGON = -

Try myFlyer->draw(); instead. The '->' is the 'member access operator' for pointers and replaces the dot.

Oh, and while we're at it, don't forget to delete the display list before you shut down your gl context.

teflon_dragon
03-28-2002, 07:15 AM
Many thanks to everybody! My prog isn't much so far but thanks to you guyziz help it does something!
About deleting the lists... if my destructor was simply
~CFlyer {
delete this;
}
would that kill everything to do with the object, including the list? Or is there a better way to do that?

Hey once again to everybody that helped out a big thanks! You've made my morning! lol

- = TEFLON DRAGON = -

[This message has been edited by teflon_dragon (edited 03-28-2002).]

zeckensack
03-28-2002, 07:21 AM
Originally posted by teflon_dragon:
~CFlyer {
delete this;
}

Don't try that at home! Might give you a nice infinite loop or something.

You have to get rid of the display list yourself, before you destroy the rendering context. So, you can put glDeleteLists() and other stuff in the destructor, but you shouldn't let an object destroy itself.

Simply put a delete myFlyer; somewhere in your shutdown code, that will do the trick.