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

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

xbox: start working on an Xbox/Direct3D port.

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