diff options
Diffstat (limited to 'src/gl')
-rw-r--r-- | src/gl/Makefile.am | 10 | ||||
-rw-r--r-- | src/gl/box.cc | 96 | ||||
-rw-r--r-- | src/gl/box.h | 41 | ||||
-rw-r--r-- | src/gl/osiriongl.cc | 214 | ||||
-rw-r--r-- | src/gl/osiriongl.h | 158 | ||||
-rw-r--r-- | src/gl/sphere.cc | 89 | ||||
-rw-r--r-- | src/gl/sphere.h | 50 |
7 files changed, 658 insertions, 0 deletions
diff --git a/src/gl/Makefile.am b/src/gl/Makefile.am new file mode 100644 index 0000000..e7f6663 --- /dev/null +++ b/src/gl/Makefile.am @@ -0,0 +1,10 @@ + +METASOURCES = AUTO + +libosiriongl_la_LDFLAGS = -avoid-version +noinst_LTLIBRARIES = libosiriongl.la + +INCLUDES = -I$(top_srcdir)/src +libosiriongl_la_SOURCES = box.h box.cc sphere.cc sphere.h osiriongl.cc \ + osiriongl.h +noinst_HEADERS = box.h box.h diff --git a/src/gl/box.cc b/src/gl/box.cc new file mode 100644 index 0000000..67a8ed0 --- /dev/null +++ b/src/gl/box.cc @@ -0,0 +1,96 @@ +/* box.cc + This file is part of the Osirion project +*/ + +// project headers +#include "box.h" +#include "osiriongl.h" + +namespace gl { + +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() +{ + 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(); + +} + +} // namespace gl + diff --git a/src/gl/box.h b/src/gl/box.h new file mode 100644 index 0000000..f612243 --- /dev/null +++ b/src/gl/box.h @@ -0,0 +1,41 @@ +/* box.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_BOX_H__ +#define __INCLUDED_BOX_H__ + +#include "common/vector3f.h" +#include "common/color.h" + +namespace gl { + +/// a drawable OpenGL block shape +class Box +{ +public: + /// create a new standard cube with edge length 1 + Box(Vector3f const & tl, Vector3f const &br); + /// copy constructor + Box(const Box &other); + + /// assignment operator + Box& operator=(const Box &other); + + /// top left vertex (1,1,1) + Vector3f topleft; + /// bottom right vertex (-1,-1,-1) + Vector3f bottomright; + + /// draw the block + void draw(); + + /// Top color + Color topcolor; + /// bottom color + Color bottomcolor; +}; + +} // namespace gl + +#endif // __INCLUDED_BOX_H__ diff --git a/src/gl/osiriongl.cc b/src/gl/osiriongl.cc new file mode 100644 index 0000000..0917eec --- /dev/null +++ b/src/gl/osiriongl.cc @@ -0,0 +1,214 @@ +/* gl.cc + * This file is part of the Osirion project + */ + +// SDL headers +#include <SDL/SDL.h> + +#include "osiriongl.h" + +namespace gl +{ + +typedef void (APIENTRY *glBegin_func)(GLenum); +typedef void (APIENTRY *glEnd_func)(); +typedef void (APIENTRY *glViewport_func)(GLint, GLint, GLsizei, GLsizei); +typedef void (APIENTRY *glDepthMask_func)(GLenum); +typedef void (APIENTRY *glFrontFace_func)(GLenum); +typedef void (APIENTRY *glCullFace_func)(GLenum); +typedef void (APIENTRY *glShadeModel_func)(GLenum); +typedef void (APIENTRY *glEnable_func)(GLenum); +typedef void (APIENTRY *glClear_func)(GLbitfield); +typedef void (APIENTRY *glClearColor_func)(GLclampf, GLclampf, GLclampf,GLclampf); +typedef void (APIENTRY *glRotatef_func)(GLfloat, GLfloat, GLfloat, GLfloat); +typedef void (APIENTRY *glTranslatef_func)(GLfloat, GLfloat, GLfloat); +typedef void (APIENTRY *glScalef_func)(GLfloat, GLfloat, GLfloat); +typedef void (APIENTRY *glVertex3f_func)(GLfloat, GLfloat, GLfloat); +typedef void (APIENTRY *glPushMatrix_func)(); +typedef void (APIENTRY *glPopMatrix_func)(); +typedef void (APIENTRY *glColor4f_func)(GLfloat, GLfloat, GLfloat, GLfloat); +typedef void (APIENTRY *glMatrixMode_func)(GLenum); +typedef void (APIENTRY *glLoadIdentity_func)(); +typedef void (APIENTRY *glFrustum_func)(GLdouble, GLdouble, GLdouble, GLdouble, GLdouble, GLdouble); + +glBegin_func osglBegin = 0; +glEnd_func osglEnd = 0; +glViewport_func osglViewport = 0; +glDepthMask_func osglDepthMask = 0; +glFrontFace_func osglFrontFace = 0; +glCullFace_func osglCullFace = 0; +glShadeModel_func osglShadeModel = 0; +glEnable_func osglEnable = 0; +glClear_func osglClear = 0; +glClearColor_func osglClearColor = 0; +glRotatef_func osglRotatef = 0; +glTranslatef_func osglTranslatef = 0; +glScalef_func osglScalef = 0; +glVertex3f_func osglVertex3f = 0; +glPushMatrix_func osglPushMatrix = 0; +glPopMatrix_func osglPopMatrix = 0; +glColor4f_func osglColor4f = 0; +glMatrixMode_func osglMatrixMode = 0; +glLoadIdentity_func osglLoadIdentity = 0; +glFrustum_func osglFrustum = 0; + +// ------------- INITIALIZE FUNCTION POINTERS --------------// +void init() +{ + osglBegin = (glBegin_func) SDL_GL_GetProcAddress("glBegin"); + osglEnd = (glEnd_func) SDL_GL_GetProcAddress("glEnd"); + osglViewport = (glViewport_func) SDL_GL_GetProcAddress("glViewport"); + osglDepthMask = (glDepthMask_func) SDL_GL_GetProcAddress("glDepthMask"); + osglFrontFace = (glFrontFace_func) SDL_GL_GetProcAddress("glFrontFace"); + osglCullFace = (glCullFace_func) SDL_GL_GetProcAddress("glCullFace"); + osglShadeModel = (glShadeModel_func) SDL_GL_GetProcAddress("glShadeModel"); + osglEnable = (glEnable_func) SDL_GL_GetProcAddress("glEnable"); + osglClear = (glClear_func) SDL_GL_GetProcAddress("glClear"); + osglClearColor = (glClearColor_func) SDL_GL_GetProcAddress("glClearColor"); + osglRotatef = (glRotatef_func) SDL_GL_GetProcAddress("glRotatef"); + osglTranslatef = (glTranslatef_func) SDL_GL_GetProcAddress("glTranslatef"); + osglScalef = (glScalef_func) SDL_GL_GetProcAddress("glScalef"); + osglVertex3f = (glVertex3f_func) SDL_GL_GetProcAddress("glVertex3f"); + osglPushMatrix = (glPushMatrix_func) SDL_GL_GetProcAddress("glPushMatrix"); + osglPopMatrix = (glPopMatrix_func) SDL_GL_GetProcAddress("glPopMatrix"); + osglColor4f = (glColor4f_func) SDL_GL_GetProcAddress("glColor4f"); + osglMatrixMode = (glMatrixMode_func) SDL_GL_GetProcAddress("glMatrixMode"); + osglLoadIdentity = (glLoadIdentity_func) SDL_GL_GetProcAddress("glLoadIdentity"); + osglFrustum = (glFrustum_func) SDL_GL_GetProcAddress("glFrustum"); +} + + +void shutdown() +{ + osglBegin = 0; + osglEnd = 0; + osglViewport = 0; + osglDepthMask = 0; + osglFrontFace = 0; + osglCullFace = 0; + osglShadeModel = 0; + osglEnable = 0; + osglClear = 0; + osglClearColor = 0; + osglRotatef = 0; + osglTranslatef = 0; + osglScalef = 0; + osglVertex3f = 0; + osglPushMatrix = 0; + osglPopMatrix = 0; + osglColor4f = 0; + osglMatrixMode = 0; + osglLoadIdentity = 0; +} + +void begin(Primitive primitive) { + osglBegin(primitive); +} + +void end() { + osglEnd(); +} + +void viewport(GLint x, GLint y, GLsizei width, GLsizei height) +{ + osglViewport(x, y, width, height); +} + +void depthmask(GLenum mode) +{ + osglDepthMask(mode); +} + + +void frontface(GLenum mode) +{ + osglFrontFace(mode); +} + +void cullface(GLenum mode) +{ + osglCullFace(mode); +} + +void shademodel(GLenum mode) +{ + osglShadeModel(mode); +} + +void enable(GLenum cap) +{ + osglEnable(cap); +} + +void clear (GLbitfield mask) { + osglClear(mask); +} + +void clearcolor(Color const & color) { + osglClearColor(color.red(), color.green(), color.blue(), color.alpha()); +} + +void clearcolor(const float r, const float g, const float b, const float a) { + osglClearColor(r,g,b, a); +} + +void rotate(const float angle, const Vector3f& vector) { + osglRotatef(angle, vector[0], vector[1], vector[2]); +} + +void rotate(const float angle, const float x, const float y, const float z) { + osglRotatef(angle, x, y, z); +} + +void translate(const Vector3f& vector) { + osglTranslatef(vector[0], vector[1], vector[2]); +} + +void translate(const float x, const float y, const float z) { + osglTranslatef(x, y, z); +} +void scale(const Vector3f& vector) { + osglScalef(vector[0], vector[1], vector[2]); +} + +void scale(const float x, const float y, const float z) { + osglScalef(x, y, z); +} + +void vertex(const Vector3f& vector) { + osglVertex3f(vector[0], vector[1], vector[2]); +} + +void vertex(const float x, const float y, const float z) { + osglVertex3f(x, y, z); +} + +void push() { + osglPushMatrix(); +} + +void pop() { + osglPopMatrix(); +} + +void color(const float r, const float g, const float b, const float a) { + osglColor4f(r,g,b,a); +} +void color(Color const & color) { + osglColor4f(color.red(), color.green(), color.blue(), color.alpha()); +} + +void matrixmode(GLenum mode) { + osglMatrixMode(mode); +} + +void loadidentity() { + osglLoadIdentity(); +} + +void frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble znear, GLdouble zfar) +{ + osglFrustum(left, right, bottom, top, znear, zfar); +} + +} // namespace gl diff --git a/src/gl/osiriongl.h b/src/gl/osiriongl.h new file mode 100644 index 0000000..92c0db3 --- /dev/null +++ b/src/gl/osiriongl.h @@ -0,0 +1,158 @@ +/* gl.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_OSIRIONGL_H__ +#define __INCLUDED_OSIRIONGL_H__ + +// OpenGL headers +#include <GL/gl.h> + +// project headers +#include "common/vector3f.h" +#include "common/color.h" + +/// 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 +{ + /// initialize the OpenGL subsystem + void init(); + + /// shutdown the OpenGL subsystem + void shutdown(); + + /// 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(Color const &color); + /// set the color used to clear to buffer + 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); + + /// glShadeModel + void shademodel(GLenum mode); + + /// glCullFace + void cullface(GLenum mode); + + /// glFrontFace + void frontface(GLenum mode); + + + /// glDepthMask + void depthmask(GLenum mode); + + /// 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 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 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 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 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(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_OSIRIONGL_H__ diff --git a/src/gl/sphere.cc b/src/gl/sphere.cc new file mode 100644 index 0000000..5e4e931 --- /dev/null +++ b/src/gl/sphere.cc @@ -0,0 +1,89 @@ +/* sphere.cc + This file is part of the Osirion project +*/ + +#include "common/functions.h" + +#include "osiriongl.h" +#include "sphere.h" + +namespace gl { + +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; +} + +void Sphere::draw() +{ + // 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(); + + } +} + + +} // namespace gl + diff --git a/src/gl/sphere.h b/src/gl/sphere.h new file mode 100644 index 0000000..f2bbf0a --- /dev/null +++ b/src/gl/sphere.h @@ -0,0 +1,50 @@ +/* sphere.h + This file is part of the Osirion project +*/ + +#ifndef __INCLUDED_SPHERE_H__ +#define __INCLUDED_SPHERE_H__ + +#include "common/vector3f.h" +#include "common/color.h" + +namespace gl { + +/// a drawable OpenGL block shape +class Sphere +{ +public: + /// create a new sphere + Sphere(Vector3f p = 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 + Vector3f position; + + /// draw the sphere + void draw(); + + /// Top color + Color topcolor; + /// bottom color + Color bottomcolor; + +private: + float *sintable; + float *costable; +}; + +} // namespace gl + +#endif // __INCLUDED_SPHERE_H__ |