summaryrefslogtreecommitdiff
path: root/progs/tests/floattex.c
blob: dd99d836c64fdb74b47dfd8a52a8a3d82db00d44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Test floating point textures.
 * No actual rendering, yet.
 */


#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include "extfuncs.h"
#include "readtex.h"
#include "shaderutil.h"


static const char *TexFile = "../images/arch.rgb";

static const char *FragShaderText = 
   "uniform sampler2D tex1; \n"
   "void main() \n"
   "{ \n"
   "   vec4 t = texture2D(tex1, gl_TexCoord[0].xy); \n"
   "   // convert from [-255,0] to [0,1] \n"
   "   gl_FragColor = t * (-1.0 / 255.0); \n"
   "} \n";

static const char *VertShaderText = 
   "void main() \n"
   "{ \n"
   "   gl_TexCoord[0] = gl_MultiTexCoord0; \n"
   "   gl_Position = ftransform(); \n"
   "} \n";

static struct uniform_info Uniforms[] = {
   { "tex1",  1, GL_INT, { 0, 0, 0, 0 }, -1 },
   END_OF_UNIFORMS
};


static GLuint Program;



static GLboolean
CheckError( int line )
{
   GLenum error = glGetError();
   if (error) {
      char *err = (char *) gluErrorString( error );
      fprintf( stderr, "GL Error: %s at line %d\n", err, line );
      return GL_TRUE;
   }
   return GL_FALSE;
}


static void
Draw(void)
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   glPushMatrix();

   glBegin(GL_POLYGON);
   glTexCoord2f( 0.0, 0.0 );   glVertex2f( -1.0, -1.0 );
   glTexCoord2f( 1.0, 0.0 );   glVertex2f(  1.0, -1.0 );
   glTexCoord2f( 1.0, 1.0 );   glVertex2f(  1.0,  1.0 );
   glTexCoord2f( 0.0, 1.0 );   glVertex2f( -1.0,  1.0 );
   glEnd();

   glPopMatrix();

   glutSwapBuffers();
}


static void
Reshape(int width, int height)
{
   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glTranslatef(0.0, 0.0, -8.0);
}


static void
Key(unsigned char key, int x, int y)
{
   (void) x;
   (void) y;
   switch (key) {
      case 27:
         exit(0);
         break;
   }
   glutPostRedisplay();
}



static void
InitTexture(void)
{
   GLenum filter = GL_LINEAR;
   GLint imgWidth, imgHeight;
   GLenum imgFormat;
   GLubyte *image = NULL;
   GLfloat *ftex;
   GLint i, t;

   image = LoadRGBImage(TexFile, &imgWidth, &imgHeight, &imgFormat);
   if (!image) {
      printf("Couldn't read %s\n", TexFile);
      exit(0);
   }

   assert(imgFormat == GL_RGB);

   ftex = (float *) malloc(imgWidth * imgHeight * 4 * sizeof(float));
   if (!ftex) {
      printf("out of memory\n");
      exit(0);
   }

   /* convert ubytes to floats, negated */
   for (i = 0; i < imgWidth * imgHeight * 3; i++) {
      ftex[i] = -1.0f * image[i];
   }

   glActiveTexture(GL_TEXTURE0);
   glBindTexture(GL_TEXTURE_2D, 42);

   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB,
                imgWidth, imgHeight, 0,
                GL_RGB, GL_FLOAT, ftex);


   /* sanity checks */
   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_RED_TYPE_ARB, &t);
   assert(t == GL_FLOAT);
   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_TYPE_ARB, &t);
   assert(t == GL_FLOAT);
   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_TYPE_ARB, &t);
   assert(t == GL_FLOAT);
   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_TYPE_ARB, &t);
   assert(t == GL_FLOAT);

   free(image);
   free(ftex);
      
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);

#if 0
   /* read back the texture and make sure values are correct */
   glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, tex2);
   CheckError(__LINE__);
   for (i = 0; i < 16; i++) {
      for (j = 0; j < 16; j++) {
         if (tex[i][j][0] != tex2[i][j][0] ||
             tex[i][j][1] != tex2[i][j][1] ||
             tex[i][j][2] != tex2[i][j][2] ||
             tex[i][j][3] != tex2[i][j][3]) {
            printf("tex[%d][%d] %g %g %g %g != tex2[%d][%d] %g %g %g %g\n",
                   i, j,
                   tex[i][j][0], tex[i][j][1], tex[i][j][2], tex[i][j][3],
                   i, j,
                   tex2[i][j][0], tex2[i][j][1], tex2[i][j][2], tex2[i][j][3]);
         }
      }
   }
#endif
}


static GLuint
CreateProgram(void)
{
   GLuint fragShader, vertShader, program;

   vertShader = CompileShaderText(GL_VERTEX_SHADER, VertShaderText);
   fragShader = CompileShaderText(GL_FRAGMENT_SHADER, FragShaderText);
   assert(vertShader);
   program = LinkShaders(vertShader, fragShader);

   assert(program);

   // InitUniforms(program, Uniforms);

   return program;
}


static void
Init(void)
{
   glClearColor(0.25, 0.25, 0.25, 0.0);

   GetExtensionFuncs();

   if (!ShadersSupported()) {
      printf("Sorry, this test requires GLSL\n");
      exit(1);
   }

   if (!glutExtensionSupported("GL_MESAX_texture_float")) {
      printf("Sorry, this test requires GL_MESAX_texture_float\n");
      exit(1);
   }

   InitTexture();

   Program = CreateProgram();
   glUseProgram_func(Program);
}


int
main(int argc, char *argv[])
{
   glutInit(&argc, argv);
   glutInitWindowPosition(0, 0);
   glutInitWindowSize(400, 400);
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
   glutCreateWindow(argv[0]);
   glewInit();
   glutReshapeFunc(Reshape);
   glutKeyboardFunc(Key);
   glutDisplayFunc(Draw);
   Init();
   glutMainLoop();
   return 0;
}