2 * Test variable array indexing in a vertex shader.
14 #include "shaderutil.h"
18 * The vertex position.z is used as a (variable) index into an
19 * array which returns a new Z value.
21 static const char *VertShaderText
=
22 "uniform sampler2D tex1; \n"
23 "uniform float HeightArray[20]; \n"
26 " vec4 pos = gl_Vertex; \n"
27 " int i = int(pos.z * 9.5); \n"
28 " pos.z = HeightArray[i]; \n"
29 " gl_Position = gl_ModelViewProjectionMatrix * pos; \n"
30 " gl_FrontColor = pos; \n"
33 static const char *FragShaderText
=
36 " gl_FragColor = gl_Color; \n"
40 static GLuint fragShader
;
41 static GLuint vertShader
;
42 static GLuint program
;
45 static GLboolean Anim
= GL_TRUE
;
46 static GLboolean WireFrame
= GL_TRUE
;
47 static GLfloat xRot
= -70.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
53 zRot
= 90 + glutGet(GLUT_ELAPSED_TIME
) * 0.05;
62 return fabs(cos(1.5*x
) + cos(1.5*y
));
69 GLfloat xmin
= -2.0, xmax
= 2.0;
70 GLfloat ymin
= -2.0, ymax
= 2.0;
71 GLuint xdivs
= 20, ydivs
= 20;
72 GLfloat dx
= (xmax
- xmin
) / xdivs
;
73 GLfloat dy
= (ymax
- ymin
) / ydivs
;
74 GLfloat ds
= 1.0 / xdivs
, dt
= 1.0 / ydivs
;
80 for (i
= 0; i
< ydivs
; i
++) {
83 glBegin(GL_QUAD_STRIP
);
84 for (j
= 0; j
< xdivs
; j
++) {
85 float z0
= fz(x
, y
), z1
= fz(x
, y
+ dy
);
90 glTexCoord2f(s
, t
+ dt
);
91 glVertex3f(x
, y
+ dy
, z1
);
105 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
108 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
110 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
113 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
114 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
115 glRotatef(zRot
, 0.0f
, 0.0f
, 1.0f
);
123 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
130 Reshape(int width
, int height
)
132 glViewport(0, 0, width
, height
);
133 glMatrixMode(GL_PROJECTION
);
135 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
136 glMatrixMode(GL_MODELVIEW
);
138 glTranslatef(0.0f
, 0.0f
, -15.0f
);
145 glDeleteShader(fragShader
);
146 glDeleteShader(vertShader
);
147 glDeleteProgram(program
);
148 glutDestroyWindow(win
);
153 Key(unsigned char key
, int x
, int y
)
155 const GLfloat step
= 2.0;
168 WireFrame
= !WireFrame
;
186 SpecialKey(int key
, int x
, int y
)
188 const GLfloat step
= 2.0;
214 GLfloat HeightArray
[20];
217 if (!ShadersSupported())
220 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, VertShaderText
);
221 fragShader
= CompileShaderText(GL_FRAGMENT_SHADER
, FragShaderText
);
222 program
= LinkShaders(vertShader
, fragShader
);
224 glUseProgram(program
);
226 /* Setup the HeightArray[] uniform */
227 for (i
= 0; i
< 20; i
++)
228 HeightArray
[i
] = i
/ 20.0;
229 u
= glGetUniformLocation(program
, "HeightArray");
230 glUniform1fv(u
, 20, HeightArray
);
232 assert(glGetError() == 0);
234 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
235 glEnable(GL_DEPTH_TEST
);
241 main(int argc
, char *argv
[])
243 glutInit(&argc
, argv
);
244 glutInitWindowSize(500, 500);
245 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
246 win
= glutCreateWindow(argv
[0]);
248 glutReshapeFunc(Reshape
);
249 glutKeyboardFunc(Key
);
250 glutSpecialFunc(SpecialKey
);
251 glutDisplayFunc(Redisplay
);