From 9167869633f85b88da2e04d20719db05a46f7a33 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Tue, 29 May 2018 19:39:27 +0100 Subject: [PATCH] Add gluOrtho2D, ortho2d example and fix glOrtho --- GL/glu.c | 6 ++- GL/matrix.c | 16 +------ Makefile | 2 +- include/glu.h | 2 + samples/Makefile | 3 +- samples/ortho2d/Makefile | 29 +++++++++++++ samples/ortho2d/main.c | 67 +++++++++++++++++++++++++++++ samples/ortho2d/romdisk/PLACEHOLDER | 0 8 files changed, 108 insertions(+), 17 deletions(-) create mode 100644 samples/ortho2d/Makefile create mode 100644 samples/ortho2d/main.c create mode 100644 samples/ortho2d/romdisk/PLACEHOLDER diff --git a/GL/glu.c b/GL/glu.c index 3432e95..ae9a590 100644 --- a/GL/glu.c +++ b/GL/glu.c @@ -2,7 +2,7 @@ #include "private.h" /* Set the Perspective */ -void gluPerspective(GLfloat angle, GLfloat aspect, +void APIENTRY gluPerspective(GLfloat angle, GLfloat aspect, GLfloat znear, GLfloat zfar) { GLfloat xmin, xmax, ymin, ymax; @@ -13,3 +13,7 @@ void gluPerspective(GLfloat angle, GLfloat aspect, glFrustum(xmin, xmax, ymin, ymax, znear, zfar); } + +void APIENTRY gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top) { + glOrtho(left, right, bottom, top, -1.0f, 1.0f); +} diff --git a/GL/matrix.c b/GL/matrix.c index 2b78f63..406deab 100644 --- a/GL/matrix.c +++ b/GL/matrix.c @@ -176,9 +176,9 @@ void APIENTRY glOrtho(GLfloat left, GLfloat right, OrthoMatrix[3][1] = -(top + bottom) / (top - bottom); OrthoMatrix[3][2] = -(zfar + znear) / (zfar - znear); - mat_load(stack_top(MATRIX_STACKS + MATRIX_MODE)); + mat_load(stack_top(MATRIX_STACKS + MATRIX_IDX)); mat_apply(&OrthoMatrix); - mat_store(stack_top(MATRIX_STACKS + MATRIX_MODE)); + mat_store(stack_top(MATRIX_STACKS + MATRIX_IDX)); } @@ -207,18 +207,6 @@ void APIENTRY glFrustum(GLfloat left, GLfloat right, mat_store(stack_top(MATRIX_STACKS + MATRIX_IDX)); } -/* Set the Perspective */ -void gluPerspective(GLfloat angle, GLfloat aspect, - GLfloat znear, GLfloat zfar) { - GLfloat xmin, xmax, ymin, ymax; - - ymax = znear * ftan(angle * F_PI / 360.0f); - ymin = -ymax; - xmin = ymin * aspect; - xmax = ymax * aspect; - - glFrustum(xmin, xmax, ymin, ymax, znear, zfar); -} /* Multiply the current matrix by an arbitrary matrix */ void glMultMatrixf(const GLfloat *m) { diff --git a/Makefile b/Makefile index 0be6dc5..5dc4723 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # Copyright (C) 2018 Luke Benstead TARGET = libGLdc.a -OBJS = GL/draw.o GL/flush.o GL/framebuffer.o GL/immediate.o GL/lighting.o GL/state.o GL/texture.o +OBJS = GL/draw.o GL/flush.o GL/framebuffer.o GL/immediate.o GL/lighting.o GL/state.o GL/texture.o GL/glu.o OBJS += GL/matrix.o GL/fog.o GL/error.o containers/stack.o containers/named_array.o containers/aligned_vector.o SUBDIRS = diff --git a/include/glu.h b/include/glu.h index d31f83a..723ae93 100644 --- a/include/glu.h +++ b/include/glu.h @@ -23,6 +23,8 @@ __BEGIN_DECLS #define GLU_FALSE 0 #define GLU_TRUE 1 +GLAPI void APIENTRY gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top); + /* gluPerspective - Set the Perspective for Rendering. */ GLAPI void APIENTRY gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar); diff --git a/samples/Makefile b/samples/Makefile index 2f338e4..4798342 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -10,4 +10,5 @@ all: $(KOS_MAKE) -C nehe02 all $(KOS_MAKE) -C nehe02va all $(KOS_MAKE) -C nehe03 all - $(KOS_MAKE) -C nehe06 all \ No newline at end of file + $(KOS_MAKE) -C nehe06 all + $(KOS_MAKE) -C ortho2d all \ No newline at end of file diff --git a/samples/ortho2d/Makefile b/samples/ortho2d/Makefile new file mode 100644 index 0000000..57dd4d5 --- /dev/null +++ b/samples/ortho2d/Makefile @@ -0,0 +1,29 @@ +TARGET = ortho2d.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/ortho2d/main.c b/samples/ortho2d/main.c new file mode 100644 index 0000000..e2173da --- /dev/null +++ b/samples/ortho2d/main.c @@ -0,0 +1,67 @@ +#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 + + gluOrtho2D(0, 640, 0, 480); + + 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(); + + gluOrtho2D(0, 640, 0, 480); + + 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 + + // draw a triangle + glBegin(GL_TRIANGLES); // start drawing a polygon + glVertex2f( 320.0f, 360.0f); // Top + glVertex2f( 120.0f, 120.0f); + glVertex2f( 640.0f - 120.0f, 120.0f); + glEnd(); // we're done with the polygon + + glKosSwapBuffers(); +} + +int main(int argc, char **argv) +{ + glKosInit(); + + InitGL(640, 480); + ReSizeGLScene(640, 480); + + while(1) { + DrawGLScene(); + } + + return 0; +} diff --git a/samples/ortho2d/romdisk/PLACEHOLDER b/samples/ortho2d/romdisk/PLACEHOLDER new file mode 100644 index 0000000..e69de29