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:
parent
7262ca313e
commit
9547824f14
@ -4,9 +4,9 @@ in uvec4 position; // This will be the position of the vertex in model-space
|
||||
in uint normal;
|
||||
|
||||
// The usual matrices are provided
|
||||
uniform mat4 cameraToClipMatrix;
|
||||
uniform mat4 worldToCameraMatrix;
|
||||
uniform mat4 modelToWorldMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 modelMatrix;
|
||||
|
||||
// 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.
|
||||
@ -40,7 +40,7 @@ void main()
|
||||
worldNormal.w = 1.0;
|
||||
|
||||
// Standard sequence of OpenGL transformations.
|
||||
worldPosition = modelToWorldMatrix * decodedPosition;
|
||||
vec4 cameraPosition = worldToCameraMatrix * worldPosition;
|
||||
gl_Position = cameraToClipMatrix * cameraPosition;
|
||||
worldPosition = modelMatrix * decodedPosition;
|
||||
vec4 cameraPosition = viewMatrix * worldPosition;
|
||||
gl_Position = projectionMatrix * cameraPosition;
|
||||
}
|
||||
|
@ -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 ivec2 material;
|
||||
|
||||
uniform mat4 cameraToClipMatrix;
|
||||
uniform mat4 worldToCameraMatrix;
|
||||
uniform mat4 modelToWorldMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 modelMatrix;
|
||||
|
||||
out vec4 worldPosition; //This is being passed to the fragment shader to calculate the normals
|
||||
out vec3 normalFromVS;
|
||||
@ -15,7 +15,7 @@ flat out ivec2 materialFromVS;
|
||||
void main()
|
||||
{
|
||||
// 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
|
||||
// just pass them through, though real code might want to deal with transforming normals appropriatly.
|
||||
|
@ -127,17 +127,17 @@ def run():
|
||||
in vec4 position;
|
||||
in vec4 normal;
|
||||
|
||||
uniform mat4 cameraToClipMatrix;
|
||||
uniform mat4 worldToCameraMatrix;
|
||||
uniform mat4 modelToWorldMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 modelMatrix;
|
||||
|
||||
flat out float theColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 temp = modelToWorldMatrix * position;
|
||||
temp = worldToCameraMatrix * temp;
|
||||
gl_Position = cameraToClipMatrix * temp;
|
||||
vec4 temp = modelMatrix * position;
|
||||
temp = viewMatrix * temp;
|
||||
gl_Position = projectionMatrix * temp;
|
||||
|
||||
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)
|
||||
|
||||
#Now grab out transformation martix locations
|
||||
modelToWorldMatrixUnif = glGetUniformLocation(shader, b"modelToWorldMatrix")
|
||||
worldToCameraMatrixUnif = glGetUniformLocation(shader, b"worldToCameraMatrix")
|
||||
cameraToClipMatrixUnif = glGetUniformLocation(shader, b"cameraToClipMatrix")
|
||||
modelMatrixUnif = glGetUniformLocation(shader, b"modelMatrix")
|
||||
viewMatrixUnif = glGetUniformLocation(shader, b"viewMatrix")
|
||||
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')
|
||||
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')
|
||||
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')
|
||||
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')
|
||||
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')
|
||||
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
|
||||
fovDeg = 45.0
|
||||
@ -198,16 +198,16 @@ def run():
|
||||
zNear = 1.0
|
||||
zFar = 1000.0
|
||||
|
||||
cameraToClipMatrix[0][0] = frustumScale
|
||||
cameraToClipMatrix[1][1] = frustumScale
|
||||
cameraToClipMatrix[2][2] = (zFar + zNear) / (zNear - zFar)
|
||||
cameraToClipMatrix[2][3] = -1.0
|
||||
cameraToClipMatrix[3][2] = (2 * zFar * zNear) / (zNear - zFar)
|
||||
projectionMatrix[0][0] = frustumScale
|
||||
projectionMatrix[1][1] = frustumScale
|
||||
projectionMatrix[2][2] = (zFar + zNear) / (zNear - zFar)
|
||||
projectionMatrix[2][3] = -1.0
|
||||
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:
|
||||
glUniformMatrix4fv(cameraToClipMatrixUnif, 1, GL_TRUE, cameraToClipMatrix)
|
||||
glUniformMatrix4fv(worldToCameraMatrixUnif, 1, GL_TRUE, worldToCameraMatrix)
|
||||
glUniformMatrix4fv(projectionMatrixUnif, 1, GL_TRUE, projectionMatrix)
|
||||
glUniformMatrix4fv(viewMatrixUnif, 1, GL_TRUE, viewMatrix)
|
||||
|
||||
#These are used to track the rotation of the volume
|
||||
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')
|
||||
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:
|
||||
glUniformMatrix4fv(modelToWorldMatrixUnif, 1, GL_TRUE, modelToWorldMatrix)
|
||||
glUniformMatrix4fv(modelMatrixUnif, 1, GL_TRUE, modelMatrix)
|
||||
glBindVertexArray(vertexArrayObject)
|
||||
|
||||
glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, None)
|
||||
|
@ -106,9 +106,9 @@ void OpenGLWidget::resizeGL(int w, int h)
|
||||
float zNear = 1.0;
|
||||
float zFar = 1000.0;
|
||||
|
||||
cameraToClipMatrix.setToIdentity();
|
||||
//cameraToClipMatrix.frustum(-aspectRatio, aspectRatio, -1, 1, zNear, zFar);
|
||||
cameraToClipMatrix.perspective(mCameraFOV, aspectRatio, zNear, zFar);
|
||||
projectionMatrix.setToIdentity();
|
||||
//projectionMatrix.frustum(-aspectRatio, aspectRatio, -1, 1, zNear, zFar);
|
||||
projectionMatrix.perspective(mCameraFOV, aspectRatio, zNear, zFar);
|
||||
}
|
||||
|
||||
void OpenGLWidget::paintGL()
|
||||
@ -154,8 +154,8 @@ void OpenGLWidget::paintGL()
|
||||
mCameraPosition -= cameraRight * deltaTime * mCameraMoveSpeed;
|
||||
}
|
||||
|
||||
worldToCameraMatrix.setToIdentity();
|
||||
worldToCameraMatrix.lookAt(
|
||||
viewMatrix.setToIdentity();
|
||||
viewMatrix.lookAt(
|
||||
mCameraPosition, // Camera is here
|
||||
mCameraPosition + cameraForward, // and looks here : at the same position, plus "direction"
|
||||
cameraUp // Head is up (set to 0,-1,0 to look upside-down)
|
||||
|
@ -66,8 +66,8 @@ protected:
|
||||
protected:
|
||||
|
||||
// Matrices
|
||||
QMatrix4x4 worldToCameraMatrix;
|
||||
QMatrix4x4 cameraToClipMatrix;
|
||||
QMatrix4x4 viewMatrix;
|
||||
QMatrix4x4 projectionMatrix;
|
||||
|
||||
// Mouse data
|
||||
QPoint m_LastFrameMousePos;
|
||||
|
@ -163,17 +163,17 @@ protected:
|
||||
mShader->bind();
|
||||
|
||||
// These two matrices are constant for all meshes.
|
||||
mShader->setUniformValue("worldToCameraMatrix", worldToCameraMatrix);
|
||||
mShader->setUniformValue("cameraToClipMatrix", cameraToClipMatrix);
|
||||
mShader->setUniformValue("viewMatrix", viewMatrix);
|
||||
mShader->setUniformValue("projectionMatrix", projectionMatrix);
|
||||
|
||||
// Iterate over each mesh which the user added to our list, and render it.
|
||||
for (OpenGLMeshData meshData : mMeshData)
|
||||
{
|
||||
//Set up the model matrrix based on provided translation and scale.
|
||||
QMatrix4x4 modelToWorldMatrix;
|
||||
modelToWorldMatrix.translate(meshData.translation);
|
||||
modelToWorldMatrix.scale(meshData.scale);
|
||||
mShader->setUniformValue("modelToWorldMatrix", modelToWorldMatrix);
|
||||
QMatrix4x4 modelMatrix;
|
||||
modelMatrix.translate(meshData.translation);
|
||||
modelMatrix.scale(meshData.scale);
|
||||
mShader->setUniformValue("modelMatrix", modelMatrix);
|
||||
|
||||
// Bind the vertex array for the current mesh
|
||||
glBindVertexArray(meshData.vertexArrayObject);
|
||||
|
@ -3,9 +3,9 @@
|
||||
in vec4 position; // This will be the position of the vertex in model-space
|
||||
|
||||
// The usual matrices are provided
|
||||
uniform mat4 cameraToClipMatrix;
|
||||
uniform mat4 worldToCameraMatrix;
|
||||
uniform mat4 modelToWorldMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 modelMatrix;
|
||||
|
||||
// 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.
|
||||
@ -14,7 +14,7 @@ out vec4 worldPosition;
|
||||
void main()
|
||||
{
|
||||
// Standard sequence of OpenGL transformations.
|
||||
worldPosition = modelToWorldMatrix * position;
|
||||
vec4 cameraPosition = worldToCameraMatrix * worldPosition;
|
||||
gl_Position = cameraToClipMatrix * cameraPosition;
|
||||
worldPosition = modelMatrix * position;
|
||||
vec4 cameraPosition = viewMatrix * worldPosition;
|
||||
gl_Position = projectionMatrix * cameraPosition;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user