Ian Romanick | 816c0c9 | 2005-02-09 22:16:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright IBM Corporation 2005 |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * on the rights to use, copy, modify, merge, publish, distribute, sub |
| 9 | * license, and/or sell copies of the Software, and to permit persons to whom |
| 10 | * the Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the next |
| 13 | * paragraph) shall be included in all copies or substantial portions of the |
| 14 | * Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 19 | * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 21 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 22 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * \file crossbar.c |
| 27 | * |
| 28 | * Simple test of GL_ARB_texture_env_crossbar functionality. Several squares |
| 29 | * are drawn with different texture combine modes, but all should be rendered |
| 30 | * with the same final color. |
| 31 | * |
| 32 | * \author Ian Romanick <idr@us.ibm.com> |
| 33 | */ |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #include <GL/glut.h> |
| 39 | |
| 40 | static const GLint tests[][8] = { |
| 41 | { 1, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR, |
| 42 | 2, GL_REPLACE, GL_TEXTURE, GL_PRIMARY_COLOR }, |
| 43 | { 3, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR, |
| 44 | 2, GL_SUBTRACT, GL_TEXTURE0, GL_TEXTURE1 }, |
| 45 | { 2, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR, |
| 46 | 2, GL_REPLACE, GL_TEXTURE0, GL_TEXTURE0 }, |
| 47 | { 2, GL_REPLACE, GL_PRIMARY_COLOR, GL_PRIMARY_COLOR, |
| 48 | 1, GL_SUBTRACT, GL_TEXTURE0, GL_TEXTURE1 }, |
| 49 | { 3, GL_ADD, GL_TEXTURE1, GL_TEXTURE1, |
| 50 | 2, GL_MODULATE, GL_TEXTURE1, GL_PREVIOUS }, |
| 51 | { 3, GL_ADD, GL_TEXTURE1, GL_TEXTURE1, |
| 52 | 4, GL_MODULATE, GL_TEXTURE0, GL_PREVIOUS }, |
| 53 | }; |
| 54 | |
| 55 | #define NUM_TESTS (sizeof(tests) / sizeof(tests[0])) |
| 56 | |
| 57 | static int Width = 100 * (NUM_TESTS + 1); |
| 58 | static int Height = 200; |
| 59 | static const GLfloat Near = 5.0, Far = 25.0; |
| 60 | |
| 61 | |
| 62 | static void Display( void ) |
| 63 | { |
| 64 | unsigned i; |
| 65 | |
| 66 | |
| 67 | glClearColor(0.2, 0.2, 0.8, 0); |
| 68 | glClear( GL_COLOR_BUFFER_BIT ); |
| 69 | |
| 70 | glPushMatrix(); |
| 71 | |
| 72 | /* This is the "reference" square. |
| 73 | */ |
| 74 | |
| 75 | glActiveTexture( GL_TEXTURE0 ); |
| 76 | glDisable( GL_TEXTURE_2D ); |
| 77 | glActiveTexture( GL_TEXTURE1 ); |
| 78 | glDisable( GL_TEXTURE_2D ); |
| 79 | |
| 80 | glTranslatef(-(NUM_TESTS * 1.5), 0, 0); |
| 81 | glBegin(GL_QUADS); |
| 82 | glColor3f( 0.5, 0.5, 0.5 ); |
| 83 | glVertex2f(-1, -1); |
| 84 | glVertex2f( 1, -1); |
| 85 | glVertex2f( 1, 1); |
| 86 | glVertex2f(-1, 1); |
| 87 | glEnd(); |
| 88 | |
| 89 | for ( i = 0 ; i < NUM_TESTS ; i++ ) { |
| 90 | glActiveTexture( GL_TEXTURE0 ); |
| 91 | glEnable( GL_TEXTURE_2D ); |
| 92 | glBindTexture( GL_TEXTURE_2D, tests[i][0] ); |
| 93 | glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); |
| 94 | glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, tests[i][1] ); |
| 95 | glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, tests[i][2] ); |
| 96 | glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, tests[i][3] ); |
| 97 | |
| 98 | glActiveTexture( GL_TEXTURE1 ); |
| 99 | glEnable( GL_TEXTURE_2D ); |
| 100 | glBindTexture( GL_TEXTURE_2D, tests[i][4] ); |
| 101 | glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); |
| 102 | glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, tests[i][5] ); |
| 103 | glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB, tests[i][6] ); |
| 104 | glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB, tests[i][7] ); |
| 105 | |
| 106 | glCallList(1); |
| 107 | } |
| 108 | |
| 109 | glPopMatrix(); |
| 110 | |
| 111 | glutSwapBuffers(); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | static void Reshape( int width, int height ) |
| 116 | { |
| 117 | GLfloat ar = (float) width / (float) height; |
| 118 | Width = width; |
| 119 | Height = height; |
| 120 | glViewport( 0, 0, width, height ); |
| 121 | glMatrixMode( GL_PROJECTION ); |
| 122 | glLoadIdentity(); |
| 123 | glFrustum( -ar, ar, -1.0, 1.0, Near, Far ); |
| 124 | glMatrixMode( GL_MODELVIEW ); |
| 125 | glLoadIdentity(); |
| 126 | glTranslatef( 0.0, 0.0, -15.0 ); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | static void Key( unsigned char key, int x, int y ) |
| 131 | { |
| 132 | (void) x; |
| 133 | (void) y; |
| 134 | switch (key) { |
| 135 | case 27: |
| 136 | exit(0); |
| 137 | break; |
| 138 | } |
| 139 | glutPostRedisplay(); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | static void Init( void ) |
| 144 | { |
| 145 | const char * const ver_string = (const char * const) |
| 146 | glGetString( GL_VERSION ); |
| 147 | float ver = strtof( ver_string, NULL ); |
| 148 | GLint tex_units; |
| 149 | GLint temp[ 256 ]; |
| 150 | |
| 151 | |
| 152 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 153 | printf("GL_VERSION = %s\n", ver_string); |
| 154 | |
| 155 | if ( (!glutExtensionSupported("GL_ARB_multitexture") |
| 156 | && (ver < 1.3)) |
| 157 | || (!glutExtensionSupported("GL_ARB_texture_env_combine") |
| 158 | && !glutExtensionSupported("GL_EXT_texture_env_combine") |
| 159 | && (ver < 1.3)) |
| 160 | || (!glutExtensionSupported("GL_ARB_texture_env_crossbar") |
| 161 | && !glutExtensionSupported("GL_NV_texture_env_combine4") |
| 162 | && (ver < 1.4)) ) { |
| 163 | printf("\nSorry, this program requires GL_ARB_multitexture and either\n" |
| 164 | "GL_ARB_texture_env_combine or GL_EXT_texture_env_combine (or OpenGL 1.3).\n" |
| 165 | "Either GL_ARB_texture_env_crossbar or GL_NV_texture_env_combine4 (or\n" |
| 166 | "OpenGL 1.4) are also required.\n"); |
| 167 | exit(1); |
| 168 | } |
| 169 | |
| 170 | glGetIntegerv( GL_MAX_TEXTURE_UNITS, & tex_units ); |
| 171 | if ( tex_units < 2 ) { |
| 172 | printf("\nSorry, this program requires at least 2 texture units.\n"); |
| 173 | exit(1); |
| 174 | } |
| 175 | |
| 176 | printf("\nAll %u squares should be the same color.\n", NUM_TESTS + 1); |
| 177 | |
| 178 | (void) memset( temp, 0x00, sizeof( temp ) ); |
| 179 | glBindTexture( GL_TEXTURE_2D, 1 ); |
| 180 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); |
| 181 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); |
| 182 | glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, |
| 183 | GL_RGBA, GL_UNSIGNED_BYTE, temp ); |
| 184 | |
| 185 | (void) memset( temp, 0x7f, sizeof( temp ) ); |
| 186 | glBindTexture( GL_TEXTURE_2D, 2 ); |
| 187 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); |
| 188 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); |
| 189 | glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, |
| 190 | GL_RGBA, GL_UNSIGNED_BYTE, temp ); |
| 191 | |
| 192 | (void) memset( temp, 0xff, sizeof( temp ) ); |
| 193 | glBindTexture( GL_TEXTURE_2D, 3 ); |
| 194 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); |
| 195 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); |
| 196 | glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, |
| 197 | GL_RGBA, GL_UNSIGNED_BYTE, temp ); |
| 198 | |
| 199 | (void) memset( temp, 0x3f, sizeof( temp ) ); |
| 200 | glBindTexture( GL_TEXTURE_2D, 4 ); |
| 201 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); |
| 202 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); |
| 203 | glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, |
| 204 | GL_RGBA, GL_UNSIGNED_BYTE, temp ); |
| 205 | |
| 206 | |
| 207 | glNewList( 1, GL_COMPILE ); |
| 208 | glTranslatef(3.0, 0, 0); |
| 209 | glBegin(GL_QUADS); |
| 210 | glColor3f( 0.9, 0.0, 0.0 ); |
| 211 | glMultiTexCoord2f( GL_TEXTURE0, 0.5, 0.5 ); |
| 212 | glMultiTexCoord2f( GL_TEXTURE1, 0.5, 0.5 ); |
| 213 | glVertex2f(-1, -1); |
| 214 | glVertex2f( 1, -1); |
| 215 | glVertex2f( 1, 1); |
| 216 | glVertex2f(-1, 1); |
| 217 | glEnd(); |
| 218 | glEndList(); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | int main( int argc, char *argv[] ) |
| 223 | { |
| 224 | glutInit( &argc, argv ); |
| 225 | glutInitWindowPosition( 0, 0 ); |
| 226 | glutInitWindowSize( Width, Height ); |
| 227 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 228 | glutCreateWindow( "GL_ARB_texture_env_crossbar test" ); |
| 229 | glutReshapeFunc( Reshape ); |
| 230 | glutKeyboardFunc( Key ); |
| 231 | glutDisplayFunc( Display ); |
| 232 | Init(); |
| 233 | glutMainLoop(); |
| 234 | return 0; |
| 235 | } |