Add polygon_offset sample (thanks mrneo240)

This commit is contained in:
Luke Benstead 2019-07-17 10:45:29 +01:00
parent a616fe8f27
commit 00c38edba9
4 changed files with 246 additions and 0 deletions

View File

@ -232,6 +232,13 @@ __BEGIN_DECLS
#define GL_SINGLE_COLOR 0x81F9
#define GL_SEPARATE_SPECULAR_COLOR 0x81FA
/* glPolygonOffset */
#define GL_POLYGON_OFFSET_FACTOR 0x8038
#define GL_POLYGON_OFFSET_UNITS 0x2A00
#define GL_POLYGON_OFFSET_POINT 0x2A01
#define GL_POLYGON_OFFSET_LINE 0x2A02
#define GL_POLYGON_OFFSET_FILL 0x8037
/* Client state caps */
#define GL_VERTEX_ARRAY 0x8074
#define GL_NORMAL_ARRAY 0x8075

View File

@ -26,3 +26,4 @@ all:
$(KOS_MAKE) -C paletted_pcx all
$(KOS_MAKE) -C depth_funcs all
$(KOS_MAKE) -C polymark all
$(KOS_MAKE) -C polygon_offset all

View File

@ -0,0 +1,23 @@
TARGET = offset.elf
OBJS = main.o
all: rm-elf $(TARGET)
include $(KOS_BASE)/Makefile.rules
clean:
-rm -f $(TARGET) $(OBJS)
rm-elf:
-rm -f $(TARGET)
$(TARGET): $(OBJS)
$(KOS_CC) $(KOS_CFLAGS) $(KOS_LDFLAGS) -o $(TARGET) $(KOS_START) \
$(OBJS) $(OBJEXTRA) -lm -lkosutils $(KOS_LIBS)
run: $(TARGET)
$(KOS_LOADER) $(TARGET)
dist: $(TARGET)
$(KOS_CC_BASE)/sh-elf/bin/objcopy -R .stack -O binary $(TARGET) $(basename $(TARGET))
$(KOS_BASE)/utils/scramble/scramble $(basename $(TARGET)) 1ST_READ.BIN

View File

@ -0,0 +1,215 @@
#include <kos.h>
typedef enum
{
false,
true
} bool;
#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.93f, 0.93f, 0.93f, 0.0f);
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LEQUAL); // 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, 1.0f, 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, 1.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
float xrot = 0.0f;
float yrot = 0.0f;
float xdiff = 0;
float ydiff = 0;
bool offset = true;
void drawBox()
{
glBegin(GL_QUADS);
// FRONT
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
// BACK
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
// LEFT
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
// RIGHT
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
// TOP
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
// BOTTOM
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glEnd();
}
void drawPolygon()
{
glBegin(GL_QUADS);
glVertex3f(-0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, 0.5f, 0.0f);
glVertex3f(-0.5f, 0.5f, 0.0f);
glEnd();
}
int frames = 0;
void check_input()
{
maple_device_t *cont;
cont_state_t *state;
cont = maple_enum_type(0, MAPLE_FUNC_CONTROLLER);
if (cont)
{
state = (cont_state_t *)maple_dev_status(cont);
if (state)
{
if (frames <= 0)
{
if (state->buttons & CONT_START)
{
offset = !offset;
frames = 60;
}
}
}
}
}
/* 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
gluLookAt(
0.0f, 0.0f, 3.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(1.0f, 0.0f, 0.0f);
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
drawBox();
if (offset)
{
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0f, 1.0f);
}
glColor3f(0.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(-0.25f, -0.25f, 0.5f);
glScalef(0.5f, 0.5f, 0.5f);
drawPolygon();
glPopMatrix();
glColor3f(1.0f, 1.0f, 0.0f);
glPushMatrix();
glTranslatef(0.25f, 0.25f, 0.5f);
glScalef(0.5f, 0.5f, 0.5f);
drawPolygon();
glPopMatrix();
if (offset)
{
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(-1.0f, -1.0f);
}
glColor3f(0.0f, 0.0f, 1.0f);
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.5f);
glScalef(0.5f, 0.5f, 0.5f);
drawPolygon();
glPopMatrix();
if (offset)
glDisable(GL_POLYGON_OFFSET_FILL);
#if 0
if (offset)
{
glEnable(GL_POLYGON_OFFSET_LINE);
glPolygonOffset(-1.0f, -1.0f);
}
glColor3f(0.0f, 0.0f, 0.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
drawBox();
if (offset)
glDisable(GL_POLYGON_OFFSET_LINE);
glFlush();
#endif
// 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)
{
check_input();
DrawGLScene();
xrot += 0.1f;
yrot += 0.25f;
frames--;
}
return 0;
}