From 61b809f5cd7bce8f2dec2f40da510551daffc6f0 Mon Sep 17 00:00:00 2001
From: Lawrence Sebald <ljsebald@users.sourceforge.net>
Date: Fri, 29 Aug 2014 15:38:42 -0400
Subject: [PATCH] Add glGetFloatv() for reading matrices as requested in
 #dreamcastdev on IRC.

This commit also adds a new glKosGetMatrix() function that does the same thing,
but isn't limited to the modelview, projection, and texture matrices, as
glGetFloatv() is.
---
 gl-api.c     | 17 ++++++++++++++++-
 gl-matrix.c  |  8 ++++++++
 include/gl.h |  8 ++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/gl-api.c b/gl-api.c
index 2577e16..c1f3313 100755
--- a/gl-api.c
+++ b/gl-api.c
@@ -1,7 +1,8 @@
 /* KallistiGL for KallistiOS ##version##
 
-   libgl/gl.h
+   libgl/gl-api.c
    Copyright (C) 2013-2014 Josh "PH3NOM" Pearson
+   Copyright (C) 2014 Lawrence Sebald
 
    Some functionality adapted from the original KOS libgl:
    Copyright (C) 2001 Dan Potter
@@ -161,6 +162,20 @@ void glGetIntegerv(GLenum pname, GLint *params) {
     }
 }
 
+void glGetFloatv(GLenum pname, GLfloat *params) {
+    switch(pname) {
+        case GL_MODELVIEW_MATRIX:
+        case GL_PROJECTION_MATRIX:
+        case GL_TEXTURE_MATRIX:
+            glKosGetMatrix(pname - GL_MODELVIEW_MATRIX + 1, params);
+            break;
+
+        default:
+            *params = (GLfloat)GL_INVALID_ENUM;
+            break;
+    }
+}
+
 static void _glKosInitTextures() {
     GLuint i;
 
diff --git a/gl-matrix.c b/gl-matrix.c
index 383ff91..f49731e 100755
--- a/gl-matrix.c
+++ b/gl-matrix.c
@@ -2,6 +2,7 @@
 
    libgl/gl-matrix.c
    Copyright (C) 2013-2014 Josh "PH3NOM" Pearson
+   Copyright (C) 2014 Lawrence Sebald
 
    Some functionality adapted from the original KOS libgl:
    Copyright (C) 2001 Dan Potter
@@ -393,3 +394,10 @@ void _glKosInitMatrix() {
     glDepthRange(0.0f, 1.0f);
     glViewport(0, 0, vid_mode->width, vid_mode->height);
 }
+
+void glKosGetMatrix(GLenum mode, GLfloat *params) {
+    if(mode < GL_SCREENVIEW || mode > GL_RENDER)
+        *params = (GLfloat)GL_INVALID_ENUM;
+
+    memcpy(params, Matrix + mode, sizeof(GLfloat) * 16);
+}
diff --git a/include/gl.h b/include/gl.h
index b56d857..f6737a1 100755
--- a/include/gl.h
+++ b/include/gl.h
@@ -2,6 +2,7 @@
 
    libgl/gl.h
    Copyright (C) 2013-2014 Josh "PH3NOM" Pearson
+   Copyright (C) 2014 Lawrence Sebald
 
    Some functionality adapted from the original KOS libgl:
    Copyright (C) 2001 Dan Potter
@@ -63,6 +64,10 @@ __BEGIN_DECLS
 #define GL_RENDER           0x05
 #define GL_MATRIX_COUNT     0x06
 
+#define GL_MODELVIEW_MATRIX   0x0BA6
+#define GL_PROJECTION_MATRIX  0x0BA7
+#define GL_TEXTURE_MATRIX     0x0BA8
+
 /* Depth buffer */
 #define GL_NEVER              0x0200
 #define GL_LESS               0x0201
@@ -478,6 +483,8 @@ GLAPI void APIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height);
 
 GLAPI void APIENTRY glScissor(GLint x, GLint y, GLsizei width, GLsizei height);
 
+GLAPI void APIENTRY glKosGetMatrix(GLenum mode, GLfloat *params);
+
 
 /* Fog Functions - client must enable GL_FOG for this to take effect */
 GLAPI void APIENTRY glFogi(GLenum pname, GLint param);
@@ -506,6 +513,7 @@ GLAPI GLuint APIENTRY glKosMipMapTexSize(GLuint width, GLuint height);
 
 /* glGet Functions */
 void glGetIntegerv(GLenum pname, GLint *params);
+void glGetFloatv(GLenum pname, GLfloat *params);
 
 /* Multi-Texture Extensions */
 GLAPI void APIENTRY glActiveTexture(GLenum texture);