2 * Vertex shader texture sampling test.
14 #include "shaderutil.h"
17 static const char *VertShaderText
=
18 "uniform sampler2D tex1; \n"
21 " vec4 pos = gl_Vertex; \n"
22 " pos.z = texture2D(tex1, gl_MultiTexCoord0.xy).x - 0.5; \n"
23 " gl_Position = gl_ModelViewProjectionMatrix * pos; \n"
24 " gl_FrontColor = pos; \n"
27 static const char *FragShaderText
=
30 " gl_FragColor = gl_Color; \n"
34 static GLuint fragShader
;
35 static GLuint vertShader
;
36 static GLuint program
;
39 static GLboolean Anim
= GL_TRUE
;
40 static GLboolean WireFrame
= GL_TRUE
;
41 static GLfloat xRot
= -70.0f
, yRot
= 0.0f
, zRot
= 0.0f
;
46 zRot
= 90 + glutGet(GLUT_ELAPSED_TIME
) * 0.05;
54 GLfloat xmin
= -2.0, xmax
= 2.0;
55 GLfloat ymin
= -2.0, ymax
= 2.0;
56 GLuint xdivs
= 20, ydivs
= 20;
57 GLfloat dx
= (xmax
- xmin
) / xdivs
;
58 GLfloat dy
= (ymax
- ymin
) / ydivs
;
59 GLfloat ds
= 1.0 / xdivs
, dt
= 1.0 / ydivs
;
65 for (i
= 0; i
< ydivs
; i
++) {
68 glBegin(GL_QUAD_STRIP
);
69 for (j
= 0; j
< xdivs
; j
++) {
72 glTexCoord2f(s
, t
+ dt
);
73 glVertex2f(x
, y
+ dy
);
88 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
90 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
92 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
95 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
96 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
97 glRotatef(zRot
, 0.0f
, 0.0f
, 1.0f
);
110 Reshape(int width
, int height
)
112 glViewport(0, 0, width
, height
);
113 glMatrixMode(GL_PROJECTION
);
115 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
116 glMatrixMode(GL_MODELVIEW
);
118 glTranslatef(0.0f
, 0.0f
, -15.0f
);
125 glDeleteShader(fragShader
);
126 glDeleteShader(vertShader
);
127 glDeleteProgram(program
);
128 glutDestroyWindow(win
);
133 Key(unsigned char key
, int x
, int y
)
135 const GLfloat step
= 2.0;
148 WireFrame
= !WireFrame
;
166 SpecialKey(int key
, int x
, int y
)
168 const GLfloat step
= 2.0;
194 const GLuint texWidth
= 64, texHeight
= 64;
195 GLfloat texImage
[64][64];
198 /* texture is basically z = f(x, y) */
199 for (i
= 0; i
< texHeight
; i
++) {
200 GLfloat y
= 2.0 * (i
/ (float) (texHeight
- 1)) - 1.0;
201 for (j
= 0; j
< texWidth
; j
++) {
202 GLfloat x
= 2.0 * (j
/ (float) (texWidth
- 1)) - 1.0;
203 GLfloat z
= 0.5 + 0.5 * (sin(4.0 * x
) * sin(4.0 * y
));
208 glActiveTexture(GL_TEXTURE0
);
209 glBindTexture(GL_TEXTURE_2D
, 42);
210 glTexImage2D(GL_TEXTURE_2D
, 0, GL_INTENSITY
, texWidth
, texHeight
, 0,
211 GL_LUMINANCE
, GL_FLOAT
, texImage
);
212 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
213 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
222 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB
, &m
);
224 printf("Error: no vertex shader texture units supported.\n");
228 if (!ShadersSupported())
231 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, VertShaderText
);
232 fragShader
= CompileShaderText(GL_FRAGMENT_SHADER
, FragShaderText
);
233 program
= LinkShaders(vertShader
, fragShader
);
235 glUseProgram(program
);
237 assert(glGetError() == 0);
241 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
243 glEnable(GL_DEPTH_TEST
);
250 main(int argc
, char *argv
[])
252 glutInit(&argc
, argv
);
253 glutInitWindowSize(500, 500);
254 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
255 win
= glutCreateWindow(argv
[0]);
257 glutReshapeFunc(Reshape
);
258 glutKeyboardFunc(Key
);
259 glutSpecialFunc(SpecialKey
);
260 glutDisplayFunc(Redisplay
);