From 09789c9adc839f42bcccd90cdaa690b2a577922a Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Fri, 15 Jun 2018 13:59:59 +0100 Subject: [PATCH] Make sure we have access to the visible bitmask --- GL/clip.c | 6 +-- GL/clip.h | 2 +- GL/draw.c | 8 ++- samples/zclip1/Makefile | 29 +++++++++++ samples/zclip1/main.c | 68 +++++++++++++++++++++++++ samples/zclip1/romdisk/PLACEHOLDER | 0 samples/zclip2/Makefile | 29 +++++++++++ samples/zclip2/main.c | 79 ++++++++++++++++++++++++++++++ samples/zclip2/romdisk/PLACEHOLDER | 0 9 files changed, 215 insertions(+), 6 deletions(-) create mode 100644 samples/zclip1/Makefile create mode 100644 samples/zclip1/main.c create mode 100644 samples/zclip1/romdisk/PLACEHOLDER create mode 100644 samples/zclip2/Makefile create mode 100644 samples/zclip2/main.c create mode 100644 samples/zclip2/romdisk/PLACEHOLDER diff --git a/GL/clip.c b/GL/clip.c index 6a7f179..9341917 100644 --- a/GL/clip.c +++ b/GL/clip.c @@ -39,7 +39,7 @@ ClipResult clipLineToNearZ(const float* v1, const float* v2, const float dist, f TriangleClipResult clipTriangleToNearZ( const float plane_dist, const unsigned short triangle_n, const pvr_vertex_t* v1, const pvr_vertex_t* v2, const pvr_vertex_t *v3, - pvr_vertex_t* v1out, pvr_vertex_t* v2out, pvr_vertex_t* v3out, pvr_vertex_t* v4out + pvr_vertex_t* v1out, pvr_vertex_t* v2out, pvr_vertex_t* v3out, pvr_vertex_t* v4out, unsigned char* visible ) { /* Fast out. Let's just see if everything is in front of the clip plane (and as in OpenGL Z comes out of the screen @@ -47,9 +47,9 @@ TriangleClipResult clipTriangleToNearZ( */ typedef unsigned char uint8; - uint8 visible = ((v1->z <= -plane_dist) ? 1 : 0) | ((v2->z <= -plane_dist) ? 2 : 0) | ((v3->z <= -plane_dist) ? 4 : 0); + *visible = ((v1->z <= -plane_dist) ? 1 : 0) | ((v2->z <= -plane_dist) ? 2 : 0) | ((v3->z <= -plane_dist) ? 4 : 0); - switch(visible) { + switch(*visible) { case 0b000: /* If behind is zero, then none of the vertices are visible */ return TRIANGLE_CLIP_RESULT_DROP_TRIANGLE; diff --git a/GL/clip.h b/GL/clip.h index d2f582f..204cc47 100644 --- a/GL/clip.h +++ b/GL/clip.h @@ -59,7 +59,7 @@ typedef enum { TriangleClipResult clipTriangleToNearZ( const float plane_dist, const unsigned short triangle_n, const pvr_vertex_t* v1, const pvr_vertex_t* v2, const pvr_vertex_t *v3, - pvr_vertex_t* v1out, pvr_vertex_t* v2out, pvr_vertex_t* v3out, pvr_vertex_t* v4out + pvr_vertex_t* v1out, pvr_vertex_t* v2out, pvr_vertex_t* v3out, pvr_vertex_t* v4out, unsigned char* visible ); #ifdef __cplusplus diff --git a/GL/draw.c b/GL/draw.c index 3f1f99e..144092e 100644 --- a/GL/draw.c +++ b/GL/draw.c @@ -267,7 +267,7 @@ static void submitVertices(GLenum mode, GLsizei first, GLsizei count, GLenum typ Triangle.vcount = 0; /* Loop 1. Calculate vertex colours, transform, but don't apply perspective division */ - for(rel = 0, i = first; i < count; ++i, ++rel) { + for(rel = 0, i = first; i < first + count; ++i, ++rel) { pvr_vertex_t* vertex = (pvr_vertex_t*) dst; vertex->u = vertex->v = 0.0f; vertex->argb = 0; @@ -350,6 +350,7 @@ static void submitVertices(GLenum mode, GLsizei first, GLsizei count, GLenum typ Triangle.vcount++; if(Triangle.vcount == 3) { pvr_vertex_t clipped[4]; + GLubyte visible; /* Bitmask of which of the 3 input vertices are visible */ /* OK we have a whole triangle, we may have to clip */ TriangleClipResult tri_result = clipTriangleToNearZ( @@ -361,7 +362,8 @@ static void submitVertices(GLenum mode, GLsizei first, GLsizei count, GLenum typ &clipped[0], &clipped[1], &clipped[2], - &clipped[3] + &clipped[3], + &visible ); /* The potential 4 new vertices that can be output by clipping the triangle. Initialized in the below branches */ @@ -450,6 +452,8 @@ static void submitVertices(GLenum mode, GLsizei first, GLsizei count, GLenum typ } } } else if(tri_result == TRIANGLE_CLIP_RESULT_ALTERED_VERTICES) { + /* We're here because a single vertex was visible, the other two vertices were changed */ + } else if(tri_result == TRIANGLE_CLIP_RESULT_ALTERED_AND_CREATED_VERTEX) { diff --git a/samples/zclip1/Makefile b/samples/zclip1/Makefile new file mode 100644 index 0000000..2c2dbda --- /dev/null +++ b/samples/zclip1/Makefile @@ -0,0 +1,29 @@ +TARGET = zclip1.elf +OBJS = main.o + +all: rm-elf $(TARGET) + +include $(KOS_BASE)/Makefile.rules + +clean: + -rm -f $(TARGET) $(OBJS) romdisk.* + +rm-elf: + -rm -f $(TARGET) romdisk.* + +$(TARGET): $(OBJS) romdisk.o + $(KOS_CC) $(KOS_CFLAGS) $(KOS_LDFLAGS) -o $(TARGET) $(KOS_START) \ + $(OBJS) romdisk.o $(OBJEXTRA) -lm -lkosutils $(KOS_LIBS) + +romdisk.img: + $(KOS_GENROMFS) -f romdisk.img -d romdisk -v + +romdisk.o: romdisk.img + $(KOS_BASE)/utils/bin2o/bin2o romdisk.img romdisk romdisk.o + +run: $(TARGET) + $(KOS_LOADER) $(TARGET) + +dist: + rm -f $(OBJS) romdisk.o romdisk.img + $(KOS_STRIP) $(TARGET) diff --git a/samples/zclip1/main.c b/samples/zclip1/main.c new file mode 100644 index 0000000..5eb9394 --- /dev/null +++ b/samples/zclip1/main.c @@ -0,0 +1,68 @@ +#include "gl.h" +#include "glu.h" +#include "glkos.h" + +/* A general OpenGL initialization function. Sets all of the initial parameters. */ +void InitGL(int Width, int Height) // We call this right after our OpenGL window is created. +{ + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black + glDepthFunc(GL_LESS); // The Type Of Depth Test To Do + glEnable(GL_DEPTH_TEST); // Enables Depth Testing + glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); // Reset The Projection Matrix + + gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window + + glMatrixMode(GL_MODELVIEW); +} + +/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */ +void ReSizeGLScene(int Width, int Height) +{ + if (Height == 0) // Prevent A Divide By Zero If The Window Is Too Small + Height = 1; + + glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); + glMatrixMode(GL_MODELVIEW); +} + + +/* The main drawing function. */ +void DrawGLScene() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer + glLoadIdentity(); // Reset The View + + glDisable(GL_CULL_FACE); + + /* Draw a triangle intersecting the screen with a single vertex visible */ + glBegin(GL_TRIANGLES); + glVertex3f(0.0f, 0.0f, -5.0f); + glVertex3f(-0.5f, -1.0f, 5.0f); + glVertex3f(0.5, -1.0f, 5.0f); + glEnd(); + + // swap buffers to display, since we're double buffered. + glKosSwapBuffers(); +} + +int main(int argc, char **argv) +{ + glKosInit(); + + InitGL(640, 480); + ReSizeGLScene(640, 480); + + while(1) { + DrawGLScene(); + } + + return 0; +} diff --git a/samples/zclip1/romdisk/PLACEHOLDER b/samples/zclip1/romdisk/PLACEHOLDER new file mode 100644 index 0000000..e69de29 diff --git a/samples/zclip2/Makefile b/samples/zclip2/Makefile new file mode 100644 index 0000000..4825fb2 --- /dev/null +++ b/samples/zclip2/Makefile @@ -0,0 +1,29 @@ +TARGET = zclip2.elf +OBJS = main.o + +all: rm-elf $(TARGET) + +include $(KOS_BASE)/Makefile.rules + +clean: + -rm -f $(TARGET) $(OBJS) romdisk.* + +rm-elf: + -rm -f $(TARGET) romdisk.* + +$(TARGET): $(OBJS) romdisk.o + $(KOS_CC) $(KOS_CFLAGS) $(KOS_LDFLAGS) -o $(TARGET) $(KOS_START) \ + $(OBJS) romdisk.o $(OBJEXTRA) -lm -lkosutils $(KOS_LIBS) + +romdisk.img: + $(KOS_GENROMFS) -f romdisk.img -d romdisk -v + +romdisk.o: romdisk.img + $(KOS_BASE)/utils/bin2o/bin2o romdisk.img romdisk romdisk.o + +run: $(TARGET) + $(KOS_LOADER) $(TARGET) + +dist: + rm -f $(OBJS) romdisk.o romdisk.img + $(KOS_STRIP) $(TARGET) diff --git a/samples/zclip2/main.c b/samples/zclip2/main.c new file mode 100644 index 0000000..1f40acb --- /dev/null +++ b/samples/zclip2/main.c @@ -0,0 +1,79 @@ +#include "gl.h" +#include "glu.h" +#include "glkos.h" + +/* A general OpenGL initialization function. Sets all of the initial parameters. */ +void InitGL(int Width, int Height) // We call this right after our OpenGL window is created. +{ + 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 + glEnable(GL_DEPTH_TEST); // Enables Depth Testing + glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); // Reset The Projection Matrix + + gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window + + glMatrixMode(GL_MODELVIEW); +} + +/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */ +void ReSizeGLScene(int Width, int Height) +{ + if (Height == 0) // Prevent A Divide By Zero If The Window Is Too Small + Height = 1; + + glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); + glMatrixMode(GL_MODELVIEW); +} + + +/* The main drawing function. */ +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 + + // 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 + glEnd(); // we're done with the polygon + + 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 + glEnd(); // done with the polygon + + // swap buffers to display, since we're double buffered. + glKosSwapBuffers(); +} + +int main(int argc, char **argv) +{ + glKosInit(); + + InitGL(640, 480); + ReSizeGLScene(640, 480); + + while(1) { + DrawGLScene(); + } + + return 0; +} diff --git a/samples/zclip2/romdisk/PLACEHOLDER b/samples/zclip2/romdisk/PLACEHOLDER new file mode 100644 index 0000000..e69de29