source: trunk/src/debugsphere.cpp @ 735

Last change on this file since 735 was 735, checked in by sam, 11 years ago

core: get rid of now useless <cstdio> includes.

  • 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#ifdef WIN32
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 "debugsphere.h"
26
27namespace lol
28{
29
30/*
31 * DebugSphere implementation class
32 */
33
34class DebugSphereData
35{
36    friend class DebugSphere;
37
38    void DrawSphere(int ndiv, GLfloat r, float *&vertex, float *&normal)
39    {
40        for (int i = 0; i < 20; i++)
41            DrawTriangle(vdata[tindices[i][0]],
42                         vdata[tindices[i][2]],
43                         vdata[tindices[i][1]], ndiv, r, vertex, normal);
44    }
45
46    void DrawTriangle(GLfloat const *a, GLfloat const *b, GLfloat const *c,
47                      int div, GLfloat r, float *&vertex, float *&normal)
48    {
49        if (div <= 0)
50        {
51            *normal++ = a[0]; *normal++ = a[1]; *normal++ = a[2];
52            *vertex++ = a[0] * r; *vertex++ = a[1] * r; *vertex++ = a[2] * r;
53            *normal++ = b[0]; *normal++ = b[1]; *normal++ = b[2];
54            *vertex++ = b[0] * r; *vertex++ = b[1] * r; *vertex++ = b[2] * r;
55            *normal++ = c[0]; *normal++ = c[1]; *normal++ = c[2];
56            *vertex++ = c[0] * r; *vertex++ = c[1] * r; *vertex++ = c[2] * r;
57        }
58        else
59        {
60            GLfloat ab[3], ac[3], bc[3];
61            for (int i = 0; i < 3; i++)
62            {
63                ab[i] = (a[i] + b[i]) * 0.5;
64                ac[i] = (a[i] + c[i]) * 0.5;
65                bc[i] = (b[i] + c[i]) * 0.5;
66            }
67            Normalize(ab); Normalize(ac); Normalize(bc);
68            DrawTriangle(a, ab, ac, div - 1, r, vertex, normal);
69            DrawTriangle(b, bc, ab, div - 1, r, vertex, normal);
70            DrawTriangle(c, ac, bc, div - 1, r, vertex, normal);
71            DrawTriangle(ab, bc, ac, div - 1, r, vertex, normal);
72        }
73    }
74
75    void Normalize(GLfloat *a)
76    {
77        GLfloat d = 1.0f / sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
78        a[0] *= d; a[1] *= d; a[2] *= d;
79    }
80
81private:
82    float time;
83    int initialised;
84    GLuint buflist[2];
85
86    static GLfloat const vdata[12][3];
87    static GLuint const tindices[20][3];
88    static GLfloat const X, Z;
89};
90
91GLfloat const DebugSphereData::X = .525731112119133606;
92GLfloat const DebugSphereData::Z = .850650808352039932;
93
94GLfloat const DebugSphereData::vdata[12][3] =
95{
96    {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
97    {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
98    {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
99};
100
101GLuint const DebugSphereData::tindices[20][3] =
102{
103    {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
104    {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
105    {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
106    {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
107};
108
109/*
110 * Public DebugSphere class
111 */
112
113DebugSphere::DebugSphere()
114  : data(new DebugSphereData())
115{
116    data->time = 0.0f;
117    data->initialised = 0;
118}
119
120void DebugSphere::TickGame(float deltams)
121{
122    Entity::TickGame(deltams);
123
124    data->time += 0.003f * deltams;
125    while (data->time > 6.0 * M_PI)
126        data->time -= 6.0 * M_PI;
127}
128
129void DebugSphere::TickDraw(float deltams)
130{
131    Entity::TickDraw(deltams);
132
133    if (IsDestroying())
134    {
135        if (data->initialised)
136        {
137            glDeleteBuffers(2, data->buflist);
138            data->initialised = 0;
139        }
140    }
141    else if (!data->initialised)
142    {
143        glGenBuffers(2, data->buflist);
144        data->initialised = 1;
145    }
146
147    float a = sinf(data->time);
148#if 0
149    float b = sinf(data->time * 0.5f);
150#endif
151
152    int const ndiv = 2;
153    int const ntriangles = 20 * (1 << (ndiv * 2))
154                              * (int)(log(1.0f / 0.01f) / log(1.1f) + 0.9999f);
155
156    float *vertex = (float *)malloc(ntriangles * 3 * 3 * sizeof(float));
157    float *normal = (float *)malloc(ntriangles * 3 * 3 * sizeof(float));
158
159    float *vertex_parser = vertex;
160    float *normal_parser = normal;
161    for (float t = 0.01f; t < 1.0f; t *= 1.1f)
162        data->DrawSphere(ndiv, t * (60.0f + 40.0f * a),
163                         vertex_parser, normal_parser);
164
165#if 0 // FIXME: does not work with GLES2
166    glEnableClientState(GL_VERTEX_ARRAY);
167    glEnableClientState(GL_NORMAL_ARRAY);
168
169    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
170    glBufferData(GL_ARRAY_BUFFER, ntriangles * 3 * 3 * sizeof(float),
171                 vertex, GL_DYNAMIC_DRAW);
172    glVertexPointer(3, GL_FLOAT, 0, NULL);
173
174    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
175    glBufferData(GL_ARRAY_BUFFER, ntriangles * 3 * 3 * sizeof(float),
176                 normal, GL_DYNAMIC_DRAW);
177    glNormalPointer(GL_FLOAT, 0, NULL);
178
179#if 0
180    glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
181#endif
182    glColor4f(1.0f, b, a, 0.1f);
183    glBindTexture(GL_TEXTURE_2D, NULL);
184
185    glTranslatef(320.0f, 240.0f, 32.0f);
186    glDrawArrays(GL_TRIANGLES, 0, ntriangles * 3);
187    glTranslatef(-320.0f, -240.0f, -32.0f);
188    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
189#if 0
190    glPopAttrib();
191#endif
192
193    glDisableClientState(GL_VERTEX_ARRAY);
194    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
195#endif
196
197    free(vertex);
198    free(normal);
199}
200
201DebugSphere::~DebugSphere()
202{
203    delete data;
204}
205
206} /* namespace lol */
207
Note: See TracBrowser for help on using the repository browser.