[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [47804] branches/soc-2012-swiss_cheese/ source/blender: Adding gpuOrtho, gpuFrustum, gpuLookAt and respective functions to math_matrix.c

Alexander Kuznetsov kuzsasha at gmail.com
Tue Jun 12 23:26:15 CEST 2012


Revision: 47804
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=47804
Author:   alexk
Date:     2012-06-12 21:26:13 +0000 (Tue, 12 Jun 2012)
Log Message:
-----------
Adding gpuOrtho, gpuFrustum, gpuLookAt and respective functions to math_matrix.c
gpuLoad*** just writes the matrix to the stack instead of multiplying 
mat4_look_from_origin is loosly based on Mesa implemintation

Modified Paths:
--------------
    branches/soc-2012-swiss_cheese/source/blender/blenlib/BLI_math_matrix.h
    branches/soc-2012-swiss_cheese/source/blender/blenlib/intern/math_matrix.c
    branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_matrix.h
    branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_matrix.c

Modified: branches/soc-2012-swiss_cheese/source/blender/blenlib/BLI_math_matrix.h
===================================================================
--- branches/soc-2012-swiss_cheese/source/blender/blenlib/BLI_math_matrix.h	2012-06-12 21:25:29 UTC (rev 47803)
+++ branches/soc-2012-swiss_cheese/source/blender/blenlib/BLI_math_matrix.h	2012-06-12 21:26:13 UTC (rev 47804)
@@ -180,6 +180,13 @@
 int is_negative_m3(float mat[3][3]);
 int is_negative_m4(float mat[4][4]);
 
+/******************************** Projections ********************************/
+
+void mat4_ortho_set(float m[4][4], float left, float right, float bottom, float top, float nearVal, float farVal);
+void mat4_frustum_set(float m[4][4], float left, float right, float bottom, float top, float nearVal, float farVal);
+
+void mat4_look_from_origin(float m[4][4], float lookdir[3], float camup[3]);
+
 /*********************************** Other ***********************************/
 
 void print_m3(const char *str, float M[3][3]);

Modified: branches/soc-2012-swiss_cheese/source/blender/blenlib/intern/math_matrix.c
===================================================================
--- branches/soc-2012-swiss_cheese/source/blender/blenlib/intern/math_matrix.c	2012-06-12 21:25:29 UTC (rev 47803)
+++ branches/soc-2012-swiss_cheese/source/blender/blenlib/intern/math_matrix.c	2012-06-12 21:26:13 UTC (rev 47804)
@@ -1850,3 +1850,83 @@
 
 	mul_serie_m4(Ainv, U, Wm, V, NULL, NULL, NULL, NULL, NULL);
 }
+
+
+void mat4_ortho_set(float m[][4], float left, float right, float bottom, float top, float nearVal, float farVal)
+{
+
+	m[0][0] = 2.0/(right-left);	m[1][0] = 0.0f;			m[2][0] = 0.0f;						m[3][0] = -((double)right+left)/(right-left);
+	m[0][1] = 0.0f;				m[1][1] = 2.0/(top-bottom);m[2][1] = 0.0f;					m[3][1] = -((double)top+bottom)/(top-bottom);
+	m[0][2] = 0.0f;				m[1][2] = 0.0f;			m[2][2] = -2.0/(farVal-nearVal);	m[3][2] = -((double)farVal+nearVal)/(farVal-nearVal);
+	m[0][3] = 0.0f;				m[1][3] = 0.0f;			m[2][3] = 0.0f;						m[3][3] = 1.0;
+
+
+}
+
+
+void mat4_frustum_set(float m[][4], float left, float right, float bottom, float top, float nearVal, float farVal)
+{
+
+	m[0][0] = 2.0*nearVal/(right-left);	m[1][0] = 0.0f;	m[2][0] = (right+left)/(right-left);	m[3][0] = 0.0f;
+	m[0][1] = 0.0f;	m[1][1] = 2.0*nearVal/(top-bottom);	m[2][1] = (top+bottom)/(top-bottom);	m[3][1] = 0.0f;
+	m[0][2] = 0.0f;	m[1][2] = 0.0f;			m[2][2] = -(farVal+nearVal)/(farVal-nearVal);	m[3][2] = -2.0*farVal*nearVal/(farVal-nearVal);
+	m[0][3] = 0.0f;	m[1][3] = 0.0f;			m[2][3] = - 1.0f;								m[3][3] = 0.0f;
+
+}
+
+/*
+
+  Following functions are loosly base on Mesa implementation:
+	mat4_look_from_origin -> gluLookAt
+
+ *
+ * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
+ * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice including the dates of first publication and
+ * either this permission notice or a reference to
+ * http://oss.sgi.com/projects/FreeB/
+ * shall be included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Except as contained in this notice, the name of Silicon Graphics, Inc.
+ * shall not be used in advertising or otherwise to promote the sale, use or
+ * other dealings in this Software without prior written authorization from
+ * Silicon Graphics, Inc.
+ */
+
+
+void mat4_look_from_origin(float m[4][4], float lookdir[3], float camup[3])
+{
+
+	float side[3];
+
+	normalize_v3(lookdir);
+
+	cross_v3_v3v3(side, lookdir, camup);
+
+	normalize_v3(side);
+
+	cross_v3_v3v3(camup, side, lookdir);
+
+	m[0][0] = side[0];		m[1][0] = side[1];		m[2][0] = side[2];		m[3][0] = 0.0f;
+	m[0][1] = camup[0];		m[1][1] = camup[1];		m[2][1] = camup[2];		m[3][1] = 0.0f;
+	m[0][2] = -lookdir[0];	m[1][2] = -lookdir[1];	m[2][2] = -lookdir[2];	m[3][2] = 0.0f;
+	m[0][3] = 0.0f;			m[1][3] = 0.0f;			m[2][3] = 0.0f;			m[3][3] = 1.0f;
+
+
+}

Modified: branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_matrix.h
===================================================================
--- branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_matrix.h	2012-06-12 21:25:29 UTC (rev 47803)
+++ branches/soc-2012-swiss_cheese/source/blender/gpu/GPU_matrix.h	2012-06-12 21:26:13 UTC (rev 47804)
@@ -3,6 +3,9 @@
 extern "C" {
 #endif
 
+#ifndef GPU_MATRIX
+#define GPU_MATRIX
+
 #define GPU_MODELVIEW	1<<0
 #define GPU_PROJECTION	1<<1
 #define GPU_TEXTURE		1<<2
@@ -21,13 +24,38 @@
 void gpuMatrixMode(int mode);
 
 void gpuLoadMatrix(const float * m);
-void gpuGetMatrix(float * m);
+float * gpuGetMatrix(float * m);
 
 void gpuLoadIdentity(void);
 
+void gpuMultMatrix(const float *m);
+
 void gpuTranslate(float x, float y, float z);
 void gpuScale(float x, float y, float z);
 
+void gpuOrtho(float left, float right, float bottom, float top, float nearVal, float farVal);
+void gpuFrustum(float left, float right, float bottom, float top, float nearVal, float farVal);
+
+void gpuLoadOrtho(float left, float right, float bottom, float top, float nearVal, float farVal);
+void gpuLoadFrustum(float left, float right, float bottom, float top, float nearVal, float farVal);
+
+void gpuLookAt(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ);
+
+
+#ifndef GPU_MAT_CAST_ANY
+#define GPU_MAT_CAST_ANY
+
+
+#define gpuLoadMatrix(m) gpuLoadMatrix((const float *) m);
+#define gpuGetMatrix(m) gpuGetMatrix((float *) m);
+#define gpuMultMatrix(m) gpuMultMatrix((const float *) m);
+
+#endif
+
+#endif
+
 #ifdef __cplusplus
 }
 #endif
+
+

Modified: branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_matrix.c
===================================================================
--- branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_matrix.c	2012-06-12 21:25:29 UTC (rev 47803)
+++ branches/soc-2012-swiss_cheese/source/blender/gpu/intern/gpu_matrix.c	2012-06-12 21:26:13 UTC (rev 47804)
@@ -4,8 +4,9 @@
 
 #include "BLI_math_matrix.h"
 #include "BLI_math_rotation.h"
+#include "BLI_math_vector.h"
 
-
+#define GPU_MAT_CAST_ANY
 #include "GPU_matrix.h"
 
 #ifdef GLES
@@ -14,6 +15,8 @@
 #include <GL/glew.h>
 #endif
 
+#define GLU_STACK_DEBUG;
+
 typedef float GPU_matrix[4][4];
 
 typedef struct GPU_matrix_stack
@@ -32,8 +35,30 @@
 
 GPU_matrix_stack * ms_current;
 
+
+
 #define CURMATRIX (ms_current->dynstack[ms_current->pos])
 
+
+/* Check if we have a good matrix */
+
+static void checkmat(float *m)
+{
+	int i;
+	for(i=0;i<16;i++){
+		if(isinf(m[i]))
+		{assert(0);};};
+
+
+}
+
+#if 0
+#define CHECKMAT checkmat(CURMATRIX);
+#else
+#define CHECKMAT
+#endif
+
+
 static void ms_init(GPU_matrix_stack * ms, int initsize)
 {
 	if(initsize == 0)
@@ -189,7 +214,7 @@
 	}
 
 #endif
-
+CHECKMAT
 }
 
 
@@ -210,17 +235,25 @@
 	}
 
 	gpuLoadMatrix(ms_current->dynstack[ms_current->pos-1]);
+	CHECKMAT
 
 }
 
 void gpuPopMatrix(void)
 {
 	ms_current->pos--;
+
+
+
+
 	ms_current->changed = 1;
+
+
 #ifdef GLU_STACK_DEBUG
 	if(ms_current->pos < 0)
 		assert(0);
 #endif	
+	CHECKMAT
 }
 
 
@@ -246,7 +279,7 @@
 	
 	}
 
-
+CHECKMAT
 }
 
 
@@ -254,12 +287,17 @@
 {
 	copy_m4_m4(CURMATRIX, m);
 	ms_current->changed = 1;
+	CHECKMAT
 }
 
-void gpuGetMatrix(float * m)
+float * gpuGetMatrix(float * m)
 {
-	copy_m4_m4((float (*)[4])m,CURMATRIX);
+	if(m)
+		copy_m4_m4((float (*)[4])m,CURMATRIX);
+	else
+		return CURMATRIX;
 	ms_current->changed = 1;
+	return 0;
 }
 
 
@@ -267,6 +305,7 @@
 {
 	unit_m4(CURMATRIX);
 	ms_current->changed = 1;
+	CHECKMAT
 }
 
 
@@ -277,6 +316,7 @@
 
 	translate_m4(CURMATRIX, x, y, z);
 	ms_current->changed = 1;
+	CHECKMAT
 
 }
 
@@ -285,7 +325,73 @@
 
 	scale_m4(CURMATRIX, x, y, z);
 	ms_current->changed = 1;
+	CHECKMAT
+}
 
+
+void gpuMultMatrix(const float *m)
+{
+	GPU_matrix cm;
+
+	copy_m4_m4(cm, CURMATRIX);
+
+	mult_m4_m4m4_q(CURMATRIX, cm, m);
+	ms_current->changed = 1;
+	CHECKMAT
+
 }
 
+void gpuLoadOrtho(float left, float right, float bottom, float top, float nearVal, float farVal)
+{
+	mat4_ortho_set(CURMATRIX, left, right, bottom, top, nearVal, farVal);
+	ms_current->changed = 1;
+	CHECKMAT
+}
 
+
+void gpuOrtho(float left, float right, float bottom, float top, float nearVal, float farVal)
+{
+	GPU_matrix om;
+
+	mat4_ortho_set(om, left, right, bottom, top, nearVal, farVal);
+
+	gpuMultMatrix(om);
+	CHECKMAT
+}
+
+
+void gpuFrustum(float left, float right, float bottom, float top, float nearVal, float farVal)
+{
+	GPU_matrix fm;
+	mat4_frustum_set(fm, left, right, bottom, top, nearVal, farVal);
+	gpuMultMatrix(fm);
+	CHECKMAT
+}
+
+void gpuLoadFrustum(float left, float right, float bottom, float top, float nearVal, float farVal)
+{
+	mat4_frustum_set(CURMATRIX, left, right, bottom, top, nearVal, farVal);
+	ms_current->changed = 1;
+	CHECKMAT
+}
+
+
+void gpuLookAt(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ)
+{
+
+	GPU_matrix cm;
+	float lookdir[3];
+	float camup[3] = {upX, upY, upZ};
+
+	lookdir[0] =  centerX - eyeX;
+	lookdir[1] =  centerY - eyeY;
+	lookdir[2] =  centerZ - eyeZ;
+
+	mat4_look_from_origin(cm, lookdir, camup);
+
+	gpuMultMatrix(cm);
+
+	gpuTranslate(-eyeX, -eyeY, -eyeZ);
+CHECKMAT
+
+}




More information about the Bf-blender-cvs mailing list