Vertex to cube using geometry shader

Started by
9 comments, last by cozzie 8 years, 3 months ago

For who's interested, here's the result.

I can now switch between the 14 and 24 vertices versions.

geocube_14vtx.jpg

geocube_24vtx.jpg

The geometry shader (relevant parts):


	// texture coordinates for the geometry shader cube: using 1 big strip, 14 vertices
	float2 gTexCStrip[14] = 
	{
		float2(0.0f, 1.0f),
		float2(1.0f, 1.0f),
		float2(0.0f, 0.0f),
		float2(1.0f, 0.0f),
		float2(0.0f, 0.0f),
		float2(0.0f, 1.0f),
		float2(1.0f, 1.0f),
		float2(0.0f, 1.0f),
		float2(1.0f, 1.0f),
		float2(0.0f, 0.0f),
		float2(1.0f, 0.0f),
		float2(0.0f, 0.0f),
		float2(1.0f, 0.0f),
		float2(1.0f, 0.0f),
	};

	// tex coords for 6 quad version of geometry shader cube
	float2 gTexC[24] = 
	{
		float2(0.0f, 1.0f),
		float2(1.0f, 1.0f),
		float2(0.0f, 0.0f),
		float2(1.0f, 0.0f),

		float2(0.0f, 1.0f),
		float2(1.0f, 1.0f),
		float2(0.0f, 0.0f),
		float2(1.0f, 0.0f),

		float2(0.0f, 0.0f),
		float2(1.0f, 0.0f),
		float2(0.0f, 1.0f),
		float2(1.0f, 1.0f),

		float2(0.0f, 0.0f),
		float2(1.0f, 0.0f),
		float2(0.0f, 1.0f),
		float2(1.0f, 1.0f),

		float2(0.0f, 1.0f),
		float2(1.0f, 1.0f),
		float2(0.0f, 0.0f),
		float2(1.0f, 0.0f),

		float2(0.0f, 0.0f),
		float2(1.0f, 0.0f),
		float2(0.0f, 1.0f),
		float2(1.0f, 1.0f)
	};

	float3 gNormalsStrip[14] =
	{
		float3(0.0f, 0.0f, -1.0f),
		float3(0.0f, 0.0f, -1.0f),
		float3(0.0f, 0.0f, -1.0f),
		float3(0.0f, 0.0f, -1.0f),

		float3(1.0f, 0.0f,  0.0f),
		float3(1.0f, 0.0f,  0.0f),
		float3(1.0f, 0.0f,  0.0f),

		float3(-1.0f, 0.0f,  0.0f),
		float3(-1.0f, 0.0f,  0.0f),
		float3(-1.0f, 0.0f,  0.0f),
		float3(-1.0f, 0.0f,  0.0f),

		float3(0.0f, 0.0f,  1.0f),
		float3(0.0f, 0.0f,  1.0f),
		float3(0.0f, 0.0f,  1.0f)
	};

	float3 gNormals[24] =
	{
		float3(0.0f, 0.0f, -1.0f),
		float3(0.0f, 0.0f, -1.0f),
		float3(0.0f, 0.0f, -1.0f),
		float3(0.0f, 0.0f, -1.0f),

		float3(1.0f, 0.0f,  0.0f),
		float3(1.0f, 0.0f,  0.0f),
		float3(1.0f, 0.0f,  0.0f),
		float3(1.0f, 0.0f,  0.0f),

		float3(0.0f, 0.0f,  1.0f),
		float3(0.0f, 0.0f,  1.0f),
		float3(0.0f, 0.0f,  1.0f),
		float3(0.0f, 0.0f,  1.0f),

		float3(-1.0f, 0.0f,  0.0f),
		float3(-1.0f, 0.0f,  0.0f),
		float3(-1.0f, 0.0f,  0.0f),
		float3(-1.0f, 0.0f,  0.0f),

		float3(0.0f, 1.0f,  0.0f),
		float3(0.0f, 1.0f,  0.0f),
		float3(0.0f, 1.0f,  0.0f),
		float3(0.0f, 1.0f,  0.0f),

		float3(0.0f, -1.0f,  0.0f),
		float3(0.0f, -1.0f,  0.0f),
		float3(0.0f, -1.0f,  0.0f),
		float3(0.0f, -1.0f,  0.0f)
	};

[maxvertexcount(24)]
void GSBoxQuads(point VertexOut gin[1], 
        uint primID : SV_PrimitiveID, 
        inout TriangleStream<GeoOut> triStream)
{	
	// calculate halfWidth and height to create cube vertices			==> ONLY USING SIZE X!
	float halfWidth		= 0.5f*gin[0].SizeW.x;
	float halfHeight	= 0.5f*gin[0].SizeW.x;
	float halfDepth		= 0.5f*gin[0].SizeW.x;
	
	// create the 24 vertices of the cube
	float4 boxv[24];
	boxv[0] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[1] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[2] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[3] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[4] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[5] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[6] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[7] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[8] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[9] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[10] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[11] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[12] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[13] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[14] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[15] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[16] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[17] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[18] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[19] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[20] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[21] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[22] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[23] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);

	// indices 6 quads * 4 vertices
	int index[24] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
	
	// after each quad, restart the strip
	GeoOut gout;
	[unroll]
	for(int q1=1;q1<25;++q1)
	{
		gout.PosH     = mul(boxv[index[q1-1]], gViewProj);
		gout.PosW     = boxv[index[q1-1]].xyz;
		gout.NormalW  = gNormals[index[q1-1]];
		gout.Tex      = gTexC[index[q1-1]];
		gout.PrimID   = primID;
		
		triStream.Append(gout);

		if(q1 % 4 == 0) triStream.RestartStrip();
	}
}


[maxvertexcount(14)]
void GSBoxStrip(point VertexOut gin[1], 
        uint primID : SV_PrimitiveID, 
        inout TriangleStream<GeoOut> triStream)
{	
	// calculate halfWidth and height to create cube vertices			==> ONLY USING SIZE X!
	float halfWidth		= 0.5f*gin[0].SizeW.x;
	float halfHeight	= 0.5f*gin[0].SizeW.x;
	float halfDepth		= 0.5f*gin[0].SizeW.x;
	
	// create the 8 vertices of the cube
	float4 boxv[14];
	boxv[0] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[1] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[2] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[3] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[4] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[5] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[6] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[7] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[8] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[9] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	-halfDepth, 1.0f);
	boxv[10] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[11] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	-halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[12] = float4(gin[0].CenterW.x	-halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);
	boxv[13] = float4(gin[0].CenterW.x	+halfWidth, gin[0].CenterW.y	+halfHeight, gin[0].CenterW.z	+halfDepth, 1.0f);

	int index[14] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

	GeoOut gout;
	[unroll]
	for(int i=0;i<14;++i)
	{
		gout.PosH     = mul(boxv[index[i]], gViewProj);
		gout.PosW     = boxv[index[i]].xyz;
		gout.NormalW  = gNormalsStrip[index[i]];
		gout.Tex      = gTexCStrip[index[i]];
		gout.PrimID   = primID;
		
		triStream.Append(gout);
	}
}

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement