Click to See Complete Forum and Search --> : classes using opengl
EscapeCharacter
02-11-2001, 06:33 AM
is it possible to use member functions of a class to create a opengl app? i ask this because all the code ive been able to find has only used normal function calls and no classes, kinda leading me to think that opengl was just for c and not c++. i have tried replacing the function calls for my own class member functions that do the exact same thing(i.e. cut and paste) but opengl doesnt seem to like this
[ 11 February 2001: Message edited by: EscapeCharacter ]
Qubit
02-11-2001, 08:03 AM
Could you post some of the offending code? I'm sure it's just a problem with the code, I have been able to build a C++ framework for OpenGL (under windows).
OpenGL is just an API.
EscapeCharacter
02-11-2001, 09:10 AM
this is basically it im not doing anything crazy just a hello world type program.
#include <gl\gl.h>
#include <gl\glut.h>
void init(void);
void display(void);
class displayme{
public:
displayme();
//void create(void);
void display();
};
displayme::displayme(){
glClearColor(0.0f ,0.0f ,0.0f ,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)250/(GLfloat)250,0.1f,100.0f);
}
void displayme::display(void){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, -10.0f);
glVertex3f(-1.0f,-1.0f, -10.0f);
glVertex3f( 1.0f,-1.0f, -10.0f);
glEnd();
glutSwapBuffers();
}
//void displayme::display(){
//glutDisplayFunc(create);
//}
int main(int argc, char *argv[])
{
displayme d;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(250,250);
glutInitWindowPosition(100,200);
glutCreateWindow("My First Glut/OpenGL Program");
init();
d.display();
glutMainLoop();
return 0;
}
void init(void)
{
glClearColor(0.0f ,0.0f ,0.0f ,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)250/(GLfloat)250,0.1f,100.0f);
}
/*void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, -10.0f);
glVertex3f(-1.0f,-1.0f, -10.0f);
glVertex3f( 1.0f,-1.0f, -10.0f);
glEnd();
glutSwapBuffers();
}
*/
the program compiles but just crashes when i run it.
Sterling
02-11-2001, 11:30 AM
Ok, I don't know any OpenGL programming, but tracing through your program in my head, I think I see a few problems.
You're doing a lot of OpenGL initialization stuff in the displayme constructor. (See following code) Stuff that looks like setting colors, transparencies, etc.
displayme::displayme(){
glClearColor(0.0f ,0.0f ,0.0f ,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)250/(GLfloat)250,0.1f,100.0f);
}
However, down in main(), this constructor gets called before glutInit(). I'm not sure if that's ok or not, but I assume glutInit() sets up a bunch of things used by various OpenGL functions, including the ones you've called in the constructor.
If this is indeed the problem, the best way to fix it is to change main() as follows:
int main(int argc, char *argv[])
{
glutInit(,argv);
displayme d;
/* ^- We can do this in C++, as it drops the silly requirement that all variables be declared at the top of their blocks */
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(250,250);
glutInitWindowPosition(100,200);
glutCreateWindow("My First Glut/OpenGL Program");
init();
d.display();
glutMainLoop();
return 0;
}
(Edit-add)Hmm... That posted main loop might not fix things - I can see a few other functions that might need to be called before said constructor. Try putting the variable declaration where you've got the call to init() now - that should mean the constructor gets called late enough to do the right thing.(/Edit-add)
[ 11 February 2001: Message edited by: Sterling ]