2 * Vertex shader texture sampling test.
16 #include "shaderutil.h"
19 static const char *VertShaderText
=
20 "uniform sampler2D tex1; \n"
23 " vec4 pos = gl_Vertex; \n"
24 " pos.z = texture2D(tex1, gl_MultiTexCoord0.xy).x - 0.5; \n"
25 " gl_Position = gl_ModelViewProjectionMatrix * pos; \n"
26 " gl_FrontColor = pos; \n"
29 static const char *FragShaderText
=
32 " gl_FragColor = gl_Color; \n"
36 static GLuint fragShader
;
37 static GLuint vertShader
;
38 static GLuint program
;
41 static GLboolean Anim
= GL_TRUE
;
42 static GLboolean WireFrame
= GL_TRUE
;
43 static GLfloat xRot
= -70.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
46 /* value[0] = tex unit */
47 static struct uniform_info Uniforms
[] = {
48 { "tex1", 1, GL_INT
, { 0, 0, 0, 0 }, -1 },
57 zRot
= 90 + glutGet(GLUT_ELAPSED_TIME
) * 0.05;
65 GLfloat xmin
= -2.0, xmax
= 2.0;
66 GLfloat ymin
= -2.0, ymax
= 2.0;
67 GLuint xdivs
= 20, ydivs
= 20;
68 GLfloat dx
= (xmax
- xmin
) / xdivs
;
69 GLfloat dy
= (ymax
- ymin
) / ydivs
;
70 GLfloat ds
= 1.0 / xdivs
, dt
= 1.0 / ydivs
;
76 for (i
= 0; i
< ydivs
; i
++) {
79 glBegin(GL_QUAD_STRIP
);
80 for (j
= 0; j
< xdivs
; j
++) {
83 glTexCoord2f(s
, t
+ dt
);
84 glVertex2f(x
, y
+ dy
);
99 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
101 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
103 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
106 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
107 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
108 glRotatef(zRot
, 0.0f
, 0.0f
, 1.0f
);
121 Reshape(int width
, int height
)
123 glViewport(0, 0, width
, height
);
124 glMatrixMode(GL_PROJECTION
);
126 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
127 glMatrixMode(GL_MODELVIEW
);
129 glTranslatef(0.0f
, 0.0f
, -15.0f
);
136 glDeleteShader_func(fragShader
);
137 glDeleteShader_func(vertShader
);
138 glDeleteProgram_func(program
);
139 glutDestroyWindow(win
);
144 Key(unsigned char key
, int x
, int y
)
146 const GLfloat step
= 2.0;
159 WireFrame
= !WireFrame
;
177 SpecialKey(int key
, int x
, int y
)
179 const GLfloat step
= 2.0;
205 const GLuint texWidth
= 64, texHeight
= 64;
206 GLfloat texImage
[64][64];
209 /* texture is basically z = f(x, y) */
210 for (i
= 0; i
< texHeight
; i
++) {
211 GLfloat y
= 2.0 * (i
/ (float) (texHeight
- 1)) - 1.0;
212 for (j
= 0; j
< texWidth
; j
++) {
213 GLfloat x
= 2.0 * (j
/ (float) (texWidth
- 1)) - 1.0;
214 GLfloat z
= 0.5 + 0.5 * (sin(4.0 * x
) * sin(4.0 * y
));
219 glActiveTexture(GL_TEXTURE0
);
220 glBindTexture(GL_TEXTURE_2D
, 42);
221 glTexImage2D(GL_TEXTURE_2D
, 0, GL_INTENSITY
, texWidth
, texHeight
, 0,
222 GL_LUMINANCE
, GL_FLOAT
, texImage
);
223 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
224 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
233 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB
, &m
);
235 printf("Error: no vertex shader texture units supported.\n");
239 if (!ShadersSupported())
244 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, VertShaderText
);
245 fragShader
= CompileShaderText(GL_FRAGMENT_SHADER
, FragShaderText
);
246 program
= LinkShaders(vertShader
, fragShader
);
248 glUseProgram_func(program
);
250 assert(glGetError() == 0);
254 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
256 glEnable(GL_DEPTH_TEST
);
263 main(int argc
, char *argv
[])
265 glutInit(&argc
, argv
);
266 glutInitWindowSize(500, 500);
267 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
268 win
= glutCreateWindow(argv
[0]);
269 glutReshapeFunc(Reshape
);
270 glutKeyboardFunc(Key
);
271 glutSpecialFunc(SpecialKey
);
272 glutDisplayFunc(Redisplay
);