sphere object

Started by
6 comments, last by dreamtwister 16 years, 10 months ago
I want to create my own sphere object, but it's not working. I've basically wrapped a gluSphere inside a CSphere class for future functionality. Everything else basically follows the NeHe tutorials. It compiles fine under VC++ 2005, but the sphere does not render. Here's what I have so far. Declaration:
class CSphere
{
public:
	CSphere(float fRadius , int nLats , int nLongs );
	~CSphere();
	void Draw();
private:
	float m_fRadius;
	int m_nLats;
	int m_nLongs;
};

Definition:
CSphere::CSphere(float fRadius , int nLats , int nLongs)
{
	m_fRadius = fRadius;
	m_nLats = nLats;
	m_nLongs = nLongs;
}

CSphere::~CSphere()
{
}

void CSphere::Draw()
{
	GLUquadricObj *ball;
	ball = gluNewQuadric();
	gluQuadricNormals(ball,GLU_SMOOTH);
	gluQuadricTexture(ball,true);
	gluSphere(ball, m_fRadius, m_nLats, m_nLongs);
	gluDeleteQuadric(ball);
}

And finally, the DrawGLScene function:
bool DrawGLScene(GLvoid)						
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-40.0f);
	glColor3f(1.0,1.0f,1.0f);
	theSphere.Draw();
	return true;							
}

If anyone has any ideas why this might be the case, I would be most grateful to hear them. [Edited by - dreamtwister on May 11, 2007 12:40:28 AM]
Advertisement
no idea but just a quick hint... edit the post and put your code in the proper "source" tags so you don't automatically drive-away potential help just because of lack of tags.

e.g.
10 i is code and i is brokenz!20 goto 10
Oops sorry, I used . Force of habit I guess. It's fixed now.
Still looking for help on this...
Do you call glBegin() and glEnd() as appropriate?
I've never needed glBegin() for a gluSphere before. I tried it anyway though, and it still won't render.

I've changed the class around a little bit. Here's the updated version:

Declaration:
class CSphere{public:	CSphere(float fRadius , int nLats , int nLongs);	~CSphere();	bool Init();	void Draw();private:	GLUquadricObj *ball;	float m_fRadius;	int m_nLats;	int m_nLongs;};


Definition:
CSphere::CSphere(float fRadius , int nLats , int nLongs){	m_fRadius = fRadius;	m_nLats = nLats;	m_nLongs = nLongs;}CSphere::~CSphere(){	gluDeleteQuadric(ball);}bool CSphere::Init(){	bool bReturn = true;	ball = gluNewQuadric();	gluQuadricNormals(ball,GLU_SMOOTH);	gluQuadricTexture(ball,GL_TRUE);	return bReturn;}void CSphere::Draw(){	gluSphere(ball, m_fRadius, m_nLats, m_nLongs);}


I declare my theSphere object with all of my other globals, then I call Init() from inside InitGL(). Here's my DrawGLScene():

bool DrawGLScene(GLvoid)						{	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();	glTranslatef(0.0f,0.0f,-20.0f);	glColor3f(1.0,1.0f,1.0f);	theSphere.Draw();	return true;}


Every line of code appears to work without the class wrapper, so I know it has to be some error with my implementation. I just can't figure out what it is.

[Edited by - dreamtwister on May 29, 2007 7:29:30 PM]
You're forgetting to set how you want to draw it (point, lines, fill)
gluQuadricDrawStyle( ball, GLU_POINT ); // or GLU_FILL or GLU_LINE
GLU_FILL is the default. The sphere will render just fine without calling gluQuadricDrawstyle(). I'll probably add a Setstyle() function to my class eventually, but for the time being, my priorety is to get a functioning CSphere object first, then add to it.

By the way, gluQuadricDrawstyle() will also take GLU_SILHOUETTE and GLU_POINT.

I did manage to solve the problem myself though. I moved the rest of the initialization out of the constructor and into the Init() function and it worked fine.

My next project is to assign it member texture and bumpmaps. Here's the complete object:

Declaration:
#ifndef __csphere_h_#ifndef __CSPHERE_H__#define __csphere_h_#define _CSPHERE_H__#ifdef __cplusplusextern "C" {#endif#include <windows.h>//////////////////////////////////////////////////////////////////////////////////CSphere class////////////////////////////////////////////////////////////////////////////////	class CSphere	{	public:		CSphere();		~CSphere();		bool Init(float fRadius , int nLats , int nLongs);		void Draw();		float Radius() {return m_fRadius;};		int Latitude() {return m_nLats;};		int Longitude() {return m_nLongs;};	private:		GLUquadricObj *ball;		float m_fRadius;		int m_nLats;		int m_nLongs;	};#ifdef __cplusplus}#endif#endif /*__CSPHERE_H__ */#endif /*__csphere_h_*/


Definitions:
#include <windows.h>#include <math.h>#include <gl/gl.h>#include <gl/glu.h>#include <gl/glaux.h>#include "CSphere.h"////////////////////////////////////////////////////////////////////////////////// CSphere Members////////////////////////////////////////////////////////////////////////////////CSphere::CSphere(){}CSphere::~CSphere(){	gluDeleteQuadric(ball);}	bool CSphere::Init(float fRadius , int nLats , int nLongs){	bool bReturn = true;	m_fRadius = fRadius;	m_nLats = nLats;	m_nLongs = nLongs;	ball = gluNewQuadric();	gluQuadricNormals(ball,GLU_SMOOTH);	return bReturn;}void CSphere::Draw(){	gluSphere(ball, m_fRadius, m_nLats, m_nLongs);}


I know this is all beginner stuff to most of you, but I feel I should share what this community helped me build.

[Edited by - dreamtwister on May 31, 2007 12:53:01 AM]

This topic is closed to new replies.

Advertisement