Sign in to follow this  

sphere object

This topic is 3850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 __cplusplus
extern "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]

Share this post


Link to post
Share on other sites

This topic is 3850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this