aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/jdesktop/j3d/examples/gl2es2pipeline/polkadot3d.vert
blob: 042267057212acee2131cb19d795a60662192014 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// This is the Vertex Shader for three dimensional polka dots.
//
// author(s):  Joshua Doss
//
// Copyright (C) 2002-2004  3Dlabs Inc. Ltd.

// GL2ES2: Java3D built-in attributes, these are calculated and passsed in if declared here
attribute vec4 glVertex;
attribute vec3 glNormal;  

// GL2ES2: Java3D built-in uniforms, these are calculated and passsed in if declared here
uniform mat4 glModelViewMatrix;
uniform mat4 glModelViewProjectionMatrix;
uniform mat3 glNormalMatrix;

//Create uniform variables for lighting to allow user interaction
//uniform float SpecularContribution;
//uniform vec3 LightPosition;

const float SpecularContribution = 0.36;
const vec3 LightPosition = vec3 (0, 4, 5);

varying vec3 MCPosition;
varying float LightIntensity;

void main(void)
{
    float diffusecontribution  = 1.0 - SpecularContribution;
    
    // compute the vertex position in eye coordinates
    // GL2ES2: swap built-in variable for Java3d built-in uniforms and attributes gl_* = gl* + declaration (at top)
    //vec3  ecPosition           = vec3(gl_ModelViewMatrix * gl_Vertex);
    vec3  ecPosition           = vec3(glModelViewMatrix * glVertex);
    
    // compute the transformed normal
    // GL2ES2: swap built-in variable for Java3d built-in uniforms and attributes gl_* = gl* + declaration (at top)
    //vec3  tnorm                = normalize(gl_NormalMatrix * gl_Normal);
    vec3  tnorm                = normalize(glNormalMatrix * glNormal);
    
    // compute a vector from the model to the light position
    vec3  lightVec             = normalize(LightPosition - ecPosition);
    
    // compute the reflection vector
    vec3  reflectVec           = reflect(-lightVec, tnorm);
    
    // compute a unit vector in direction of viewing position
    vec3  viewVec              = normalize(-ecPosition);
    
    // calculate amount of diffuse light based on normal and light angle
    float diffuse              = max(dot(lightVec, tnorm), 0.0);
    float spec                 = 0.0;
    
    // if there is diffuse lighting, calculate specular
    if(diffuse > 0.0)
       {
          spec = max(dot(reflectVec, viewVec), 0.0);
          spec = pow(spec, 16.0);
       }
    
    // add up the light sources, since this is a varying (global) it will pass to frag shader     
    LightIntensity  = diffusecontribution * diffuse * 1.5 +
                          SpecularContribution * spec;
    
    // the varying variable MCPosition will be used by the fragment shader to determine where
    //    in model space the current pixel is           
    // GL2ES2: swap built-in variable for Java3d built-in uniforms and attributes gl_* = gl* + declaration (at top)           
    //MCPosition      = vec3 (gl_Vertex);
    MCPosition      = vec3 (glVertex);
    
    // send vertex information
    // GL2ES2: swap built-in variable for Java3d built-in uniforms and attributes gl_* = gl* + declaration (at top)
	//gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	gl_Position = glModelViewProjectionMatrix * glVertex;
}