Renamed matrices. I quite like names such as 'modelToWorldMatrix' and 'cameraToClipMatrix' because they were very explicit about what the transform was doing. However OpenGL uses common terms such as 'model matrix' and 'projection matrix', so other people wlil be able to follow the code more easily if we stick to these conventions.

This commit is contained in:
David Williams 2015-02-24 16:08:55 +01:00
parent 7262ca313e
commit 9547824f14
7 changed files with 51 additions and 51 deletions

View File

@ -4,9 +4,9 @@ in uvec4 position; // This will be the position of the vertex in model-space
in uint normal; in uint normal;
// The usual matrices are provided // The usual matrices are provided
uniform mat4 cameraToClipMatrix; uniform mat4 projectionMatrix;
uniform mat4 worldToCameraMatrix; uniform mat4 viewMatrix;
uniform mat4 modelToWorldMatrix; uniform mat4 modelMatrix;
// This will be used by the fragment shader to calculate flat-shaded normals. This is an unconventional approach // This will be used by the fragment shader to calculate flat-shaded normals. This is an unconventional approach
// but we use it in this example framework because not all surface extractor generate surface normals. // but we use it in this example framework because not all surface extractor generate surface normals.
@ -40,7 +40,7 @@ void main()
worldNormal.w = 1.0; worldNormal.w = 1.0;
// Standard sequence of OpenGL transformations. // Standard sequence of OpenGL transformations.
worldPosition = modelToWorldMatrix * decodedPosition; worldPosition = modelMatrix * decodedPosition;
vec4 cameraPosition = worldToCameraMatrix * worldPosition; vec4 cameraPosition = viewMatrix * worldPosition;
gl_Position = cameraToClipMatrix * cameraPosition; gl_Position = projectionMatrix * cameraPosition;
} }

View File

@ -4,9 +4,9 @@ in vec4 position; // This will be the position of the vertex in model-space
in vec4 normal; // The normal data may not have been set in vec4 normal; // The normal data may not have been set
in ivec2 material; in ivec2 material;
uniform mat4 cameraToClipMatrix; uniform mat4 projectionMatrix;
uniform mat4 worldToCameraMatrix; uniform mat4 viewMatrix;
uniform mat4 modelToWorldMatrix; uniform mat4 modelMatrix;
out vec4 worldPosition; //This is being passed to the fragment shader to calculate the normals out vec4 worldPosition; //This is being passed to the fragment shader to calculate the normals
out vec3 normalFromVS; out vec3 normalFromVS;
@ -15,7 +15,7 @@ flat out ivec2 materialFromVS;
void main() void main()
{ {
// Compute the usual OpenGL transformation to clip space. // Compute the usual OpenGL transformation to clip space.
gl_Position = cameraToClipMatrix * worldToCameraMatrix * modelToWorldMatrix * position; gl_Position = projectionMatrix * viewMatrix * modelMatrix * position;
// This example is demonstrating the marching cubes mesh, which does have per-vertex normals. We can // This example is demonstrating the marching cubes mesh, which does have per-vertex normals. We can
// just pass them through, though real code might want to deal with transforming normals appropriatly. // just pass them through, though real code might want to deal with transforming normals appropriatly.

View File

@ -127,17 +127,17 @@ def run():
in vec4 position; in vec4 position;
in vec4 normal; in vec4 normal;
uniform mat4 cameraToClipMatrix; uniform mat4 projectionMatrix;
uniform mat4 worldToCameraMatrix; uniform mat4 viewMatrix;
uniform mat4 modelToWorldMatrix; uniform mat4 modelMatrix;
flat out float theColor; flat out float theColor;
void main() void main()
{ {
vec4 temp = modelToWorldMatrix * position; vec4 temp = modelMatrix * position;
temp = worldToCameraMatrix * temp; temp = viewMatrix * temp;
gl_Position = cameraToClipMatrix * temp; gl_Position = projectionMatrix * temp;
theColor = clamp(abs(dot(normalize(normal.xyz), normalize(vec3(0.9,0.1,0.5)))), 0, 1); theColor = clamp(abs(dot(normalize(normal.xyz), normalize(vec3(0.9,0.1,0.5)))), 0, 1);
} }
@ -183,13 +183,13 @@ def run():
glDisableVertexAttribArray(0) glDisableVertexAttribArray(0)
#Now grab out transformation martix locations #Now grab out transformation martix locations
modelToWorldMatrixUnif = glGetUniformLocation(shader, b"modelToWorldMatrix") modelMatrixUnif = glGetUniformLocation(shader, b"modelMatrix")
worldToCameraMatrixUnif = glGetUniformLocation(shader, b"worldToCameraMatrix") viewMatrixUnif = glGetUniformLocation(shader, b"viewMatrix")
cameraToClipMatrixUnif = glGetUniformLocation(shader, b"cameraToClipMatrix") projectionMatrixUnif = glGetUniformLocation(shader, b"projectionMatrix")
modelToWorldMatrix = np.array([[1.0,0.0,0.0,-32.0],[0.0,1.0,0.0,-32.0],[0.0,0.0,1.0,-32.0],[0.0,0.0,0.0,1.0]], dtype='f') modelMatrix = np.array([[1.0,0.0,0.0,-32.0],[0.0,1.0,0.0,-32.0],[0.0,0.0,1.0,-32.0],[0.0,0.0,0.0,1.0]], dtype='f')
worldToCameraMatrix = np.array([[1.0,0.0,0.0,0.0],[0.0,1.0,0.0,0.0],[0.0,0.0,1.0,-50.0],[0.0,0.0,0.0,1.0]], dtype='f') viewMatrix = np.array([[1.0,0.0,0.0,0.0],[0.0,1.0,0.0,0.0],[0.0,0.0,1.0,-50.0],[0.0,0.0,0.0,1.0]], dtype='f')
cameraToClipMatrix = np.array([[0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0]], dtype='f') projectionMatrix = np.array([[0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0]], dtype='f')
#These next few lines just set up our camera frustum #These next few lines just set up our camera frustum
fovDeg = 45.0 fovDeg = 45.0
@ -198,16 +198,16 @@ def run():
zNear = 1.0 zNear = 1.0
zFar = 1000.0 zFar = 1000.0
cameraToClipMatrix[0][0] = frustumScale projectionMatrix[0][0] = frustumScale
cameraToClipMatrix[1][1] = frustumScale projectionMatrix[1][1] = frustumScale
cameraToClipMatrix[2][2] = (zFar + zNear) / (zNear - zFar) projectionMatrix[2][2] = (zFar + zNear) / (zNear - zFar)
cameraToClipMatrix[2][3] = -1.0 projectionMatrix[2][3] = -1.0
cameraToClipMatrix[3][2] = (2 * zFar * zNear) / (zNear - zFar) projectionMatrix[3][2] = (2 * zFar * zNear) / (zNear - zFar)
#worldToCameraMatrix and cameraToClipMatrix don't change ever so just set them once here #viewMatrix and projectionMatrix don't change ever so just set them once here
with shader: with shader:
glUniformMatrix4fv(cameraToClipMatrixUnif, 1, GL_TRUE, cameraToClipMatrix) glUniformMatrix4fv(projectionMatrixUnif, 1, GL_TRUE, projectionMatrix)
glUniformMatrix4fv(worldToCameraMatrixUnif, 1, GL_TRUE, worldToCameraMatrix) glUniformMatrix4fv(viewMatrixUnif, 1, GL_TRUE, viewMatrix)
#These are used to track the rotation of the volume #These are used to track the rotation of the volume
LastFrameMousePos = (0,0) LastFrameMousePos = (0,0)
@ -240,10 +240,10 @@ def run():
rotateAroundX = np.array([[1.0,0.0,0.0,0.0],[0.0,cos(radians(yRotation)),-sin(radians(yRotation)),0.0],[0.0,sin(radians(yRotation)),cos(radians(yRotation)),0.0],[0.0,0.0,0.0,1.0]], dtype='f') rotateAroundX = np.array([[1.0,0.0,0.0,0.0],[0.0,cos(radians(yRotation)),-sin(radians(yRotation)),0.0],[0.0,sin(radians(yRotation)),cos(radians(yRotation)),0.0],[0.0,0.0,0.0,1.0]], dtype='f')
rotateAroundY = np.array([[cos(radians(xRotation)),0.0,sin(radians(xRotation)),0.0],[0.0,1.0,0.0,0.0],[-sin(radians(xRotation)),0.0,cos(radians(xRotation)),0.0],[0.0,0.0,0.0,1.0]], dtype='f') rotateAroundY = np.array([[cos(radians(xRotation)),0.0,sin(radians(xRotation)),0.0],[0.0,1.0,0.0,0.0],[-sin(radians(xRotation)),0.0,cos(radians(xRotation)),0.0],[0.0,0.0,0.0,1.0]], dtype='f')
modelToWorldMatrix = rotateAroundY.dot(rotateAroundX.dot(moveToOrigin)) modelMatrix = rotateAroundY.dot(rotateAroundX.dot(moveToOrigin))
with shader: with shader:
glUniformMatrix4fv(modelToWorldMatrixUnif, 1, GL_TRUE, modelToWorldMatrix) glUniformMatrix4fv(modelMatrixUnif, 1, GL_TRUE, modelMatrix)
glBindVertexArray(vertexArrayObject) glBindVertexArray(vertexArrayObject)
glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None) glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None)

View File

@ -106,9 +106,9 @@ void OpenGLWidget::resizeGL(int w, int h)
float zNear = 1.0; float zNear = 1.0;
float zFar = 1000.0; float zFar = 1000.0;
cameraToClipMatrix.setToIdentity(); projectionMatrix.setToIdentity();
//cameraToClipMatrix.frustum(-aspectRatio, aspectRatio, -1, 1, zNear, zFar); //projectionMatrix.frustum(-aspectRatio, aspectRatio, -1, 1, zNear, zFar);
cameraToClipMatrix.perspective(mCameraFOV, aspectRatio, zNear, zFar); projectionMatrix.perspective(mCameraFOV, aspectRatio, zNear, zFar);
} }
void OpenGLWidget::paintGL() void OpenGLWidget::paintGL()
@ -154,8 +154,8 @@ void OpenGLWidget::paintGL()
mCameraPosition -= cameraRight * deltaTime * mCameraMoveSpeed; mCameraPosition -= cameraRight * deltaTime * mCameraMoveSpeed;
} }
worldToCameraMatrix.setToIdentity(); viewMatrix.setToIdentity();
worldToCameraMatrix.lookAt( viewMatrix.lookAt(
mCameraPosition, // Camera is here mCameraPosition, // Camera is here
mCameraPosition + cameraForward, // and looks here : at the same position, plus "direction" mCameraPosition + cameraForward, // and looks here : at the same position, plus "direction"
cameraUp // Head is up (set to 0,-1,0 to look upside-down) cameraUp // Head is up (set to 0,-1,0 to look upside-down)

View File

@ -66,8 +66,8 @@ protected:
protected: protected:
// Matrices // Matrices
QMatrix4x4 worldToCameraMatrix; QMatrix4x4 viewMatrix;
QMatrix4x4 cameraToClipMatrix; QMatrix4x4 projectionMatrix;
// Mouse data // Mouse data
QPoint m_LastFrameMousePos; QPoint m_LastFrameMousePos;

View File

@ -163,17 +163,17 @@ protected:
mShader->bind(); mShader->bind();
// These two matrices are constant for all meshes. // These two matrices are constant for all meshes.
mShader->setUniformValue("worldToCameraMatrix", worldToCameraMatrix); mShader->setUniformValue("viewMatrix", viewMatrix);
mShader->setUniformValue("cameraToClipMatrix", cameraToClipMatrix); mShader->setUniformValue("projectionMatrix", projectionMatrix);
// Iterate over each mesh which the user added to our list, and render it. // Iterate over each mesh which the user added to our list, and render it.
for (OpenGLMeshData meshData : mMeshData) for (OpenGLMeshData meshData : mMeshData)
{ {
//Set up the model matrrix based on provided translation and scale. //Set up the model matrrix based on provided translation and scale.
QMatrix4x4 modelToWorldMatrix; QMatrix4x4 modelMatrix;
modelToWorldMatrix.translate(meshData.translation); modelMatrix.translate(meshData.translation);
modelToWorldMatrix.scale(meshData.scale); modelMatrix.scale(meshData.scale);
mShader->setUniformValue("modelToWorldMatrix", modelToWorldMatrix); mShader->setUniformValue("modelMatrix", modelMatrix);
// Bind the vertex array for the current mesh // Bind the vertex array for the current mesh
glBindVertexArray(meshData.vertexArrayObject); glBindVertexArray(meshData.vertexArrayObject);

View File

@ -3,9 +3,9 @@
in vec4 position; // This will be the position of the vertex in model-space in vec4 position; // This will be the position of the vertex in model-space
// The usual matrices are provided // The usual matrices are provided
uniform mat4 cameraToClipMatrix; uniform mat4 projectionMatrix;
uniform mat4 worldToCameraMatrix; uniform mat4 viewMatrix;
uniform mat4 modelToWorldMatrix; uniform mat4 modelMatrix;
// This will be used by the fragment shader to calculate flat-shaded normals. This is an unconventional approach // This will be used by the fragment shader to calculate flat-shaded normals. This is an unconventional approach
// but we use it in this example framework because not all surface extractor generate surface normals. // but we use it in this example framework because not all surface extractor generate surface normals.
@ -14,7 +14,7 @@ out vec4 worldPosition;
void main() void main()
{ {
// Standard sequence of OpenGL transformations. // Standard sequence of OpenGL transformations.
worldPosition = modelToWorldMatrix * position; worldPosition = modelMatrix * position;
vec4 cameraPosition = worldToCameraMatrix * worldPosition; vec4 cameraPosition = viewMatrix * worldPosition;
gl_Position = cameraToClipMatrix * cameraPosition; gl_Position = projectionMatrix * cameraPosition;
} }