From df3cdf0ebbb66639eebe8b18183a97842a0a2c95 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Thu, 7 Mar 2019 21:41:25 +0000 Subject: [PATCH] Fix drawing polygons. Fixes #35 --- GL/draw.c | 31 ++++++++++++++++--------------- samples/nehe02/main.c | 35 ++++++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/GL/draw.c b/GL/draw.c index b922d21..0ca5b88 100644 --- a/GL/draw.c +++ b/GL/draw.c @@ -756,6 +756,8 @@ static void genArraysTriangleStrip( output[count - 1].flags = PVR_CMD_VERTEX_EOL; } +#define MAX_POLYGON_SIZE 10 + static void genArraysTriangleFan( ClipVertex* output, GLsizei count, @@ -772,25 +774,23 @@ static void genArraysTriangleFan( doTexture, doMultitexture, doLighting ); + assert(count < MAX_POLYGON_SIZE); + static ClipVertex buffer[MAX_POLYGON_SIZE]; + + memcpy(buffer, output, sizeof(ClipVertex) * count); + + // First 3 vertices are in the right place, just end early swapVertex(&output[1], &output[2]); output[2].flags = PVR_CMD_VERTEX_EOL; - GLsizei i = 3; + GLsizei i = 3, target = 3; ClipVertex* first = &output[0]; - for(; i < count - 1; ++i) { - ClipVertex* next = &output[i + 1]; - ClipVertex* previous = &output[i - 1]; - ClipVertex* vertex = &output[i]; - - *next = *first; - - swapVertex(next, vertex); - - vertex = next + 1; - *vertex = *previous; - - vertex->flags = PVR_CMD_VERTEX_EOL; + for(; i < count; ++i) { + output[target++] = *first; + output[target++] = buffer[i]; + output[target] = buffer[i - 1]; + output[target++].flags = PVR_CMD_VERTEX_EOL; } } @@ -838,6 +838,7 @@ static void generate(ClipVertex* output, const GLenum mode, const GLsizei first, doTexture, doMultitexture, doLighting ); break; + case GL_POLYGON: case GL_TRIANGLE_FAN: genArraysTriangleFan( output, @@ -887,7 +888,7 @@ static void generate(ClipVertex* output, const GLenum mode, const GLsizei first, nptr, nstride, doTexture, doMultitexture, doLighting ); - } else if(mode == GL_TRIANGLE_FAN) { + } else if(mode == GL_TRIANGLE_FAN || mode == GL_POLYGON) { genElementsTriangleFan( output, count, diff --git a/samples/nehe02/main.c b/samples/nehe02/main.c index 1f40acb..e8ec6f1 100644 --- a/samples/nehe02/main.c +++ b/samples/nehe02/main.c @@ -7,7 +7,7 @@ void InitGL(int Width, int Height) // We call this right after our OpenG { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black glClearDepth(1.0); // Enables Clearing Of The Depth Buffer - glDepthFunc(GL_LESS); // The Type Of Depth Test To Do + glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do glEnable(GL_DEPTH_TEST); // Enables Depth Testing glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading @@ -41,23 +41,36 @@ void DrawGLScene() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glLoadIdentity(); // Reset The View - glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0 + glTranslatef(-3.0f, 0.0f, -10.0f); // Move Left 1.5 Units And Into The Screen 6.0 // draw a triangle - glBegin(GL_POLYGON); // start drawing a polygon - glVertex3f( 0.0f, 1.0f, 0.0f); // Top - glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right - glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left + glBegin(GL_TRIANGLES); // start drawing a polygon + glVertex3f( 0.0f, 1.0f, 0.0f); // Top + glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right + glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left glEnd(); // we're done with the polygon - glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units + glTranslatef(3.0f, 0.0f, 0.0f); // Move Right 3 Units // draw a square (quadrilateral) glBegin(GL_QUADS); // start drawing a polygon (4 sided) - glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left - glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right - glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right - glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left + glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left + glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right + glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right + glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left + glEnd(); // done with the polygon + + glTranslatef(3.0f, 0.0f, 0.0f); + + glBegin(GL_POLYGON); // start drawing a polygon (4 sided) + glVertex3f(-0.0f, 1.0f, 0.0f); // Top Left + glVertex3f(-0.75f, 0.75f, 0.0f); + glVertex3f(-1.0f, 0.0f, 0.0f); // Top Right + glVertex3f(-0.75f,-0.75f, 0.0f); // Bottom Right + glVertex3f(-0.0f,-1.0f, 0.0f); // Bottom Left + glVertex3f( 0.75f,-0.75f, 0.0f); // Bottom Right + glVertex3f( 1.0f, 0.0f, 0.0f); // Top Right + glVertex3f( 0.75f, 0.75f, 0.0f); glEnd(); // done with the polygon // swap buffers to display, since we're double buffered.