|
From: Tapio V. <aa...@us...> - 2009-11-10 09:06:19
|
Module: performous
Branch: master
Commit: 0ca83ba16a2c7cf8b61293244f6e2e33a14b4561
Author: Tapio Vierros <tap...@gm...>
Date: Tue Nov 10 10:37:41 2009 +0200
3d objects drawn with display lists.
---
game/3dobject.cc | 11 ++++++-----
game/3dobject.hh | 19 ++++++++++++++++---
game/glutil.hh | 7 +++++++
3 files changed, 29 insertions(+), 8 deletions(-)
diff --git a/game/3dobject.cc b/game/3dobject.cc
index 1bac829..c17db3e 100644
--- a/game/3dobject.cc
+++ b/game/3dobject.cc
@@ -12,7 +12,6 @@
#include <cmath>
-// TODO: use display lists for drawing?
// TODO: test & fix faces that doesn't have texcoords in the file
// TODO: group handling for loader
@@ -87,7 +86,9 @@ void Object3d::loadWavefrontObj(std::string filepath, float scale) {
}
-void Object3d::draw(float x, float y, float z) const {
+void Object3d::generateDisplayList() {
+ m_displist = glGenLists(1);
+ glutil::DisplayList displist(m_displist, GL_COMPILE);
//UseTexture tex(*m_texture);
std::vector<Face>::const_iterator it;
// Iterate through faces
@@ -113,9 +114,9 @@ void Object3d::draw(float x, float y, float z) const {
m_normals[it->normals[i]].z);
// Vertices
glVertex3f(
- m_vertices[it->vertices[i]].x+x,
- m_vertices[it->vertices[i]].y+y,
- m_vertices[it->vertices[i]].z+z);
+ m_vertices[it->vertices[i]].x,
+ m_vertices[it->vertices[i]].y,
+ m_vertices[it->vertices[i]].z);
}
}
}
diff --git a/game/3dobject.hh b/game/3dobject.hh
index 9baef99..d4dfce0 100644
--- a/game/3dobject.hh
+++ b/game/3dobject.hh
@@ -6,6 +6,7 @@
#include <stdexcept>
#include <string>
+#include <boost/noncopyable.hpp>
#include "surface.hh"
#include "glutil.hh"
@@ -32,22 +33,34 @@ struct Face {
};
-class Object3d {
+class Object3d: boost::noncopyable {
private:
std::vector<Vertex> m_vertices;
std::vector<TexCoord> m_texcoords;
std::vector<Vertex> m_normals;
std::vector<Face> m_faces;
+ GLuint m_displist;
//boost::scoped_ptr<Texture> m_texture;
/// load a Wavefront .obj 3d object file
void loadWavefrontObj(std::string filepath, float scale = 1.0);
+ /// generates a display list for the object
+ void generateDisplayList();
public:
/// constructors
Object3d();
- Object3d(std::string filepath, float scale = 1.0) {
+ Object3d(std::string filepath, float scale = 1.0): m_displist(0) {
//m_texture.reset(new Texture("button.svg"));
loadWavefrontObj(filepath, scale);
+ generateDisplayList();
+ }
+ /// destructor
+ ~Object3d() {
+ if (m_displist != 0) glDeleteLists(m_displist, 1);
}
/// draws the object
- void draw(float x = 0, float y = 0, float z = 0) const;
+ void draw(float x = 0, float y = 0, float z = 0) const {
+ glTranslatef(x, y, z);
+ glCallList(m_displist);
+ glTranslatef(-x, -y, -z);
+ }
};
diff --git a/game/glutil.hh b/game/glutil.hh
index 3ce9949..a3c7cd1 100644
--- a/game/glutil.hh
+++ b/game/glutil.hh
@@ -24,6 +24,13 @@ namespace glutil {
~Begin() { glEnd(); }
};
+ /// wrapper struct for RAII
+ struct DisplayList {
+ /// call glNewList with given list id and mode
+ DisplayList(GLuint id, GLenum mode) { glNewList(id, mode); }
+ ~DisplayList() { glEndList(); }
+ };
+
/// struct to store color information
struct Color {
float r, ///< red component
|