Sign in to follow this  

Question of HLSL Syntax...

This topic is 2124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys
I was wondering why do i get an error when i try to initialize an array in HLSL like this:
[code]


struct Wall
{
float3 Pos;
float2 Normal;
float3 TexC;
};

void Generate()
{
Wall w[3];
w[0] = { float3(-1.0f,-1.0f,-1.0f), float3(0.0f,0.0f,-1.0f), float2(0.0f,1.0f) };
w[1] = { float3(-1.0f, 1.0f,-1.0f), float3(0.0f,0.0f,-1.0f), float2(0.0f,0.0f) };
w[2] = { float3( 1.0f, 1.0f,-1.0f), float3(0.0f,0.0f,-1.0f), float2(1.0f,0.0f) };
}[/code]

or having constructors in structures like this:
[code]

struct Vec
{
Vec(float X, float Y, float Z)
{
x = X;
y = Y;
z = Z;
}
float x;
float y;
float z;
};
[/code]

Thanks

Share this post


Link to post
Share on other sites
Hi!

HLSL doen't have constructors for types others than the native types.
Usually people just write a global method that fills in the members.
[CODE]
struct Wall
{
float3 Pos;
float3 Normal;
float2 TexC;
};

Wall make_wall(float3 pos, float3 normal, float2 texC)
{
Wall wall;
wall.Pos = pos;
wall.Normal = normal;
wall.TexC = texC;
return wall;
}

void Generate()
{
Wall w[3]; // no array-like initialization, since not initialized at declaration time.
w[0] = make_wall(float3(-1.0f,-1.0f,-1.0f), float3(0.0f,0.0f,-1.0f), float2(0.0f,1.0f));
w[1] = make_wall(float3(-1.0f, 1.0f,-1.0f), float3(0.0f,0.0f,-1.0f), float2(0.0f,0.0f));
w[2] = make_wall(float3( 1.0f, 1.0f,-1.0f), float3(0.0f,0.0f,-1.0f), float2(1.0f,0.0f));
}
[/CODE]

As for the initialization with the array like expression: you can only use those at the moment you declare a variable/array.
[CODE]
void Generate2()
{
// Initialize an array while declaring it.
Wall w2[3] = {
{ float3(-1.0f,-1.0f,-1.0f), float3(0.0f,0.0f,-1.0f), float2(0.0f,1.0f) },
{ float3(-1.0f, 1.0f,-1.0f), float3(0.0f,0.0f,-1.0f), float2(0.0f,0.0f) },
{ float3( 1.0f, 1.0f,-1.0f), float3(0.0f,0.0f,-1.0f), float2(1.0f,0.0f) } };

// Initialize a variable while declaring it.
Wall w3 = { float3(-1.0f,-1.0f,-1.0f), float3(0.0f,0.0f,-1.0f), float2(0.0f,1.0f) };
}
[/CODE]

Cheers! :)

Share this post


Link to post
Share on other sites

This topic is 2124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this