Jump to content
  • Advertisement
Sign in to follow this  
VISQI

Question of HLSL Syntax...

This topic is 2493 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:



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) };
}


or having constructors in structures like this:


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


Thanks

Share this post


Link to post
Share on other sites
Advertisement
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.

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));
}


As for the initialization with the array like expression: you can only use those at the moment you declare a variable/array.

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) };
}


Cheers! :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!