summaryrefslogtreecommitdiffstats
path: root/kue/sphere.cpp
blob: 321b8bad867ca41aee6c8928e84fc7b793c66361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <GL/gl.h>
#include <GL/glu.h>

#include <math.h>
#include <tdeconfig.h>
#include <tdeglobal.h>
#include "sphere.h"

const int SPHERE_DISPLAY_LIST = 2;
double sphere_list_radius = 0.0;

void sphere::draw(double x, double y, double r, double rot_x, double rot_y)
{
	glPushMatrix();
	glTranslated(x, y, r);

	// Rotate the balls the specified the amount
	glRotated(rot_x, 0.0, 1.0, 0.0);
	glRotated(rot_y, 1.0, 0.0, 0.0);

	// Do we have this sphere cached?
	if ((r == sphere_list_radius) && (glIsList(SPHERE_DISPLAY_LIST) == GL_TRUE))
	{
		// It was cached, call the list
		glCallList(SPHERE_DISPLAY_LIST);
	}
	else
	{
		// Figure out the number of sphere divisons we need
		TDEGlobal::config()->setGroup("Graphics");
		int sphere_divisions = TDEGlobal::config()->readNumEntry("Sphere Divisions", 8);

		// sphere_divisions < 3 causes OpenGL to draw nothing at all,
		// so clamp to value to 3.
		if (sphere_divisions < 3)
			sphere_divisions = 3;

		// Make the quadratic object
		GLUquadricObj *quad = gluNewQuadric();

		// We need normals for lighting to work
		gluQuadricNormals(quad, GLU_SMOOTH);
		// We also need texture points
		gluQuadricTexture(quad, GL_TRUE);

		// Create the display list
		glNewList(SPHERE_DISPLAY_LIST, GL_COMPILE_AND_EXECUTE);
		// Draw the sphere
		gluSphere(quad, r, sphere_divisions, sphere_divisions);
		// End the display list
		glEndList();

		// Update the cached radius
		sphere_list_radius = r;

		// Delete quadatric object
		gluDeleteQuadric(quad);
	}

	glPopMatrix();
}