libGL Frustum implementation

Change-Id: I3ffce981fd8238ca1767e05929da3502cd3b2a39
Reviewed-on: https://swiftshader-review.googlesource.com/2510
Tested-by: Maxime Grégoire <mgregoire@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
diff --git a/src/OpenGL/libGL/Context.cpp b/src/OpenGL/libGL/Context.cpp
index ba6d2e5..41e32d5 100644
--- a/src/OpenGL/libGL/Context.cpp
+++ b/src/OpenGL/libGL/Context.cpp
@@ -3184,6 +3184,16 @@
     currentMatrixStack().multiply(m);

 }

 

+void Context::frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)

+{

+	if(drawing)

+	{

+		return error(GL_INVALID_OPERATION);

+	}

+

+	currentMatrixStack().frustum(left, right, bottom, top, zNear, zFar);

+}

+

 void Context::ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)

 {

     if(drawing)

diff --git a/src/OpenGL/libGL/Context.h b/src/OpenGL/libGL/Context.h
index ba3c285..e2e933f 100644
--- a/src/OpenGL/libGL/Context.h
+++ b/src/OpenGL/libGL/Context.h
@@ -683,6 +683,7 @@
 	void scale(GLfloat x, GLfloat y, GLfloat z);

 	void multiply(const GLdouble *m);

     void multiply(const GLfloat *m);

+	void frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar);

     void ortho(double left, double right, double bottom, double top, double zNear, double zFar);   // FIXME: GLdouble

 

     void setLighting(bool enabled);

diff --git a/src/OpenGL/libGL/MatrixStack.cpp b/src/OpenGL/libGL/MatrixStack.cpp
index c91e152..8d6c3b4 100644
--- a/src/OpenGL/libGL/MatrixStack.cpp
+++ b/src/OpenGL/libGL/MatrixStack.cpp
@@ -113,8 +113,8 @@
 

 		float A = (r + l) / (r - l);

 		float B = (t + b) / (t - b);

-		float C = -(f + n) / (r - n);

-		float D = -2 * r * n / (f - n);

+		float C = -(f + n) / (f - n);

+		float D = -2 * f * n / (f - n);

 

 		Matrix frustum(2 * n / (r - l), 0,               A,  0,

 		               0,               2 * n / (t - b), B,  0,

diff --git a/src/OpenGL/libGL/libGL.cpp b/src/OpenGL/libGL/libGL.cpp
index 8458b78..1cb1e55 100644
--- a/src/OpenGL/libGL/libGL.cpp
+++ b/src/OpenGL/libGL/libGL.cpp
@@ -6239,7 +6239,19 @@
 

 void APIENTRY glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zNear, GLdouble zFar)

 {

-	UNIMPLEMENTED();

+	TRACE("(*)");

+

+	gl::Context *context = gl::getContext();

+

+	if(context)

+	{

+		if(context->getListIndex() != 0)

+		{

+			UNIMPLEMENTED();

+		}

+

+		context->frustum(left, right, bottom, top, zNear, zFar);

+	}

 }

 

 GLuint APIENTRY glGenLists(GLsizei range)

diff --git a/tests/OGLSimpleCube/OGLSimpleCube.cpp b/tests/OGLSimpleCube/OGLSimpleCube.cpp
index 1454931..bf6a043 100644
--- a/tests/OGLSimpleCube/OGLSimpleCube.cpp
+++ b/tests/OGLSimpleCube/OGLSimpleCube.cpp
@@ -33,9 +33,6 @@
 // Rotation matrix
 GLfloat R[16] = { 1, 0, 0, 0, 0, cos(theta), -sin(theta), 0, 0, sin(theta), cos(theta), 0, 0, 0, 0, 1 };
 
-// Projection matrix (mimic the glFrustum function, which is unimplemented as of now)
-GLfloat P[16] = { 2.0f, 0, 0, 0, 0, 2.0f, 0, 0, 0, 0, -2.0f, -1.0f, 0, 0, -3.0f, 0 };
-
 // Scaling matrix
 GLfloat S[16] = { SCALE_FACTOR, 0, 0, 0, 0, SCALE_FACTOR, 0, 0, 0, 0, SCALE_FACTOR, 0, 0, 0, 0, 1 };
 
@@ -74,7 +71,7 @@
 {
 	// Set viewing projection
 	glMatrixMode(GL_PROJECTION);
-	glMultMatrixf(P);
+	glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0);
 
 	// Position viewer
 	glMatrixMode(GL_MODELVIEW);