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 * Based on the trivial/quad-tex-2d program.
2632001f49Smrg * Modified by Jakob Bornecrantz <jakob@tungstengraphics.com>
2732001f49Smrg */
2832001f49Smrg
2932001f49Smrg#include <stdio.h>
3032001f49Smrg#include <string.h>
3132001f49Smrg#include <stdlib.h>
3232001f49Smrg#include <GL/glew.h>
3332001f49Smrg#include "glut_wrap.h"
3432001f49Smrg
3532001f49Smrgstatic GLenum Target = GL_TEXTURE_2D;
3632001f49Smrgstatic GLenum Filter = GL_NEAREST;
3732001f49SmrgGLenum doubleBuffer;
3832001f49Smrgstatic float Rot = 0;
3932001f49Smrgstatic int win = 0;
4032001f49Smrg
4132001f49Smrgstatic void Init(void)
4232001f49Smrg{
4332001f49Smrg	int internalFormat;
4432001f49Smrg	int sourceFormat;
4532001f49Smrg
4632001f49Smrg	fprintf(stderr, "GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));
4732001f49Smrg	fprintf(stderr, "GL_VERSION    = %s\n", (char *) glGetString(GL_VERSION));
4832001f49Smrg	fprintf(stderr, "GL_VENDOR     = %s\n", (char *) glGetString(GL_VENDOR));
4932001f49Smrg
5032001f49Smrg	glClearColor(0.0, 0.0, 1.0, 0.0);
5132001f49Smrg
5232001f49Smrg#define SIZE 16
5332001f49Smrg	{
5432001f49Smrg		GLubyte tex2d[SIZE][SIZE][4];
5532001f49Smrg		GLint s, t;
5632001f49Smrg
5732001f49Smrg		for (s = 0; s < SIZE; s++) {
5832001f49Smrg			for (t = 0; t < SIZE; t++) {
5932001f49Smrg				tex2d[t][s][0] = 0xff;
6032001f49Smrg				tex2d[t][s][1] = 0xff;
6132001f49Smrg				tex2d[t][s][2] = 0xff;
6232001f49Smrg				tex2d[t][s][3] = 0xff;
6332001f49Smrg			}
6432001f49Smrg		}
6532001f49Smrg
6632001f49Smrg		internalFormat = GL_LUMINANCE8;
6732001f49Smrg		sourceFormat = GL_RGBA;
6832001f49Smrg
6932001f49Smrg		glTexImage2D(Target,
7032001f49Smrg			0,
7132001f49Smrg			internalFormat,
7232001f49Smrg			SIZE, SIZE,
7332001f49Smrg			0,
7432001f49Smrg			sourceFormat,
7532001f49Smrg			GL_UNSIGNED_BYTE,
7632001f49Smrg			/* GL_UNSIGNED_INT, */
7732001f49Smrg			tex2d);
7832001f49Smrg
7932001f49Smrg		glEnable(Target);
8032001f49Smrg		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
8132001f49Smrg		glTexParameterf(Target, GL_TEXTURE_WRAP_R, GL_REPEAT);
8232001f49Smrg		glTexParameterf(Target, GL_TEXTURE_MIN_FILTER, Filter);
8332001f49Smrg		glTexParameterf(Target, GL_TEXTURE_MAG_FILTER, Filter);
8432001f49Smrg	}
8532001f49Smrg}
8632001f49Smrg
8732001f49Smrgstatic void Reshape(int width, int height)
8832001f49Smrg{
8932001f49Smrg	glViewport(0, 0, (GLint)width, (GLint)height);
9032001f49Smrg	glMatrixMode(GL_PROJECTION);
9132001f49Smrg	glLoadIdentity();
9232001f49Smrg#if 0
9332001f49Smrg	glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
9432001f49Smrg#else
9532001f49Smrg	glFrustum(-1, 1, -1, 1, 10, 20);
9632001f49Smrg#endif
9732001f49Smrg	glMatrixMode(GL_MODELVIEW);
9832001f49Smrg	glLoadIdentity();
9932001f49Smrg	glTranslatef(0, 0, -15);
10032001f49Smrg}
10132001f49Smrg
10232001f49Smrgstatic void Key(unsigned char key, int x, int y)
10332001f49Smrg{
10432001f49Smrg	switch (key) {
10532001f49Smrg	case 'r':
10632001f49Smrg		Rot += 10.0;
10732001f49Smrg		break;
10832001f49Smrg	case 'R':
10932001f49Smrg		Rot -= 10.0;
11032001f49Smrg		break;
11132001f49Smrg	case 27:
11232001f49Smrg		glutDestroyWindow(win);
11332001f49Smrg		exit(0);
11432001f49Smrg	default:
11532001f49Smrg		return;
11632001f49Smrg	}
11732001f49Smrg	glutPostRedisplay();
11832001f49Smrg}
11932001f49Smrg
12032001f49Smrgstatic void Draw(void)
12132001f49Smrg{
12232001f49Smrg	glClear(GL_COLOR_BUFFER_BIT);
12332001f49Smrg
12432001f49Smrg	glPushMatrix();
12532001f49Smrg	glRotatef(Rot, 0, 1, 0);
12632001f49Smrg
12732001f49Smrg	glBegin(GL_QUADS);
12832001f49Smrg	glTexCoord2f(1,0);
12932001f49Smrg	glVertex3f( 0.9, -0.9, 0.0);
13032001f49Smrg	glTexCoord2f(1,1);
13132001f49Smrg	glVertex3f( 0.9,  0.9, 0.0);
13232001f49Smrg	glTexCoord2f(0,1);
13332001f49Smrg	glVertex3f(-0.9,  0.9, 0.0);
13432001f49Smrg	glTexCoord2f(0,0);
13532001f49Smrg	glVertex3f(-0.9,  -0.9, 0.0);
13632001f49Smrg	glEnd();
13732001f49Smrg
13832001f49Smrg	glPopMatrix();
13932001f49Smrg
14032001f49Smrg	glFlush();
14132001f49Smrg
14232001f49Smrg	if (doubleBuffer) {
14332001f49Smrg		glutSwapBuffers();
14432001f49Smrg	}
14532001f49Smrg}
14632001f49Smrg
14732001f49Smrgstatic GLenum Args(int argc, char **argv)
14832001f49Smrg{
14932001f49Smrg	GLint i;
15032001f49Smrg
15132001f49Smrg	doubleBuffer = GL_FALSE;
15232001f49Smrg
15332001f49Smrg	for (i = 1; i < argc; i++) {
15432001f49Smrg		if (strcmp(argv[i], "-sb") == 0) {
15532001f49Smrg			doubleBuffer = GL_FALSE;
15632001f49Smrg		} else if (strcmp(argv[i], "-db") == 0) {
15732001f49Smrg			doubleBuffer = GL_TRUE;
15832001f49Smrg		} else {
15932001f49Smrg			fprintf(stderr, "%s (Bad option).\n", argv[i]);
16032001f49Smrg			return GL_FALSE;
16132001f49Smrg		}
16232001f49Smrg	}
16332001f49Smrg	return GL_TRUE;
16432001f49Smrg}
16532001f49Smrg
16632001f49Smrgint main(int argc, char **argv)
16732001f49Smrg{
16832001f49Smrg	GLenum type;
16932001f49Smrg
17032001f49Smrg	glutInit(&argc, argv);
17132001f49Smrg
17232001f49Smrg	if (Args(argc, argv) == GL_FALSE) {
17332001f49Smrg		exit(1);
17432001f49Smrg	}
17532001f49Smrg
17632001f49Smrg	glutInitWindowPosition(0, 0);
17732001f49Smrg	glutInitWindowSize(250, 250);
17832001f49Smrg
17932001f49Smrg	type = GLUT_RGB;
18032001f49Smrg	type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
18132001f49Smrg	glutInitDisplayMode(type);
18232001f49Smrg
18332001f49Smrg	win = glutCreateWindow("Tex test");
18432001f49Smrg        glewInit();
18532001f49Smrg	if (!win) {
18632001f49Smrg		exit(1);
18732001f49Smrg	}
18832001f49Smrg
18932001f49Smrg	Init();
19032001f49Smrg
19132001f49Smrg	glutReshapeFunc(Reshape);
19232001f49Smrg	glutKeyboardFunc(Key);
19332001f49Smrg	glutDisplayFunc(Draw);
19432001f49Smrg	glutMainLoop();
19532001f49Smrg	return 0;
19632001f49Smrg}
197