Browse Source

Add a debugging sphere object to test transparency.

legacy
Sam Hocevar sam 14 years ago
parent
commit
1f3226dc2f
5 changed files with 188 additions and 3 deletions
  1. +2
    -1
      src/Makefile.am
  2. +148
    -0
      src/debugsphere.cpp
  3. +34
    -0
      src/debugsphere.h
  4. +1
    -1
      src/gtk/editor.cpp
  5. +3
    -1
      src/test-map.cpp

+ 2
- 1
src/Makefile.am View File

@@ -10,7 +10,8 @@ libcommon_a_SOURCES = \
forge.cpp forge.h video.cpp video.h timer.cpp timer.h bitfield.h \
profiler.cpp profiler.h input.h input.cpp world.cpp world.h \
debugfps.cpp debugfps.h debugsprite.cpp debugsprite.h \
debugrecord.cpp debugrecord.h debugstats.cpp debugstats.h
debugrecord.cpp debugrecord.h debugstats.cpp debugstats.h \
debugsphere.cpp debugsphere.h
libcommon_a_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image`

test_map_SOURCES = test-map.cpp sdlinput.cpp sdlinput.h


+ 148
- 0
src/debugsphere.cpp View File

@@ -0,0 +1,148 @@
//
// Deus Hax (working title)
// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
//

#if defined HAVE_CONFIG_H
# include "config.h"
#endif

#include <cstdio>
#include <cmath>

#ifdef WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
#if defined __APPLE__ && defined __MACH__
# include <OpenGL/gl.h>
#else
# define GL_GLEXT_PROTOTYPES
# include <GL/gl.h>
#endif

#include "core.h"
#include "debugsphere.h"

/*
* DebugSphere implementation class
*/

class DebugSphereData
{
friend class DebugSphere;

void DrawSphere(int ndiv, GLfloat r)
{
glBegin(GL_TRIANGLES);
for (int i = 0; i < 20; i++)
DrawTriangle(vdata[tindices[i][0]],
vdata[tindices[i][2]],
vdata[tindices[i][1]], ndiv, r);
glEnd();
}

void DrawTriangle(GLfloat const *a, GLfloat const *b, GLfloat const *c,
int div, GLfloat r)
{
if (div <= 0)
{
glNormal3fv(a); glVertex3f(a[0] * r, a[1] * r, a[2] * r);
glNormal3fv(b); glVertex3f(b[0] * r, b[1] * r, b[2] * r);
glNormal3fv(c); glVertex3f(c[0] * r, c[1] * r, c[2] * r);
}
else
{
GLfloat ab[3], ac[3], bc[3];
for (int i = 0; i < 3; i++)
{
ab[i] = (a[i] + b[i]) * 0.5;
ac[i] = (a[i] + c[i]) * 0.5;
bc[i] = (b[i] + c[i]) * 0.5;
}
Normalize(ab); Normalize(ac); Normalize(bc);
DrawTriangle(a, ab, ac, div - 1, r);
DrawTriangle(b, bc, ab, div - 1, r);
DrawTriangle(c, ac, bc, div - 1, r);
DrawTriangle(ab, bc, ac, div - 1, r);
}
}

void Normalize(GLfloat *a)
{
GLfloat d = 1.0f / sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
a[0] *= d; a[1] *= d; a[2] *= d;
}

private:
float time;

static GLfloat const vdata[12][3];
static GLuint const tindices[20][3];
static GLfloat const X, Z;
};

GLfloat const DebugSphereData::X = .525731112119133606;
GLfloat const DebugSphereData::Z = .850650808352039932;

GLfloat const DebugSphereData::vdata[12][3] =
{
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
{0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
{Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
};

GLuint const DebugSphereData::tindices[20][3] =
{
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
};

/*
* Public DebugSphere class
*/

DebugSphere::DebugSphere()
{
data = new DebugSphereData();
data->time = 0.0f;
}

Entity::Group DebugSphere::GetGroup()
{
return GROUP_DEFAULT;
}

void DebugSphere::TickGame(float deltams)
{
Entity::TickGame(deltams);

data->time += 0.003f * deltams;
while (data->time > 6.0 * M_PI)
data->time -= 6.0 * M_PI;
}

void DebugSphere::TickDraw(float deltams)
{
Entity::TickDraw(deltams);

float a = sinf(data->time);
float b = sinf(data->time * 0.5f);

glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
glBindTexture(GL_TEXTURE_2D, NULL);
glColor4f(1.0f, b, a, 0.2f);
glTranslatef(320.0f, 240.0f, 32.0f);
for (float t = 0.01f; t < 1.0f; t *= 1.3f)
data->DrawSphere(2, t * (60.0f + 40.0f * a));
glTranslatef(-320.0f, -240.0f, -32.0f);
glPopAttrib();
}

DebugSphere::~DebugSphere()
{
delete data;
}


+ 34
- 0
src/debugsphere.h View File

@@ -0,0 +1,34 @@
//
// Deus Hax (working title)
// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
//

//
// The DebugSphere class
// ---------------------
//

#if !defined __DH_DEBUGSPHERE_H__
#define __DH_DEBUGSPHERE_H__

#include "entity.h"

class DebugSphereData;

class DebugSphere : public Entity
{
public:
DebugSphere();
virtual ~DebugSphere();

protected:
virtual Group GetGroup();
virtual void TickGame(float deltams);
virtual void TickDraw(float deltams);

private:
DebugSphereData *data;
};

#endif // __DH_DEBUGSPHERE_H__


+ 1
- 1
src/gtk/editor.cpp View File

@@ -56,7 +56,7 @@ int main(int argc, char **argv)
GTK_SIGNAL_FUNC(delayed_quit), NULL);
g_object_unref(G_OBJECT(builder));

glmapview->LoadMap("maps/testmap2.tmx");
glmapview->LoadMap("maps/testmap.tmx");
new DebugFps();

gtk_main();


+ 3
- 1
src/test-map.cpp View File

@@ -16,6 +16,7 @@
#include "sdlinput.h"
#include "debugfps.h"
#include "debugsprite.h"
#include "debugsphere.h"
#include "debugrecord.h"
#include "debugstats.h"

@@ -48,12 +49,13 @@ int main(int argc, char **argv)
Video::Setup(video->w, video->h);

/* Create a game */
Game *game = new Game("maps/testmap2.tmx");
Game *game = new Game("maps/testmap.tmx");

/* Register an input driver and some debug stuff */
new SdlInput();
new DebugFps();
new DebugSprite(game);
new DebugSphere();
//new DebugRecord("lolengine.ogg");
new DebugStats("stats.txt");



Loading…
Cancel
Save