Skip to main content
GameDev.net gamedev.net
🔒 Locked

Rendering a sphere unsing glDrawElements and GL_TRIANGLE_STRIP???

Started by golgoth Sep 22, 2004 at 4:18 PM 8 replies 11.8k views
Original Post
golgoth
golgoth
Hi all! I'm trying to make a bank of primitives in my 3d engine including all kind of geometries... plane, cube are pretty easy but when it comes to sphere it's getting a little confusing to index faces!!! I'm looking for any Hint on the subject using either GL_TRIANGLE_STRIP or GL_TRIANGLES... plz no samples using glbegin... THX golgoth
jm_hewitt
jm_hewitt
Shouldn't this be in OpenGL forums?

Anything wrong with gluSphere?
golgoth
golgoth
nothing wrong with glusphere... but developping with the glut lib is not a part of our plan... since we will eventually use our primitives for dirext x and/or software rendering!

THX
jm_hewitt
jm_hewitt
sounds reasonable....

but remember glu is seperate from glut.
Enigma
Enigma
Here are a couple of versions to start you off. Both can be further optimised to remove duplicate vertices at the poles, but I'll leave that to you.
#include <cmath>#include <vector>#include <gl/glut.h>const float PI = 3.14159265358979323846f;#pragma pack(1)template <typename TYPE>class GeometryVector{	public:		GeometryVector(const TYPE x_ = TYPE(), const TYPE y_ = TYPE(), const TYPE z_ = TYPE());		const TYPE x;		const TYPE y;		const TYPE z;};#pragma pack()template <typename TYPE>GeometryVector<TYPE>::GeometryVector(const TYPE x_, const TYPE y_, const TYPE z_)	:	x(x_),	y(y_),	z(z_){}class StackedSphere{	public:		StackedSphere(const float radius = 1.0f, const unsigned int stacks = 8, const unsigned int slices = 16);		void render() const;	private:		std::vector<GeometryVector<float> > geometryData_;		std::vector<unsigned short> indexData_;};StackedSphere::StackedSphere(const float radius, const unsigned int stacks, const unsigned int slices){	for (unsigned int stackNumber = 0; stackNumber <= stacks; ++stackNumber)	{		for (unsigned int sliceNumber = 0; sliceNumber < slices; ++sliceNumber)		{			float theta = stackNumber * PI / stacks;			float phi = sliceNumber * 2 * PI / slices;			float sinTheta = std::sin(theta);			float sinPhi = std::sin(phi);			float cosTheta = std::cos(theta);			float cosPhi = std::cos(phi);			geometryData_.push_back(GeometryVector<float>(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta));		}	}	for (unsigned int stackNumber = 0; stackNumber < stacks; ++stackNumber)	{		for (unsigned int sliceNumber = 0; sliceNumber <= slices; ++sliceNumber)		{			indexData_.push_back((stackNumber * slices) + (sliceNumber % slices));			indexData_.push_back(((stackNumber + 1) * slices) + (sliceNumber % slices));		}	}}void StackedSphere::render() const{	glVertexPointer(3, GL_FLOAT, 0, &geometryData_[0]);	glEnableClientState(GL_VERTEX_ARRAY);	glDrawElements(GL_TRIANGLE_STRIP, indexData_.size(), GL_UNSIGNED_SHORT, &indexData_[0]);}class SpiralSphere{	public:		SpiralSphere(const float radius = 1.0f, const unsigned int loops = 8, const unsigned int segmentsPerLoop = 16);		void render() const;	private:		std::vector<GeometryVector<float> > geometryData_;		std::vector<unsigned short> indexData_;};SpiralSphere::SpiralSphere(const float radius, const unsigned int loops, const unsigned int segmentsPerLoop){	for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)	{		float theta = 0;		float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop;		float sinTheta = std::sin(theta);		float sinPhi = std::sin(phi);		float cosTheta = std::cos(theta);		float cosPhi = std::cos(phi);		geometryData_.push_back(GeometryVector<float>(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta));			}	for (unsigned int loopNumber = 0; loopNumber <= loops; ++loopNumber)	{		for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)		{			float theta = (loopNumber * PI / loops) + ((PI * loopSegmentNumber) / (segmentsPerLoop * loops));			if (loopNumber == loops)			{				theta = PI;			}			float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop;			float sinTheta = std::sin(theta);			float sinPhi = std::sin(phi);			float cosTheta = std::cos(theta);			float cosPhi = std::cos(phi);			geometryData_.push_back(GeometryVector<float>(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta));					}	}	for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)	{		indexData_.push_back(loopSegmentNumber);		indexData_.push_back(segmentsPerLoop + loopSegmentNumber);	}	for (unsigned int loopNumber = 0; loopNumber < loops; ++loopNumber)	{		for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)		{			indexData_.push_back(((loopNumber + 1) * segmentsPerLoop) + loopSegmentNumber);			indexData_.push_back(((loopNumber + 2) * segmentsPerLoop) + loopSegmentNumber);		}	}}void SpiralSphere::render() const{	glVertexPointer(3, GL_FLOAT, 0, &geometryData_[0]);	glEnableClientState(GL_VERTEX_ARRAY);	glDrawElements(GL_TRIANGLE_STRIP, indexData_.size(), GL_UNSIGNED_SHORT, &indexData_[0]);}StackedSphere sphere1(4, 8, 16);SpiralSphere sphere2(4, 8, 16);int r = 0;void reshape(int width, int height){	if (height == 0)	{		height = 1;	}	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	glViewport(0, 0, width, height);	gluPerspective(45.0f, GLfloat(width) / GLfloat(height), 0.1f, 50.0f);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();}void display(){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();	glTranslatef(0, 0, -25);	glPushMatrix();		glTranslatef(-5, 0, 0);		glRotatef(r, 0, 1, 0);		sphere1.render();	glPopMatrix();	glPushMatrix();		glTranslatef(5, 0, 0);		glRotatef(r, 0, 1, 0);		sphere2.render();	glPopMatrix();	glutSwapBuffers();	r++;}int main(int argc, char** argv){	glutInit(&argc, argv);	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);	glutInitWindowPosition(0, 0);	glutInitWindowSize(512,512);	glutCreateWindow("Sphere Test");	glutReshapeFunc(reshape);	glutDisplayFunc(display);	glutIdleFunc(display);	glPolygonMode(GL_FRONT, GL_LINE);	glPolygonMode(GL_BACK, GL_LINE);	glutMainLoop();	return 0;}


Enigma

EDIT: Small bug in building of StackedSphere.
Deranged
Deranged
Quote:
Original post by golgoth
nothing wrong with glusphere... but developping with the glut lib is not a part of our plan... since we will eventually use our primitives for dirext x and/or software rendering!

THX


REasonable yes, but i must correct you in GLU is not glut and glu is part of the opengl standard.
golgoth
golgoth
yes my mistake... i meant glu...
Deranged
Deranged
glu is open source at sgi, you can grab the source to all your favorite funtions and use them individually.
golgoth
golgoth
glu is open source at sgi, you can grab the source to all your favorite funtions and use them individually.

Is this what you refering to???

http://pyopengl.sourceforge.net/documentation/manual/gluLookAt.3G.html

i cant find the open source files you mentioned!

Golgoth

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.