summaryrefslogtreecommitdiffstats
path: root/progs/tests/cylwrap.c
blob: 9d541e319f82b647da769801205465373a8d8b1c (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
/*
 * Test cylindrical texcoord wrapping
 */


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

static int Win;
static int WinWidth = 600, WinHeight = 400;
static GLboolean CylWrap = GL_TRUE;



static void
PrintString(const char *s)
{
   while (*s) {
      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
      s++;
   }
}


static void
DrawSample(GLboolean wrap)
{
   float p;

   glEnable(GL_TEXTURE_2D);

   /* texured quad */
   glBegin(GL_QUAD_STRIP);
   for (p = 0.0; p <= 1.001; p += 0.1) {
      float x = -2.0 + p * 4.0;
      float s = p + 0.5;
      if (wrap && s > 1.0)
         s -= 1.0;
      glTexCoord2f(s, 0);  glVertex2f(x, -1);
      glTexCoord2f(s, 1);  glVertex2f(x, +1);
   }
   glEnd();

   glDisable(GL_TEXTURE_2D);

   /* hash marks */
   glColor3f(0,0,0);
   glBegin(GL_LINES);
   for (p = 0.0; p <= 1.001; p += 0.1) {
      float x = -2.0 + p * 4.0;
      glVertex2f(x, -1.1);
      glVertex2f(x, -0.8);
   }
   glEnd();

   /* labels */
   glColor3f(1,1,1);
   for (p = 0.0; p <= 1.001; p += 0.1) {
      char str[100];
      float x = -2.0 + p * 4.0;
      float s = p + 0.5;

      if (wrap && s > 1.0)
         s -= 1.0;

      sprintf(str, "%3.1f", s);
      glRasterPos2f(x, -1.2);
      glBitmap(0, 0, 0, 0, -11, 0, NULL);
      PrintString(str);
      if (p == 0.0) {
         glBitmap(0, 0, 0, 0, -55, 0, NULL);
         PrintString("s =");
      }
   }
}


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

   glPushMatrix();
      glTranslatef(0, +1.2, 0);
      DrawSample(GL_FALSE);
   glPopMatrix();

   /* set Mesa back-door state for testing cylindrical wrap mode */
   if (CylWrap)
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 0.125);

   glPushMatrix();
      glTranslatef(0, -1.2, 0);
      DrawSample(GL_TRUE);
   glPopMatrix();

   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);

   glutSwapBuffers();
}


static void
Reshape(int width, int height)
{
   WinWidth = width;
   WinHeight = 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, -15.0);
}


static void
Key(unsigned char key, int x, int y)
{
   const GLfloat step = 3.0;
   (void) x;
   (void) y;
   switch (key) {
   case 'c':
   case 'C':
      CylWrap = !CylWrap;
      if (CylWrap)
         printf("Cylindrical wrap on.\n");
      else
         printf("Cylindrical wrap off.\n");
      break;
   case 27:
      glutDestroyWindow(Win);
      exit(0);
      break;
   }
   glutPostRedisplay();
}


static void
MakeSineWaveTexture(void)
{
   GLubyte tex[128][512][4];
   int i, j;

   for (j = 0; j < 128; j++) {
      for (i = 0; i < 512; i++) {
         float x = i / 511.0 * 2.0 * M_PI + M_PI * 0.5;
         float y0 = sin(x) * 0.5 + 0.5;
         int jy0 = y0 * 128;
         float y1 = sin(x + M_PI) * 0.5 + 0.5;
         int jy1 = y1 * 128;
         if (j < jy0)
            tex[j][i][0] = 0xff;
         else
            tex[j][i][0] = 0;
         if (j < jy1)
            tex[j][i][1] = 0xff;
         else
            tex[j][i][1] = 0;
         tex[j][i][2] = 0;
         tex[j][i][3] = 0xff;
      }
   }

   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 128, 0,
                GL_RGBA, GL_UNSIGNED_BYTE, tex);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}


static void
Init(void)
{
   glBindTexture(GL_TEXTURE_2D, 5);
   MakeSineWaveTexture();

   glClearColor(0.5, 0.5, 0.5, 0.0);
   glPointSize(3.0);

   printf("Press 'c' to toggle cylindrical wrap mode.\n");
}


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