2 * Test variable array indexing in a vertex shader.
16 #include "shaderutil.h"
20 * The vertex position.z is used as a (variable) index into an
21 * array which returns a new Z value.
23 static const char *VertShaderText
=
24 "uniform sampler2D tex1; \n"
25 "uniform float HeightArray[20]; \n"
28 " vec4 pos = gl_Vertex; \n"
29 " int i = int(pos.z * 9.5); \n"
30 " pos.z = HeightArray[i]; \n"
31 " gl_Position = gl_ModelViewProjectionMatrix * pos; \n"
32 " gl_FrontColor = pos; \n"
35 static const char *FragShaderText
=
38 " gl_FragColor = gl_Color; \n"
42 static GLuint fragShader
;
43 static GLuint vertShader
;
44 static GLuint program
;
47 static GLboolean Anim
= GL_TRUE
;
48 static GLboolean WireFrame
= GL_TRUE
;
49 static GLfloat xRot
= -70.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
55 zRot
= 90 + glutGet(GLUT_ELAPSED_TIME
) * 0.05;
64 return fabs(cos(1.5*x
) + cos(1.5*y
));
71 GLfloat xmin
= -2.0, xmax
= 2.0;
72 GLfloat ymin
= -2.0, ymax
= 2.0;
73 GLuint xdivs
= 20, ydivs
= 20;
74 GLfloat dx
= (xmax
- xmin
) / xdivs
;
75 GLfloat dy
= (ymax
- ymin
) / ydivs
;
76 GLfloat ds
= 1.0 / xdivs
, dt
= 1.0 / ydivs
;
82 for (i
= 0; i
< ydivs
; i
++) {
85 glBegin(GL_QUAD_STRIP
);
86 for (j
= 0; j
< xdivs
; j
++) {
87 float z0
= fz(x
, y
), z1
= fz(x
, y
+ dy
);
92 glTexCoord2f(s
, t
+ dt
);
93 glVertex3f(x
, y
+ dy
, z1
);
107 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
110 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
112 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
115 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
116 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
117 glRotatef(zRot
, 0.0f
, 0.0f
, 1.0f
);
125 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
132 Reshape(int width
, int height
)
134 glViewport(0, 0, width
, height
);
135 glMatrixMode(GL_PROJECTION
);
137 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
138 glMatrixMode(GL_MODELVIEW
);
140 glTranslatef(0.0f
, 0.0f
, -15.0f
);
147 glDeleteShader_func(fragShader
);
148 glDeleteShader_func(vertShader
);
149 glDeleteProgram_func(program
);
150 glutDestroyWindow(win
);
155 Key(unsigned char key
, int x
, int y
)
157 const GLfloat step
= 2.0;
170 WireFrame
= !WireFrame
;
188 SpecialKey(int key
, int x
, int y
)
190 const GLfloat step
= 2.0;
216 GLfloat HeightArray
[20];
219 if (!ShadersSupported())
224 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, VertShaderText
);
225 fragShader
= CompileShaderText(GL_FRAGMENT_SHADER
, FragShaderText
);
226 program
= LinkShaders(vertShader
, fragShader
);
228 glUseProgram_func(program
);
230 /* Setup the HeightArray[] uniform */
231 for (i
= 0; i
< 20; i
++)
232 HeightArray
[i
] = i
/ 20.0;
233 u
= glGetUniformLocation_func(program
, "HeightArray");
234 glUniform1fv_func(u
, 20, HeightArray
);
236 assert(glGetError() == 0);
238 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
239 glEnable(GL_DEPTH_TEST
);
245 main(int argc
, char *argv
[])
247 glutInit(&argc
, argv
);
248 glutInitWindowSize(500, 500);
249 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
250 win
= glutCreateWindow(argv
[0]);
251 glutReshapeFunc(Reshape
);
252 glutKeyboardFunc(Key
);
253 glutSpecialFunc(SpecialKey
);
254 glutDisplayFunc(Redisplay
);