Skip to main content
gamedev.net
🔒 Locked

cube map coordinate problem

Started by llfnana Nov 22, 2011 at 2:15 PM 11 replies 2.5k views
Original Post
llfnana
llfnana
Hello, everyone?In my renderer to deal with some of the lightmap, I packaged them into a cubemap, I made ??a test sample cubemap only the + X axis, this is my code?


sampler2D baseMap;
samplerCUBE Lightmap1;

struct PS_INPUT
{
float4 Diffuse : COLOR0;
float2 Texcoord : TEXCOORD0;
float2 Texcoord1 : TEXCOORD1;
};

float4 ps_main( PS_INPUT Input ) : COLOR0
{

float4 Tex1,Tex2;

Tex1=tex2D(baseMap,Input.Texcoord);

Tex2=texCUBE(Lightmap1,float3(Input.Texcoord1,0));
return Tex1*Tex2;
}

The result is wrong, I know the texture coordinates wrong, but I do not know how to calculate the correct cube mapcoordinates. Please give me some tips, thanks, I am sorry my English
kauna
kauna

Hello, everyone?In my renderer to deal with some of the lightmap, I packaged them into a cubemap, I made ??a test sample cubemap only the + X axis, this is my code?


sampler2D baseMap;
samplerCUBE Lightmap1;

struct PS_INPUT
{
float4 Diffuse : COLOR0;
float2 Texcoord : TEXCOORD0;
float2 Texcoord1 : TEXCOORD1;
};

float4 ps_main( PS_INPUT Input ) : COLOR0
{

float4 Tex1,Tex2;

Tex1=tex2D(baseMap,Input.Texcoord);

Tex2=texCUBE(Lightmap1,float3(Input.Texcoord1,0));
return Tex1*Tex2;
}

The result is wrong, I know the texture coordinates wrong, but I do not know how to calculate the correct cube mapcoordinates. Please give me some tips, thanks, I am sorry my English


You'll need all the x,y,z-coordinates to access any of sides of the cube map. In your code for example, you set your z-axis to zero and you can sample just a thin slice of +X,-X,+Y,-Y planes. To access +X plane, you'll need texture coordinates between (1,1,1) and (1,-1,-1)

The cubemap texture coordinate can be considered as a direction vector inside the cube starting from middle of the cube and the heading out of the cube (all the vectors except 0,0,0). So practically finding the texel which the direction vector points is a question of ray / box intersection test. By knowing that, you should be able to calculate the direction vector for any texel in any cube map side.

Since only the direction of the vector matters, for example, vectors (1,1,1) and (2,2,2) point actually to the same texel.

Why are you storing light maps to a cubemap?

Best regards!
Krypt0n
Krypt0n
are you sure that's what you want to do? I think it's not common to use a cubemap for lightmaps.

in order to access cubemap sides, you need to make one coordinate the biggest magnitude


lets say you have a



float2 UV;

int SideIndex;




to access sides, you'll create UV's like that



float Bias=1.001f;



float3 CubeUV;

if( SideIndex==0)

CubeUV =float3(max(abs(UV.x),abs(UV.y))* Bias,UV.x,UV.y);

elseif( SideIndex==1)

CubeUV =float3(-max(abs(UV.x),abs(UV.y))*Bias,UV.x,UV.y);

else

if( SideIndex==2)

CubeUV =float3(UV.x,max(abs(UV.x),abs(UV.y))*Bias,UV.y);

else

if( SideIndex==3)

CubeUV =float3(UV.x,-max(abs(UV.x),abs(UV.y))*Bias,UV.y);

else

if( SideIndex==4)

CubeUV =float3(UV.x, UV.y,max(abs(UV.x),abs(UV.y))*Bias);

else

CubeUV =float3(UV.x, UV.y,-max(abs(UV.x),abs(UV.y))*Bias);

Tex2=texCUBE(Lightmap1,CubeUV);






indexing in one 2d-texture might be less complicated.
llfnana
llfnana
All scene objects are lightmap, if not packaged them together, there are many batches, can not use the instance, which will cost performance, although the light maps can be packed into a large 2d texture, but my idea is this:

My scene objects may be normal maps, specular maps, etc., then I want to lightmap packaged into a cubemap texture, normal map to a cubemap to another package, so I chose the cube map, it used as a texture array.Is there a better way?
Please tell me, thanks.
llfnana
llfnana
Before this, I selected the dynamic light maps, but the effect is wrong

sampler2D baseMap[16];


struct PS_INPUT
{
float4 Diffuse : COLOR0;
float2 Texcoord : TEXCOORD0;
float2 Texcoord1 : TEXCOORD1;
float2 Texcoord2 : TEXCOORD2;
};

float4 ps_main( PS_INPUT Input ) : COLOR0
{
int iMapIndex;
float4 Tex1,Tex2;

iMapIndex=Input.Texcoord2.x;
Tex1=tex2D(baseMap[0],Input.Texcoord);

if(iMapIndex==1)
{
Tex2=tex2D(baseMap[1],Input.Texcoord1);
}
if(iMapIndex==2)
{
Tex2=tex2D(baseMap[2],Input.Texcoord1);
}

if(iMapIndex==3)
{
Tex2=tex2D(baseMap[3],Input.Texcoord1);
}

.................................

if(iMapIndex==15)
{
Tex2=tex2D(baseMap[15],Input.Texcoord1);

}
return Tex1*Tex2;
}

other people say can not be used in the control flow inside the tex2D?why?

llfnana
llfnana
There will be a compilation error, error is sampler array index must be a literal expression
kauna
kauna
[font="Arial"]Hi,[/font]

[font="Arial"]I think that you cannot access a sampler with variable index. eg.

Tex2=tex2D(baseMap[iMapIndex],Input.Texcoord1); It has to be known at compile time.

[/font]

[font="Arial"]I think that you are trying to write overly generic shader. You should narrow down the cases which you'll encounter in your project. Also, you may use compile time flags #ifdef's to generate a shader with a specific need.[/font]

[font="Arial"]Best regards![/font]

llfnana
llfnana
Thank you all, I try to code
llfnana
llfnana
volume texture?Thank you, I think it should be pre-generated, because it did not like the cubemap texture data can be used UpdateTexture update ,in real time, to update the volume texture it is low performance, is it?
llfnana
llfnana
but I used DirectX 9,DirectX 9 cannot render to 3D texture

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.