source: trunk/src/debugsphere.cpp @ 221

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

Complete Lol Engine / Deus Hax / Monsterz split.

  • Property svn:keywords set to Id
File size: 3.7 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#if defined __APPLE__ && defined __MACH__
21#   include <OpenGL/gl.h>
22#else
23#   define GL_GLEXT_PROTOTYPES
24#   include <GL/gl.h>
25#endif
26
27#include <cstdio>
28#include <cmath>
29
30#include "core.h"
31#include "debugsphere.h"
32
33/*
34 * DebugSphere implementation class
35 */
36
37class DebugSphereData
38{
39    friend class DebugSphere;
40
41    void DrawSphere(int ndiv, GLfloat r)
42    {
43        glBegin(GL_TRIANGLES);
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);
48        glEnd();
49    }
50
51    void DrawTriangle(GLfloat const *a, GLfloat const *b, GLfloat const *c,
52                      int div, GLfloat r)
53    {
54        if (div <= 0)
55        {
56            glNormal3fv(a); glVertex3f(a[0] * r, a[1] * r, a[2] * r);
57            glNormal3fv(b); glVertex3f(b[0] * r, b[1] * r, b[2] * r);
58            glNormal3fv(c); glVertex3f(c[0] * r, c[1] * r, 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);
71            DrawTriangle(b, bc, ab, div - 1, r);
72            DrawTriangle(c, ac, bc, div - 1, r);
73            DrawTriangle(ab, bc, ac, div - 1, r);
74        }
75    }
76
77    void Normalize(GLfloat *a)
78    {
79        GLfloat d = 1.0f / 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
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{
115    data = new DebugSphereData();
116    data->time = 0.0f;
117}
118
119void DebugSphere::TickGame(float deltams)
120{
121    Entity::TickGame(deltams);
122
123    data->time += 0.003f * deltams;
124    while (data->time > 6.0 * M_PI)
125        data->time -= 6.0 * M_PI;
126}
127
128void DebugSphere::TickDraw(float deltams)
129{
130    Entity::TickDraw(deltams);
131
132    float a = sinf(data->time);
133    float b = sinf(data->time * 0.5f);
134
135    glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
136    glBindTexture(GL_TEXTURE_2D, NULL);
137    glColor4f(1.0f, b, a, 0.1f);
138    glTranslatef(320.0f, 240.0f, 32.0f);
139    for (float t = 0.01f; t < 1.0f; t *= 1.1f)
140        data->DrawSphere(2, t * (60.0f + 40.0f * a));
141    glTranslatef(-320.0f, -240.0f, -32.0f);
142    glPopAttrib();
143}
144
145DebugSphere::~DebugSphere()
146{
147    delete data;
148}
149
Note: See TracBrowser for help on using the repository browser.