diff options
author | Ixtli <cg@325i.org> | 2010-03-11 17:53:45 +0900 |
---|---|---|
committer | Enne Walker <ennewalker@users.sourceforge.net> | 2010-04-24 10:19:43 -0400 |
commit | 7510c643fbb71607c069309eb2f29957af76edbc (patch) | |
tree | 9074cc7f546b882cb218b71c43d4b2da305a4c48 /crawl-ref/source/glwrapper-ogl.cc | |
parent | b91812a7a4b1b3bf1f8d47e382b9ba3327ca9cba (diff) | |
download | crawl-ref-7510c643fbb71607c069309eb2f29957af76edbc.tar.gz crawl-ref-7510c643fbb71607c069309eb2f29957af76edbc.zip |
Fixed -h file names referring to specific implementations.
The makefile should now name the .o's based on what libraries you're using, as
well.
Diffstat (limited to 'crawl-ref/source/glwrapper-ogl.cc')
-rw-r--r-- | crawl-ref/source/glwrapper-ogl.cc | 273 |
1 files changed, 273 insertions, 0 deletions
diff --git a/crawl-ref/source/glwrapper-ogl.cc b/crawl-ref/source/glwrapper-ogl.cc new file mode 100644 index 0000000000..10a4851fc3 --- /dev/null +++ b/crawl-ref/source/glwrapper-ogl.cc @@ -0,0 +1,273 @@ +#include "AppHdr.h" + +#include "debug.h" + +#ifdef USE_TILE + +// How do we get access to the GL calls? +// If other UI types use the -ogl wrapper they should +// include more conditional includes here. +#ifdef USE_SDL +#include <SDL_opengl.h> +#endif + +#ifdef USE_GL +#include "glwrapper.h" + +///////////////////////////////////////////////////////////////////////////// +// GLPrimitive +GLPrimitive::GLPrimitive(long unsigned int sz, size_t ct, unsigned int vs, + const void* v_pt, const void *c_pt, const void *t_pt) : + mode(GLW_QUADS), + vert_size(vs), + size(sz), + count(ct), + vert_pointer(v_pt), + colour_pointer(c_pt), + texture_pointer(t_pt), + pretranslate(NULL), + prescale(NULL) +{ +} + +///////////////////////////////////////////////////////////////////////////// +// GLState + +// Note: these defaults should match the OpenGL defaults +GLState::GLState() : + array_vertex(false), + array_texcoord(false), + array_colour(false), + blend(false), + texture(false), + depthtest(false), + alphatest(false), + alpharef(0) +{ +} + +///////////////////////////////////////////////////////////////////////////// +// GLStateManager + +void GLStateManager::init() +{ + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glClearColor(0.0, 0.0, 0.0, 1.0f); + glDepthFunc(GL_LEQUAL); +} + +void GLStateManager::set(const GLState& state) +{ + if (state.array_vertex) + glEnableClientState(GL_VERTEX_ARRAY); + else + glDisableClientState(GL_VERTEX_ARRAY); + + if (state.array_texcoord) + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + else + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + + if (state.array_colour) + { + glEnableClientState(GL_COLOR_ARRAY); + } + else + { + glDisableClientState(GL_COLOR_ARRAY); + + // [enne] This should *not* be necessary, but the Linux OpenGL + // driver that I'm using sets this to the last colour of the + // colour array. So, we need to unset it here. + glColor3f(1.0f, 1.0f, 1.0f); + } + + if (state.texture) + glEnable(GL_TEXTURE_2D); + else + glDisable(GL_TEXTURE_2D); + + if (state.blend) + glEnable(GL_BLEND); + else + glDisable(GL_BLEND); + + if (state.depthtest) + glEnable(GL_DEPTH_TEST); + else + glDisable(GL_DEPTH_TEST); + + if (state.alphatest) + { + glEnable(GL_ALPHA_TEST); + glAlphaFunc(GL_NOTEQUAL, state.alpharef); + } + else + glDisable(GL_ALPHA_TEST); +} + +void GLStateManager::set_transform(const GLW_3VF *trans, const GLW_3VF *scale) +{ + glLoadIdentity(); + if (trans) + glTranslatef(trans->x, trans->y, trans->z); + if (scale) + glScalef(scale->x, scale->y, scale->z); +} + +void GLStateManager::reset_view_for_resize(coord_def &m_windowsz) +{ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + // For ease, vertex positions are pixel positions. + glOrtho(0, m_windowsz.x, m_windowsz.y, 0, -1000, 1000); +} + +void GLStateManager::reset_transform() +{ + glLoadIdentity(); + glTranslatef(0,0,0); + glScalef(1,1,1); +} + +void GLStateManager::pixelstore_unpack_alignment(unsigned int bpp) +{ + glPixelStorei(GL_UNPACK_ALIGNMENT, bpp); +} + +void GLStateManager::draw_primitive(const GLPrimitive &prim) +{ + // Handle errors + if ( !prim.vert_pointer || prim.count < 1 || prim.size < 1 ) + return; + ASSERT(_valid(prim.count, prim.mode)); + + // Set pointers + glVertexPointer(prim.vert_size, GL_FLOAT, prim.size, prim.vert_pointer); + if ( prim.texture_pointer ) + glTexCoordPointer(2, GL_FLOAT, prim.size, prim.texture_pointer); + if ( prim.colour_pointer ) + glColorPointer(4, GL_UNSIGNED_BYTE, prim.size, prim.colour_pointer); + + // Handle pre-render matrix manipulations + if ( prim.pretranslate || prim.prescale ) + { + glPushMatrix(); + if ( prim.pretranslate ) + { + glTranslatef( prim.pretranslate->x, + prim.pretranslate->y, + prim.pretranslate->z); + } + if ( prim.prescale ) + glScalef(prim.prescale->x, prim.prescale->y, prim.prescale->z); + } + + // Draw! + switch( prim.mode ) + { + case GLW_QUADS: + glDrawArrays(GL_QUADS, 0, prim.count); + break; + case GLW_LINES: + glDrawArrays(GL_LINES, 0, prim.count); + break; + default: + break; + } + + // Clean up + if ( prim.pretranslate || prim.prescale ) + { + glPopMatrix(); + } +} + +void GLStateManager::delete_textures(size_t count, unsigned int *textures) +{ + glDeleteTextures(count, (GLuint*)textures); +} + +void GLStateManager::generate_textures( size_t count, unsigned int *textures) +{ + glGenTextures(count, (GLuint*)textures); +} + +void GLStateManager::bind_texture(unsigned int texture) +{ + glBindTexture(GL_TEXTURE_2D, texture); +} + +void GLStateManager::load_texture(unsigned char *pixels, unsigned int width, + unsigned int height, MipMapOptions mip_opt) +{ + // Assumptions... + const unsigned int bpp = 4; + const GLenum texture_format = GL_RGBA; + const GLenum format = GL_UNSIGNED_BYTE; + + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + + if (mip_opt == MIPMAP_CREATE) + { + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + GL_LINEAR_MIPMAP_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + gluBuild2DMipmaps(GL_TEXTURE_2D, bpp, width, height, + texture_format, format, pixels); + } + else + { + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, bpp, width, height, 0, + texture_format, format, pixels); + } +} + +void GLStateManager::reset_view_for_redraw(float x, float y) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(x, y , 1.0f); +} + +void GLStateManager::set_current_color(GLW_3VF &color) +{ + glColor3f(color.r, color.g, color.b); +} + +void GLStateManager::set_current_color(GLW_4VF &color) +{ + glColor4f(color.r, color.g, color.b, color.a); +} + +#ifdef DEBUG +bool GLStateManager::_valid(int num_verts, drawing_modes mode) +{ + switch( mode ) + { + case GLW_QUADS: + case GLW_TRIANGLE_STRIP: + return (num_verts % 4 == 0); + case GLW_TRIANGLES: + return (num_verts % 3 == 0); + case GLW_LINES: + return (num_verts % 2 == 0); + case GLW_POINTS: + return (true); + default: + return (false); + } +} +#endif + +///////////////////////////////////////////////////////////////////////////// +// Static Methods + +#endif // USE_SDL +#endif // USE_TILE |