source: trunk/sys/TinyGL/examples/texobj.c @ 249

Last change on this file since 249 was 1, checked in by alain, 7 years ago

First import

File size: 3.9 KB
Line 
1/*
2 * Example of using the 1.1 texture object functions.
3 * Also, this demo utilizes Mesa's fast texture map path.
4 *
5 * Brian Paul   June 1996
6 */
7
8#include <math.h>
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12
13#include <GL/glx.h> 
14#include <GL/gl.h> 
15#include "ui.h"
16
17static GLuint TexObj[2];
18static GLfloat Angle = 0.0f;
19
20static int cnt=0,v=0;
21
22void 
23draw(void)
24{
25  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
26
27  glColor3f(1.0, 1.0, 1.0);
28
29  /* draw first polygon */
30  glPushMatrix();
31  glTranslatef(-1.0, 0.0, 0.0);
32  glRotatef(Angle, 0.0, 0.0, 1.0);
33  glBindTexture(GL_TEXTURE_2D, TexObj[v]);
34
35  glEnable(GL_TEXTURE_2D);
36  glBegin(GL_QUADS);
37  glTexCoord2f(0.0, 0.0);
38  glVertex2f(-1.0, -1.0);
39  glTexCoord2f(1.0, 0.0);
40  glVertex2f(1.0, -1.0);
41  glTexCoord2f(1.0, 1.0);
42  glVertex2f(1.0, 1.0);
43  glTexCoord2f(0.0, 1.0);
44  glVertex2f(-1.0, 1.0);
45  glEnd();
46  glDisable(GL_TEXTURE_2D);
47  glPopMatrix();
48
49  /* draw second polygon */
50  glPushMatrix();
51  glTranslatef(1.0, 0.0, 0.0);
52  glRotatef(Angle - 90.0, 0.0, 1.0, 0.0);
53
54  glBindTexture(GL_TEXTURE_2D, TexObj[1-v]);
55
56  glEnable(GL_TEXTURE_2D);
57  glBegin(GL_QUADS);
58  glTexCoord2f(0.0, 0.0);
59  glVertex2f(-1.0, -1.0);
60  glTexCoord2f(1.0, 0.0);
61  glVertex2f(1.0, -1.0);
62  glTexCoord2f(1.0, 1.0);
63  glVertex2f(1.0, 1.0);
64  glTexCoord2f(0.0, 1.0);
65  glVertex2f(-1.0, 1.0);
66  glEnd();
67  glDisable(GL_TEXTURE_2D);
68
69  glPopMatrix();
70
71  tkSwapBuffers();
72}
73
74
75/* new window size or exposure */
76void 
77reshape(int width, int height)
78{
79  glViewport(0, 0, (GLint) width, (GLint) height);
80  glMatrixMode(GL_PROJECTION);
81  glLoadIdentity();
82  /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 ); */
83  glFrustum(-2.0, 2.0, -2.0, 2.0, 6.0, 20.0);
84  glMatrixMode(GL_MODELVIEW);
85  glLoadIdentity();
86  glTranslatef(0.0, 0.0, -8.0);
87}
88
89
90void bind_texture(int texobj,int image)
91{
92  static int width = 8, height = 8;
93  static int color[2][3]={
94    {255,0,0},
95    {0,255,0},
96  };
97  GLubyte tex[64][3];
98  static GLubyte texchar[2][8*8] = {
99  {
100    0, 0, 0, 0, 0, 0, 0, 0,
101    0, 0, 0, 0, 1, 0, 0, 0,
102    0, 0, 0, 1, 1, 0, 0, 0,
103    0, 0, 0, 0, 1, 0, 0, 0,
104    0, 0, 0, 0, 1, 0, 0, 0,
105    0, 0, 0, 0, 1, 0, 0, 0,
106    0, 0, 0, 1, 1, 1, 0, 0,
107    0, 0, 0, 0, 0, 0, 0, 0},
108  {
109    0, 0, 0, 0, 0, 0, 0, 0,
110    0, 0, 0, 2, 2, 0, 0, 0,
111    0, 0, 2, 0, 0, 2, 0, 0,
112    0, 0, 0, 0, 0, 2, 0, 0,
113    0, 0, 0, 0, 2, 0, 0, 0,
114    0, 0, 0, 2, 0, 0, 0, 0,
115    0, 0, 2, 2, 2, 2, 0, 0,
116    0, 0, 0, 0, 0, 0, 0, 0}};
117
118  int i,j;
119 
120  glBindTexture(GL_TEXTURE_2D, texobj);
121
122  /* red on white */
123  for (i = 0; i < height; i++) {
124    for (j = 0; j < width; j++) {
125      int p = i * width + j;
126      if (texchar[image][(height - i - 1) * width + j]) {
127        tex[p][0] = color[image][0];
128        tex[p][1] = color[image][1];
129        tex[p][2] = color[image][2];
130      } else {
131        tex[p][0] = 255;
132        tex[p][1] = 255;
133        tex[p][2] = 255;
134      }
135    }
136  }
137  glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0,
138    GL_RGB, GL_UNSIGNED_BYTE, tex);
139  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
140  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
141  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
142  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
143  /* end of texture object */
144}
145
146
147
148void 
149init(void)
150{
151  glEnable(GL_DEPTH_TEST);
152
153  /* Setup texturing */
154  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
155  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
156
157  /* generate texture object IDs */
158  glGenTextures(2, TexObj);
159  bind_texture(TexObj[0],0);
160  bind_texture(TexObj[1],1);
161 
162}
163
164void 
165idle(void)
166{
167 
168  Angle += 2.0;
169
170  if (++cnt==5) {
171    cnt=0;
172    v=!v;
173  }
174  draw();
175}
176
177/* change view angle, exit upon ESC */
178GLenum key(int k, GLenum mask)
179{
180   switch (k) {
181      case 'q':
182      case KEY_ESCAPE:
183          exit(0);
184   }
185   return GL_FALSE;
186}
187
188int main(int argc, char **argv) 
189{
190    return ui_loop(argc, argv, "texobj");
191}
192
193
Note: See TracBrowser for help on using the repository browser.