Advertisement

I just wanted to draw a cube...

Started by April 03, 2005 10:00 AM
2 comments, last by Jucksalbe 19 years, 7 months ago
Hi! After reading some of the OpenGL tutorials on this page (which are really good, by the way), I started to set up my own OpenGL project. I tried to use OOP as much as possible and I guess that's why it isn't really working. All it does so far is to set up a fullscreen window and draw a cube. The problem is that the cube isn't drawn and I don't know why. I spent quite a lot of time figuring it out (and already found some mistakes) but it still doesn't work. The project isn't very big so far, and most of it is copied from the tutorials. I hope anyone can help me. I've uploaded the whole project (don't worry <20 KB) to: www.travedsl.de/~stephan.kaufmann/Kula%20Cooler.zip It's all written in Visual C++ 6.0. Thanks for ANY help! :-)
OK, the problem apears to be you're not setting the window (or matrix, whatever the thingy's called) to perspective mode. The way I did this was to create a seperate method in your GLWindow class like so:
//StartGL.h#ifndef START_GL_H //Inclusion gaurd#define START_GL_H#include <windows.h>#include <gl\gl.h>//Don't need the other headers!class GLWindow{public:	GLWindow(char * title, int width, int height, BYTE bpp);	bool InitGL(void);	bool PrepareDrawing(void);	void FlipBuffer(void);	~GLWindow();	void SetPerspective(double fov = 45, double farPlane = 100, double nearPlane = 1) const;private:	int width;	int height;};#endif //End inclusion gaurd

//StartGL.cpp#include <cmath> //Need this for tan//Other functions code unchangedvoid GLWindow::FlipBuffer(void){	glFlush(); //Make sure everything finished drawing before we swap the buffers!	SwapBuffers(hdc);}void GLWindow::SetPerspective(double fov, double farPlane, double nearPlane) const{	const double degToRad = 0.017453292519943295769236907684886; //convert degrees to radians (180 / pi)	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	double fH = std::tan(fov * degToRad) * nearPlane * 0.5;	double fW = fH * width / height;	glFrustum(-fW, fW, -fH, fH, nearPlane, farPlane);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();	glClear(GL_DEPTH_BUFFER_BIT);}


You then need to call it just after your call to InitGL, like so:
mainWindow->InitGL();
mainWindow->SetPerspective();

The code was slighly modified from here. The advantage of using this code is that you don't need to include glaux or glu!

Now, a minor point about your code, if you don't mind me criticizing. If the class members don't modify their data, mark them as const (as I have done here). Also, use inclusion gaurds in headers (again, see above code). Also, consider getting rid of all these nasty getters/setters and replace it with a Plain Old Data (POD) type.
class Vertex{private:	GLfloat x;	GLfloat y;	GLfloat z;public:	GLfloat getX(void){return(x);}	GLfloat getY(void){return(y);}	GLfloat getZ(void){return(z);}	void setX(GLfloat val){x=val;}	void setY(GLfloat val){y=val;}	void setZ(GLfloat val){z=val;}};

change to:
struct Vertex{	Vertex(float x_ = 0, float y_ = 0, float z_ = 0):x(x_), y(y_), z(z_) {}	float x;	float y;	float z;};

Then you can get rid of those getters/setters in your Solid class, and have a single getter/setter for a Vertex. Also, your Solid class should have a virtual destructor. A general rule, if any of your class member functions are marked as virtual, then your destructor should be marked as virtual as well.

Final thing, which is a matter of style really, return isn't a function!! (i.e. don't return(true);, you return true;!
Advertisement
OK, the problem apears to be you're not setting the window (or matrix, whatever the thingy's called) to perspective mode. The way I did this was to create a seperate method in your GLWindow class like so:
//StartGL.h#ifndef START_GL_H //Inclusion gaurd#define START_GL_H#include <windows.h>#include <gl\gl.h>//Don't need the other headers!class GLWindow{public:	GLWindow(char * title, int width, int height, BYTE bpp);	bool InitGL(void);	bool PrepareDrawing(void);	void FlipBuffer(void);	~GLWindow();	void SetPerspective(double fov = 45, double farPlane = 100, double nearPlane = 1) const;private:	int width;	int height;};#endif //End inclusion gaurd

//StartGL.cpp#include <cmath> //Need this for tan//Other functions code unchangedvoid GLWindow::FlipBuffer() //Don;t need to specify 'void' in the parameters!{	glFlush(); //Make sure everything finished drawing before we swap the buffers!	SwapBuffers(hdc);}void GLWindow::SetPerspective(double fov, double farPlane, double nearPlane) const{	const double degToRad = 0.017453292519943295769236907684886; //convert degrees to radians (180 / pi)	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	double fH = std::tan(fov * degToRad) * nearPlane * 0.5;	double fW = fH * width / height;	glFrustum(-fW, fW, -fH, fH, nearPlane, farPlane);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();	glClear(GL_DEPTH_BUFFER_BIT);}


You then need to call it just after your call to InitGL, like so:
mainWindow->InitGL();mainWindow->SetPerspective();

The code was slighly modified from here. The advantage of using this code is that you don't need to include glaux or glu!

Now, a minor point about your code, if you don't mind me criticizing. If the class members don't modify their data, mark them as const (see above code). Also, use inclusion gaurds in headers (again, see above code). Also, consider getting rid of all these nasty getters/setters and replace it with a Plain Old Data (POD) type.
class Vertex{private:	GLfloat x;	GLfloat y;	GLfloat z;public:	GLfloat getX(void){return(x);}	GLfloat getY(void){return(y);}	GLfloat getZ(void){return(z);}	void setX(GLfloat val){x=val;}	void setY(GLfloat val){y=val;}	void setZ(GLfloat val){z=val;}};

change to:
struct Vertex{	Vertex(float x_ = 0, float y_ = 0, float z_ = 0):x(x_), y(y_), z(z_) {}	float x;	float y;	float z;};

Then you can get rid of those getters/setters in your Solid class, and have a single getter/setter for a Vertex. Also, your Solid class should have a virtual destructor. A general rule, if any of your class member functions are marked as virtual, then your destructor should be marked as virtual as well.

Final thing, which is a matter of style really, return isn't a function!! (i.e. don't return(true);, you return true;)

[EDIT] Damit, the above AP is me apparently, can the mods delete it?
Thank you. That helped a lot. :-)


Quote: Original post by desertcube
Now, a minor point about your code, if you don't mind me criticizing.


Never would. ;-)

Quote: Original post by desertcube
Also, use inclusion gaurds in headers (again, see above code).


That was one of the things I was looking for while I was programming that project. I forgot how this works. I haven't done very much with C++ yet.


Quote: Original post by desertcube
Also, consider getting rid of all these nasty getters/setters and replace it with a Plain Old Data (POD) type.


Never seen something like this before. Maybe I should read that up, it seems a lot easier. :-)

Quote: Original post by desertcube
Final thing, which is a matter of style really, return isn't a function!! (i.e. don't return(true);, you return true;)


I know, but I like it better with the brackets. It's not like this changes anything, is it?

This topic is closed to new replies.

Advertisement