Jump to content
  • Advertisement
Sign in to follow this  
szalejot

GLSL get vec4 component

This topic is 2388 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

Hi,

I'm wondering if I can extract component of vec4 (this is 4 numbers in fact), for example third number. It will optimize my memory usage. Now as vertex shader attributes I use two vec4: (x,y,z,1.0) and (1.0,1.0,1.0,s). If I can extract components of vec4 I can use (x,y,z,s) vector as attribute and extract components for calculations.

Share this post


Link to post
Share on other sites
Advertisement
I'm not completely sure what you mean, but if I'm understanding correctly, this is what you want:

vec4 foo = vec4(1.0, 2.0, 3.0, 4.0); // Just an example vec4

float x = foo.x; // Get the x component of the vec4
float y = foo.y; // Get the y component of the vec4

// You can also get multiple components at once
vec2 xy = foo.xy;
vec3 xyz = foo.xyz;

Then obviously you can use this concept for more complex operations.

Share this post


Link to post
Share on other sites
It's worth to mention that this (syntactic and hardware) feature of shading languages is called SWIZZLING - http://en.wikipedia.org/wiki/Swizzling.
Just in case since it isn't obvious what you're asking, according to my knowledge, you cannot index your built-in vector/matrix types' components by variables (nor literals) in current shading language (such as vec3 v; float x = v[2]).

Share this post


Link to post
Share on other sites

It's worth to mention that this (syntactic and hardware) feature of shading languages is called SWIZZLING - http://en.wikipedia.org/wiki/Swizzling.
Just in case since it isn't obvious what you're asking, according to my knowledge, you cannot index your built-in vector/matrix types' components by variables (nor literals) in current shading language (such as vec3 v; float x = v[2]).


Actually, you can indeed use the subscript operator to index vector elements (at least, my GLSL v400 specs tell me so - I don't know what version this was introduced):



Array subscripting syntax can also be applied to vectors to provide numeric indexing. So in
vec4 pos;
pos[2] refers to the third element of pos and is equivalent to pos.z. This allows variable indexing into a
vector, as well as a generic way of accessing components. Any integer expression can be used as the
subscript. The first component is at index zero. Reading from or writing to a vector using a constant
integral expression with a value that is negative or greater than or equal to the size of the vector is illegal.
When indexing with non-constant expressions, behavior is undefined if the index is negative, or greater
than or equal to the size of the vector.
[/quote]

Share this post


Link to post
Share on other sites
Wow, awesome, I didn't know, my info seems pretty outdated. I've just tried in HLSL5 and it seems to work too, awesome! :-D

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!