source: trunk/src/debugsphere.cpp @ 210

Last change on this file since 210 was 210, checked in by sam, 10 years ago

Allow for different priorities in game and draw tick levels.

  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <cstdio>
11#include <cmath>
12
13#ifdef WIN32
14#   define WIN32_LEAN_AND_MEAN
15#   include <windows.h>
16#endif
17#if defined __APPLE__ && defined __MACH__
18#   include <OpenGL/gl.h>
19#else
20#   define GL_GLEXT_PROTOTYPES
21#   include <GL/gl.h>
22#endif
23
24#include "core.h"
25#include "debugsphere.h"
26
27/*
28 * DebugSphere implementation class
29 */
30
31class DebugSphereData
32{
33    friend class DebugSphere;
34
35    void DrawSphere(int ndiv, GLfloat r)
36    {
37        glBegin(GL_TRIANGLES);
38        for (int i = 0; i < 20; i++)
39            DrawTriangle(vdata[tindices[i][0]],
40                         vdata[tindices[i][2]],
41                         vdata[tindices[i][1]], ndiv, r);
42        glEnd();
43    }
44
45    void DrawTriangle(GLfloat const *a, GLfloat const *b, GLfloat const *c,
46                      int div, GLfloat r)
47    {
48        if (div <= 0)
49        {
50            glNormal3fv(a); glVertex3f(a[0] * r, a[1] * r, a[2] * r);
51            glNormal3fv(b); glVertex3f(b[0] * r, b[1] * r, b[2] * r);
52            glNormal3fv(c); glVertex3f(c[0] * r, c[1] * r, c[2] * r);
53        }
54        else
55        {
56            GLfloat ab[3], ac[3], bc[3];
57            for (int i = 0; i < 3; i++)
58            {
59                ab[i] = (a[i] + b[i]) * 0.5;
60                ac[i] = (a[i] + c[i]) * 0.5;
61                bc[i] = (b[i] + c[i]) * 0.5;
62            }
63            Normalize(ab); Normalize(ac); Normalize(bc);
64            DrawTriangle(a, ab, ac, div - 1, r);
65            DrawTriangle(b, bc, ab, div - 1, r);
66            DrawTriangle(c, ac, bc, div - 1, r);
67            DrawTriangle(ab, bc, ac, div - 1, r);
68        }
69    }
70
71    void Normalize(GLfloat *a)
72    {
73        GLfloat d = 1.0f / sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
74        a[0] *= d; a[1] *= d; a[2] *= d;
75    }
76
77private:
78    float time;
79
80    static GLfloat const vdata[12][3];
81    static GLuint const tindices[20][3];
82    static GLfloat const X, Z;
83};
84
85GLfloat const DebugSphereData::X = .525731112119133606;
86GLfloat const DebugSphereData::Z = .850650808352039932;
87
88GLfloat const DebugSphereData::vdata[12][3] =
89{
90    {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
91    {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
92    {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
93};
94
95GLuint const DebugSphereData::tindices[20][3] =
96{
97    {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
98    {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
99    {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
100    {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
101};
102
103/*
104 * Public DebugSphere class
105 */
106
107DebugSphere::DebugSphere()
108{
109    data = new DebugSphereData();
110    data->time = 0.0f;
111}
112
113void DebugSphere::TickGame(float deltams)
114{
115    Entity::TickGame(deltams);
116
117    data->time += 0.003f * deltams;
118    while (data->time > 6.0 * M_PI)
119        data->time -= 6.0 * M_PI;
120}
121
122void DebugSphere::TickDraw(float deltams)
123{
124    Entity::TickDraw(deltams);
125
126    float a = sinf(data->time);
127    float b = sinf(data->time * 0.5f);
128
129    glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
130    glBindTexture(GL_TEXTURE_2D, NULL);
131    glColor4f(1.0f, b, a, 0.2f);
132    glTranslatef(320.0f, 240.0f, 32.0f);
133    for (float t = 0.01f; t < 1.0f; t *= 1.3f)
134        data->DrawSphere(2, t * (60.0f + 40.0f * a));
135    glTranslatef(-320.0f, -240.0f, -32.0f);
136    glPopAttrib();
137}
138
139DebugSphere::~DebugSphere()
140{
141    delete data;
142}
143
Note: See TracBrowser for help on using the repository browser.