summaryrefslogtreecommitdiffstats
path: root/progs
diff options
context:
space:
mode:
authorBrian Paul <[email protected]>2009-11-30 15:31:30 -0700
committerBrian Paul <[email protected]>2009-11-30 15:31:34 -0700
commit344a2a9abbe22236e8511d8166023bf81c85b03f (patch)
tree974f83b649c9a036d99ac967c32bcc9c04eafe8f /progs
parent7505510c7b7c33f3c571647c0398da7e1b823806 (diff)
progs/trivial: added two simple texture tests
One draws a series of quads with different textures. The other draws with one texture which is changed with glTexSubImage2D().
Diffstat (limited to 'progs')
-rw-r--r--progs/trivial/Makefile2
-rw-r--r--progs/trivial/SConscript2
-rw-r--r--progs/trivial/sub-tex.c137
-rw-r--r--progs/trivial/tex-quads.c143
4 files changed, 284 insertions, 0 deletions
diff --git a/progs/trivial/Makefile b/progs/trivial/Makefile
index 70728616d28..a78837611c1 100644
--- a/progs/trivial/Makefile
+++ b/progs/trivial/Makefile
@@ -90,6 +90,8 @@ SOURCES = \
quadstrip-flat.c \
quadstrip.c \
readpixels.c \
+ sub-tex.c \
+ tex-quads.c \
tri-alpha.c \
tri-alpha-tex.c \
tri-array-interleaved.c \
diff --git a/progs/trivial/SConscript b/progs/trivial/SConscript
index 9a1f3575bdc..37a53293bfc 100644
--- a/progs/trivial/SConscript
+++ b/progs/trivial/SConscript
@@ -77,6 +77,8 @@ progs = [
'quadstrip-cont',
'quadstrip-flat',
'quadstrip',
+ 'sub-tex',
+ 'tex-quads',
'tri-alpha',
'tri-blend-color',
'tri-blend-max',
diff --git a/progs/trivial/sub-tex.c b/progs/trivial/sub-tex.c
new file mode 100644
index 00000000000..0b8bb28182e
--- /dev/null
+++ b/progs/trivial/sub-tex.c
@@ -0,0 +1,137 @@
+/**
+ * Draw a series of textured quads after each quad, use glTexSubImage()
+ * to change one row of the texture image.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <GL/glew.h>
+#include <GL/glut.h>
+
+
+static GLint Win = 0;
+static GLuint Tex = 0;
+
+
+static void Init(void)
+{
+ fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
+ fflush(stderr);
+
+ glGenTextures(1, &Tex);
+ glBindTexture(GL_TEXTURE_2D, Tex);
+
+ glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+}
+
+
+static void Reshape(int width, int height)
+{
+ float ar = (float) width / height;
+ glViewport(0, 0, width, height);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(-ar, ar, -1.0, 1.0, -1.0, 1.0);
+ glMatrixMode(GL_MODELVIEW);
+}
+
+
+static void Key(unsigned char key, int x, int y)
+{
+ if (key == 27) {
+ glDeleteTextures(1, &Tex);
+ glutDestroyWindow(Win);
+ exit(1);
+ }
+ glutPostRedisplay();
+}
+
+
+static void Draw(void)
+{
+ GLubyte tex[16][16][4];
+ GLubyte row[16][4];
+ int i, j;
+
+ for (i = 0; i < 16; i++) {
+ for (j = 0; j < 16; j++) {
+ if ((i + j) & 1) {
+ tex[i][j][0] = 128;
+ tex[i][j][1] = 128;
+ tex[i][j][2] = 128;
+ tex[i][j][3] = 255;
+ }
+ else {
+ tex[i][j][0] = 255;
+ tex[i][j][1] = 255;
+ tex[i][j][2] = 255;
+ tex[i][j][3] = 255;
+ }
+ }
+ }
+
+ for (i = 0; i < 16; i++) {
+ row[i][0] = 255;
+ row[i][1] = 0;
+ row[i][2] = 0;
+ row[i][3] = 255;
+ }
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, tex);
+ glEnable(GL_TEXTURE_2D);
+
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ for (i = 0; i < 9; i++) {
+
+ glPushMatrix();
+ glTranslatef(-4.0 + i, 0, 0);
+ glScalef(0.5, 0.5, 1.0);
+
+ glBegin(GL_QUADS);
+ glTexCoord2f(1,0);
+ glVertex3f( 0.9, -0.9, 0.0);
+ glTexCoord2f(1,1);
+ glVertex3f( 0.9, 0.9, 0.0);
+ glTexCoord2f(0,1);
+ glVertex3f(-0.9, 0.9, 0.0);
+ glTexCoord2f(0,0);
+ glVertex3f(-0.9, -0.9, 0.0);
+ glEnd();
+
+ glPopMatrix();
+
+ /* replace a row of the texture image with red texels */
+ if (i * 2 < 16)
+ glTexSubImage2D(GL_TEXTURE_2D, 0, 0, i*2, 16, 1,
+ GL_RGBA, GL_UNSIGNED_BYTE, row);
+ }
+
+
+ glutSwapBuffers();
+}
+
+
+int main(int argc, char **argv)
+{
+ glutInit(&argc, argv);
+ glutInitWindowSize(900, 200);
+ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
+ Win = glutCreateWindow(*argv);
+ if (!Win) {
+ exit(1);
+ }
+ glewInit();
+ Init();
+ glutReshapeFunc(Reshape);
+ glutKeyboardFunc(Key);
+ glutDisplayFunc(Draw);
+ glutMainLoop();
+ return 0;
+}
diff --git a/progs/trivial/tex-quads.c b/progs/trivial/tex-quads.c
new file mode 100644
index 00000000000..626e178b873
--- /dev/null
+++ b/progs/trivial/tex-quads.c
@@ -0,0 +1,143 @@
+/**
+ * Draw a series of quads, each with a different texture.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <GL/glew.h>
+#include <GL/glut.h>
+
+#define NUM_TEX 10
+
+static GLint Win = 0;
+static GLuint Tex[NUM_TEX];
+
+
+static void Init(void)
+{
+ int i;
+
+ fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
+ fflush(stderr);
+
+ glGenTextures(NUM_TEX, Tex);
+
+ for (i = 0; i < NUM_TEX; i++) {
+ glBindTexture(GL_TEXTURE_2D, Tex[i]);
+
+ glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ }
+}
+
+
+static void Reshape(int width, int height)
+{
+ float ar = (float) width / height;
+ glViewport(0, 0, width, height);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(-ar, ar, -1.0, 1.0, -1.0, 1.0);
+ glMatrixMode(GL_MODELVIEW);
+}
+
+
+static void Key(unsigned char key, int x, int y)
+{
+ if (key == 27) {
+ glDeleteTextures(NUM_TEX, Tex);
+ glutDestroyWindow(Win);
+ exit(1);
+ }
+ glutPostRedisplay();
+}
+
+
+static void Draw(void)
+{
+ GLubyte tex[16][16][4];
+ int t, i, j;
+
+ for (t = 0; t < NUM_TEX; t++) {
+
+ for (i = 0; i < 16; i++) {
+ for (j = 0; j < 16; j++) {
+ if (i < t) {
+ /* red row */
+ tex[i][j][0] = 255;
+ tex[i][j][1] = 0;
+ tex[i][j][2] = 0;
+ tex[i][j][3] = 255;
+ }
+ else if ((i + j) & 1) {
+ tex[i][j][0] = 128;
+ tex[i][j][1] = 128;
+ tex[i][j][2] = 128;
+ tex[i][j][3] = 255;
+ }
+ else {
+ tex[i][j][0] = 255;
+ tex[i][j][1] = 255;
+ tex[i][j][2] = 255;
+ tex[i][j][3] = 255;
+ }
+ }
+ }
+
+ glBindTexture(GL_TEXTURE_2D, Tex[t]);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, tex);
+ }
+
+ glEnable(GL_TEXTURE_2D);
+
+ glClear(GL_COLOR_BUFFER_BIT);
+
+ for (i = 0; i < NUM_TEX; i++) {
+
+ glBindTexture(GL_TEXTURE_2D, Tex[i]);
+
+ glPushMatrix();
+ glTranslatef(-4.0 + i, 0, 0);
+ glScalef(0.5, 0.5, 1.0);
+
+ glBegin(GL_QUADS);
+ glTexCoord2f(1,0);
+ glVertex3f( 0.9, -0.9, 0.0);
+ glTexCoord2f(1,1);
+ glVertex3f( 0.9, 0.9, 0.0);
+ glTexCoord2f(0,1);
+ glVertex3f(-0.9, 0.9, 0.0);
+ glTexCoord2f(0,0);
+ glVertex3f(-0.9, -0.9, 0.0);
+ glEnd();
+
+ glPopMatrix();
+ }
+
+
+ glutSwapBuffers();
+}
+
+
+int main(int argc, char **argv)
+{
+ glutInit(&argc, argv);
+ glutInitWindowSize(900, 200);
+ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
+ Win = glutCreateWindow(*argv);
+ if (!Win) {
+ exit(1);
+ }
+ glewInit();
+ Init();
+ glutReshapeFunc(Reshape);
+ glutKeyboardFunc(Key);
+ glutDisplayFunc(Draw);
+ glutMainLoop();
+ return 0;
+}