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
;
44 /* value[0] = tex unit */
45 static struct uniform_info Uniforms
[] = {
46 { "tex1", 1, GL_SAMPLER_2D
, { 0, 0, 0, 0 }, -1 },
55 zRot
= 90 + glutGet(GLUT_ELAPSED_TIME
) * 0.05;
63 GLfloat xmin
= -2.0, xmax
= 2.0;
64 GLfloat ymin
= -2.0, ymax
= 2.0;
65 GLuint xdivs
= 20, ydivs
= 20;
66 GLfloat dx
= (xmax
- xmin
) / xdivs
;
67 GLfloat dy
= (ymax
- ymin
) / ydivs
;
68 GLfloat ds
= 1.0 / xdivs
, dt
= 1.0 / ydivs
;
74 for (i
= 0; i
< ydivs
; i
++) {
77 glBegin(GL_QUAD_STRIP
);
78 for (j
= 0; j
< xdivs
; j
++) {
81 glTexCoord2f(s
, t
+ dt
);
82 glVertex2f(x
, y
+ dy
);
97 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
99 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
101 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
104 glRotatef(xRot
, 1.0f
, 0.0f
, 0.0f
);
105 glRotatef(yRot
, 0.0f
, 1.0f
, 0.0f
);
106 glRotatef(zRot
, 0.0f
, 0.0f
, 1.0f
);
119 Reshape(int width
, int height
)
121 glViewport(0, 0, width
, height
);
122 glMatrixMode(GL_PROJECTION
);
124 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
125 glMatrixMode(GL_MODELVIEW
);
127 glTranslatef(0.0f
, 0.0f
, -15.0f
);
134 glDeleteShader(fragShader
);
135 glDeleteShader(vertShader
);
136 glDeleteProgram(program
);
137 glutDestroyWindow(win
);
142 Key(unsigned char key
, int x
, int y
)
144 const GLfloat step
= 2.0;
157 WireFrame
= !WireFrame
;
175 SpecialKey(int key
, int x
, int y
)
177 const GLfloat step
= 2.0;
203 const GLuint texWidth
= 64, texHeight
= 64;
204 GLfloat texImage
[64][64];
207 /* texture is basically z = f(x, y) */
208 for (i
= 0; i
< texHeight
; i
++) {
209 GLfloat y
= 2.0 * (i
/ (float) (texHeight
- 1)) - 1.0;
210 for (j
= 0; j
< texWidth
; j
++) {
211 GLfloat x
= 2.0 * (j
/ (float) (texWidth
- 1)) - 1.0;
212 GLfloat z
= 0.5 + 0.5 * (sin(4.0 * x
) * sin(4.0 * y
));
217 glActiveTexture(GL_TEXTURE0
);
218 glBindTexture(GL_TEXTURE_2D
, 42);
219 glTexImage2D(GL_TEXTURE_2D
, 0, GL_INTENSITY
, texWidth
, texHeight
, 0,
220 GL_LUMINANCE
, GL_FLOAT
, texImage
);
221 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
222 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
231 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB
, &m
);
233 printf("Error: no vertex shader texture units supported.\n");
237 if (!ShadersSupported())
240 vertShader
= CompileShaderText(GL_VERTEX_SHADER
, VertShaderText
);
241 fragShader
= CompileShaderText(GL_FRAGMENT_SHADER
, FragShaderText
);
242 program
= LinkShaders(vertShader
, fragShader
);
244 glUseProgram(program
);
246 assert(glGetError() == 0);
250 glClearColor(0.4f
, 0.4f
, 0.8f
, 0.0f
);
252 glEnable(GL_DEPTH_TEST
);
259 main(int argc
, char *argv
[])
261 glutInit(&argc
, argv
);
262 glutInitWindowSize(500, 500);
263 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
264 win
= glutCreateWindow(argv
[0]);
266 glutReshapeFunc(Reshape
);
267 glutKeyboardFunc(Key
);
268 glutSpecialFunc(SpecialKey
);
269 glutDisplayFunc(Redisplay
);