summaryrefslogtreecommitdiff
path: root/progs/osdemos
diff options
context:
space:
mode:
Diffstat (limited to 'progs/osdemos')
-rw-r--r--progs/osdemos/Makefile82
-rw-r--r--progs/osdemos/osdemo.c323
-rw-r--r--progs/osdemos/osdemo16.c291
-rw-r--r--progs/osdemos/osdemo32.c308
-rw-r--r--progs/osdemos/ostest1.c470
5 files changed, 1474 insertions, 0 deletions
diff --git a/progs/osdemos/Makefile b/progs/osdemos/Makefile
new file mode 100644
index 0000000000..f8cba9ee99
--- /dev/null
+++ b/progs/osdemos/Makefile
@@ -0,0 +1,82 @@
+# progs/demos/Makefile
+
+TOP = ../..
+include $(TOP)/configs/current
+
+INCDIR = $(TOP)/include
+
+OSMESA_LIBS = -L$(TOP)/$(LIB_DIR) -lOSMesa $(APP_LIB_DEPS)
+
+OSMESA16_LIBS = -L$(TOP)/$(LIB_DIR) -lglut -lOSMesa16 -lGLU -lGL $(APP_LIB_DEPS)
+
+OSMESA32_LIBS = -L$(TOP)/$(LIB_DIR) -lglut -lOSMesa32 -lGLU -lGL $(APP_LIB_DEPS)
+
+LIB_DEP = $(TOP)/$(LIB_DIR)/$(GL_LIB_NAME) $(TOP)/$(LIB_DIR)/$(GLU_LIB_NAME) $(TOP)/$(LIB_DIR)/$(GLUT_LIB_NAME)
+
+PROGS = \
+ osdemo \
+ ostest1
+
+
+##### RULES #####
+
+.SUFFIXES:
+.SUFFIXES: .c
+
+
+# make executable from .c file:
+.c: $(LIB_DEP) readtex.o
+ $(CC) -I$(INCDIR) $(CFLAGS) $< readtex.o $(APP_LIB_DEPS) -o $@
+
+
+##### TARGETS #####
+
+default: readtex.o $(PROGS)
+
+
+readtex.c: $(TOP)/progs/util/readtex.c
+ cp $< .
+
+readtex.h: $(TOP)/progs/util/readtex.h
+ cp $< .
+
+readtex.o: readtex.c readtex.h
+ $(CC) -c -I$(INCDIR) $(CFLAGS) readtex.c
+
+
+showbuffer.c: $(TOP)/progs/util/showbuffer.c
+ cp $< .
+
+showbuffer.h: $(TOP)/progs/util/showbuffer.h
+ cp $< .
+
+showbuffer.o: showbuffer.c showbuffer.h
+ $(CC) -c -I$(INCDIR) $(CFLAGS) showbuffer.c
+
+
+# special case: need the -lOSMesa library:
+osdemo: osdemo.c
+ $(CC) -I$(INCDIR) $(CFLAGS) osdemo.c $(OSMESA_LIBS) -o $@
+
+# special case: need the -lOSMesa library:
+ostest1: ostest1.c
+ $(CC) -I$(INCDIR) $(CFLAGS) ostest1.c $(OSMESA_LIBS) -o $@
+
+# another special case: need the -lOSMesa16 library:
+osdemo16: osdemo16.c
+ $(CC) -I$(INCDIR) $(CFLAGS) osdemo16.c $(OSMESA16_LIBS) -o $@
+
+# another special case: need the -lOSMesa32 library:
+osdemo32: osdemo32.c
+ $(CC) -I$(INCDIR) $(CFLAGS) osdemo32.c $(OSMESA32_LIBS) -o $@
+
+
+
+clean:
+ -rm -f $(PROGS)
+ -rm -f *.o *~
+ -rm -f readtex.[ch] showbuffer.[ch]
+ -rm -f *.ppm
+ -rm -f osdemo16 osdemo32
+
+
diff --git a/progs/osdemos/osdemo.c b/progs/osdemos/osdemo.c
new file mode 100644
index 0000000000..bc0168fb97
--- /dev/null
+++ b/progs/osdemos/osdemo.c
@@ -0,0 +1,323 @@
+/*
+ * Demo of off-screen Mesa rendering
+ *
+ * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
+ *
+ * If you want to render BIG images you'll probably have to increase
+ * MAX_WIDTH and MAX_Height in src/config.h.
+ *
+ * This program is in the public domain.
+ *
+ * Brian Paul
+ *
+ * PPM output provided by Joerg Schmalzl.
+ * ASCII PPM output added by Brian Paul.
+ *
+ * Usage: osdemo [filename]
+ */
+
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "GL/osmesa.h"
+#include "GL/glu.h"
+
+
+#define SAVE_TARGA
+
+static int Width = 400;
+static int Height = 400;
+
+
+static void
+Sphere(float radius, int slices, int stacks)
+{
+ GLUquadric *q = gluNewQuadric();
+ gluQuadricNormals(q, GLU_SMOOTH);
+ gluSphere(q, radius, slices, stacks);
+ gluDeleteQuadric(q);
+}
+
+
+static void
+Cone(float base, float height, int slices, int stacks)
+{
+ GLUquadric *q = gluNewQuadric();
+ gluQuadricDrawStyle(q, GLU_FILL);
+ gluQuadricNormals(q, GLU_SMOOTH);
+ gluCylinder(q, base, 0.0, height, slices, stacks);
+ gluDeleteQuadric(q);
+}
+
+
+static void
+Torus(float innerRadius, float outerRadius, int sides, int rings)
+{
+ /* from GLUT... */
+ int i, j;
+ GLfloat theta, phi, theta1;
+ GLfloat cosTheta, sinTheta;
+ GLfloat cosTheta1, sinTheta1;
+ const GLfloat ringDelta = 2.0 * M_PI / rings;
+ const GLfloat sideDelta = 2.0 * M_PI / sides;
+
+ theta = 0.0;
+ cosTheta = 1.0;
+ sinTheta = 0.0;
+ for (i = rings - 1; i >= 0; i--) {
+ theta1 = theta + ringDelta;
+ cosTheta1 = cos(theta1);
+ sinTheta1 = sin(theta1);
+ glBegin(GL_QUAD_STRIP);
+ phi = 0.0;
+ for (j = sides; j >= 0; j--) {
+ GLfloat cosPhi, sinPhi, dist;
+
+ phi += sideDelta;
+ cosPhi = cos(phi);
+ sinPhi = sin(phi);
+ dist = outerRadius + innerRadius * cosPhi;
+
+ glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
+ glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, innerRadius * sinPhi);
+ glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
+ glVertex3f(cosTheta * dist, -sinTheta * dist, innerRadius * sinPhi);
+ }
+ glEnd();
+ theta = theta1;
+ cosTheta = cosTheta1;
+ sinTheta = sinTheta1;
+ }
+}
+
+
+static void
+render_image(void)
+{
+ GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
+ GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
+ GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
+ GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
+ GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 };
+ GLfloat green_mat[] = { 0.2, 1.0, 0.2, 1.0 };
+ GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
+
+
+ glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
+ glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
+ glLightfv(GL_LIGHT0, GL_POSITION, light_position);
+
+ glEnable(GL_LIGHTING);
+ glEnable(GL_LIGHT0);
+ glEnable(GL_DEPTH_TEST);
+
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
+ glMatrixMode(GL_MODELVIEW);
+
+ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
+
+ glPushMatrix();
+ glRotatef(20.0, 1.0, 0.0, 0.0);
+
+ glPushMatrix();
+ glTranslatef(-0.75, 0.5, 0.0);
+ glRotatef(90.0, 1.0, 0.0, 0.0);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
+ Torus(0.275, 0.85, 20, 20);
+ glPopMatrix();
+
+ glPushMatrix();
+ glTranslatef(-0.75, -0.5, 0.0);
+ glRotatef(270.0, 1.0, 0.0, 0.0);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
+ Cone(1.0, 2.0, 16, 1);
+ glPopMatrix();
+
+ glPushMatrix();
+ glTranslatef(0.75, 0.0, -1.0);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
+ Sphere(1.0, 20, 20);
+ glPopMatrix();
+
+ glPopMatrix();
+
+ /* This is very important!!!
+ * Make sure buffered commands are finished!!!
+ */
+ glFinish();
+}
+
+
+#ifdef SAVE_TARGA
+
+static void
+write_targa(const char *filename, const GLubyte *buffer, int width, int height)
+{
+ FILE *f = fopen( filename, "w" );
+ if (f) {
+ int i, x, y;
+ const GLubyte *ptr = buffer;
+ printf ("osdemo, writing tga file \n");
+ fputc (0x00, f); /* ID Length, 0 => No ID */
+ fputc (0x00, f); /* Color Map Type, 0 => No color map included */
+ fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
+ fputc (0x00, f); /* Next five bytes are about the color map entries */
+ fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
+ fputc (0x00, f);
+ fputc (0x00, f);
+ fputc (0x00, f);
+ fputc (0x00, f); /* X-origin of Image */
+ fputc (0x00, f);
+ fputc (0x00, f); /* Y-origin of Image */
+ fputc (0x00, f);
+ fputc (Width & 0xff, f); /* Image Width */
+ fputc ((Width>>8) & 0xff, f);
+ fputc (Height & 0xff, f); /* Image Height */
+ fputc ((Height>>8) & 0xff, f);
+ fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
+ fputc (0x20, f); /* Image Descriptor */
+ fclose(f);
+ f = fopen( filename, "ab" ); /* reopen in binary append mode */
+ for (y=height-1; y>=0; y--) {
+ for (x=0; x<width; x++) {
+ i = (y*width + x) * 4;
+ fputc(ptr[i+2], f); /* write blue */
+ fputc(ptr[i+1], f); /* write green */
+ fputc(ptr[i], f); /* write red */
+ }
+ }
+ }
+}
+
+#else
+
+static void
+write_ppm(const char *filename, const GLubyte *buffer, int width, int height)
+{
+ const int binary = 0;
+ FILE *f = fopen( filename, "w" );
+ if (f) {
+ int i, x, y;
+ const GLubyte *ptr = buffer;
+ if (binary) {
+ fprintf(f,"P6\n");
+ fprintf(f,"# ppm-file created by osdemo.c\n");
+ fprintf(f,"%i %i\n", width,height);
+ fprintf(f,"255\n");
+ fclose(f);
+ f = fopen( filename, "ab" ); /* reopen in binary append mode */
+ for (y=height-1; y>=0; y--) {
+ for (x=0; x<width; x++) {
+ i = (y*width + x) * 4;
+ fputc(ptr[i], f); /* write red */
+ fputc(ptr[i+1], f); /* write green */
+ fputc(ptr[i+2], f); /* write blue */
+ }
+ }
+ }
+ else {
+ /*ASCII*/
+ int counter = 0;
+ fprintf(f,"P3\n");
+ fprintf(f,"# ascii ppm file created by osdemo.c\n");
+ fprintf(f,"%i %i\n", width, height);
+ fprintf(f,"255\n");
+ for (y=height-1; y>=0; y--) {
+ for (x=0; x<width; x++) {
+ i = (y*width + x) * 4;
+ fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
+ counter++;
+ if (counter % 5 == 0)
+ fprintf(f, "\n");
+ }
+ }
+ }
+ fclose(f);
+ }
+}
+
+#endif
+
+
+
+int
+main(int argc, char *argv[])
+{
+ OSMesaContext ctx;
+ void *buffer;
+ char *filename = NULL;
+
+ if (argc < 2) {
+ fprintf(stderr, "Usage:\n");
+ fprintf(stderr, " osdemo filename [width height]\n");
+ return 0;
+ }
+
+ filename = argv[1];
+ if (argc == 4) {
+ Width = atoi(argv[2]);
+ Height = atoi(argv[3]);
+ }
+
+ /* Create an RGBA-mode context */
+#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
+ /* specify Z, stencil, accum sizes */
+ ctx = OSMesaCreateContextExt( OSMESA_RGBA, 16, 0, 0, NULL );
+#else
+ ctx = OSMesaCreateContext( OSMESA_RGBA, NULL );
+#endif
+ if (!ctx) {
+ printf("OSMesaCreateContext failed!\n");
+ return 0;
+ }
+
+ /* Allocate the image buffer */
+ buffer = malloc( Width * Height * 4 * sizeof(GLubyte) );
+ if (!buffer) {
+ printf("Alloc image buffer failed!\n");
+ return 0;
+ }
+
+ /* Bind the buffer to the context and make it current */
+ if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, Width, Height )) {
+ printf("OSMesaMakeCurrent failed!\n");
+ return 0;
+ }
+
+
+ {
+ int z, s, a;
+ glGetIntegerv(GL_DEPTH_BITS, &z);
+ glGetIntegerv(GL_STENCIL_BITS, &s);
+ glGetIntegerv(GL_ACCUM_RED_BITS, &a);
+ printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a);
+ }
+
+ render_image();
+
+ if (filename != NULL) {
+#ifdef SAVE_TARGA
+ write_targa(filename, buffer, Width, Height);
+#else
+ write_ppm(filename, buffer, Width, Height);
+#endif
+ }
+ else {
+ printf("Specify a filename if you want to make an image file\n");
+ }
+
+ printf("all done\n");
+
+ /* free the image buffer */
+ free( buffer );
+
+ /* destroy the context */
+ OSMesaDestroyContext( ctx );
+
+ return 0;
+}
diff --git a/progs/osdemos/osdemo16.c b/progs/osdemos/osdemo16.c
new file mode 100644
index 0000000000..10ed695d7a
--- /dev/null
+++ b/progs/osdemos/osdemo16.c
@@ -0,0 +1,291 @@
+/*
+ * Demo of off-screen Mesa rendering with 16-bit color channels.
+ * This requires the libOSMesa16.so library.
+ *
+ * Compile with something like this:
+ *
+ * gcc osdemo16.c -I../../include -L../../lib -lglut -lGLU -lOSMesa16 -lm -o osdemo16
+ */
+
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "GL/osmesa.h"
+#include "GL/glut.h"
+
+
+#define SAVE_TARGA
+
+
+#define WIDTH 400
+#define HEIGHT 400
+
+
+
+static void render_image( void )
+{
+ GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
+ GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
+ GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
+ GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
+ GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 };
+ GLfloat green_mat[] = { 0.2, 1.0, 0.2, 0.5 };
+ GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
+ GLfloat white_mat[] = { 1.0, 1.0, 1.0, 1.0 };
+ GLfloat purple_mat[] = { 1.0, 0.2, 1.0, 1.0 };
+ GLUquadricObj *qobj = gluNewQuadric();
+
+ glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
+ glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
+ glLightfv(GL_LIGHT0, GL_POSITION, light_position);
+
+ glEnable(GL_LIGHTING);
+ glEnable(GL_LIGHT0);
+ glEnable(GL_DEPTH_TEST);
+
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
+ glMatrixMode(GL_MODELVIEW);
+
+ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
+
+ glPushMatrix();
+ glRotatef(20.0, 1.0, 0.0, 0.0);
+
+#if 0
+ glPushMatrix();
+ glTranslatef(-0.75, 0.5, 0.0);
+ glRotatef(90.0, 1.0, 0.0, 0.0);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
+ glutSolidTorus(0.275, 0.85, 20, 20);
+ glPopMatrix();
+#endif
+
+ /* red square */
+ glPushMatrix();
+ glTranslatef(0.0, -0.5, 0.0);
+ glRotatef(90, 1, 0.5, 0);
+ glScalef(3, 3, 3);
+ glDisable(GL_LIGHTING);
+ glColor4f(1, 0, 0, 0.5);
+ glBegin(GL_POLYGON);
+ glVertex2f(-1, -1);
+ glVertex2f( 1, -1);
+ glVertex2f( 1, 1);
+ glVertex2f(-1, 1);
+ glEnd();
+ glEnable(GL_LIGHTING);
+ glPopMatrix();
+
+#if 0
+ /* green square */
+ glPushMatrix();
+ glTranslatef(0.0, 0.5, 0.1);
+ glDisable(GL_LIGHTING);
+ glColor3f(0, 1, 0);
+ glBegin(GL_POLYGON);
+ glVertex2f(-1, -1);
+ glVertex2f( 1, -1);
+ glVertex2f( 1, 1);
+ glVertex2f(-1, 1);
+ glEnd();
+ glEnable(GL_LIGHTING);
+ glPopMatrix();
+
+ /* blue square */
+ glPushMatrix();
+ glTranslatef(0.75, 0.5, 0.3);
+ glDisable(GL_LIGHTING);
+ glColor3f(0, 0, 0.5);
+ glBegin(GL_POLYGON);
+ glVertex2f(-1, -1);
+ glVertex2f( 1, -1);
+ glVertex2f( 1, 1);
+ glVertex2f(-1, 1);
+ glEnd();
+ glEnable(GL_LIGHTING);
+ glPopMatrix();
+#endif
+ glPushMatrix();
+ glTranslatef(-0.75, -0.5, 0.0);
+ glRotatef(270.0, 1.0, 0.0, 0.0);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
+ glColor4f(0,1,0,0.5);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ gluCylinder(qobj, 1.0, 0.0, 2.0, 16, 1);
+ glDisable(GL_BLEND);
+ glPopMatrix();
+
+ glPushMatrix();
+ glTranslatef(0.75, 1.0, 1.0);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
+ gluSphere(qobj, 1.0, 20, 20);
+ glPopMatrix();
+
+ glPopMatrix();
+
+ /* This is very important!!!
+ * Make sure buffered commands are finished!!!
+ */
+ glFinish();
+
+ gluDeleteQuadric(qobj);
+
+ {
+ GLint r, g, b, a;
+ glGetIntegerv(GL_RED_BITS, &r);
+ glGetIntegerv(GL_GREEN_BITS, &g);
+ glGetIntegerv(GL_BLUE_BITS, &b);
+ glGetIntegerv(GL_ALPHA_BITS, &a);
+ printf("channel sizes: %d %d %d %d\n", r, g, b, a);
+ }
+}
+
+
+
+static void
+write_targa(const char *filename, const GLushort *buffer, int width, int height)
+{
+ FILE *f = fopen( filename, "w" );
+ if (f) {
+ int i, x, y;
+ const GLushort *ptr = buffer;
+ printf ("osdemo, writing tga file \n");
+ fputc (0x00, f); /* ID Length, 0 => No ID */
+ fputc (0x00, f); /* Color Map Type, 0 => No color map included */
+ fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
+ fputc (0x00, f); /* Next five bytes are about the color map entries */
+ fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
+ fputc (0x00, f);
+ fputc (0x00, f);
+ fputc (0x00, f);
+ fputc (0x00, f); /* X-origin of Image */
+ fputc (0x00, f);
+ fputc (0x00, f); /* Y-origin of Image */
+ fputc (0x00, f);
+ fputc (WIDTH & 0xff, f); /* Image Width */
+ fputc ((WIDTH>>8) & 0xff, f);
+ fputc (HEIGHT & 0xff, f); /* Image Height */
+ fputc ((HEIGHT>>8) & 0xff, f);
+ fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
+ fputc (0x20, f); /* Image Descriptor */
+ fclose(f);
+ f = fopen( filename, "ab" ); /* reopen in binary append mode */
+ for (y=height-1; y>=0; y--) {
+ for (x=0; x<width; x++) {
+ i = (y*width + x) * 4;
+ /* just write 8 high bits */
+ fputc(ptr[i+2] >> 8, f); /* write blue */
+ fputc(ptr[i+1] >> 8, f); /* write green */
+ fputc(ptr[i] >> 8, f); /* write red */
+ }
+ }
+ }
+}
+
+
+static void
+write_ppm(const char *filename, const GLushort *buffer, int width, int height)
+{
+ const int binary = 0;
+ FILE *f = fopen( filename, "w" );
+ if (f) {
+ int i, x, y;
+ const GLushort *ptr = buffer;
+ if (binary) {
+ fprintf(f,"P6\n");
+ fprintf(f,"# ppm-file created by osdemo.c\n");
+ fprintf(f,"%i %i\n", width,height);
+ fprintf(f,"255\n");
+ fclose(f);
+ f = fopen( filename, "ab" ); /* reopen in binary append mode */
+ for (y=height-1; y>=0; y--) {
+ for (x=0; x<width; x++) {
+ i = (y*width + x) * 4;
+ /* just write 8 high bits */
+ fputc(ptr[i] >> 8, f); /* write red */
+ fputc(ptr[i+1] >> 8, f); /* write green */
+ fputc(ptr[i+2] >> 8, f); /* write blue */
+ }
+ }
+ }
+ else {
+ /*ASCII*/
+ int counter = 0;
+ fprintf(f,"P3\n");
+ fprintf(f,"# ascii ppm file created by osdemo.c\n");
+ fprintf(f,"%i %i\n", width, height);
+ fprintf(f,"255\n");
+ for (y=height-1; y>=0; y--) {
+ for (x=0; x<width; x++) {
+ i = (y*width + x) * 4;
+ /* just write 8 high bits */
+ fprintf(f, " %3d %3d %3d", ptr[i] >> 8, ptr[i+1] >> 8, ptr[i+2] >> 8);
+ counter++;
+ if (counter % 5 == 0)
+ fprintf(f, "\n");
+ }
+ }
+ }
+ fclose(f);
+ }
+}
+
+
+
+int main( int argc, char *argv[] )
+{
+ GLushort *buffer;
+
+ /* Create an RGBA-mode context */
+#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
+ /* specify Z, stencil, accum sizes */
+ OSMesaContext ctx = OSMesaCreateContextExt( GL_RGBA, 16, 0, 0, NULL );
+#else
+ OSMesaContext ctx = OSMesaCreateContext( GL_RGBA, NULL );
+#endif
+ if (!ctx) {
+ printf("OSMesaCreateContext failed!\n");
+ return 0;
+ }
+
+ /* Allocate the image buffer */
+ buffer = (GLushort *) malloc( WIDTH * HEIGHT * 4 * sizeof(GLushort));
+ if (!buffer) {
+ printf("Alloc image buffer failed!\n");
+ return 0;
+ }
+
+ /* Bind the buffer to the context and make it current */
+ if (!OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_SHORT, WIDTH, HEIGHT )) {
+ printf("OSMesaMakeCurrent failed!\n");
+ return 0;
+ }
+
+ render_image();
+
+ if (argc>1) {
+#ifdef SAVE_TARGA
+ write_targa(argv[1], buffer, WIDTH, HEIGHT);
+#else
+ write_ppm(argv[1], buffer, WIDTH, HEIGHT);
+#endif
+ }
+ else {
+ printf("Specify a filename if you want to make an image file\n");
+ }
+
+ printf("all done\n");
+
+ /* free the image buffer */
+ free( buffer );
+
+ /* destroy the context */
+ OSMesaDestroyContext( ctx );
+
+ return 0;
+}
diff --git a/progs/osdemos/osdemo32.c b/progs/osdemos/osdemo32.c
new file mode 100644
index 0000000000..7295b46a83
--- /dev/null
+++ b/progs/osdemos/osdemo32.c
@@ -0,0 +1,308 @@
+/*
+ * Demo of off-screen Mesa rendering with 32-bit float color channels.
+ * This requires the libOSMesa32.so library.
+ *
+ * Compile with something like this:
+ *
+ * gcc osdemo32.c -I../../include -L../../lib -lglut -lGLU -lOSMesa32 -lm -o osdemo32
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "GL/osmesa.h"
+#include "GL/glut.h"
+
+
+#define SAVE_TARGA
+
+
+#define WIDTH 400
+#define HEIGHT 400
+
+
+
+static void render_image( void )
+{
+ GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
+ GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
+ GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
+ GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
+ GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 };
+ GLfloat green_mat[] = { 0.2, 1.0, 0.2, 0.5 };
+ GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
+ GLfloat white_mat[] = { 1.0, 1.0, 1.0, 1.0 };
+ GLfloat purple_mat[] = { 1.0, 0.2, 1.0, 1.0 };
+ GLUquadricObj *qobj = gluNewQuadric();
+
+ glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
+ glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
+ glLightfv(GL_LIGHT0, GL_POSITION, light_position);
+
+ glEnable(GL_LIGHTING);
+ glEnable(GL_LIGHT0);
+ glEnable(GL_DEPTH_TEST);
+
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
+ glMatrixMode(GL_MODELVIEW);
+
+ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
+
+ glPushMatrix();
+ glRotatef(20.0, 1.0, 0.0, 0.0);
+
+#if 0
+ glPushMatrix();
+ glTranslatef(-0.75, 0.5, 0.0);
+ glRotatef(90.0, 1.0, 0.0, 0.0);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
+ glutSolidTorus(0.275, 0.85, 20, 20);
+ glPopMatrix();
+#endif
+
+ /* red square */
+ glPushMatrix();
+ glTranslatef(0.0, -0.5, 0.0);
+ glRotatef(90, 1, 0.5, 0);
+ glScalef(3, 3, 3);
+ glDisable(GL_LIGHTING);
+ glColor4f(1, 0, 0, 0.5);
+ glBegin(GL_POLYGON);
+ glVertex2f(-1, -1);
+ glVertex2f( 1, -1);
+ glVertex2f( 1, 1);
+ glVertex2f(-1, 1);
+ glEnd();
+ glEnable(GL_LIGHTING);
+ glPopMatrix();
+
+#if 0
+ /* green square */
+ glPushMatrix();
+ glTranslatef(0.0, 0.5, 0.1);
+ glDisable(GL_LIGHTING);
+ glColor3f(0, 1, 0);
+ glBegin(GL_POLYGON);
+ glVertex2f(-1, -1);
+ glVertex2f( 1, -1);
+ glVertex2f( 1, 1);
+ glVertex2f(-1, 1);
+ glEnd();
+ glEnable(GL_LIGHTING);
+ glPopMatrix();
+
+ /* blue square */
+ glPushMatrix();
+ glTranslatef(0.75, 0.5, 0.3);
+ glDisable(GL_LIGHTING);
+ glColor3f(0, 0, 0.5);
+ glBegin(GL_POLYGON);
+ glVertex2f(-1, -1);
+ glVertex2f( 1, -1);
+ glVertex2f( 1, 1);
+ glVertex2f(-1, 1);
+ glEnd();
+ glEnable(GL_LIGHTING);
+ glPopMatrix();
+#endif
+ glPushMatrix();
+ glTranslatef(-0.75, -0.5, 0.0);
+ glRotatef(270.0, 1.0, 0.0, 0.0);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
+ glColor4f(0,1,0,0.5);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ gluCylinder(qobj, 1.0, 0.0, 2.0, 16, 1);
+ glDisable(GL_BLEND);
+ glPopMatrix();
+
+ glPushMatrix();
+ glTranslatef(0.75, 1.0, 1.0);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
+ gluSphere(qobj, 1.0, 20, 20);
+ glPopMatrix();
+
+ glPopMatrix();
+
+ /* This is very important!!!
+ * Make sure buffered commands are finished!!!
+ */
+ glFinish();
+
+ gluDeleteQuadric(qobj);
+
+ {
+ GLint r, g, b, a;
+ glGetIntegerv(GL_RED_BITS, &r);
+ glGetIntegerv(GL_GREEN_BITS, &g);
+ glGetIntegerv(GL_BLUE_BITS, &b);
+ glGetIntegerv(GL_ALPHA_BITS, &a);
+ printf("channel sizes: %d %d %d %d\n", r, g, b, a);
+ }
+}
+
+
+
+static void
+write_targa(const char *filename, const GLfloat *buffer, int width, int height)
+{
+ FILE *f = fopen( filename, "w" );
+ if (f) {
+ int i, x, y;
+ const GLfloat *ptr = buffer;
+ printf ("osdemo, writing tga file \n");
+ fputc (0x00, f); /* ID Length, 0 => No ID */
+ fputc (0x00, f); /* Color Map Type, 0 => No color map included */
+ fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */
+ fputc (0x00, f); /* Next five bytes are about the color map entries */
+ fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */
+ fputc (0x00, f);
+ fputc (0x00, f);
+ fputc (0x00, f);
+ fputc (0x00, f); /* X-origin of Image */
+ fputc (0x00, f);
+ fputc (0x00, f); /* Y-origin of Image */
+ fputc (0x00, f);
+ fputc (WIDTH & 0xff, f); /* Image Width */
+ fputc ((WIDTH>>8) & 0xff, f);
+ fputc (HEIGHT & 0xff, f); /* Image Height */
+ fputc ((HEIGHT>>8) & 0xff, f);
+ fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */
+ fputc (0x20, f); /* Image Descriptor */
+ fclose(f);
+ f = fopen( filename, "ab" ); /* reopen in binary append mode */
+ for (y=height-1; y>=0; y--) {
+ for (x=0; x<width; x++) {
+ int r, g, b;
+ i = (y*width + x) * 4;
+ r = (int) (ptr[i+0] * 255.0);
+ g = (int) (ptr[i+1] * 255.0);
+ b = (int) (ptr[i+2] * 255.0);
+ if (r > 255) r = 255;
+ if (g > 255) g = 255;
+ if (b > 255) b = 255;
+ fputc(b, f); /* write blue */
+ fputc(g, f); /* write green */
+ fputc(r, f); /* write red */
+ }
+ }
+ }
+}
+
+
+static void
+write_ppm(const char *filename, const GLfloat *buffer, int width, int height)
+{
+ const int binary = 0;
+ FILE *f = fopen( filename, "w" );
+ if (f) {
+ int i, x, y;
+ const GLfloat *ptr = buffer;
+ if (binary) {
+ fprintf(f,"P6\n");
+ fprintf(f,"# ppm-file created by osdemo.c\n");
+ fprintf(f,"%i %i\n", width,height);
+ fprintf(f,"255\n");
+ fclose(f);
+ f = fopen( filename, "ab" ); /* reopen in binary append mode */
+ for (y=height-1; y>=0; y--) {
+ for (x=0; x<width; x++) {
+ int r, g, b;
+ i = (y*width + x) * 4;
+ r = (int) (ptr[i+0] * 255.0);
+ g = (int) (ptr[i+1] * 255.0);
+ b = (int) (ptr[i+2] * 255.0);
+ if (r > 255) r = 255;
+ if (g > 255) g = 255;
+ if (b > 255) b = 255;
+ fputc(r, f); /* write red */
+ fputc(g, f); /* write green */
+ fputc(b, f); /* write blue */
+ }
+ }
+ }
+ else {
+ /*ASCII*/
+ int counter = 0;
+ fprintf(f,"P3\n");
+ fprintf(f,"# ascii ppm file created by osdemo.c\n");
+ fprintf(f,"%i %i\n", width, height);
+ fprintf(f,"255\n");
+ for (y=height-1; y>=0; y--) {
+ for (x=0; x<width; x++) {
+ int r, g, b;
+ i = (y*width + x) * 4;
+ r = (int) (ptr[i+0] * 255.0);
+ g = (int) (ptr[i+1] * 255.0);
+ b = (int) (ptr[i+2] * 255.0);
+ if (r > 255) r = 255;
+ if (g > 255) g = 255;
+ if (b > 255) b = 255;
+ fprintf(f, " %3d %3d %3d", r, g, b);
+ counter++;
+ if (counter % 5 == 0)
+ fprintf(f, "\n");
+ }
+ }
+ }
+ fclose(f);
+ }
+}
+
+
+
+int main( int argc, char *argv[] )
+{
+ GLfloat *buffer;
+
+ /* Create an RGBA-mode context */
+#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
+ /* specify Z, stencil, accum sizes */
+ OSMesaContext ctx = OSMesaCreateContextExt( GL_RGBA, 16, 0, 0, NULL );
+#else
+ OSMesaContext ctx = OSMesaCreateContext( GL_RGBA, NULL );
+#endif
+ if (!ctx) {
+ printf("OSMesaCreateContext failed!\n");
+ return 0;
+ }
+
+ /* Allocate the image buffer */
+ buffer = (GLfloat *) malloc( WIDTH * HEIGHT * 4 * sizeof(GLfloat));
+ if (!buffer) {
+ printf("Alloc image buffer failed!\n");
+ return 0;
+ }
+
+ /* Bind the buffer to the context and make it current */
+ if (!OSMesaMakeCurrent( ctx, buffer, GL_FLOAT, WIDTH, HEIGHT )) {
+ printf("OSMesaMakeCurrent failed!\n");
+ return 0;
+ }
+
+ render_image();
+
+ if (argc>1) {
+#ifdef SAVE_TARGA
+ write_targa(argv[1], buffer, WIDTH, HEIGHT);
+#else
+ write_ppm(argv[1], buffer, WIDTH, HEIGHT);
+#endif
+ }
+ else {
+ printf("Specify a filename if you want to make an image file\n");
+ }
+
+ printf("all done\n");
+
+ /* free the image buffer */
+ free( buffer );
+
+ /* destroy the context */
+ OSMesaDestroyContext( ctx );
+
+ return 0;
+}
diff --git a/progs/osdemos/ostest1.c b/progs/osdemos/ostest1.c
new file mode 100644
index 0000000000..001e368616
--- /dev/null
+++ b/progs/osdemos/ostest1.c
@@ -0,0 +1,470 @@
+/*
+ * Test OSMesa interface at 8, 16 and 32 bits/channel.
+ *
+ * Usage: osdemo [options]
+ *
+ * Options:
+ * -f generate image files
+ * -g render gradient and print color values
+ */
+
+#include <assert.h>
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "GL/osmesa.h"
+#include "GL/glu.h"
+
+
+#define WIDTH 600
+#define HEIGHT 600
+
+static GLboolean WriteFiles = GL_FALSE;
+static GLboolean Gradient = GL_FALSE;
+
+
+static void
+Sphere(float radius, int slices, int stacks)
+{
+ GLUquadric *q = gluNewQuadric();
+ gluQuadricNormals(q, GLU_SMOOTH);
+ gluSphere(q, radius, slices, stacks);
+ gluDeleteQuadric(q);
+}
+
+
+static void
+Cone(float base, float height, int slices, int stacks)
+{
+ GLUquadric *q = gluNewQuadric();
+ gluQuadricDrawStyle(q, GLU_FILL);
+ gluQuadricNormals(q, GLU_SMOOTH);
+ gluCylinder(q, base, 0.0, height, slices, stacks);
+ gluDeleteQuadric(q);
+}
+
+
+static void
+Torus(float innerRadius, float outerRadius, int sides, int rings)
+{
+ /* from GLUT... */
+ int i, j;
+ GLfloat theta, phi, theta1;
+ GLfloat cosTheta, sinTheta;
+ GLfloat cosTheta1, sinTheta1;
+ const GLfloat ringDelta = 2.0 * M_PI / rings;
+ const GLfloat sideDelta = 2.0 * M_PI / sides;
+
+ theta = 0.0;
+ cosTheta = 1.0;
+ sinTheta = 0.0;
+ for (i = rings - 1; i >= 0; i--) {
+ theta1 = theta + ringDelta;
+ cosTheta1 = cos(theta1);
+ sinTheta1 = sin(theta1);
+ glBegin(GL_QUAD_STRIP);
+ phi = 0.0;
+ for (j = sides; j >= 0; j--) {
+ GLfloat cosPhi, sinPhi, dist;
+
+ phi += sideDelta;
+ cosPhi = cos(phi);
+ sinPhi = sin(phi);
+ dist = outerRadius + innerRadius * cosPhi;
+
+ glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
+ glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, innerRadius * sinPhi);
+ glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
+ glVertex3f(cosTheta * dist, -sinTheta * dist, innerRadius * sinPhi);
+ }
+ glEnd();
+ theta = theta1;
+ cosTheta = cosTheta1;
+ sinTheta = sinTheta1;
+ }
+}
+
+
+static void Cube(float size)
+{
+ size = 0.5 * size;
+
+ glBegin(GL_QUADS);
+ /* +X face */
+ glNormal3f(1, 0, 0);
+ glVertex3f(size, -size, size);
+ glVertex3f(size, -size, -size);
+ glVertex3f(size, size, -size);
+ glVertex3f(size, size, size);
+
+ /* -X face */
+ glNormal3f(-1, 0, 0);
+ glVertex3f(-size, size, size);
+ glVertex3f(-size, size, -size);
+ glVertex3f(-size, -size, -size);
+ glVertex3f(-size, -size, size);
+
+ /* +Y face */
+ glNormal3f(0, 1, 0);
+ glVertex3f(-size, size, size);
+ glVertex3f( size, size, size);
+ glVertex3f( size, size, -size);
+ glVertex3f(-size, size, -size);
+
+ /* -Y face */
+ glNormal3f(0, -1, 0);
+ glVertex3f(-size, -size, -size);
+ glVertex3f( size, -size, -size);
+ glVertex3f( size, -size, size);
+ glVertex3f(-size, -size, size);
+
+ /* +Z face */
+ glNormal3f(0, 0, 1);
+ glVertex3f(-size, -size, size);
+ glVertex3f( size, -size, size);
+ glVertex3f( size, size, size);
+ glVertex3f(-size, size, size);
+
+ /* -Z face */
+ glNormal3f(0, 0, -1);
+ glVertex3f(-size, size, -size);
+ glVertex3f( size, size, -size);
+ glVertex3f( size, -size, -size);
+ glVertex3f(-size, -size, -size);
+
+ glEnd();
+}
+
+
+
+/**
+ * Draw red/green gradient across bottom of image.
+ * Read pixels to check deltas.
+ */
+static void
+render_gradient(void)
+{
+ GLfloat row[WIDTH][4];
+ int i;
+
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(-1, 1, -1, 1, -1, 1);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+
+ glBegin(GL_POLYGON);
+ glColor3f(1, 0, 0);
+ glVertex2f(-1, -1.0);
+ glVertex2f(-1, -0.9);
+ glColor3f(0, 1, 0);
+ glVertex2f(1, -0.9);
+ glVertex2f(1, -1.0);
+ glEnd();
+ glFinish();
+
+ glReadPixels(0, 0, WIDTH, 1, GL_RGBA, GL_FLOAT, row);
+ for (i = 0; i < 4; i++) {
+ printf("row[i] = %f, %f, %f\n", row[i][0], row[i][1], row[i][2]);
+ }
+}
+
+
+static void
+render_image(void)
+{
+ static const GLfloat light_ambient[4] = { 0.0, 0.0, 0.0, 1.0 };
+ static const GLfloat light_diffuse[4] = { 1.0, 1.0, 1.0, 1.0 };
+ static const GLfloat light_specular[4] = { 1.0, 1.0, 1.0, 1.0 };
+ static const GLfloat light_position[4] = { 1.0, 1.0, 1.0, 0.0 };
+ static const GLfloat red_mat[4] = { 1.0, 0.2, 0.2, 1.0 };
+ static const GLfloat green_mat[4] = { 0.2, 1.0, 0.2, 1.0 };
+ static const GLfloat blue_mat[4] = { 0.2, 0.2, 1.0, 1.0 };
+#if 0
+ static const GLfloat yellow_mat[4] = { 0.8, 0.8, 0.0, 1.0 };
+#endif
+ static const GLfloat purple_mat[4] = { 0.8, 0.4, 0.8, 0.6 };
+
+ glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
+ glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
+ glLightfv(GL_LIGHT0, GL_POSITION, light_position);
+
+ glEnable(GL_DEPTH_TEST);
+ glEnable(GL_LIGHT0);
+
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 50.0);
+ glMatrixMode(GL_MODELVIEW);
+ glTranslatef(0, 0.5, -7);
+
+ glClearColor(0.3, 0.3, 0.7, 0.0);
+ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
+
+ glPushMatrix();
+ glRotatef(20.0, 1.0, 0.0, 0.0);
+
+ /* ground */
+ glEnable(GL_TEXTURE_2D);
+ glBegin(GL_POLYGON);
+ glNormal3f(0, 1, 0);
+ glTexCoord2f(0, 0); glVertex3f(-5, -1, -5);
+ glTexCoord2f(1, 0); glVertex3f( 5, -1, -5);
+ glTexCoord2f(1, 1); glVertex3f( 5, -1, 5);
+ glTexCoord2f(0, 1); glVertex3f(-5, -1, 5);
+ glEnd();
+ glDisable(GL_TEXTURE_2D);
+
+ glEnable(GL_LIGHTING);
+
+ glPushMatrix();
+ glTranslatef(-1.5, 0.5, 0.0);
+ glRotatef(90.0, 1.0, 0.0, 0.0);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
+ Torus(0.275, 0.85, 20, 20);
+ glPopMatrix();
+
+ glPushMatrix();
+ glTranslatef(-1.5, -0.5, 0.0);
+ glRotatef(270.0, 1.0, 0.0, 0.0);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
+ Cone(1.0, 2.0, 16, 1);
+ glPopMatrix();
+
+ glPushMatrix();
+ glTranslatef(0.95, 0.0, -0.8);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
+ glLineWidth(2.0);
+ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ Sphere(1.2, 20, 20);
+ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+ glPopMatrix();
+
+#if 0
+ glPushMatrix();
+ glTranslatef(0.75, 0.0, 1.3);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, yellow_mat );
+ glutWireTeapot(1.0);
+ glPopMatrix();
+#endif
+
+ glPushMatrix();
+ glTranslatef(-0.25, 0.0, 2.5);
+ glRotatef(40, 0, 1, 0);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable(GL_BLEND);
+ glEnable(GL_CULL_FACE);
+ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, purple_mat );
+ Cube(1.0);
+ glDisable(GL_BLEND);
+ glDisable(GL_CULL_FACE);
+ glPopMatrix();
+
+ glDisable(GL_LIGHTING);
+
+ glPopMatrix();
+
+ glDisable(GL_DEPTH_TEST);
+}
+
+
+static void
+init_context(void)
+{
+ const GLint texWidth = 64, texHeight = 64;
+ GLubyte *texImage;
+ int i, j;
+
+ /* checker image */
+ texImage = malloc(texWidth * texHeight * 4);
+ for (i = 0; i < texHeight; i++) {
+ for (j = 0; j < texWidth; j++) {
+ int k = (i * texWidth + j) * 4;
+ if ((i % 5) == 0 || (j % 5) == 0) {
+ texImage[k+0] = 200;
+ texImage[k+1] = 200;
+ texImage[k+2] = 200;
+ texImage[k+3] = 255;
+ }
+ else {
+ if ((i % 5) == 1 || (j % 5) == 1) {
+ texImage[k+0] = 50;
+ texImage[k+1] = 50;
+ texImage[k+2] = 50;
+ texImage[k+3] = 255;
+ }
+ else {
+ texImage[k+0] = 100;
+ texImage[k+1] = 100;
+ texImage[k+2] = 100;
+ texImage[k+3] = 255;
+ }
+ }
+ }
+ }
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, texImage);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+ free(texImage);
+}
+
+
+static void
+write_ppm(const char *filename, const GLubyte *buffer, int width, int height)
+{
+ const int binary = 0;
+ FILE *f = fopen( filename, "w" );
+ if (f) {
+ int i, x, y;
+ const GLubyte *ptr = buffer;
+ if (binary) {
+ fprintf(f,"P6\n");
+ fprintf(f,"# ppm-file created by osdemo.c\n");
+ fprintf(f,"%i %i\n", width,height);
+ fprintf(f,"255\n");
+ fclose(f);
+ f = fopen( filename, "ab" ); /* reopen in binary append mode */
+ for (y=height-1; y>=0; y--) {
+ for (x=0; x<width; x++) {
+ i = (y*width + x) * 4;
+ fputc(ptr[i], f); /* write red */
+ fputc(ptr[i+1], f); /* write green */
+ fputc(ptr[i+2], f); /* write blue */
+ }
+ }
+ }
+ else {
+ /*ASCII*/
+ int counter = 0;
+ fprintf(f,"P3\n");
+ fprintf(f,"# ascii ppm file created by osdemo.c\n");
+ fprintf(f,"%i %i\n", width, height);
+ fprintf(f,"255\n");
+ for (y=height-1; y>=0; y--) {
+ for (x=0; x<width; x++) {
+ i = (y*width + x) * 4;
+ fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
+ counter++;
+ if (counter % 5 == 0)
+ fprintf(f, "\n");
+ }
+ }
+ }
+ fclose(f);
+ }
+}
+
+
+static GLboolean
+test(GLenum type, GLint bits, const char *filename)
+{
+ const GLint z = 16, stencil = 0, accum = 0;
+ OSMesaContext ctx;
+ void *buffer;
+ GLint cBits;
+
+ assert(bits == 8 ||
+ bits == 16 ||
+ bits == 32);
+
+ assert(type == GL_UNSIGNED_BYTE ||
+ type == GL_UNSIGNED_SHORT ||
+ type == GL_FLOAT);
+
+ ctx = OSMesaCreateContextExt(OSMESA_RGBA, z, stencil, accum, NULL );
+ if (!ctx) {
+ printf("OSMesaCreateContextExt() failed!\n");
+ return 0;
+ }
+
+ /* Allocate the image buffer */
+ buffer = malloc(WIDTH * HEIGHT * 4 * bits / 8);
+ if (!buffer) {
+ printf("Alloc image buffer failed!\n");
+ return 0;
+ }
+
+ /* Bind the buffer to the context and make it current */
+ if (!OSMesaMakeCurrent( ctx, buffer, type, WIDTH, HEIGHT )) {
+ printf("OSMesaMakeCurrent (%d bits/channel) failed!\n", bits);
+ free(buffer);
+ OSMesaDestroyContext(ctx);
+ return 0;
+ }
+
+ /* sanity checks */
+ glGetIntegerv(GL_RED_BITS, &cBits);
+ assert(cBits == bits);
+ glGetIntegerv(GL_GREEN_BITS, &cBits);
+ assert(cBits == bits);
+ glGetIntegerv(GL_BLUE_BITS, &cBits);
+ assert(cBits == bits);
+ glGetIntegerv(GL_ALPHA_BITS, &cBits);
+ assert(cBits == bits);
+
+ printf("Rendering %d bit/channel image: %s\n", bits, filename);
+
+ init_context();
+ render_image();
+ if (Gradient)
+ render_gradient();
+
+ /* Make sure buffered commands are finished! */
+ glFinish();
+
+
+ if (WriteFiles && filename != NULL) {
+ if (type == GL_UNSIGNED_SHORT) {
+ GLushort *buffer16 = (GLushort *) buffer;
+ GLubyte *buffer8 = malloc(WIDTH * HEIGHT * 4);
+ int i;
+ for (i = 0; i < WIDTH * HEIGHT * 4; i++)
+ buffer8[i] = buffer16[i] >> 8;
+ write_ppm(filename, buffer8, WIDTH, HEIGHT);
+ free(buffer8);
+ }
+ else if (type == GL_FLOAT) {
+ GLfloat *buffer32 = (GLfloat *) buffer;
+ GLubyte *buffer8 = malloc(WIDTH * HEIGHT * 4);
+ int i;
+ for (i = 0; i < WIDTH * HEIGHT * 4; i++)
+ buffer8[i] = (GLubyte) (buffer32[i] * 255.0);
+ write_ppm(filename, buffer8, WIDTH, HEIGHT);
+ free(buffer8);
+ }
+ else {
+ write_ppm(filename, buffer, WIDTH, HEIGHT);
+ }
+ }
+
+ OSMesaDestroyContext(ctx);
+
+ free(buffer);
+
+ return 1;
+}
+
+
+int
+main( int argc, char *argv[] )
+{
+ int i;
+
+ for (i = 1; i < argc; i++) {
+ if (strcmp(argv[i], "-f") == 0)
+ WriteFiles = GL_TRUE;
+ else if (strcmp(argv[i], "-g") == 0)
+ Gradient = GL_TRUE;
+ }
+
+ test(GL_UNSIGNED_BYTE, 8, "image8.ppm");
+ test(GL_UNSIGNED_SHORT, 16, "image16.ppm");
+ test(GL_FLOAT, 32, "image32.ppm");
+
+ return 0;
+}