Keith Whitwell | a90909e | 2005-10-20 21:40:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc. |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that (i) the above copyright notices and this permission notice appear in |
| 7 | * all copies of the software and related documentation, and (ii) the name of |
| 8 | * Silicon Graphics may not be used in any advertising or |
| 9 | * publicity relating to the software without the specific, prior written |
| 10 | * permission of Silicon Graphics. |
| 11 | * |
| 12 | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF |
| 13 | * ANY KIND, |
| 14 | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 15 | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 16 | * |
| 17 | * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR |
| 18 | * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 19 | * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 20 | * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 21 | * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 22 | * OF THIS SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include <assert.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #define GL_GLEXT_PROTOTYPES |
| 30 | #include <GL/glut.h> |
| 31 | |
| 32 | #include "readtex.c" |
| 33 | |
| 34 | |
| 35 | #define TEXTURE_FILE "../images/girl.rgb" |
| 36 | |
| 37 | |
| 38 | GLenum doubleBuffer; |
| 39 | |
| 40 | static void Init( void ) |
| 41 | { |
| 42 | static const char *modulate2D = |
| 43 | "!!ARBfp1.0\n" |
| 44 | "TEX result.color, fragment.color, texture[0], 2D; \n" |
| 45 | "END" |
| 46 | ; |
| 47 | GLuint modulateProg; |
| 48 | GLuint Texture; |
| 49 | |
| 50 | if (!glutExtensionSupported("GL_ARB_fragment_program")) { |
| 51 | printf("Error: GL_ARB_fragment_program not supported!\n"); |
| 52 | exit(1); |
| 53 | } |
| 54 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 55 | |
| 56 | /* Setup the fragment program */ |
| 57 | glGenProgramsARB(1, &modulateProg); |
| 58 | glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, modulateProg); |
| 59 | glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, |
| 60 | strlen(modulate2D), (const GLubyte *)modulate2D); |
| 61 | |
| 62 | printf("glGetError = 0x%x\n", (int) glGetError()); |
| 63 | printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n", |
| 64 | (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB)); |
| 65 | assert(glIsProgramARB(modulateProg)); |
| 66 | |
| 67 | glEnable(GL_FRAGMENT_PROGRAM_ARB); |
| 68 | |
| 69 | /* Load texture */ |
| 70 | glGenTextures(1, &Texture); |
| 71 | glBindTexture(GL_TEXTURE_2D, Texture); |
| 72 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 73 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 74 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 75 | if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) { |
| 76 | printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE); |
| 77 | exit(1); |
| 78 | } |
| 79 | /* XXX this enable shouldn't really be needed!!! */ |
| 80 | glEnable(GL_TEXTURE_2D); |
| 81 | |
| 82 | glClearColor(.3, .3, .3, 0); |
| 83 | } |
| 84 | |
| 85 | static void Reshape(int width, int height) |
| 86 | { |
| 87 | |
| 88 | glViewport(0, 0, (GLint)width, (GLint)height); |
| 89 | |
| 90 | glMatrixMode(GL_PROJECTION); |
| 91 | glLoadIdentity(); |
| 92 | glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); |
| 93 | glMatrixMode(GL_MODELVIEW); |
| 94 | } |
| 95 | |
| 96 | static void Key(unsigned char key, int x, int y) |
| 97 | { |
| 98 | |
| 99 | switch (key) { |
| 100 | case 27: |
| 101 | exit(1); |
| 102 | default: |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | glutPostRedisplay(); |
| 107 | } |
| 108 | |
| 109 | static void Draw(void) |
| 110 | { |
| 111 | glClear(GL_COLOR_BUFFER_BIT); |
| 112 | |
| 113 | glBegin(GL_TRIANGLES); |
| 114 | glColor3f(0,0,1); |
| 115 | /* glTexCoord2f(1, 0); */ |
| 116 | glVertex3f( 0.9, -0.9, -30.0); |
| 117 | glColor3f(1,0,0); |
| 118 | /* glTexCoord2f(1, 1); */ |
| 119 | glVertex3f( 0.9, 0.9, -30.0); |
| 120 | glColor3f(0,1,0); |
| 121 | /* glTexCoord2f(0, .5); */ |
| 122 | glVertex3f(-0.9, 0.0, -30.0); |
| 123 | glEnd(); |
| 124 | |
| 125 | glFlush(); |
| 126 | |
| 127 | if (doubleBuffer) { |
| 128 | glutSwapBuffers(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static GLenum Args(int argc, char **argv) |
| 133 | { |
| 134 | GLint i; |
| 135 | |
| 136 | doubleBuffer = GL_FALSE; |
| 137 | |
| 138 | for (i = 1; i < argc; i++) { |
| 139 | if (strcmp(argv[i], "-sb") == 0) { |
| 140 | doubleBuffer = GL_FALSE; |
| 141 | } else if (strcmp(argv[i], "-db") == 0) { |
| 142 | doubleBuffer = GL_TRUE; |
| 143 | } else { |
| 144 | fprintf(stderr, "%s (Bad option).\n", argv[i]); |
| 145 | return GL_FALSE; |
| 146 | } |
| 147 | } |
| 148 | return GL_TRUE; |
| 149 | } |
| 150 | |
| 151 | int main(int argc, char **argv) |
| 152 | { |
| 153 | GLenum type; |
| 154 | |
| 155 | glutInit(&argc, argv); |
| 156 | |
| 157 | if (Args(argc, argv) == GL_FALSE) { |
| 158 | exit(1); |
| 159 | } |
| 160 | |
| 161 | glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250); |
| 162 | |
| 163 | type = GLUT_RGB; |
| 164 | type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; |
| 165 | glutInitDisplayMode(type); |
| 166 | |
| 167 | if (glutCreateWindow("First Tri") == GL_FALSE) { |
| 168 | exit(1); |
| 169 | } |
| 170 | |
| 171 | Init(); |
| 172 | |
| 173 | glutReshapeFunc(Reshape); |
| 174 | glutKeyboardFunc(Key); |
| 175 | glutDisplayFunc(Draw); |
| 176 | glutMainLoop(); |
| 177 | return 0; |
| 178 | } |