replacing glTRansform by gpu/Transform features

This commit is contained in:
Sam Gateau 2015-02-19 15:12:13 -08:00
parent b27b599643
commit d08142d37d
12 changed files with 113 additions and 54 deletions

View file

@ -33,8 +33,6 @@ public:
public: public:
Mat4 _model; Mat4 _model;
Mat4 _modelInverse; Mat4 _modelInverse;
Mat4 _modelView;
Mat4 _modelViewInverseTranspose;
}; };
class TransformCamera { class TransformCamera {
@ -42,6 +40,7 @@ public:
Mat4 _projection; Mat4 _projection;
Mat4 _view; Mat4 _view;
Mat4 _viewInverse; Mat4 _viewInverse;
Mat4 _projectionViewUntranslated;
}; };
template< typename T > template< typename T >

View file

@ -484,11 +484,10 @@ void GLBackend::updateTransform() {
_transform._model.getInverseMatrix(_transform._transformObject._modelInverse); _transform._model.getInverseMatrix(_transform._transformObject._modelInverse);
} }
if (_transform._invalidView || _transform._invalidModel) { if (_transform._invalidView || _transform._invalidProj) {
Transform mvx; Mat4 viewUntranslated = _transform._transformCamera._view;
Transform::inverseMult(mvx, _transform._view, _transform._model); // viewUntranslated[3] = Vec4(0.0f, 0.0f, 0.0f, 1.0f);
mvx.getMatrix(_transform._transformObject._modelView); _transform._transformCamera._projectionViewUntranslated = _transform._transformCamera._projection * viewUntranslated;
mvx.getInverseTransposeMatrix(_transform._transformObject._modelViewInverseTranspose);
} }
if (_transform._invalidView || _transform._invalidProj) { if (_transform._invalidView || _transform._invalidProj) {
@ -499,7 +498,7 @@ void GLBackend::updateTransform() {
CHECK_GL_ERROR(); CHECK_GL_ERROR();
} }
if (_transform._invalidView || _transform._invalidModel) { if (_transform._invalidModel) {
glBindBufferBase(GL_UNIFORM_BUFFER, 6, 0); glBindBufferBase(GL_UNIFORM_BUFFER, 6, 0);
glBindBuffer(GL_ARRAY_BUFFER, _transform._transformObjectBuffer); glBindBuffer(GL_ARRAY_BUFFER, _transform._transformObjectBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(_transform._transformObject), (const void*) &_transform._transformObject, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(_transform._transformObject), (const void*) &_transform._transformObject, GL_DYNAMIC_DRAW);

View file

@ -13,23 +13,38 @@
struct TransformObject { struct TransformObject {
mat4 _model; mat4 _model;
mat4 _modelInverse; mat4 _modelInverse;
mat4 _modelView;
mat4 _modelViewInverseTranspose;
}; };
struct TransformCamera { struct TransformCamera {
mat4 _projection; mat4 _projection;
mat4 _view; mat4 _view;
mat4 _viewInverse; mat4 _viewInverse;
mat4 _projectionViewUntranslated;
}; };
vec4 transform(TransformCamera camera, TransformObject object, vec4 pos) { vec4 transform(TransformCamera camera, TransformObject object, vec4 pos) {
/* vec4 res = object._model * pos;
return camera._projection * (object._modelView * pos); res += pos.w * camera._viewInverse[3];
return camera._projectionViewUntranslated * res;
*/
return camera._projectionViewUntranslated * object._model * pos;
// return camera._projection * camera._view * object._model * pos;
} }
vec3 transformDir(TransformCamera camera, TransformObject object, vec3 dir) { vec3 transformDir(TransformCamera camera, TransformObject object, vec3 dir) {
return (object._modelViewInverseTranspose * vec4(dir, 0.0)).xyz; vec3 mIr0 = vec3(object._modelInverse[0].x, object._modelInverse[1].x, object._modelInverse[2].x);
vec3 mIr1 = vec3(object._modelInverse[0].y, object._modelInverse[1].y, object._modelInverse[2].y);
vec3 mIr2 = vec3(object._modelInverse[0].z, object._modelInverse[1].z, object._modelInverse[2].z);
vec3 mvIc0 = vec3(dot(camera._viewInverse[0].xyz, mIr0), dot(camera._viewInverse[0].xyz, mIr1), dot(camera._viewInverse[0].xyz, mIr2));
vec3 mvIc1 = vec3(dot(camera._viewInverse[1].xyz, mIr0), dot(camera._viewInverse[1].xyz, mIr1), dot(camera._viewInverse[1].xyz, mIr2));
vec3 mvIc2 = vec3(dot(camera._viewInverse[2].xyz, mIr0), dot(camera._viewInverse[2].xyz, mIr1), dot(camera._viewInverse[2].xyz, mIr2));
vec3 result = vec3(dot(mvIc0, dir),
dot(mvIc1, dir),
dot(mvIc2, dir));
return result;
} }
<@if GLPROFILE == PC_GL@> <@if GLPROFILE == PC_GL@>

View file

@ -18,22 +18,6 @@ uniform mat4 texcoordMatrices[MAX_TEXCOORDS];
// the interpolated normal // the interpolated normal
varying vec4 normal; varying vec4 normal;
/*
void main(void) {
// transform and store the normal for interpolation
normal = normalize(gl_ModelViewMatrix * vec4(gl_Normal, 0.0));
// pass along the diffuse color
gl_FrontColor = gl_Color;
// and the texture coordinates
gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0);
// use standard pipeline transform
gl_Position = gl_ModelViewProjectionMatrix * vec4(vin_position, 1.0);
}
*/
void main(void) { void main(void) {
@ -44,9 +28,6 @@ void main(void) {
gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0);
// use standard pipeline transform // use standard pipeline transform
//gl_Position = ftransform();
//gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
//gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
TransformCamera cam = getTransformCamera(); TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject(); TransformObject obj = getTransformObject();

View file

@ -12,6 +12,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include gpu/Transform.slh@>
const int MAX_TEXCOORDS = 2; const int MAX_TEXCOORDS = 2;
uniform mat4 texcoordMatrices[MAX_TEXCOORDS]; uniform mat4 texcoordMatrices[MAX_TEXCOORDS];
@ -25,9 +27,6 @@ varying vec4 normal;
varying vec2 interpolatedTexcoord1; varying vec2 interpolatedTexcoord1;
void main(void) { void main(void) {
// transform and store the normal for interpolation
normal = normalize(gl_ModelViewMatrix * vec4(gl_Normal, 0.0));
// pass along the diffuse color // pass along the diffuse color
gl_FrontColor = gl_Color; gl_FrontColor = gl_Color;
@ -37,6 +36,12 @@ void main(void) {
interpolatedTexcoord1 = vec2(texcoordMatrices[1] * vec4(texcoord1.xy, 0.0, 1.0)).xy; interpolatedTexcoord1 = vec2(texcoordMatrices[1] * vec4(texcoord1.xy, 0.0, 1.0)).xy;
// use standard pipeline transform // use standard pipeline transform
gl_Position = ftransform(); TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
gl_Position = transform(cam, obj, gl_Vertex);
// transform and store the normal for interpolation
normal = vec4(normalize(transformDir(cam, obj, gl_Normal)), 0.0);
} }

View file

@ -12,6 +12,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include gpu/Transform.slh@>
const int MAX_TEXCOORDS = 2; const int MAX_TEXCOORDS = 2;
uniform mat4 texcoordMatrices[MAX_TEXCOORDS]; uniform mat4 texcoordMatrices[MAX_TEXCOORDS];
@ -32,8 +34,8 @@ varying vec2 interpolatedTexcoord1;
void main(void) { void main(void) {
// transform and store the normal and tangent for interpolation // transform and store the normal and tangent for interpolation
interpolatedNormal = gl_ModelViewMatrix * vec4(gl_Normal, 0.0); //interpolatedNormal = gl_ModelViewMatrix * vec4(gl_Normal, 0.0);
interpolatedTangent = gl_ModelViewMatrix * vec4(tangent, 0.0); //interpolatedTangent = gl_ModelViewMatrix * vec4(tangent, 0.0);
// pass along the diffuse color // pass along the diffuse color
gl_FrontColor = gl_Color; gl_FrontColor = gl_Color;
@ -43,5 +45,12 @@ void main(void) {
interpolatedTexcoord1 = vec2(texcoordMatrices[1] * vec4(texcoord1.xy, 0.0, 1.0)).xy; interpolatedTexcoord1 = vec2(texcoordMatrices[1] * vec4(texcoord1.xy, 0.0, 1.0)).xy;
// use standard pipeline transform // use standard pipeline transform
gl_Position = ftransform(); TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
gl_Position = transform(cam, obj, gl_Vertex);
// transform and store the normal for interpolation
interpolatedNormal = vec4(normalize(transformDir(cam, obj, gl_Normal)), 0.0);
interpolatedTangent = vec4(normalize(transformDir(cam, obj, tangent)), 0.0);
} }

View file

@ -12,6 +12,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include gpu/Transform.slh@>
const int MAX_TEXCOORDS = 2; const int MAX_TEXCOORDS = 2;
uniform mat4 texcoordMatrices[MAX_TEXCOORDS]; uniform mat4 texcoordMatrices[MAX_TEXCOORDS];
@ -27,8 +29,8 @@ varying vec4 interpolatedTangent;
void main(void) { void main(void) {
// transform and store the normal and tangent for interpolation // transform and store the normal and tangent for interpolation
interpolatedNormal = gl_ModelViewMatrix * vec4(gl_Normal, 0.0); //interpolatedNormal = gl_ModelViewMatrix * vec4(gl_Normal, 0.0);
interpolatedTangent = gl_ModelViewMatrix * vec4(tangent, 0.0); //interpolatedTangent = gl_ModelViewMatrix * vec4(tangent, 0.0);
// pass along the diffuse color // pass along the diffuse color
gl_FrontColor = gl_Color; gl_FrontColor = gl_Color;
@ -37,5 +39,12 @@ void main(void) {
gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0);
// use standard pipeline transform // use standard pipeline transform
gl_Position = ftransform(); TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
gl_Position = transform(cam, obj, gl_Vertex);
// transform and store the normal for interpolation
interpolatedNormal = vec4(normalize(transformDir(cam, obj, gl_Normal)), 0.0);
interpolatedTangent = vec4(normalize(transformDir(cam, obj, tangent)), 0.0);
} }

View file

@ -11,8 +11,11 @@
// Distributed under the Apache License, Version 2.0. // Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include gpu/Transform.slh@>
void main(void) { void main(void) {
// just use standard pipeline transform // use standard pipeline transform
gl_Position = ftransform(); TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
gl_Position = transform(cam, obj, gl_Vertex);
} }

View file

@ -12,6 +12,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include gpu/Transform.slh@>
const int MAX_TEXCOORDS = 2; const int MAX_TEXCOORDS = 2;
const int MAX_CLUSTERS = 128; const int MAX_CLUSTERS = 128;
const int INDICES_PER_VERTEX = 4; const int INDICES_PER_VERTEX = 4;
@ -35,13 +37,18 @@ void main(void) {
normal += clusterMatrix * vec4(gl_Normal, 0.0) * clusterWeight; normal += clusterMatrix * vec4(gl_Normal, 0.0) * clusterWeight;
} }
normal = normalize(gl_ModelViewMatrix * normal);
// pass along the diffuse color // pass along the diffuse color
gl_FrontColor = gl_Color; gl_FrontColor = gl_Color;
// and the texture coordinates // and the texture coordinates
gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0);
gl_Position = gl_ModelViewProjectionMatrix * position; // use standard pipeline transform
TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
gl_Position = transform(cam, obj, position);
// transform and store the normal for interpolation
normal = vec4(normalize(transformDir(cam, obj, normal.xyz)), 0.0);
} }

View file

@ -12,6 +12,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include gpu/Transform.slh@>
const int MAX_TEXCOORDS = 2; const int MAX_TEXCOORDS = 2;
const int MAX_CLUSTERS = 128; const int MAX_CLUSTERS = 128;
const int INDICES_PER_VERTEX = 4; const int INDICES_PER_VERTEX = 4;
@ -42,8 +44,8 @@ void main(void) {
interpolatedNormal += clusterMatrix * vec4(gl_Normal, 0.0) * clusterWeight; interpolatedNormal += clusterMatrix * vec4(gl_Normal, 0.0) * clusterWeight;
interpolatedTangent += clusterMatrix * vec4(tangent, 0.0) * clusterWeight; interpolatedTangent += clusterMatrix * vec4(tangent, 0.0) * clusterWeight;
} }
interpolatedNormal = gl_ModelViewMatrix * interpolatedNormal; // interpolatedNormal = gl_ModelViewMatrix * interpolatedNormal;
interpolatedTangent = gl_ModelViewMatrix * interpolatedTangent; // interpolatedTangent = gl_ModelViewMatrix * interpolatedTangent;
// pass along the diffuse color // pass along the diffuse color
gl_FrontColor = gl_Color; gl_FrontColor = gl_Color;
@ -52,4 +54,13 @@ void main(void) {
gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0); gl_TexCoord[0] = texcoordMatrices[0] * vec4(gl_MultiTexCoord0.xy, 0.0, 1.0);
gl_Position = gl_ModelViewProjectionMatrix * interpolatedPosition; gl_Position = gl_ModelViewProjectionMatrix * interpolatedPosition;
// use standard pipeline transform
TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
gl_Position = transform(cam, obj, interpolatedPosition);
interpolatedNormal = vec4(normalize(transformDir(cam, obj, interpolatedNormal.xyz)), 0.0);
interpolatedTangent = vec4(normalize(transformDir(cam, obj, interpolatedTangent.xyz)), 0.0);
} }

View file

@ -12,6 +12,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
<@include gpu/Transform.slh@>
const int MAX_CLUSTERS = 128; const int MAX_CLUSTERS = 128;
const int INDICES_PER_VERTEX = 4; const int INDICES_PER_VERTEX = 4;
@ -27,5 +29,10 @@ void main(void) {
float clusterWeight = clusterWeights[i]; float clusterWeight = clusterWeights[i];
position += clusterMatrix * gl_Vertex * clusterWeight; position += clusterMatrix * gl_Vertex * clusterWeight;
} }
gl_Position = gl_ModelViewProjectionMatrix * position;
// use standard pipeline transform
TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
gl_Position = transform(cam, obj, position);
} }

View file

@ -92,6 +92,9 @@ public:
Mat4& getInverseMatrix(Mat4& result) const; Mat4& getInverseMatrix(Mat4& result) const;
Mat4& getInverseTransposeMatrix(Mat4& result) const; Mat4& getInverseTransposeMatrix(Mat4& result) const;
Mat4& getRotationScaleMatrix(Mat4& result) const;
Mat4& getRotationScaleMatrixInverse(Mat4& result) const;
Transform& evalInverse(Transform& result) const; Transform& evalInverse(Transform& result) const;
static void evalRotationScale(Quat& rotation, Vec3& scale, const Mat3& rotationScaleMatrix); static void evalRotationScale(Quat& rotation, Vec3& scale, const Mat3& rotationScaleMatrix);
@ -336,6 +339,17 @@ inline Transform::Mat4& Transform::getInverseTransposeMatrix(Transform::Mat4& re
return result; return result;
} }
inline Transform::Mat4& Transform::getRotationScaleMatrix(Mat4& result) const {
getMatrix(result);
result[3] = Vec4(0.0f, 0.0f, 0.0f, 1.0f);
return result;
}
inline Transform::Mat4& Transform::getRotationScaleMatrixInverse(Mat4& result) const {
getInverseMatrix(result);
result[3] = Vec4(0.0f, 0.0f, 0.0f, 1.0f);
return result;
}
inline void Transform::evalFromRawMatrix(const Mat4& matrix) { inline void Transform::evalFromRawMatrix(const Mat4& matrix) {
// for now works only in the case of TRS transformation // for now works only in the case of TRS transformation