source: trunk/src/debug/sphere.cpp @ 1163

Last change on this file since 1163 was 1163, checked in by sam, 8 years ago

math: ensure real::fabs() is never chosen over std::fabs() for arguments
that are not explicitly real, even with namespace mistakes.

  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined WIN32 && !defined _XBOX
16#   define _USE_MATH_DEFINES /* for M_PI */
17#   define WIN32_LEAN_AND_MEAN
18#   include <windows.h>
19#endif
20
21#include <cmath>
22
23#include "core.h"
24#include "lolgl.h"
25#include "loldebug.h"
26
27using namespace std;
28
29namespace lol
30{
31
32/*
33 * DebugSphere implementation class
34 */
35
36class DebugSphereData
37{
38    friend class DebugSphere;
39
40    void DrawSphere(int ndiv, GLfloat r, float *&vertex, float *&normal)
41    {
42        for (int i = 0; i < 20; i++)
43            DrawTriangle(vdata[tindices[i][0]],
44                         vdata[tindices[i][2]],
45                         vdata[tindices[i][1]], ndiv, r, vertex, normal);
46    }
47
48    void DrawTriangle(GLfloat const *a, GLfloat const *b, GLfloat const *c,
49                      int div, GLfloat r, float *&vertex, float *&normal)
50    {
51        if (div <= 0)
52        {
53            *normal++ = a[0]; *normal++ = a[1]; *normal++ = a[2];
54            *vertex++ = a[0] * r; *vertex++ = a[1] * r; *vertex++ = a[2] * r;
55            *normal++ = b[0]; *normal++ = b[1]; *normal++ = b[2];
56            *vertex++ = b[0] * r; *vertex++ = b[1] * r; *vertex++ = b[2] * r;
57            *normal++ = c[0]; *normal++ = c[1]; *normal++ = c[2];
58            *vertex++ = c[0] * r; *vertex++ = c[1] * r; *vertex++ = c[2] * r;
59        }
60        else
61        {
62            GLfloat ab[3], ac[3], bc[3];
63            for (int i = 0; i < 3; i++)
64            {
65                ab[i] = (a[i] + b[i]) * 0.5;
66                ac[i] = (a[i] + c[i]) * 0.5;
67                bc[i] = (b[i] + c[i]) * 0.5;
68            }
69            Normalize(ab); Normalize(ac); Normalize(bc);
70            DrawTriangle(a, ab, ac, div - 1, r, vertex, normal);
71            DrawTriangle(b, bc, ab, div - 1, r, vertex, normal);
72            DrawTriangle(c, ac, bc, div - 1, r, vertex, normal);
73            DrawTriangle(ab, bc, ac, div - 1, r, vertex, normal);
74        }
75    }
76
77    void Normalize(GLfloat *a)
78    {
79        GLfloat d = 1.0f / std::sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
80        a[0] *= d; a[1] *= d; a[2] *= d;
81    }
82
83private:
84    float time;
85    int initialised;
86    GLuint buflist[2];
87
88    static GLfloat const vdata[12][3];
89    static GLuint const tindices[20][3];
90    static GLfloat const X, Z;
91};
92
93GLfloat const DebugSphereData::X = .525731112119133606;
94GLfloat const DebugSphereData::Z = .850650808352039932;
95
96GLfloat const DebugSphereData::vdata[12][3] =
97{
98    {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
99    {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
100    {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
101};
102
103GLuint const DebugSphereData::tindices[20][3] =
104{
105    {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
106    {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
107    {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
108    {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
109};
110
111/*
112 * Public DebugSphere class
113 */
114
115DebugSphere::DebugSphere()
116  : data(new DebugSphereData())
117{
118    data->time = 0.0f;
119    data->initialised = 0;
120}
121
122void DebugSphere::TickGame(float deltams)
123{
124    Entity::TickGame(deltams);
125
126    data->time += 0.003f * deltams;
127    while (data->time > 6.0 * M_PI)
128        data->time -= 6.0 * M_PI;
129}
130
131void DebugSphere::TickDraw(float deltams)
132{
133    Entity::TickDraw(deltams);
134
135    if (IsDestroying())
136    {
137        if (data->initialised)
138        {
139            glDeleteBuffers(2, data->buflist);
140            data->initialised = 0;
141        }
142    }
143    else if (!data->initialised)
144    {
145        glGenBuffers(2, data->buflist);
146        data->initialised = 1;
147    }
148
149    float a = sinf(data->time);
150#if 0
151    float b = sinf(data->time * 0.5f);
152#endif
153
154    int const ndiv = 2;
155    using std::log;
156    int const ntriangles = 20 * (1 << (ndiv * 2))
157                              * (int)(log(1.0f / 0.01f) / log(1.1f) + 0.9999f);
158
159    float *vertex = (float *)malloc(ntriangles * 3 * 3 * sizeof(float));
160    float *normal = (float *)malloc(ntriangles * 3 * 3 * sizeof(float));
161
162    float *vertex_parser = vertex;
163    float *normal_parser = normal;
164    for (float t = 0.01f; t < 1.0f; t *= 1.1f)
165        data->DrawSphere(ndiv, t * (60.0f + 40.0f * a),
166                         vertex_parser, normal_parser);
167
168#if 0 // FIXME: does not work with GLES2
169    glEnableClientState(GL_VERTEX_ARRAY);
170    glEnableClientState(GL_NORMAL_ARRAY);
171
172    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
173    glBufferData(GL_ARRAY_BUFFER, ntriangles * 3 * 3 * sizeof(float),
174                 vertex, GL_DYNAMIC_DRAW);
175    glVertexPointer(3, GL_FLOAT, 0, NULL);
176
177    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
178    glBufferData(GL_ARRAY_BUFFER, ntriangles * 3 * 3 * sizeof(float),
179                 normal, GL_DYNAMIC_DRAW);
180    glNormalPointer(GL_FLOAT, 0, NULL);
181
182#if 0
183    glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
184#endif
185    glColor4f(1.0f, b, a, 0.1f);
186    glBindTexture(GL_TEXTURE_2D, NULL);
187
188    glTranslatef(320.0f, 240.0f, 32.0f);
189    glDrawArrays(GL_TRIANGLES, 0, ntriangles * 3);
190    glTranslatef(-320.0f, -240.0f, -32.0f);
191    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
192#if 0
193    glPopAttrib();
194#endif
195
196    glDisableClientState(GL_VERTEX_ARRAY);
197    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
198#endif
199
200    free(vertex);
201    free(normal);
202}
203
204DebugSphere::~DebugSphere()
205{
206    delete data;
207}
208
209} /* namespace lol */
210
Note: See TracBrowser for help on using the repository browser.