Jump to content

Geometry Shader and Matrix Multiplication


SpiderPig
 Share

Go to solution Solved by SpiderPig,

Recommended Posts

I have officially been driven mad by this issue over the last few days.  Something I've had working in the past and thought I understood now doesn't want to behave.

Here's a simplified example.  I'll be creating vertices in the geometry shader and those new positions will need multiplying by the model and projection matrices.  But I'm getting mixed result of vertices flying off screen and sometimes only one vertex in the centre of the model.  I know I'm doing something wrong.

Here I'm applying the shader to a grid made with CreatePlane and 8 x & y segments.  It should be a flat grid of points but the code below makes them fly around with the camera, this is just one shot were they were in view.

Even when I pass the entity matrix, projection matrix and raw vertex position as an output to the geometry shader I can't get it to work.  I'm sure a quick discussion with guys will help resolve this.

GeometryShaderIssue.thumb.jpg.53035a0e919668ae652ab0ad5ad01ae5.jpg

Geometry Shader Code.

#version 450
#extension GL_GOOGLE_include_directive : enable
#extension GL_ARB_separate_shader_objects : enable
#extension GL_EXT_multiview : enable

layout ( triangles ) in;
layout ( points , max_vertices = 1 ) out;

layout(location = 9 ) in flat uint inFlags[];
layout(location = 25 ) in flat uint inEntityindex[];
layout(location = 2 ) in vec4 inTexCoords[];
layout(location = 3 ) in vec3 inTangent[];
layout(location = 4 ) in vec3 inBitangent[];
layout(location = 5 ) in flat uint inMaterialIndex[];
layout(location = 0 ) in vec4 inColor[];
layout(location = 6 ) in vec4 inVertexCameraPosition[];
layout(location = 23 ) in vec3 inScreenvelocity[];
layout(location = 1 ) in vec3 inNormal[];
layout(location = 7 ) in vec4 inVertexWorldPosition[];
layout(location = 17 ) in vec4 inVertexPosition[];


layout(location = 9 ) out flat uint outFlags;
layout(location = 25 ) out flat uint outEntityindex;
layout(location = 2 ) out vec4 outTexCoords;
layout(location = 3 ) out vec3 outTangent;
layout(location = 4 ) out vec3 outBitangent;
layout(location = 5 ) out flat uint outMaterialIndex;
layout(location = 0 ) out vec4 outColor;
layout(location = 6 ) out vec4 outVertexCameraPosition;
layout(location = 23 ) out vec3 outScreenvelocity;
layout(location = 1 ) out vec3 outNormal;
layout(location = 7 ) out vec4 outVertexWorldPosition;
layout(location = 17 ) out vec4 outVertexPosition;

#include "../../../../../../../../UltraEngine/Templates/Common/Source/Shaders/Base/EntityInfo.glsl"
#include "../../../../../../../../UltraEngine/Templates/Common/Source/Shaders/Base/CameraInfo.glsl"

void main() {
	//This works, only the first vertex of every triangle should make a point and it does just that
	/*for (int vertex_id = 0; vertex_id < 1; vertex_id++) {
		gl_Position = gl_in[vertex_id].gl_Position;
		gl_PointSize = 4.0f;
		
		outFlags = inFlags[vertex_id];
		outEntityindex = inEntityindex[vertex_id];
		outTexCoords = inTexCoords[vertex_id];
		outTangent = inTangent[vertex_id];
		outBitangent = inBitangent[vertex_id];
		outMaterialIndex = inMaterialIndex[vertex_id];
		outColor = inColor[vertex_id];
		outVertexCameraPosition = inVertexCameraPosition[vertex_id];
		outScreenvelocity = inScreenvelocity[vertex_id];
		outNormal = inNormal[vertex_id];
		outVertexWorldPosition = inVertexWorldPosition[vertex_id];
		outVertexPosition = inVertexPosition[vertex_id];
		EmitVertex();
	}
	EndPrimitive();*/

  	//Here I'm trying to prove that I can take a local vertex position and re-multiply it by the correct matrices
  	//As f ar as I know to get the local vertex postion back I need to undo a few things first...
  	//I actually don't know what the vertex 'w' component should be.  I'm pretty sure it should be 1.0f...
 
	mat4 entityMatrix = ExtractEntityMatrix(inEntityindex[0]);
	mat4 projectionMatrix = ExtractCameraProjectionMatrix(CameraID, gl_ViewIndex);
	
	vec4 localVertPos = gl_in[0].gl_Position;
	localVertPos.z *= 2.0f;
	localVertPos.z -= localVertPos.w;
	
	localVertPos *= inverse(projectionMatrix);
	localVertPos *= inverse(entityMatrix);
	
	//This is what the vertex shader does as far as I can tell
	gl_Position = projectionMatrix * (entityMatrix * localVertPos);
	gl_Position.z = (gl_Position.z + gl_Position.w) * 0.5f;
	gl_PointSize = 4.0f;
		
	EmitVertex();
	EndPrimitive();
}

 

DisplayNormals.zip

Link to comment
Share on other sites

  • Solution

Once again simply posting has helped solve the problem... This is not the correct order to multiply matrices in.

localVertPos *= inverse(projectionMatrix);
localVertPos *= inverse(entityMatrix);

This is.

localVertPos = inverse(projectionMatrix) * localVertPos;
localVertPos = inverse(entityMatrix) * localVertPos;

And the other issue of points all appearing in the centre of the mesh.  Both vertex and geometry shaders need to be in the mask section of a shader family. 

Fozzie Bear Reaction GIF

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...