TinyGL 0.4.1 for MinGW
texobj.c
Go to the documentation of this file.
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
14
15#include <windows.h>
16
17//#include <GL/glx.h>
18#include <GL/gdi.h>
19#include <GL/gl.h>
20#include "ui.h"
21
22static GLuint TexObj[2];
23static GLfloat Angle = 0.0f;
24
25static int cnt=0,v=0;
26
27void
28draw(void)
29{
31
32 glColor3f(1.0, 1.0, 1.0);
33
34 /* draw first polygon */
36 glTranslatef(-1.0, 0.0, 0.0);
37 glRotatef(Angle, 0.0, 0.0, 1.0);
38 glBindTexture(GL_TEXTURE_2D, TexObj[v]);
39
42 glTexCoord2f(0.0, 0.0);
43 glVertex2f(-1.0, -1.0);
44 glTexCoord2f(1.0, 0.0);
45 glVertex2f(1.0, -1.0);
46 glTexCoord2f(1.0, 1.0);
47 glVertex2f(1.0, 1.0);
48 glTexCoord2f(0.0, 1.0);
49 glVertex2f(-1.0, 1.0);
50 glEnd();
53
54 /* draw second polygon */
56 glTranslatef(1.0, 0.0, 0.0);
57 glRotatef(Angle - 90.0, 0.0, 1.0, 0.0);
58
59 glBindTexture(GL_TEXTURE_2D, TexObj[1-v]);
60
63 glTexCoord2f(0.0, 0.0);
64 glVertex2f(-1.0, -1.0);
65 glTexCoord2f(1.0, 0.0);
66 glVertex2f(1.0, -1.0);
67 glTexCoord2f(1.0, 1.0);
68 glVertex2f(1.0, 1.0);
69 glTexCoord2f(0.0, 1.0);
70 glVertex2f(-1.0, 1.0);
71 glEnd();
73
75
77}
78
79
80/* new window size or exposure */
81void
82reshape(int width, int height)
83{
84 glViewport(0, 0, (GLint) width, (GLint) height);
87 /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 ); */
88 glFrustum(-2.0, 2.0, -2.0, 2.0, 6.0, 20.0);
91 glTranslatef(0.0, 0.0, -8.0);
92}
93
94
95void bind_texture(int texobj,int image)
96{
97 static int width = 8, height = 8;
98 static int color[2][3]={
99 {255,0,0},
100 {0,255,0},
101 };
102 GLubyte tex[64][3];
103 static GLubyte texchar[2][8*8] = {
104 {
105 0, 0, 0, 0, 0, 0, 0, 0,
106 0, 0, 0, 0, 1, 0, 0, 0,
107 0, 0, 0, 1, 1, 0, 0, 0,
108 0, 0, 0, 0, 1, 0, 0, 0,
109 0, 0, 0, 0, 1, 0, 0, 0,
110 0, 0, 0, 0, 1, 0, 0, 0,
111 0, 0, 0, 1, 1, 1, 0, 0,
112 0, 0, 0, 0, 0, 0, 0, 0},
113 {
114 0, 0, 0, 0, 0, 0, 0, 0,
115 0, 0, 0, 2, 2, 0, 0, 0,
116 0, 0, 2, 0, 0, 2, 0, 0,
117 0, 0, 0, 0, 0, 2, 0, 0,
118 0, 0, 0, 0, 2, 0, 0, 0,
119 0, 0, 0, 2, 0, 0, 0, 0,
120 0, 0, 2, 2, 2, 2, 0, 0,
121 0, 0, 0, 0, 0, 0, 0, 0}};
122
123 int i,j;
124
126
127 /* red on white */
128 for (i = 0; i < height; i++) {
129 for (j = 0; j < width; j++) {
130 int p = i * width + j;
131 if (texchar[image][(height - i - 1) * width + j]) {
132 tex[p][0] = color[image][0];
133 tex[p][1] = color[image][1];
134 tex[p][2] = color[image][2];
135 } else {
136 tex[p][0] = 255;
137 tex[p][1] = 255;
138 tex[p][2] = 255;
139 }
140 }
141 }
142 glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0,
148 /* end of texture object */
149}
150
151
152
153void
154init(void)
155{
157
158 /* Setup texturing */
161
162 /* generate texture object IDs */
163 glGenTextures(2, TexObj);
164 bind_texture(TexObj[0],0);
165 bind_texture(TexObj[1],1);
166
167}
168
169void
170idle(void)
171{
172
173 Angle += 2.0;
174
175 if (++cnt==5) {
176 cnt=0;
177 v=!v;
178 }
179 //draw();
180}
181
182/* change view angle, exit upon ESC */
183GLenum key(int k, GLenum mask)
184{
185 switch (k) {
186 case 'q':
187 case KEY_ESCAPE:
188 exit(0);
189 }
190 return GL_FALSE;
191}
192
193int main(int argc, char **argv)
194{
195 return ui_loop(argc, argv, "texobj");
196}
197
198
void glColor3f(float x, float y, float z)
Definition: api.c:92
void glVertex2f(float x, float y)
Definition: api.c:18
void glTexCoord2f(float s, float t)
Definition: api.c:118
void glFrustum(double left, double right, double bottom, double top, double near, double far)
Definition: api.c:354
unsigned char GLubyte
Definition: gl.h:668
@ GL_UNSIGNED_BYTE
Definition: gl.h:20
@ GL_FALSE
Definition: gl.h:15
@ GL_FASTEST
Definition: gl.h:427
@ GL_NEAREST
Definition: gl.h:513
@ GL_PERSPECTIVE_CORRECTION_HINT
Definition: gl.h:423
@ GL_TEXTURE_MAG_FILTER
Definition: gl.h:491
@ GL_TEXTURE_2D
Definition: gl.h:488
@ GL_RGB
Definition: gl.h:345
@ GL_TEXTURE_ENV
Definition: gl.h:485
@ GL_DEPTH_TEST
Definition: gl.h:149
@ GL_PROJECTION
Definition: gl.h:95
@ GL_TEXTURE_ENV_MODE
Definition: gl.h:486
@ GL_QUADS
Definition: gl.h:39
@ GL_MODELVIEW
Definition: gl.h:94
@ GL_TEXTURE_WRAP_S
Definition: gl.h:489
@ GL_DECAL
Definition: gl.h:511
@ GL_REPEAT
Definition: gl.h:514
@ GL_TEXTURE_WRAP_T
Definition: gl.h:490
@ GL_TEXTURE_MIN_FILTER
Definition: gl.h:492
int GLenum
Definition: gl.h:662
void glPushMatrix(void)
Definition: api.c:285
void glPopMatrix(void)
Definition: api.c:294
void glTexImage2D(int target, int level, int components, int width, int height, int border, int format, int type, void *pixels)
Definition: api.c:507
float GLfloat
Definition: gl.h:671
void glViewport(int x, int y, int width, int height)
Definition: api.c:341
int GLint
Definition: gl.h:667
void glClear(int mask)
Definition: api.c:471
void glMatrixMode(int mode)
Definition: api.c:244
void glHint(int target, int mode)
Definition: api.c:650
void glRotatef(float angle, float x, float y, float z)
Definition: api.c:303
void glBindTexture(int target, int texture)
Definition: api.c:528
unsigned int GLuint
Definition: gl.h:670
void glTexEnvi(int target, int pname, int param)
Definition: api.c:539
void glBegin(int type)
Definition: api.c:223
void glTranslatef(float x, float y, float z)
Definition: api.c:316
void glLoadIdentity(void)
Definition: api.c:265
void glEnable(int code)
Definition: api.c:199
void glDisable(int code)
Definition: api.c:210
@ GL_COLOR_BUFFER_BIT
Definition: gl.h:651
@ GL_DEPTH_BUFFER_BIT
Definition: gl.h:645
void glTexParameteri(int target, int pname, int param)
Definition: api.c:555
void glEnd(void)
Definition: api.c:233
void glGenTextures(int n, unsigned int *textures)
Definition: texture.c:70
int ui_loop(int argc, char **argv, const char *name)
Definition: mingw32.c:310
void tkSwapBuffers(void)
Definition: mingw32.c:305
void idle(void)
Definition: texobj.c:170
GLenum key(int k, GLenum mask)
Definition: texobj.c:183
void init(void)
Definition: texobj.c:154
void bind_texture(int texobj, int image)
Definition: texobj.c:95
int main(int argc, char **argv)
Definition: texobj.c:193
void reshape(int width, int height)
Definition: texobj.c:82
void draw(void)
Definition: texobj.c:28
#define KEY_ESCAPE
Definition: ui.h:16