From fb60a9bcd8ffb941483413328383c430db1e64e4 Mon Sep 17 00:00:00 2001 From: rogerman Date: Wed, 16 Jan 2013 06:17:24 +0000 Subject: [PATCH] OpenGL Renderer: - Do some more code cleanup. --- desmume/src/OGLRender.cpp | 71 ++++++++++++++++++-------------------- desmume/src/gfx3d.cpp | 16 ++++----- desmume/src/gfx3d.h | 72 ++++++++++++++++++++++----------------- 3 files changed, 82 insertions(+), 77 deletions(-) diff --git a/desmume/src/OGLRender.cpp b/desmume/src/OGLRender.cpp index 046717408..10529eb8a 100644 --- a/desmume/src/OGLRender.cpp +++ b/desmume/src/OGLRender.cpp @@ -692,7 +692,7 @@ static char OGLInit(void) // Maintain our own vertex index buffer for vertex batching and primitive // conversions. Such conversions are necessary since OpenGL deprecates - // primitives like GL_QUADS and GL_QUAD_STRIP in later versions. + // primitives like GFX3D_QUADS and GFX3D_QUAD_STRIP in later versions. vertIndexBuffer = new GLushort[VERT_INDEX_BUFFER_SIZE]; #ifndef __APPLE__ @@ -1088,7 +1088,7 @@ static void texDeleteCallback(TexCacheItem* item) currTexture = NULL; } -static void SetupTexture(POLY *thePoly) +static void SetupTexture(const POLY *thePoly) { PolygonTexParams params = thePoly->getTexParams(); @@ -1173,7 +1173,7 @@ static void SetupTexture(POLY *thePoly) } } -static void SetupPolygon(POLY *thePoly) +static void SetupPolygon(const POLY *thePoly) { static unsigned int lastTexBlendMode = 0; static int lastStencilState = -1; @@ -1307,7 +1307,7 @@ static void SetupPolygon(POLY *thePoly) } } -static void SetupViewport(POLY *thePoly) +static void SetupViewport(const POLY *thePoly) { VIEWPORT viewport; viewport.decode(thePoly->viewport); @@ -1474,9 +1474,6 @@ static void HandleClearImage() static void OGLRender() { - static const GLenum frm[] = {GL_TRIANGLES, GL_QUADS, GL_TRIANGLE_STRIP, GL_QUAD_STRIP, - GL_LINE_LOOP, GL_LINE_LOOP, GL_LINE_STRIP, GL_LINE_STRIP}; - if(!BEGINGL()) return; Control(); @@ -1591,11 +1588,20 @@ static void OGLRender() } } + // Map GFX3D_QUADS and GFX3D_QUAD_STRIP to GL_TRIANGLES since we will convert them. + // + // Also map GFX3D_TRIANGLE_STRIP to GL_TRIANGLES. This is okay since this is actually + // how the POLY struct stores triangle strip vertices, which is in sets of 3 vertices + // each. This redefinition is necessary since uploading more than 3 indices at a time + // will cause glDrawElements() to draw the triangle strip incorrectly. + static const GLenum oglPrimitiveType[] = {GL_TRIANGLES, GL_TRIANGLES, GL_TRIANGLES, GL_TRIANGLES, + GL_LINE_LOOP, GL_LINE_LOOP, GL_LINE_STRIP, GL_LINE_STRIP}; + // Render all polygons for(unsigned int i = 0; i < polyCount; i++) { - POLY *poly = &gfx3d.polylist->list[gfx3d.indexlist.list[i]]; - polyPrimitive = frm[poly->vtxFormat]; + const POLY *poly = &gfx3d.polylist->list[gfx3d.indexlist.list[i]]; + polyPrimitive = oglPrimitiveType[poly->vtxFormat]; polyType = poly->type; // Set up the polygon if it changed @@ -1625,14 +1631,14 @@ static void OGLRender() { for(unsigned int j = 0; j < polyType; j++) { - GLushort vertIndex = poly->vertIndexes[j]; + const GLushort vertIndex = poly->vertIndexes[j]; // While we're looping through our vertices, add each vertex index to - // a buffer. For GL_QUADS and GL_QUAD_STRIP, we also add additional vertices - // here to convert them to GL_TRIANGLES, which are much easier to work with - // and won't be deprecated in future OpenGL versions. + // a buffer. For GFX3D_QUADS and GFX3D_QUAD_STRIP, we also add additional + // vertices here to convert them to GL_TRIANGLES, which are much easier + // to work with and won't be deprecated in future OpenGL versions. vertIndexBuffer[vertIndexCount++] = vertIndex; - if (polyPrimitive == GL_QUADS || polyPrimitive == GL_QUAD_STRIP) + if (poly->vtxFormat == GFX3D_QUADS || poly->vtxFormat == GFX3D_QUAD_STRIP) { if (j == 2) { @@ -1649,8 +1655,8 @@ static void OGLRender() { for(unsigned int j = 0; j < polyType; j++) { - GLushort vertIndex = poly->vertIndexes[j]; - GLushort colorIndex = vertIndex * 4; + const GLushort vertIndex = poly->vertIndexes[j]; + const GLushort colorIndex = vertIndex * 4; // Consolidate the vertex color and the poly alpha to our internal color buffer // so that OpenGL can use it. @@ -1660,12 +1666,12 @@ static void OGLRender() color4fBuffer[colorIndex+2] = material_8bit_to_float[vert->color[2]]; color4fBuffer[colorIndex+3] = polyAlpha; - // While we're looping through our vertices, add each vertex index to - // a buffer. For GL_QUADS and GL_QUAD_STRIP, we also add additional vertices - // here to convert them to GL_TRIANGLES, which are much easier to work with - // and won't be deprecated in future OpenGL versions. + // While we're looping through our vertices, add each vertex index to a + // buffer. For GFX3D_QUADS and GFX3D_QUAD_STRIP, we also add additional + // vertices here to convert them to GL_TRIANGLES, which are much easier + // to work with and won't be deprecated in future OpenGL versions. vertIndexBuffer[vertIndexCount++] = vertIndex; - if (polyPrimitive == GL_QUADS || polyPrimitive == GL_QUAD_STRIP) + if (poly->vtxFormat == GFX3D_QUADS || poly->vtxFormat == GFX3D_QUAD_STRIP) { if (j == 2) { @@ -1686,16 +1692,16 @@ static void OGLRender() // the same and we're not drawing a line loop or line strip. if (i+1 < polyCount) { - POLY *nextPoly = &gfx3d.polylist->list[gfx3d.indexlist.list[i+1]]; + const POLY *nextPoly = &gfx3d.polylist->list[gfx3d.indexlist.list[i+1]]; if (lastPolyAttr == nextPoly->polyAttr && lastTexParams == nextPoly->texParam && lastTexPalette == nextPoly->texPalette && - polyPrimitive == frm[nextPoly->vtxFormat] && + polyPrimitive == oglPrimitiveType[nextPoly->vtxFormat] && polyPrimitive != GL_LINE_LOOP && polyPrimitive != GL_LINE_STRIP && - frm[nextPoly->vtxFormat] != GL_LINE_LOOP && - frm[nextPoly->vtxFormat] != GL_LINE_STRIP) + oglPrimitiveType[nextPoly->vtxFormat] != GL_LINE_LOOP && + oglPrimitiveType[nextPoly->vtxFormat] != GL_LINE_STRIP) { needVertexUpload = false; } @@ -1706,24 +1712,13 @@ static void OGLRender() { // In wireframe mode, redefine all primitives as GL_LINE_LOOP rather than // setting the polygon mode to GL_LINE though glPolygonMode(). Not only is - // drawing more accurate this way, but it also allows GL_QUADS and - // GL_QUAD_STRIP primitives to properly draw as wireframe without the + // drawing more accurate this way, but it also allows GFX3D_QUADS and + // GFX3D_QUAD_STRIP primitives to properly draw as wireframe without the // extra diagonal line. if (poly->isWireframe()) { polyPrimitive = GL_LINE_LOOP; } - else if (polyPrimitive == GL_TRIANGLE_STRIP || polyPrimitive == GL_QUADS || polyPrimitive == GL_QUAD_STRIP) - { - // Redefine GL_QUADS and GL_QUAD_STRIP as GL_TRIANGLES since we converted them. - // - // Also redefine GL_TRIANGLE_STRIP as GL_TRIANGLES. This is okay since this - // is actually how the POLY struct stores triangle strip vertices, which is - // in sets of 3 vertices each. This redefinition is necessary since uploading - // more than 3 indices at a time will cause glDrawElements() to draw the - // triangle strip incorrectly. - polyPrimitive = GL_TRIANGLES; - } // Upload the vertices to the framebuffer. glDrawElements(polyPrimitive, vertIndexCount, GL_UNSIGNED_SHORT, vertIndexBuffer); diff --git a/desmume/src/gfx3d.cpp b/desmume/src/gfx3d.cpp index 40c82436b..30c089fc9 100644 --- a/desmume/src/gfx3d.cpp +++ b/desmume/src/gfx3d.cpp @@ -330,7 +330,7 @@ static u8 MM3x3ind = 0; // Data for vertex submission static CACHE_ALIGN s16 s16coord[4] = {0, 0, 0, 0}; static char coordind = 0; -static u32 vtxFormat = 0; +static u32 vtxFormat = GFX3D_TRIANGLES; static BOOL inBegin = FALSE; // Data for basic transforms @@ -519,7 +519,7 @@ void gfx3d_reset() mode = 0; s16coord[0] = s16coord[1] = s16coord[2] = s16coord[3] = 0; coordind = 0; - vtxFormat = 0; + vtxFormat = GFX3D_TRIANGLES; memset(trans, 0, sizeof(trans)); transind = 0; memset(scale, 0, sizeof(scale)); @@ -634,9 +634,9 @@ static void SetVertex() //TODO - viewport transform? int continuation = 0; - if(vtxFormat==2 && !tempVertInfo.first) + if(vtxFormat==GFX3D_TRIANGLE_STRIP && !tempVertInfo.first) continuation = 2; - else if(vtxFormat==3 && !tempVertInfo.first) + else if(vtxFormat==GFX3D_QUAD_STRIP && !tempVertInfo.first) continuation = 2; //record the vertex @@ -675,7 +675,7 @@ static void SetVertex() { polygonListCompleted = 2; switch(vtxFormat) { - case 0: //GL_TRIANGLES + case GFX3D_TRIANGLES: if(tempVertInfo.count!=3) break; polygonListCompleted = 1; @@ -687,7 +687,7 @@ static void SetVertex() polylist->list[polylist->count].type = 3; tempVertInfo.count = 0; break; - case 1: //GL_QUADS + case GFX3D_QUADS: if(tempVertInfo.count!=4) break; polygonListCompleted = 1; @@ -699,7 +699,7 @@ static void SetVertex() polylist->list[polylist->count].type = 4; tempVertInfo.count = 0; break; - case 2: //GL_TRIANGLE_STRIP + case GFX3D_TRIANGLE_STRIP: if(tempVertInfo.count!=3) break; polygonListCompleted = 1; @@ -722,7 +722,7 @@ static void SetVertex() tempVertInfo.first = false; tempVertInfo.count = 2; break; - case 3: //GL_QUAD_STRIP + case GFX3D_QUAD_STRIP: if(tempVertInfo.count!=4) break; polygonListCompleted = 1; diff --git a/desmume/src/gfx3d.h b/desmume/src/gfx3d.h index c8a75f079..1ced6b480 100644 --- a/desmume/src/gfx3d.h +++ b/desmume/src/gfx3d.h @@ -113,6 +113,16 @@ inline u32 RGB15TO6665(u16 col, u8 alpha5) // 15-bit to 24-bit depth formula from http://nocash.emubase.de/gbatek.htm#ds3drearplane #define DS_DEPTH15TO24(depth) ( dsDepthExtend_15bit_to_24bit[depth & 0x7FFF] ) +// POLYGON PRIMITIVE TYPES +enum +{ + GFX3D_TRIANGLES = 0, + GFX3D_QUADS = 1, + GFX3D_TRIANGLE_STRIP = 2, + GFX3D_QUAD_STRIP = 3, + GFX3D_LINE = 4 +}; + // POLYGON ATTRIBUTES - BIT LOCATIONS enum { @@ -257,7 +267,7 @@ struct POLY { else type = 3; } - u8 getAttributeEnableLightFlags() + u8 getAttributeEnableLightFlags() const { return ((polyAttr & (POLYGON_ATTR_ENABLE_LIGHT0_MASK | POLYGON_ATTR_ENABLE_LIGHT1_MASK | @@ -265,83 +275,83 @@ struct POLY { POLYGON_ATTR_ENABLE_LIGHT3_MASK)) >> POLYGON_ATTR_ENABLE_LIGHT0_BIT); } - bool getAttributeEnableLight0() + bool getAttributeEnableLight0() const { return ((polyAttr & POLYGON_ATTR_ENABLE_LIGHT0_MASK) > 0); } - bool getAttributeEnableLight1() + bool getAttributeEnableLight1() const { return ((polyAttr & POLYGON_ATTR_ENABLE_LIGHT1_MASK) > 0); } - bool getAttributeEnableLight2() + bool getAttributeEnableLight2() const { return ((polyAttr & POLYGON_ATTR_ENABLE_LIGHT2_MASK) > 0); } - bool getAttributeEnableLight3() + bool getAttributeEnableLight3() const { return ((polyAttr & POLYGON_ATTR_ENABLE_LIGHT3_MASK) > 0); } - u8 getAttributePolygonMode() + u8 getAttributePolygonMode() const { return ((polyAttr & POLYGON_ATTR_MODE_MASK) >> POLYGON_ATTR_MODE_BIT); } - u8 getAttributeEnableFaceCullingFlags() + u8 getAttributeEnableFaceCullingFlags() const { return ((polyAttr & (POLYGON_ATTR_ENABLE_BACK_SURFACE_MASK | POLYGON_ATTR_ENABLE_FRONT_SURFACE_MASK)) >> POLYGON_ATTR_ENABLE_BACK_SURFACE_BIT); } - bool getAttributeEnableBackSurface() + bool getAttributeEnableBackSurface() const { return ((polyAttr & POLYGON_ATTR_ENABLE_BACK_SURFACE_MASK) > 0); } - bool getAttributeEnableFrontSurface() + bool getAttributeEnableFrontSurface() const { return ((polyAttr & POLYGON_ATTR_ENABLE_FRONT_SURFACE_MASK) > 0); } - bool getAttributeEnableAlphaDepthWrite() + bool getAttributeEnableAlphaDepthWrite() const { return ((polyAttr & POLYGON_ATTR_ENABLE_ALPHA_DEPTH_WRITE_MASK) > 0); } - bool getAttributeEnableRenderOnFarPlaneIntersect() + bool getAttributeEnableRenderOnFarPlaneIntersect() const { return ((polyAttr & POLYGON_ATTR_ENABLE_RENDER_ON_FAR_PLANE_INTERSECT_MASK) > 0); } - bool getAttributeEnableOneDotRender() + bool getAttributeEnableOneDotRender() const { return ((polyAttr & POLYGON_ATTR_ENABLE_ONE_DOT_RENDER_MASK) > 0); } - bool getAttributeEnableDepthTest() + bool getAttributeEnableDepthTest() const { return ((polyAttr & POLYGON_ATTR_ENABLE_DEPTH_TEST_MASK) > 0); } - bool getAttributeEnableFog() + bool getAttributeEnableFog() const { return ((polyAttr & POLYGON_ATTR_ENABLE_FOG_MASK) > 0); } - u8 getAttributeAlpha() + u8 getAttributeAlpha() const { return ((polyAttr & POLYGON_ATTR_ALPHA_MASK) >> POLYGON_ATTR_ALPHA_BIT); } - u8 getAttributePolygonID() + u8 getAttributePolygonID() const { return ((polyAttr & POLYGON_ATTR_POLYGON_ID_MASK) >> POLYGON_ATTR_POLYGON_ID_BIT); } - PolygonAttributes getAttributes() + PolygonAttributes getAttributes() const { PolygonAttributes theAttr; @@ -368,57 +378,57 @@ struct POLY { return theAttr; } - u16 getTexParamVRAMOffset() + u16 getTexParamVRAMOffset() const { return ((texParam & TEXTURE_PARAM_VRAM_OFFSET_MASK) >> TEXTURE_PARAM_VRAM_OFFSET_BIT); } - bool getTexParamEnableRepeatS() + bool getTexParamEnableRepeatS() const { return ((texParam & TEXTURE_PARAM_ENABLE_REPEAT_S_MASK) > 0); } - bool getTexParamEnableRepeatT() + bool getTexParamEnableRepeatT() const { return ((texParam & TEXTURE_PARAM_ENABLE_REPEAT_T_MASK) > 0); } - bool getTexParamEnableMirroredRepeatS() + bool getTexParamEnableMirroredRepeatS() const { return ((texParam & TEXTURE_PARAM_ENABLE_MIRRORED_REPEAT_S_MASK) > 0); } - bool getTexParamEnableMirroredRepeatT() + bool getTexParamEnableMirroredRepeatT() const { return ((texParam & TEXTURE_PARAM_ENABLE_MIRRORED_REPEAT_T_MASK) > 0); } - u8 getTexParamSizeS() + u8 getTexParamSizeS() const { return ((texParam & TEXTURE_PARAM_SIZE_S_MASK) >> TEXTURE_PARAM_SIZE_S_BIT); } - u8 getTexParamSizeT() + u8 getTexParamSizeT() const { return ((texParam & TEXTURE_PARAM_SIZE_T_MASK) >> TEXTURE_PARAM_SIZE_T_BIT); } - u8 getTexParamTexFormat() + u8 getTexParamTexFormat() const { return ((texParam & TEXTURE_PARAM_FORMAT_MASK) >> TEXTURE_PARAM_FORMAT_BIT); } - bool getTexParamEnableTransparentColor0() + bool getTexParamEnableTransparentColor0() const { return ((texParam & TEXTURE_PARAM_ENABLE_TRANSPARENT_COLOR0_MASK) > 0); } - u8 getTexParamCoordTransformMode() + u8 getTexParamCoordTransformMode() const { return ((texParam & TEXTURE_PARAM_COORD_TRANSFORM_MODE_MASK) >> TEXTURE_PARAM_COORD_TRANSFORM_MODE_BIT); } - PolygonTexParams getTexParams() + PolygonTexParams getTexParams() const { PolygonTexParams theTexParams; @@ -436,17 +446,17 @@ struct POLY { return theTexParams; } - bool isWireframe() + bool isWireframe() const { return (this->getAttributeAlpha() == 0); } - bool isOpaque() + bool isOpaque() const { return (this->getAttributeAlpha() == 31); } - bool isTranslucent() + bool isTranslucent() const { // First, check if the polygon is wireframe or opaque. // If neither, then it must be translucent.