Cube reflection mapping problem [solved]

Problems building or running the engine, queries about how to use features etc.
Post Reply
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1067
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
x 43
Contact:

Cube reflection mapping problem [solved]

Post by Oogst »

I am trying to do reflection mapping using a cube map, but I get quite odd results. Now I thought the code for this would be incredibly simple, but I am able to do something wrong anyway. So: what am I doing wrong here?

Material definition:

Code: Select all

vertex_program Cg_Reflector_VP cg
{
	source Reflector.cg
	entry_point Reflector_VP
	profiles vs_2_0

	default_params
	{
		param_named_auto worldViewProjMatrix worldviewproj_matrix
		param_named_auto cameraPosition camera_position_object_space
	}
}

fragment_program Cg_Reflector_FP cg
{
	source Reflector.cg
	entry_point Reflector_FP
	profiles ps_2_0
}

material Reflector
{
	technique
	{
		pass
		{
			vertex_program_ref Cg_Reflector_VP
			{
			}
			fragment_program_ref Cg_Reflector_FP
			{
			}
			texture_unit
			{
				cubic_texture FR.tga BR.tga LF.tga RT.tga UP.tga DN.tga separateUV
			}
		}
	}
}
Shaders:

Code: Select all

void Reflector_VP
	(
		float4 position : POSITION,
		float3 normal : NORMAL,

		out float4 oPosition : POSITION,
		out float3 oReflection : TEXCOORD0,

		uniform float4x4 worldViewProjMatrix,
		uniform float3 cameraPosition
	)
{
	oPosition = mul(worldViewProjMatrix, position);
	oReflection = reflect(position - cameraPosition, normal);
}

void Reflector_FP
	(
		float3 reflection : TEXCOORD0,

		out float4 oColour : COLOR,

		uniform samplerCUBE cubeTexture
	)
{
	oColour = texCUBE(cubeTexture, reflection);
}
Last edited by Oogst on Fri Apr 13, 2007 6:44 pm, edited 3 times in total.
My dev blog
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/X1/PS3/PS4)
Blightbound: coop online dungeon crawler (PC)
Swords & Soldiers: side-scrolling RTS (Switch/PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance
The Ageless Gate: cello album
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Post by sinbad »

Code: Select all

float3 reflection : TEXCOORD3, 
That should be TEXCOORD0.
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1067
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
x 43
Contact:

Post by Oogst »

Ow, sorry, that TEXCOORD3 is actually a problem that came from simplifying my larger shader to just post the cube map problem here. So that is not the cause.

Maybe it helps if I show what is wrong. This is what my shader looks like right now:

Image

Image

Image

As can be seen, the number of reflections changes with the distance to the camera. That really should not happen.
My dev blog
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/X1/PS3/PS4)
Blightbound: coop online dungeon crawler (PC)
Swords & Soldiers: side-scrolling RTS (Switch/PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance
The Ageless Gate: cello album
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1067
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
x 43
Contact:

Post by Oogst »

Does no one have a clue what I am doing wrong?
My dev blog
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/X1/PS3/PS4)
Blightbound: coop online dungeon crawler (PC)
Swords & Soldiers: side-scrolling RTS (Switch/PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance
The Ageless Gate: cello album
jjp
Silver Sponsor
Silver Sponsor
Posts: 597
Joined: Sun Jan 07, 2007 11:55 pm
Location: Cologne, Germany
Contact:

Post by jjp »

I am not 100% sure because I don't know a lot about shaders yet but my guess is: you need to make sure that all your variables are in the correct space when you do the calculation. An environment map is typically oriented in world space. So that's where your position, normal and camera position have to be as well.
Enough is never enough.
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Post by sinbad »

Can you confirm that's actually a cube map? Have you tried using the fixed-function cubic_reflection option?

BTW: I had a weekend break from the forums which is why I didn't reply.
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1067
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
x 43
Contact:

Post by Oogst »

I tried putting in the fixed function version and that seems to work:

Image

Image

Reflections move nicely when I move the camera around. A bit odd, though, is that the manual says this for cubic_reflection: "Requires that you bind a cubic_texture to this texture unit and use the 'combinedUVW' option." I tried using combinedUVW, but got an entirely white object. I then tried with separateUV and that works perfectly well. Just to be sure, these are my cube-map textures:

Image

Image

Image

Image

Image

Image

I think the allignment of these is wrong, so some of these names should actually be flipped, but that should just cause seams and the air in the wrong place, not the odd repetitions I am getting with my shader.

This is my fixed function version:

Code: Select all

material Reflector
{ 
   technique 
   { 
      pass 
      { 
         texture_unit
         { 
         	env_map cubic_reflection
         	cubic_texture FR.tga BR.tga LF.tga RT.tga UP.tga DN.tga separateUV
         } 
      }
   }
}
My dev blog
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/X1/PS3/PS4)
Blightbound: coop online dungeon crawler (PC)
Swords & Soldiers: side-scrolling RTS (Switch/PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance
The Ageless Gate: cello album
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Post by sinbad »

No, that's not working - you're just seeing one of the cube faces there. You do need to use combinedUVW to create a proper cube map - separateUV generates 6 2D textures needed for skyboxes.

This is the underlying problem - I missed that in the material script. The shader pipeline is just wrapping differently. Take a second look at the manul - you can't use combinedUVW with 6 textures, you actually need a proper 3D texture or just a base name (this is a nuance in the way the loading works). Either rename your files or better, use the DirectX texture tool to combine those 6 textures into a cubic DDS texture, so you can use the one filename version..
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1067
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
x 43
Contact:

Post by Oogst »

Ah, I totally missed that aspect! Stupid of me! I'll give it a try (cannot do that at school right now).
My dev blog
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/X1/PS3/PS4)
Blightbound: coop online dungeon crawler (PC)
Swords & Soldiers: side-scrolling RTS (Switch/PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance
The Ageless Gate: cello album
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1067
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
x 43
Contact:

Post by Oogst »

OK, I tried it and indeed that was the solution to the problem. Thank you very much! :D
My dev blog
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/X1/PS3/PS4)
Blightbound: coop online dungeon crawler (PC)
Swords & Soldiers: side-scrolling RTS (Switch/PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance
The Ageless Gate: cello album
OGRE_DT
Gnoblar
Posts: 5
Joined: Tue Jul 20, 2010 2:32 pm

Re: Cube reflection mapping problem [solved]

Post by OGRE_DT »

Hi,I'm doing it now ,but i have a problem is error C3004: function "texCUBE" not supported in this profile. Why?
21:23:09: OGRE EXCEPTION(7:InternalErrorException): Unable to compile Cg program fragment_dispersion: CG ERROR : The compile returned an error.
(9) : error C3004: function "texCUBE" not supported in this profile
(12) : error C3004: function "texCUBE" not supported in this profile
(13) : error C3004: function "texCUBE" not supported in this profile
(14) : error C3004: function "texCUBE" not supported in this profile
in CgProgram::loadFromSource at f:\codingextra\ogre\shoggoth_vc8\ogre\plugins\cgprogrammanager\src\ogrecgprogrammanagerdll.cpp (line 66)
21:23:09: High-level program fragment_dispersion encountered an error during loading and is thus not supported.
OGRE EXCEPTION(7:InternalErrorException): Unable to compile Cg program fragment_dispersion: CG ERROR : The compile returned an error.
(9) : error C3004: function "texCUBE" not supported in this profile
(12) : error C3004: function "texCUBE" not supported in this profile
(13) : error C3004: function "texCUBE" not supported in this profile
(14) : error C3004: function "texCUBE" not supported in this profile
in CgProgram::loadFromSource at f:\codingextra\ogre\shoggoth_vc8\ogre\plugins\cgprogrammanager\src\ogrecgprogrammanagerdll.cpp (line 66)
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Re: Cube reflection mapping problem [solved]

Post by sinbad »

The error is accuate, it means the shader profile doesn't support texCUBE. The most likely reason is that you accidentally used a vertex shader profile for a fragment shader. It's clear you didn't just copy the example because the shader name is different.
OGRE_DT
Gnoblar
Posts: 5
Joined: Tue Jul 20, 2010 2:32 pm

Re: Cube reflection mapping problem [solved]

Post by OGRE_DT »

sinbad wrote:The error is accuate, it means the shader profile doesn't support texCUBE. The most likely reason is that you accidentally used a vertex shader profile for a fragment shader. It's clear you didn't just copy the example because the shader name is different.
yes, you are right. I changed the vertex shader profile for vs_1_1 vp40. And it works. Thank you for your help.
Post Reply