source: trunk/src/debugsphere.cpp @ 673

Last change on this file since 673 was 673, checked in by sam, 9 years ago

Put the OpenGL header handling in <lolgl.h> for more convenience.

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