132001f49Smrg/*
232001f49Smrg * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
332001f49Smrg *
432001f49Smrg * Permission to use, copy, modify, distribute, and sell this software and
532001f49Smrg * its documentation for any purpose is hereby granted without fee, provided
632001f49Smrg * that (i) the above copyright notices and this permission notice appear in
732001f49Smrg * all copies of the software and related documentation, and (ii) the name of
832001f49Smrg * Silicon Graphics may not be used in any advertising or
932001f49Smrg * publicity relating to the software without the specific, prior written
1032001f49Smrg * permission of Silicon Graphics.
1132001f49Smrg *
1232001f49Smrg * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
1332001f49Smrg * ANY KIND,
1432001f49Smrg * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
1532001f49Smrg * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
1632001f49Smrg *
1732001f49Smrg * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
1832001f49Smrg * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
1932001f49Smrg * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
2032001f49Smrg * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
2132001f49Smrg * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
2232001f49Smrg * OF THIS SOFTWARE.
2332001f49Smrg */
2432001f49Smrg
2532001f49Smrg#include <stdio.h>
2632001f49Smrg#include <string.h>
2732001f49Smrg#include <stdlib.h>
2832001f49Smrg#include <GL/glew.h>
2932001f49Smrg#include "glut_wrap.h"
3032001f49Smrg
3132001f49Smrg#define NR_VERTS 4
3232001f49Smrg
3332001f49Smrgstruct {
3432001f49Smrg   GLfloat position[NR_VERTS][4];
3532001f49Smrg   GLubyte color[NR_VERTS][4];
3632001f49Smrg   GLfloat tex0[NR_VERTS][2];
3732001f49Smrg   GLfloat tex1[NR_VERTS][2];
3832001f49Smrg} verts = {
3932001f49Smrg
4032001f49Smrg   { {  0.9, -0.9, 0.0, 1.0 },
4132001f49Smrg     {  0.9,  0.9, 0.0, 1.0 },
4232001f49Smrg     { -0.9,  0.9, 0.0, 1.0 },
4332001f49Smrg     { -0.9, -0.9, 0.0, 1.0 } },
4432001f49Smrg
4532001f49Smrg   { { 0x00, 0x00, 0xff, 0x00 },
4632001f49Smrg     { 0x00, 0xff, 0x00, 0x00 },
4732001f49Smrg     { 0xff, 0x00, 0x00, 0x00 },
4832001f49Smrg     { 0xff, 0xff, 0xff, 0x00 }
4932001f49Smrg   },
5032001f49Smrg
5132001f49Smrg   { {  1, -1 },
5232001f49Smrg     {  1,  1 },
5332001f49Smrg     { -1,  1 },
5432001f49Smrg     { -1, -1 } },
5532001f49Smrg
5632001f49Smrg   { { 3, 0 },
5732001f49Smrg     { 0, 3 },
5832001f49Smrg     { -3, 0 },
5932001f49Smrg     {  0, -3} },
6032001f49Smrg
6132001f49Smrg};
6232001f49Smrg
6332001f49SmrgGLuint indices[] = { 0, 1, 2, 3 };
6432001f49Smrg
6532001f49SmrgGLuint arrayObj, elementObj;
6632001f49Smrg
6732001f49Smrg
6832001f49SmrgGLenum doubleBuffer;
6932001f49Smrg
7032001f49Smrg
7132001f49Smrg#define Offset(ptr, member) (void *)((const char *)&((ptr)->member) - (const char *)(ptr))
7232001f49Smrg
7332001f49Smrgstatic void Init(void)
7432001f49Smrg{
7532001f49Smrg   GLuint texObj[2];
7632001f49Smrg
7732001f49Smrg   fprintf(stderr, "GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));
7832001f49Smrg   fprintf(stderr, "GL_VERSION    = %s\n", (char *) glGetString(GL_VERSION));
7932001f49Smrg   fprintf(stderr, "GL_VENDOR     = %s\n", (char *) glGetString(GL_VENDOR));
8032001f49Smrg   fflush(stderr);
8132001f49Smrg
8232001f49Smrg    glClearColor(0.0, 0.0, 1.0, 0.0);
8332001f49Smrg
8432001f49Smrg   glGenTextures(2, texObj);
8532001f49Smrg
8632001f49Smrg#define SIZE 32
8732001f49Smrg   {
8832001f49Smrg      GLubyte tex2d[SIZE][SIZE][3];
8932001f49Smrg      GLint s, t;
9032001f49Smrg
9132001f49Smrg      for (s = 0; s < SIZE; s++) {
9232001f49Smrg	 for (t = 0; t < SIZE; t++) {
9332001f49Smrg	    tex2d[t][s][0] = s*255/(SIZE-1);
9432001f49Smrg	    tex2d[t][s][1] = t*255/(SIZE-1);
9532001f49Smrg	    tex2d[t][s][2] = 0;
9632001f49Smrg	 }
9732001f49Smrg      }
9832001f49Smrg
9932001f49Smrg      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
10032001f49Smrg      glActiveTextureARB(GL_TEXTURE0_ARB);
10132001f49Smrg      glBindTexture(GL_TEXTURE_2D, texObj[0]);
10232001f49Smrg
10332001f49Smrg
10432001f49Smrg      glTexImage2D(GL_TEXTURE_2D, 0, 3, SIZE, SIZE, 0,
10532001f49Smrg                   GL_RGB, GL_UNSIGNED_BYTE, tex2d);
10632001f49Smrg
10732001f49Smrg      glEnable(GL_TEXTURE_2D);
10832001f49Smrg      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
10932001f49Smrg      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
11032001f49Smrg      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
11132001f49Smrg      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
11232001f49Smrg
11332001f49Smrg      glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
11432001f49Smrg   }
11532001f49Smrg
11632001f49Smrg   {
11732001f49Smrg      GLubyte tex2d[SIZE][SIZE][3];
11832001f49Smrg      GLint s, t;
11932001f49Smrg
12032001f49Smrg      for (s = 0; s < SIZE; s++) {
12132001f49Smrg	 for (t = 0; t < SIZE; t++) {
12232001f49Smrg            GLboolean on = ((s/4) ^ (t/4)) & 1;
12332001f49Smrg	    tex2d[t][s][0] = on ? 128 : 0;
12432001f49Smrg	    tex2d[t][s][1] = on ? 128 : 0;
12532001f49Smrg	    tex2d[t][s][2] = on ? 128 : 0;
12632001f49Smrg	 }
12732001f49Smrg      }
12832001f49Smrg
12932001f49Smrg      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
13032001f49Smrg      glActiveTextureARB(GL_TEXTURE1_ARB);
13132001f49Smrg      glBindTexture(GL_TEXTURE_2D, texObj[1]);
13232001f49Smrg
13332001f49Smrg      glTexImage2D(GL_TEXTURE_2D, 0, 3, SIZE, SIZE, 0,
13432001f49Smrg                   GL_RGB, GL_UNSIGNED_BYTE, tex2d);
13532001f49Smrg
13632001f49Smrg      glEnable(GL_TEXTURE_2D);
13732001f49Smrg      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
13832001f49Smrg      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
13932001f49Smrg      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
14032001f49Smrg      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
14132001f49Smrg
14232001f49Smrg      glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
14332001f49Smrg   }
14432001f49Smrg
14532001f49Smrg   glActiveTextureARB( GL_TEXTURE0_ARB );
14632001f49Smrg
14732001f49Smrg
14832001f49Smrg   {
14932001f49Smrg
15032001f49Smrg      glGenBuffersARB(1, &arrayObj);
15132001f49Smrg      glGenBuffersARB(1, &elementObj);
15232001f49Smrg
15332001f49Smrg      glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrayObj);
15432001f49Smrg      glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, elementObj);
15532001f49Smrg
15632001f49Smrg      glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), &verts, GL_STATIC_DRAW_ARB);
15732001f49Smrg      glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(indices), indices, GL_STATIC_DRAW_ARB);
15832001f49Smrg
15932001f49Smrg      glEnableClientState( GL_VERTEX_ARRAY );
16032001f49Smrg      glVertexPointer( 4, GL_FLOAT, 0, Offset(&verts, position) );
16132001f49Smrg
16232001f49Smrg      glEnableClientState( GL_COLOR_ARRAY );
16332001f49Smrg      glColorPointer( 4, GL_UNSIGNED_BYTE, 0, Offset(&verts, color) );
16432001f49Smrg
16532001f49Smrg      glClientActiveTextureARB( GL_TEXTURE0_ARB );
16632001f49Smrg      glEnableClientState( GL_TEXTURE_COORD_ARRAY );
16732001f49Smrg      glTexCoordPointer( 2, GL_FLOAT, 0, Offset(&verts, tex0) );
16832001f49Smrg
16932001f49Smrg      glClientActiveTextureARB( GL_TEXTURE1_ARB );
17032001f49Smrg      glEnableClientState( GL_TEXTURE_COORD_ARRAY );
17132001f49Smrg      glTexCoordPointer( 2, GL_FLOAT, 0, Offset(&verts, tex1) );
17232001f49Smrg
17332001f49Smrg      glClientActiveTextureARB( GL_TEXTURE0_ARB );
17432001f49Smrg   }
17532001f49Smrg}
17632001f49Smrg
17732001f49Smrgstatic void Reshape(int width, int height)
17832001f49Smrg{
17932001f49Smrg
18032001f49Smrg    glViewport(0, 0, (GLint)width, (GLint)height);
18132001f49Smrg
18232001f49Smrg    glMatrixMode(GL_PROJECTION);
18332001f49Smrg    glLoadIdentity();
18432001f49Smrg/*     glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); */
18532001f49Smrg    glMatrixMode(GL_MODELVIEW);
18632001f49Smrg}
18732001f49Smrg
18832001f49Smrgstatic void Key(unsigned char key, int x, int y)
18932001f49Smrg{
19032001f49Smrg
19132001f49Smrg    switch (key) {
19232001f49Smrg      case 27:
19332001f49Smrg	exit(1);
19432001f49Smrg      default:
19532001f49Smrg	break;
19632001f49Smrg    }
19732001f49Smrg
19832001f49Smrg    glutPostRedisplay();
19932001f49Smrg}
20032001f49Smrg
20132001f49Smrgstatic void Draw(void)
20232001f49Smrg{
20332001f49Smrg   glClear(GL_COLOR_BUFFER_BIT);
20432001f49Smrg
20532001f49Smrg   glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, NULL );
20632001f49Smrg
20732001f49Smrg   glFlush();
20832001f49Smrg
20932001f49Smrg   if (doubleBuffer) {
21032001f49Smrg      glutSwapBuffers();
21132001f49Smrg   }
21232001f49Smrg}
21332001f49Smrg
21432001f49Smrgstatic GLenum Args(int argc, char **argv)
21532001f49Smrg{
21632001f49Smrg    GLint i;
21732001f49Smrg
21832001f49Smrg    doubleBuffer = GL_FALSE;
21932001f49Smrg
22032001f49Smrg    for (i = 1; i < argc; i++) {
22132001f49Smrg        if (strcmp(argv[i], "-sb") == 0) {
22232001f49Smrg	    doubleBuffer = GL_FALSE;
22332001f49Smrg	} else if (strcmp(argv[i], "-db") == 0) {
22432001f49Smrg	    doubleBuffer = GL_TRUE;
22532001f49Smrg	} else {
22632001f49Smrg	    fprintf(stderr, "%s (Bad option).\n", argv[i]);
22732001f49Smrg	    return GL_FALSE;
22832001f49Smrg	}
22932001f49Smrg    }
23032001f49Smrg    return GL_TRUE;
23132001f49Smrg}
23232001f49Smrg
23332001f49Smrgint main(int argc, char **argv)
23432001f49Smrg{
23532001f49Smrg    GLenum type;
23632001f49Smrg
23732001f49Smrg    glutInit(&argc, argv);
23832001f49Smrg
23932001f49Smrg    if (Args(argc, argv) == GL_FALSE) {
24032001f49Smrg	exit(1);
24132001f49Smrg    }
24232001f49Smrg
24332001f49Smrg    glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
24432001f49Smrg
24532001f49Smrg    type = GLUT_RGB | GLUT_ALPHA;
24632001f49Smrg    type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
24732001f49Smrg    glutInitDisplayMode(type);
24832001f49Smrg
24932001f49Smrg    if (glutCreateWindow(*argv) == GL_FALSE) {
25032001f49Smrg	exit(1);
25132001f49Smrg    }
25232001f49Smrg
25332001f49Smrg    glewInit();
25432001f49Smrg    Init();
25532001f49Smrg
25632001f49Smrg    glutReshapeFunc(Reshape);
25732001f49Smrg    glutKeyboardFunc(Key);
25832001f49Smrg    glutDisplayFunc(Draw);
25932001f49Smrg    glutMainLoop();
26032001f49Smrg	return 0;
26132001f49Smrg}
262