From 344a2a9abbe22236e8511d8166023bf81c85b03f Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Mon, 30 Nov 2009 15:31:30 -0700 Subject: 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(). --- progs/trivial/Makefile | 2 + progs/trivial/SConscript | 2 + progs/trivial/sub-tex.c | 137 ++++++++++++++++++++++++++++++++++++++++++++ progs/trivial/tex-quads.c | 143 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 284 insertions(+) create mode 100644 progs/trivial/sub-tex.c create mode 100644 progs/trivial/tex-quads.c (limited to 'progs') diff --git a/progs/trivial/Makefile b/progs/trivial/Makefile index 70728616d2..a78837611c 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 9a1f3575bd..37a53293bf 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 0000000000..0b8bb28182 --- /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 +#include +#include +#include +#include + + +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 0000000000..626e178b87 --- /dev/null +++ b/progs/trivial/tex-quads.c @@ -0,0 +1,143 @@ +/** + * Draw a series of quads, each with a different texture. + */ + +#include +#include +#include +#include +#include + +#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; +} -- cgit v1.2.3 From 52081f0a2c5d1d54d9e0f323c681b30d7186d8e4 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Thu, 3 Dec 2009 11:43:19 -0700 Subject: progs/trivial: readback and print Z value in tri-z-eq.c --- progs/trivial/tri-z-eq.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'progs') diff --git a/progs/trivial/tri-z-eq.c b/progs/trivial/tri-z-eq.c index b81c992f7d..195e8a26f6 100644 --- a/progs/trivial/tri-z-eq.c +++ b/progs/trivial/tri-z-eq.c @@ -69,6 +69,8 @@ static void Key(unsigned char key, int x, int y) static void Draw(void) { + float z = 1.0; + glClearColor(0.0, 0.0, 1.0, 0.0); glClearDepth(1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -77,15 +79,21 @@ static void Draw(void) glBegin(GL_TRIANGLES); glColor3f(0,0,.7); - glVertex3f( 0.9, -0.9, 1.0); + glVertex3f( 0.9, -0.9, z); glColor3f(.8,0,0); - glVertex3f( 0.9, 0.9, 1.0); + glVertex3f( 0.9, 0.9, z); glColor3f(0,.9,0); - glVertex3f(-0.9, 0.0, 1.0); + glVertex3f(-0.9, 0.0, z); glEnd(); glFlush(); + { + GLfloat z; + glReadPixels(125, 125, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z); + printf("Z at (125, 125) = %f\n", z); + } + if (doubleBuffer) { glutSwapBuffers(); } -- cgit v1.2.3 From 288ea9770a2c9323ffa9a4b9f3a818d8aa02acd9 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Wed, 9 Dec 2009 14:53:53 -0700 Subject: progs/demos: call glutDestroyWindow() in gloss.c --- progs/demos/gloss.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'progs') diff --git a/progs/demos/gloss.c b/progs/demos/gloss.c index 69694b23a0..56d48b5a9e 100644 --- a/progs/demos/gloss.c +++ b/progs/demos/gloss.c @@ -41,6 +41,7 @@ /* for convolution */ #define FILTER_SIZE 7 +static GLint Win; static GLint WinWidth = 500, WinHeight = 500; static GLuint CylinderObj = 0; static GLuint TeapotObj = 0; @@ -215,6 +216,7 @@ static void Key( unsigned char key, int x, int y ) ToggleAnimate(); break; case 27: + glutDestroyWindow(Win); exit(0); break; } @@ -439,7 +441,7 @@ int main( int argc, char *argv[] ) glutInit( &argc, argv ); glutInitWindowSize(WinWidth, WinHeight); glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); - glutCreateWindow(argv[0] ); + Win = glutCreateWindow(argv[0] ); glewInit(); glutReshapeFunc( Reshape ); glutKeyboardFunc( Key ); -- cgit v1.2.3 From edf11da7f8e2fbe090e60e58c12c6a5ece3089bc Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Wed, 9 Dec 2009 16:23:30 -0700 Subject: progs/demos/gloss: press 'n' to advance by one frame --- progs/demos/gloss.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'progs') diff --git a/progs/demos/gloss.c b/progs/demos/gloss.c index 56d48b5a9e..d32e8f8c68 100644 --- a/progs/demos/gloss.c +++ b/progs/demos/gloss.c @@ -215,6 +215,9 @@ static void Key( unsigned char key, int x, int y ) case ' ': ToggleAnimate(); break; + case 'n': + Idle(); + break; case 27: glutDestroyWindow(Win); exit(0); -- cgit v1.2.3 From 8b29d39e99185d94a1fddafbe8b2ab0856fcdcc0 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Thu, 14 Jan 2010 19:22:36 -0700 Subject: progs/trival: updated tri-scissor-tri.c test The scissor left/right/bottom/top bounds can be moved by pressing the l/r/b/t and L/R/B/T keys. --- progs/trivial/tri-scissor-tri.c | 58 +++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'progs') diff --git a/progs/trivial/tri-scissor-tri.c b/progs/trivial/tri-scissor-tri.c index d65502d91b..1bb15501bb 100644 --- a/progs/trivial/tri-scissor-tri.c +++ b/progs/trivial/tri-scissor-tri.c @@ -31,10 +31,14 @@ #define CI_OFFSET_1 16 #define CI_OFFSET_2 32 -GLint Width = 250, Height = 250; +GLint Width = 300, Height = 300; GLenum doubleBuffer; +/* scissor bounds */ +static GLint Left, Right, Bottom, Top; + + static void Init(void) { fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); @@ -47,26 +51,57 @@ static void Init(void) static void Reshape(int width, int height) { - glViewport(0, 0, (GLint)width, (GLint)height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); glMatrixMode(GL_MODELVIEW); + + Width = width; + Height = height; + + Left = Width / 4; + Right = Width * 3 / 4; + Bottom = Height / 4; + Top = Height * 3 / 4; } static void Key(unsigned char key, int x, int y) { + int step = 2; + switch (key) { + case 'l': + Left -= step; + break; + case 'L': + Left += step; + break; + case 'r': + Right -= step; + break; + case 'R': + Right += step; + break; + case 'b': + Bottom -= step; + break; + case 'B': + Bottom += step; + break; + case 't': + Top -= step; + break; + case 'T': + Top += step; + break; + case 27: + exit(1); + default: + ; + } - switch (key) { - case 27: - exit(1); - default: - break; - } - - glutPostRedisplay(); + glutPostRedisplay(); } static void Draw(void) @@ -82,7 +117,8 @@ static void Draw(void) glVertex3f(-0.9, 0.0, -30.0); glEnd(); - glScissor(Width / 4, Height / 4, Width / 2, Height / 2); + printf("Scissor %d, %d .. %d, %d\n", Left, Bottom, Right, Top); + glScissor(Left, Bottom, Right-Left, Top-Bottom); glEnable(GL_SCISSOR_TEST); glBegin(GL_TRIANGLES); -- cgit v1.2.3 From e5829ccc2b0cb1eed27c89763e8e4c6775dd6d4c Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Thu, 21 Jan 2010 15:41:03 -0700 Subject: progs/demos: call glutDestroyWindow() upon exit --- progs/demos/engine.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'progs') diff --git a/progs/demos/engine.c b/progs/demos/engine.c index 7e485111da..a4148357d4 100644 --- a/progs/demos/engine.c +++ b/progs/demos/engine.c @@ -26,6 +26,8 @@ /* Target engine speed: */ const int RPM = 100.0; +static int Win = 0; + /** * Engine description. @@ -1154,6 +1156,7 @@ OptRotate(void) static void OptExit(void) { + glutDestroyWindow(Win); exit(0); } @@ -1323,7 +1326,7 @@ main(int argc, char *argv[]) glutInitWindowSize(WinWidth, WinHeight); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); - glutCreateWindow("OpenGL Engine Demo"); + Win = glutCreateWindow("OpenGL Engine Demo"); glewInit(); glutReshapeFunc(Reshape); glutMouseFunc(Mouse); -- cgit v1.2.3