ParagonArcade

02-09-2016, 02:44 PM

I have been working on my 3D engine in GLSL for a long time now and everything works fine with the 3 matrices, Model, View, Projection... I have large terrain meshs chunked out with LOD and full lighting...

I had been calculating the Normal Matrix in the vertex shader. I am now trying to shift the computations to the CPU side to save GPU cycles. When I add the Normal matrix to the shader it corrupts my output and locks my camera. Like the matrices are switching locations...? Even if I don't use the Normal matrix in the shader, just adding the uniform to the shader causes it to become corrupted. I think it has to do with the mapping of the Matrix4f uniforms to the vertex shader. Anyone have an idea whats going on? I cant seem to figure this out.

I already triple checked my normal matrix computation and it works fine for all cases. It has to be something to do with the shader uniforms interacting.

Setup of Shader & Uniforms

glUseProgram(ID);

addUniform("Model");

addUniform("Normal");

addUniform("Projection");

addUniform("View");

.

.

.

// Setting the NormalMatrix later

shader.setUniformMatrix4f("Normal", getTransform()->getNormalTransformationFloatBuffer(videoEngine->CurrentCamera->getActiveView()));

Vertex Shader

#version 440

layout (location = 0) in vec4 position;

layout (location = 1) in vec4 normal;

layout (location = 2) in vec4 color;

layout (location = 3) in vec4 mixture;

uniform mat4 Model;

uniform mat4 Normal;

uniform mat4 Projection;

uniform mat4 View;

out vec4 vPosition;

out vec3 vNormal;

out vec4 vColor;

out vec4 vMixture;

out vec2 vSample;

invariant gl_Position;

const vec2 MADSET = vec2(1.0f, 0.0f);

void main()

{

vPosition = View * Model * vec4(position.xyz, 1.0f);

//vNormal = normalize(mat3(Normal) * normal.xyz);

vNormal = normalize(transpose(inverse(mat3(View * Model))) * normal.xyz);

vColor = color;

vMixture = mixture;

vSample = vec2(position.w, normal.w);

vec4 FragPosition = Projection * vPosition;

gl_Position = FragPosition.xyzw * MADSET.xxxx + MADSET.yyyy;

}

Using the second vNormal line works fine when I comment out the shader line loading in the Normal matrix but when its un-commented and I use the first vNormal line. It's corrupted. Even tried changing the uniform name and ordering them in alphabetical order the way its loaded into the vertex shader but alas still doesn't work :tired:

What does the image uploader not work anymore? I can't upload screenshots... :disgust:

I had been calculating the Normal Matrix in the vertex shader. I am now trying to shift the computations to the CPU side to save GPU cycles. When I add the Normal matrix to the shader it corrupts my output and locks my camera. Like the matrices are switching locations...? Even if I don't use the Normal matrix in the shader, just adding the uniform to the shader causes it to become corrupted. I think it has to do with the mapping of the Matrix4f uniforms to the vertex shader. Anyone have an idea whats going on? I cant seem to figure this out.

I already triple checked my normal matrix computation and it works fine for all cases. It has to be something to do with the shader uniforms interacting.

Setup of Shader & Uniforms

glUseProgram(ID);

addUniform("Model");

addUniform("Normal");

addUniform("Projection");

addUniform("View");

.

.

.

// Setting the NormalMatrix later

shader.setUniformMatrix4f("Normal", getTransform()->getNormalTransformationFloatBuffer(videoEngine->CurrentCamera->getActiveView()));

Vertex Shader

#version 440

layout (location = 0) in vec4 position;

layout (location = 1) in vec4 normal;

layout (location = 2) in vec4 color;

layout (location = 3) in vec4 mixture;

uniform mat4 Model;

uniform mat4 Normal;

uniform mat4 Projection;

uniform mat4 View;

out vec4 vPosition;

out vec3 vNormal;

out vec4 vColor;

out vec4 vMixture;

out vec2 vSample;

invariant gl_Position;

const vec2 MADSET = vec2(1.0f, 0.0f);

void main()

{

vPosition = View * Model * vec4(position.xyz, 1.0f);

//vNormal = normalize(mat3(Normal) * normal.xyz);

vNormal = normalize(transpose(inverse(mat3(View * Model))) * normal.xyz);

vColor = color;

vMixture = mixture;

vSample = vec2(position.w, normal.w);

vec4 FragPosition = Projection * vPosition;

gl_Position = FragPosition.xyzw * MADSET.xxxx + MADSET.yyyy;

}

Using the second vNormal line works fine when I comment out the shader line loading in the Normal matrix but when its un-commented and I use the first vNormal line. It's corrupted. Even tried changing the uniform name and ordering them in alphabetical order the way its loaded into the vertex shader but alas still doesn't work :tired:

What does the image uploader not work anymore? I can't upload screenshots... :disgust: