Permalink
Browse files

Getting rid of some warnings in the shaders

  • Loading branch information...
1 parent 0cea85e commit 917956a8c2296546279a3e2df2dd5863c627ec57 @ejcaruso ejcaruso committed Dec 9, 2011
Showing with 25 additions and 48 deletions.
  1. +0 −1 lab/glwidget.cpp
  2. +0 −3 shaders/terrain.frag
  3. +25 −44 shaders/terrain.vert
View
@@ -281,7 +281,6 @@ void GLWidget::renderScene() {
glPolygonMode(GL_FRONT, GL_FILL);
m_shaderPrograms["terrain"]->bind();
- m_planet->render();
// load textures
glActiveTexture(GL_TEXTURE0);
@@ -9,8 +9,5 @@ uniform sampler2D tex3;
uniform sampler2D tex4;
void main() {
- // gl_FragColor = intensity;
gl_FragColor = texture2D(tex1, gl_TexCoord[0].st) * intensity;
- // gl_FragColor = norm;
- // gl_FragColor.rg = gl_TexCoord[0].st;
}
View
@@ -2,18 +2,18 @@
uniform float global_amp_scale = 0.05;
uniform float global_pos_scale = 2.0;
uniform float norm_eps = 0.00001;
+uniform int planet_seed = 98738;
+uniform int noise_octaves = 2;
varying float intensity;
varying float height;
-varying vec3 norm;
-
vec3 roty(vec3 v, float angle) {
- return mat3(cos(angle),0,-sin(angle),0,1,0,sin(angle),0,cos(angle))*v;
+ return mat3(cos(angle),0.0,-sin(angle),0.0,1.0,0.0,sin(angle),0.0,cos(angle))*v;
}
vec3 rotx(vec3 v, float angle) {
- return mat3(1,0,0,0,cos(angle),sin(angle),0,-sin(angle),cos(angle))*v;
+ return mat3(1.0,0.0,0.0,0.0,cos(angle),sin(angle),0.0,-sin(angle),cos(angle))*v;
}
float curve(float x) {
@@ -62,6 +62,7 @@ float interpolatedValue(vec3 pos, int seed) {
return mix(t_3, t_4, cx);
}
*/
+
vec3 randomVector(int x, int y, int z, int seed) {
x = 57 * y + 13 * z + x;
y = 47 * z + 19 * x + y;
@@ -70,9 +71,9 @@ vec3 randomVector(int x, int y, int z, int seed) {
y = (y << 13) ^ (y + seed);
z = (z << 13) ^ (z + seed);
- vec3 random = vec3( ( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0),
- ( 1.0 - ( (y * (y * y * 18637 + 829463) + 1265124733) & 0x7fffffff) / 1073741824.0),
- ( 1.0 - ( (z * (z * z * 14737 + 758629) + 1220927501) & 0x7fffffff) / 1073741824.0));
+ vec3 random = vec3( ( 1.0 - float( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0),
+ ( 1.0 - float( (y * (y * y * 18637 + 829463) + 1265124733) & 0x7fffffff) / 1073741824.0),
+ ( 1.0 - float( (z * (z * z * 14737 + 758629) + 1220927501) & 0x7fffffff) / 1073741824.0));
return normalize(random);
}
@@ -101,51 +102,49 @@ float interpolatedNoise(vec3 pos, int seed) {
vec3 v_7 = randomVector(px+1, py+1, pz, seed);
vec3 v_8 = randomVector(px+1, py+1, pz+1, seed);
- float tv_1 = mix(dot(vec3(fx, fy, fz), v_1), dot(vec3(fx, fy, fz-1), v_2), cz);
- float tv_2 = mix(dot(vec3(fx, fy-1, fz), v_3), dot(vec3(fx, fy-1, fz-1), v_4), cz);
+ float tv_1 = mix(dot(vec3(fx, fy, fz), v_1), dot(vec3(fx, fy, fz-1.0), v_2), cz);
+ float tv_2 = mix(dot(vec3(fx, fy-1.0, fz), v_3), dot(vec3(fx, fy-1.0, fz-1.0), v_4), cz);
float t_3 = mix(tv_1, tv_2, cy);
- tv_1 = mix(dot(vec3(fx-1, fy, fz), v_5), dot(vec3(fx-1, fy, fz-1), v_6), cz);
- tv_2 = mix(dot(vec3(fx-1, fy-1, fz), v_7), dot(vec3(fx-1, fy-1, fz-1), v_8), cz);
+ tv_1 = mix(dot(vec3(fx-1.0, fy, fz), v_5), dot(vec3(fx-1.0, fy, fz-1.0), v_6), cz);
+ tv_2 = mix(dot(vec3(fx-1.0, fy-1.0, fz), v_7), dot(vec3(fx-1.0, fy-1.0, fz-1.0), v_8), cz);
float t_4 = mix(tv_1, tv_2, cy);
return mix(t_3, t_4, cx);
}
float perlinNoise(vec3 pos, int seed, int octaves) {
pos += vec3(1.0,1.0,1.0);
- float offset = 0;
+ float offset = 0.0;
float frequency, amplitude;
for (int i = 0; i < octaves; i++) {
- frequency = 2 << i;
- amplitude = 1 / sqrt(frequency);
+ frequency = float(2 << i);
+ amplitude = 1.0 / sqrt(frequency);
offset += interpolatedNoise(frequency * pos, seed) * amplitude;
}
return offset;
}
-vec3 perturbedNormal(vec3 pos, vec3 norm, int seed, int octaves) {
+vec3 perturbedNormal(vec3 pos, vec3 norm, float disp, int seed, int octaves) {
float pi = 3.1415926;
float u = mod(gl_MultiTexCoord0.s+0.25,1.0);
- float v = 1-gl_MultiTexCoord0.t;
+ float v = 1.0-gl_MultiTexCoord0.t;
- float theta = u * 2 * pi;
+ float theta = u * 2.0 * pi;
float phi = v * pi;
- vec3 ortho_1 = roty(rotx(vec3(-1,0,0),phi),theta);
- vec3 ortho_2 = roty(rotx(vec3(0,0,1),phi),theta);
+ vec3 ortho_1 = roty(rotx(vec3(-1.0,0.0,0.0),phi),theta);
+ vec3 ortho_2 = roty(rotx(vec3(0.0,0.0,1.0),phi),theta);
vec3 eps_o1 = norm_eps * ortho_1;
vec3 eps_o2 = norm_eps * ortho_2;
- float n_0 = global_amp_scale * perlinNoise(pos,seed,octaves);
+ float n_0 = disp;
float n_1 = global_amp_scale * perlinNoise(pos+eps_o1,seed,octaves);
float n_2 = global_amp_scale * perlinNoise(pos-eps_o1,seed,octaves);
float n_3 = global_amp_scale * perlinNoise(pos+eps_o2,seed,octaves);
float n_4 = global_amp_scale * perlinNoise(pos-eps_o2,seed,octaves);
- // n_1 = n_2 = n_3 = n_4 = n_0;
-
vec3 v_1 = eps_o1+(n_1-n_0)*norm;
vec3 v_2 = -eps_o1+(n_2-n_0)*norm;
vec3 v_3 = eps_o2+(n_3-n_0)*norm;
@@ -154,33 +153,15 @@ vec3 perturbedNormal(vec3 pos, vec3 norm, int seed, int octaves) {
return normalize(cross(v_1,v_3)+cross(v_3,v_2)+cross(v_2,v_4)+cross(v_4,v_1));
}
-/*
- vec3 ortho_1 = vec3(-norm.y,norm.x,0);
- vec3 ortho_2 = cross(norm,ortho_1);
-
- vec3 eps_o1 = norm_eps * ortho_1;
- vec3 eps_o2 = norm_eps * ortho_2;
-
- float n_1 = perlinNoise(pos+eps_o1,seed,octaves);
- float n_2 = perlinNoise(pos-eps_o1,seed,octaves);
- float n_3 = perlinNoise(pos+eps_o2,seed,octaves);
- float n_4 = perlinNoise(pos-eps_o2,seed,octaves);
-
- return normalize(cross(2*eps_o1+(n_1-n_2)*norm,2*eps_o2+(n_3-n_4)*norm));
-*/
-
void main() {
- vec3 perturbedVertex = gl_Vertex.xyz + global_amp_scale * perlinNoise(gl_Vertex.xyz, 98738, 2) * gl_Normal;
+ height = global_amp_scale * perlinNoise(gl_Vertex.xyz, planet_seed, noise_octaves);
+ vec3 perturbedVertex = gl_Vertex.xyz + height * gl_Normal;
gl_Position = gl_ModelViewProjectionMatrix * vec4(perturbedVertex, 1);
- // vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
- vec3 normal = normalize(gl_NormalMatrix * perturbedNormal(gl_Vertex, gl_Normal, 98738, 2));
- norm = vec4(perturbedNormal(gl_Vertex, gl_Normal, 98738, 10),1);
- vec3 light = normalize((gl_ModelViewMatrix*vec4(0.0,0.0,10.0,1.0)) - (gl_ModelViewMatrix * gl_Vertex)).xyz;
+ vec3 normal = normalize((gl_NormalMatrix * perturbedNormal(gl_Vertex, gl_Normal, height, planet_seed, noise_octaves)).xyz);
+ vec3 light = normalize((gl_LightSource[0].position - (gl_ModelViewMatrix * gl_Vertex)).xyz);
intensity = max(0.0, dot(normal, light));
gl_TexCoord[0] = gl_MultiTexCoord0;
-
- height = perturbedVertex.z;
}

0 comments on commit 917956a

Please sign in to comment.