opengl - Elliptical gradient rotation in GLSL -


i implemented basic elliptical gradient in glsl , working fine. failed rotating gradient. code below:
vertex shader

uniform mat4 camera; uniform mat4 model;  in vec3 vert; // coordinates of vertex in vec2 verttexcoord; //pseudo texture coordinates, used calculating relative fragment position  out vec2 fragtexcoord;  void main() {     fragtexcoord = verttexcoord; //pass fragment shader     gl_position = camera * model * vec4(vert, 1); //apply transformations } 

fragment shader

uniform vec2 gradientcenter; //center of gradient uniform vec2 gradientdimensions; //how far gradient goes in right , direction respectively uniform vec2 gradientdirection; //rotation of gradient, not used..yet  in vec2 fragtexcoord;  out vec4 finalcolor;  void main() {     vec2 gradient = gradientcenter - fragtexcoord; //gradient     gradient.x = gradient.x * (1.0 / gradientdimensions.x); //relative scale on right direction, x axis     gradient.y = gradient.y * (1.0 / gradientdimensions.y); //relative scale on direction, y axis     float distancefromlight = length(gradient); //lenght determines output color     finalcolor = mix(vec4(1.0, 0.0, 0.0, 1.0), vec4(0.0, 0.0, 1.0, 1.0), distancefromlight * 2); //mixing red , blue, placeholder colors } 

to better illustrate, in upper picture have , working, in lower picture goal. how improve code allow elliptical gradient manipulation shown on lower picture? illustration

i assume gradientdirection normalized direction of first principal axis. can calculate coordinates in local system dot product:

vec2 secondaryprincipal = vec2(gradientdirection.y, -gradientdirection.x); vec2 gradient = gradientcenter - fragtexcoord; //gradient vec2 localgradient(dot(gradient, gradientdirection)  * (1.0 / gradientdimensions.x),                    dot(gradient, secondaryprincipal) * (1.0 / gradientdimensions.y)); float distancefromlight = length(localgradient);  //... 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

django - CSRF verification failed. Request aborted. CSRF cookie not set -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -