From 67f8a7a783e550cab8e6a77d997b31815ee8cd7e Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 2 Feb 2008 14:53:46 +0000 Subject: introduced librender --- src/render/Makefile.am | 7 +++ src/render/box.cc | 102 ++++++++++++++++++++++++++++++ src/render/box.h | 44 +++++++++++++ src/render/gl.cc | 163 +++++++++++++++++++++++++++++++++++++++++++++++ src/render/gl.h | 168 +++++++++++++++++++++++++++++++++++++++++++++++++ src/render/render.cc | 27 ++++++++ src/render/render.h | 22 +++++++ src/render/sphere.cc | 95 ++++++++++++++++++++++++++++ src/render/sphere.h | 51 +++++++++++++++ 9 files changed, 679 insertions(+) create mode 100644 src/render/Makefile.am create mode 100644 src/render/box.cc create mode 100644 src/render/box.h create mode 100644 src/render/gl.cc create mode 100644 src/render/gl.h create mode 100644 src/render/render.cc create mode 100644 src/render/render.h create mode 100644 src/render/sphere.cc create mode 100644 src/render/sphere.h (limited to 'src/render') diff --git a/src/render/Makefile.am b/src/render/Makefile.am new file mode 100644 index 0000000..a993703 --- /dev/null +++ b/src/render/Makefile.am @@ -0,0 +1,7 @@ +INCLUDES = -I$(top_srcdir)/src +METASOURCES = AUTO +noinst_LTLIBRARIES = librender.la +librender_la_LDFLAGS = -avoid-version -no-undefined @GL_LIBS@ +librender_la_LIBADD = $(top_builddir)/src/math/libmath.la +librender_la_SOURCES = box.cc gl.cc render.cc sphere.cc +noinst_HEADERS = box.h gl.h render.h sphere.h diff --git a/src/render/box.cc b/src/render/box.cc new file mode 100644 index 0000000..8819a40 --- /dev/null +++ b/src/render/box.cc @@ -0,0 +1,102 @@ +/* + gl/box.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +// project headers +#include "render/box.h" + +namespace render { + +using math::Vector3f; +using math::Color; + +Box::Box(Vector3f const & tl, Vector3f const &br) : + topleft(tl), bottomright(br) +{ + topcolor = Color::White(); + bottomcolor= Color::White() * 0.7f; +} + +Box::Box(const Box & other) +{ + (*this) = other; +} + +Box& Box::operator=(const Box &other) +{ + bottomcolor = other.bottomcolor; + topcolor = other.topcolor; + + topleft = other.topleft; + bottomright = other.bottomright; + return (*this); +} + +void Box::draw() +{ + using namespace gl; + + Vector3f v0(topleft.x, bottomright.y, bottomright.z); + Vector3f v1(topleft.x, topleft.y, bottomright.z); + Vector3f v2(topleft.x, topleft.y, topleft.z); + Vector3f v3(topleft.x, bottomright.y, topleft.z); + + Vector3f v4(bottomright.x, bottomright.y, bottomright.z); + Vector3f v5(bottomright.x, topleft.y, bottomright.z); + Vector3f v6(bottomright.x, topleft.y, topleft.z); + Vector3f v7(bottomright.x, bottomright.y, topleft.z); + + begin(Quads); + + // top + color(topcolor); + vertex(v2); + vertex(v1); + vertex(v5); + vertex(v6); + + // sides + color(bottomcolor); + vertex(v0); + color(topcolor); + vertex(v1); + vertex(v2); + color(bottomcolor); + vertex(v3); + + vertex(v3); + color(topcolor); + vertex(v2); + vertex(v6); + color(bottomcolor); + vertex(v7); + + vertex(v4); + color(topcolor); + vertex(v5); + vertex(v1); + color(bottomcolor); + vertex(v0); + + vertex(v7); + color(topcolor); + vertex(v6); + vertex(v5); + color(bottomcolor); + vertex(v4); + + // bottom + color(bottomcolor); + vertex(v4); + vertex(v0); + vertex(v3); + vertex(v7); + + end(); + +} + +} + diff --git a/src/render/box.h b/src/render/box.h new file mode 100644 index 0000000..9552b78 --- /dev/null +++ b/src/render/box.h @@ -0,0 +1,44 @@ +/* + gl/box.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_GL_BOX_H__ +#define __INCLUDED_GL_BOX_H__ + +#include "render/render.h" +#include "math/mathlib.h" + +namespace render { + +/// a drawable OpenGL block shape +class Box +{ +public: + /// create a new standard cube with edge length 1 + Box(math::Vector3f const & tl, math::Vector3f const &br); + /// copy constructor + Box(const Box &other); + + /// assignment operator + Box& operator=(const Box &other); + + /// top left vertex (1,1,1) + math::Vector3f topleft; + /// bottom right vertex (-1,-1,-1) + math::Vector3f bottomright; + + /// draw the block + void draw(); + + /// Top color + math::Color topcolor; + /// bottom color + math::Color bottomcolor; +}; + +} + +#endif // __INCLUDED_GL_BOX_H__ + diff --git a/src/render/gl.cc b/src/render/gl.cc new file mode 100644 index 0000000..6eb91b1 --- /dev/null +++ b/src/render/gl.cc @@ -0,0 +1,163 @@ +/* + render/gl.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +// project includes +#include "render/gl.h" + +// system includes +#include "GL/gl.h" + +using math::Vector3f; +using math::Color; + +namespace render { + +namespace gl { + +std::string renderer() +{ + return std::string ((char *)glGetString(GL_RENDERER)); +} + +std::string vendor() +{ + return std::string ((char *)glGetString(GL_VENDOR)); +} + +std::string version() +{ + return std::string ((char *)glGetString(GL_VERSION)); +} + +std::string extensions() +{ + return std::string ((char *)glGetString(GL_EXTENSIONS)); +} + +void begin(Primitive primitive) +{ + ::glBegin(primitive); +} + +void end() { + ::glEnd(); +} + +void viewport(GLint x, GLint y, GLsizei width, GLsizei height) +{ + ::glViewport(x, y, width, height); +} + +void depthmask(GLenum mode) +{ + ::glDepthMask(mode); +} + + +void frontface(GLenum mode) +{ + glFrontFace(mode); +} + +void cullface(GLenum mode) +{ + glCullFace(mode); +} + +void shademodel(GLenum mode) +{ + glShadeModel(mode); +} + +void blendfunc(GLenum sfactor, GLenum dfactor) +{ + glBlendFunc(sfactor, dfactor); +} + +void enable(GLenum cap) +{ + glEnable(cap); +} + +void disable(GLenum cap) +{ + glDisable(cap); +} + +void clear (GLbitfield mask) { + glClear(mask); +} + +void clearcolor(Color const & color) { + glClearColor(color.red(), color.green(), color.blue(), color.alpha()); +} + +void clearcolor(const float r, const float g, const float b, const float a) { + glClearColor(r,g,b, a); +} + +void rotate(const float angle, const Vector3f& vector) { + glRotatef(angle, vector[0], vector[1], vector[2]); +} + +void rotate(const float angle, const float x, const float y, const float z) { + glRotatef(angle, x, y, z); +} + +void translate(const Vector3f& vector) { + glTranslatef(vector[0], vector[1], vector[2]); +} + +void translate(const float x, const float y, const float z) { + glTranslatef(x, y, z); +} +void scale(const Vector3f& vector) { + glScalef(vector[0], vector[1], vector[2]); +} + +void scale(const float x, const float y, const float z) { + glScalef(x, y, z); +} + +void vertex(const Vector3f& vector) { + glVertex3f(vector[0], vector[1], vector[2]); +} + +void vertex(const float x, const float y, const float z) { + glVertex3f(x, y, z); +} + +void push() { + glPushMatrix(); +} + +void pop() { + glPopMatrix(); +} + +void color(const float r, const float g, const float b, const float a) { + glColor4f(r,g,b,a); +} +void color(Color const & color) { + glColor4f(color.red(), color.green(), color.blue(), color.alpha()); +} + +void matrixmode(GLenum mode) { + glMatrixMode(mode); +} + +void loadidentity() { + glLoadIdentity(); +} + +void frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble znear, GLdouble zfar) +{ + glFrustum(left, right, bottom, top, znear, zfar); +} + +} // namespace gl + +} // namespace render diff --git a/src/render/gl.h b/src/render/gl.h new file mode 100644 index 0000000..0513410 --- /dev/null +++ b/src/render/gl.h @@ -0,0 +1,168 @@ +/* + render/gl.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_RENDER_GL_H__ +#define __INCLUDED_RENDER_GL_H__ + +// project headers +#include "math/vector3f.h" +#include "math/color.h" + +// OpenGL headers +#include + +namespace render { + +/// wrapper namespace for OpenGL operations +/** The gl namespace provides a wrapper to the OpenGL library functions. + * All methods take floats or Vector3f and Color as parameters. + */ +namespace gl +{ + /// name of the hardware OpenGL renderer + std::string renderer(); + /// vender of the system OpenGL implementation + std::string vendor(); + /// version of the system OpenGL implementation + std::string version(); + /// Opengl Extensions string + std::string extensions(); + + /// enum to denote Vertex drawing modes + enum Primitive { + Points=GL_POINTS, + Lines=GL_LINES, + LineStrip=GL_LINE_STRIP, + LineLoop=GL_LINE_LOOP, + Triangles=GL_TRIANGLES, + TriangleStrip=GL_TRIANGLE_STRIP, + TriangleFan=GL_TRIANGLE_FAN, + Quads=GL_QUADS, + QuadStrip=GL_QUAD_STRIP, + Polygon=GL_POLYGON + }; + + /// glViewPort + void viewport(GLint x, GLint y, GLsizei width, GLsizei height ); + + /// set the color used to clear to buffer + void clearcolor(math::Color const &color); + void clearcolor(const float r, const float g, const float b, const float a); + + /// clear buffers to preset values + void clear (GLbitfield mask); + + /// glMatrixMode + void matrixmode(GLenum mode); + + /// glEnable + void enable(GLenum cap); + + /// glDisable + void disable(GLenum cap); + + /// glShadeModel + void shademodel(GLenum mode); + + /// glCullFace + void cullface(GLenum mode); + + /// glFrontFace + void frontface(GLenum mode); + + /// glDepthMask + void depthmask(GLenum mode); + + /// glBlendFunc + void blendfunc(GLenum sfactor, GLenum dfactor); + + /// Delimite the start of a sequence of verteces describing a primitive or group of primitives + /** @param primitive The type of drawing primitive + * @see end() + */ + void begin(Primitive primitive); + + /// delimit the end of a sequence of verteces describing a primitive or group of primitives + /** @see begin() + */ + void end(); + + /// Add the next vertex the the current drawing operation + /** From the glVertex() description: + * vertex() commands are used within begin()/end() pairs to specify point, + * line, and polygon vertices. The current color, normal, and texture + * coordinates are associated with the vertex when vertex() is called. + */ + void vertex(const math::Vector3f& vector); + + void vertex(const float x, const float y, const float z); + + /// multiply the current matrix by a general rotation matrix + /** @param angle The angle of the rotation, in degrees [0-360] + * @param vector The rotation axes, relative to the origin (0,0,0) + */ + void rotate(const float angle, const math::Vector3f& vector); + + /// multiply the current matrix by a general rotation matrix + /** @param angle The angle of the rotation, in degrees + * @param x The x-coordinate of the rotation vector + * @param y The y-coordinate of the rotation vector + * @param z The z-coordinate of the rotation vector + */ + void rotate(const float angle, const float x, const float y, const float z); + + /// multiply the current matrix by a general translation matrix + /** @param vector The translation vector, relative to the origin (0,0,0) + */ + void translate(const math::Vector3f& vector); + + /// multiply the current matrix by a general translation matrix + /** @param x The x-coordinate of the translation vector + * @param y The y-coordinate of the translation vector + * @param z The z-coordinate of the translation vector + */ + void translate(const float x, const float y, const float z); + + /// multiply the current matrix by a general scaling matrix + /** @param vector The scale factor for all 3 axes + */ + void scale(const math::Vector3f& vector); + + /// multiply the current matrix by a general scaling matrix + /** @param x x-scaling factor + * @param y y-scaling factor + * @param z z-scaling factor + */ + void scale(const float x, const float y, const float z); + + /// specify the drawing color for the next GL functions + /** @param color the new drawing color + */ + void color(math::Color const & color); + + /** @param r red value of the new drawing color + * @param g green value of the new drawing color + * @param b blue value of the new drawing color + * @param a alpha value of the new drawing color + */ + void color(const float r, const float g, const float b, const float a=1.0f); + + /// Push the current transformation matrix to the stack + void push(); + + /// Replace the transformation matrix with the top from the stack + void pop(); + + /// Replace the transformation matrix with the identity matrtix + void loadidentity(); + + /// Perspective matrix + void frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble znear, GLdouble zfar); +} + +} + +#endif // __INCLUDED_RENDER_GL_H__ diff --git a/src/render/render.cc b/src/render/render.cc new file mode 100644 index 0000000..2440b25 --- /dev/null +++ b/src/render/render.cc @@ -0,0 +1,27 @@ +/* + render/render.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +// project headers +#include "render/render.h" +#include "sys/sys.h" + +namespace render { + +void init() +{ + con_debug << "Initializing render..." << std::endl; + + con_print << "Renderer: " << gl::renderer() << std::endl; + con_print << "Vendor: " << gl::vendor() << std::endl; + con_print << "Version: " << gl::version() << std::endl; +} + +void shutdown() +{ + con_debug << "Shutting down render..." << std::endl; +} + +} diff --git a/src/render/render.h b/src/render/render.h new file mode 100644 index 0000000..eaaef73 --- /dev/null +++ b/src/render/render.h @@ -0,0 +1,22 @@ +/* + render/render.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_RENDER_H__ +#define __INCLUDED_RENDER_H__ + + +namespace render { + + /// initialize the render subsystem + void init(); + + /// shutdown the render subsystem + void shutdown(); +} + +#include "render/gl.h" + +#endif // __INCLUDED_RENDER_H__ diff --git a/src/render/sphere.cc b/src/render/sphere.cc new file mode 100644 index 0000000..1715263 --- /dev/null +++ b/src/render/sphere.cc @@ -0,0 +1,95 @@ +/* + gl/sphere.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "render/sphere.h" +#include "math/mathlib.h" + +using math::Vector3f; +using math::Color; + +namespace render { + +const int segments = 33; + +Sphere::Sphere(Vector3f p , float r) +{ + position = p; + radius = r; + + // TODO make global sine-cosine lists + sintable = new float[segments]; + costable = new float[segments]; + float d = 2 * M_PI / segments; + + for (int i=0; i < segments; i++) { + sintable[i] = sin( d * (float) i ); + costable[i] = cos ( d * (float) i ); + } +} + +Sphere::~Sphere() +{ + delete[] sintable; + delete[] costable; +} + +Sphere::Sphere(const Sphere &other) +{ + (*this) = other; +} + +Sphere& Sphere::operator=(const Sphere &other) +{ + position = other.position; + radius = other.radius; + return (*this); +} + +void Sphere::draw() +{ + using namespace gl; + + // draw top + // TODO upside-down + float r = radius*sintable[1]; + float h = radius*costable[1]; + + begin(LineLoop); + //begin(Polygon); + for (int i = segments-1; i >= 0; i--) + vertex(r*costable[i], h, r*sintable[i]); + end(); + + // draw bottom + // TODO upside-down + begin(LineLoop); + for (int i = 0; i< segments; i++) + vertex(r*costable[i], -h, r*sintable[i]); + end(); + + // draw body + for (int j=1; j < segments-1; j++) { + r = radius*sintable[j]; + float r1 = radius*sintable[j+1]; + + begin(QuadStrip); + vertex(r1, radius*costable[j+1], 0); + vertex(r, radius*costable[j], 0); + + for (int i = segments-1; i >= 0; i--) { + vertex(r1*costable[i], radius*costable[j+1], r1*sintable[i]); + vertex(r*costable[i], radius*costable[j], r*sintable[i]); + //vertex(r*costable[i-1], radius*costable[j], r*sintable[i-1]); + //vertex(r1*costable[i-1], radius*costable[j+1], r1*sintable[i-1]); + } + end(); + + } +} + + +} + diff --git a/src/render/sphere.h b/src/render/sphere.h new file mode 100644 index 0000000..b3ac826 --- /dev/null +++ b/src/render/sphere.h @@ -0,0 +1,51 @@ +/* + gl/sphere.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_GL_SPHERE_H__ +#define __INCLUDED_GL_SPHERE_H__ + +#include "render/gl.h" + +namespace render { + +/// a drawable OpenGL block shape +class Sphere +{ +public: + /// create a new sphere + Sphere(math::Vector3f p = math::Vector3f(), float r = 1.0f); + + /// copy constructor + Sphere(const Sphere &other); + + /// destructor + ~Sphere(); + + /// assignment operator + Sphere& operator=(const Sphere &other); + + /// radius of the sphere + float radius; + + /// position of the sphere + math::Vector3f position; + + /// draw the sphere + void draw(); + + /// Top color + math::Color topcolor; + /// bottom color + math::Color bottomcolor; + +private: + float *sintable; + float *costable; +}; + +} // namespace gl + +#endif // __INCLUDED_GL_SPHERE_H__ -- cgit v1.2.3