Roland Scheidegger | 45b8e76 | 2009-03-12 15:07:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009 VMware, Inc. |
| 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 | * Simple test for testing ATI_envmap_bumpmap support. |
| 27 | */ |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <math.h> |
| 33 | #include <GL/glut.h> |
| 34 | |
| 35 | #include "readtex.h" |
| 36 | |
| 37 | static PFNGLGETTEXBUMPPARAMETERFVATIPROC glGetTexBumpParameterfvATI_func = NULL; |
| 38 | static PFNGLGETTEXBUMPPARAMETERIVATIPROC glGetTexBumpParameterivATI_func = NULL; |
| 39 | static PFNGLTEXBUMPPARAMETERFVATIPROC glTexBumpParameterfvATI_func = NULL; |
| 40 | |
| 41 | static const char *TexFile = "../images/arch.rgb"; |
| 42 | |
| 43 | static const GLfloat Near = 5.0, Far = 25.0; |
| 44 | |
| 45 | static void Display( void ) |
| 46 | { |
| 47 | /* together with the construction of dudv map, do fixed translation |
| 48 | in y direction (up), some cosine deformation in x and more |
| 49 | deformation in y dir */ |
| 50 | GLfloat bumpMatrix[4] = {0.1, 0.0, 0.2, 0.1}; |
| 51 | |
| 52 | |
| 53 | glClearColor(0.2, 0.2, 0.8, 0); |
| 54 | glClear( GL_COLOR_BUFFER_BIT ); |
| 55 | |
| 56 | glPushMatrix(); |
| 57 | |
| 58 | /* this is the base map */ |
| 59 | glActiveTexture( GL_TEXTURE0 ); |
| 60 | glEnable( GL_TEXTURE_2D ); |
| 61 | glBindTexture( GL_TEXTURE_2D, 1 ); |
| 62 | glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); |
| 63 | glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE ); |
| 64 | glTexEnvf( GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE ); |
| 65 | |
| 66 | /* bump map */ |
| 67 | glActiveTexture( GL_TEXTURE1 ); |
| 68 | glEnable( GL_TEXTURE_2D ); |
| 69 | glBindTexture( GL_TEXTURE_2D, 2 ); |
| 70 | glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); |
| 71 | glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_BUMP_ENVMAP_ATI ); |
| 72 | glTexEnvf( GL_TEXTURE_ENV, GL_BUMP_TARGET_ATI, GL_TEXTURE0); |
| 73 | |
| 74 | glTexBumpParameterfvATI_func(GL_BUMP_ROT_MATRIX_ATI, bumpMatrix); |
| 75 | |
| 76 | glCallList(1); |
| 77 | |
| 78 | glPopMatrix(); |
| 79 | |
| 80 | glutSwapBuffers(); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static void Reshape( int width, int height ) |
| 85 | { |
| 86 | GLfloat ar = (float) width / (float) height; |
| 87 | glViewport( 0, 0, width, height ); |
| 88 | glMatrixMode( GL_PROJECTION ); |
| 89 | glLoadIdentity(); |
| 90 | glFrustum( -ar, ar, -1.0, 1.0, Near, Far ); |
| 91 | glMatrixMode( GL_MODELVIEW ); |
| 92 | glLoadIdentity(); |
| 93 | glTranslatef( 0.0, 0.0, -6.0 ); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static void Key( unsigned char key, int x, int y ) |
| 98 | { |
| 99 | (void) x; |
| 100 | (void) y; |
| 101 | switch (key) { |
| 102 | case 27: |
| 103 | exit(0); |
| 104 | break; |
| 105 | } |
| 106 | glutPostRedisplay(); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static void Init( void ) |
| 111 | { |
| 112 | const char * const ver_string = (const char * const) |
| 113 | glGetString( GL_VERSION ); |
| 114 | GLfloat temp[16][16][2]; |
| 115 | GLubyte *image = NULL; |
| 116 | GLint imgWidth, imgHeight; |
| 117 | GLenum imgFormat; |
| 118 | GLint i,j; |
| 119 | GLint param, paramArray[16]; |
| 120 | GLfloat paramMat[4]; |
| 121 | |
| 122 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 123 | printf("GL_VERSION = %s\n", ver_string); |
| 124 | |
| 125 | if ( !glutExtensionSupported("GL_ATI_envmap_bumpmap")) { |
| 126 | printf("\nSorry, this program requires GL_ATI_envmap_bumpmap\n"); |
| 127 | exit(1); |
| 128 | } |
| 129 | |
| 130 | glGetTexBumpParameterfvATI_func = glutGetProcAddress("glGetTexBumpParameterfvATI"); |
| 131 | glGetTexBumpParameterivATI_func = glutGetProcAddress("glGetTexBumpParameterivATI"); |
| 132 | glTexBumpParameterfvATI_func = glutGetProcAddress("glTexBumpParameterfvATI"); |
| 133 | |
| 134 | glGetTexBumpParameterivATI_func(GL_BUMP_ROT_MATRIX_SIZE_ATI, ¶m); |
| 135 | printf("BUMP_ROT_MATRIX_SIZE_ATI = %d\n", param); |
| 136 | glGetTexBumpParameterivATI_func(GL_BUMP_NUM_TEX_UNITS_ATI, ¶m); |
| 137 | printf("BUMP_NUM_TEX_UNITS_ATI = %d\n", param); |
| 138 | glGetTexBumpParameterfvATI_func(GL_BUMP_ROT_MATRIX_ATI, paramMat); |
| 139 | printf("initial rot matrix %f %f %f %f\n", paramMat[0], paramMat[1], paramMat[2], paramMat[3]); |
| 140 | glGetTexBumpParameterivATI_func(GL_BUMP_TEX_UNITS_ATI, paramArray); |
| 141 | printf("units supporting bump mapping: "); |
| 142 | for (i = 0; i < param; i++) |
| 143 | printf("%d ", paramArray[i] - GL_TEXTURE0); |
| 144 | printf("\n"); |
| 145 | |
| 146 | image = LoadRGBImage(TexFile, &imgWidth, &imgHeight, &imgFormat); |
| 147 | if (!image) { |
| 148 | printf("Couldn't read %s\n", TexFile); |
| 149 | exit(0); |
| 150 | } |
| 151 | |
| 152 | glBindTexture( GL_TEXTURE_2D, 1 ); |
| 153 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); |
| 154 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); |
| 155 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); |
| 156 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); |
| 157 | glTexImage2D( GL_TEXTURE_2D, 0, imgFormat, imgWidth, imgHeight, 0, |
| 158 | imgFormat, GL_UNSIGNED_BYTE, image ); |
| 159 | |
| 160 | for (j = 0; j < 16; j++) { |
| 161 | for (i = 0; i < 16; i++) { |
| 162 | temp[j][i][0] = cos((float)(i) * 3.1415 / 16.0); |
| 163 | temp[j][i][1] = -0.5; |
| 164 | } |
| 165 | } |
| 166 | glBindTexture( GL_TEXTURE_2D, 2 ); |
| 167 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); |
| 168 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); |
| 169 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); |
| 170 | glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); |
| 171 | glTexImage2D( GL_TEXTURE_2D, 0, GL_DU8DV8_ATI, 16, 16, 0, |
| 172 | GL_DUDV_ATI, GL_FLOAT, temp ); |
| 173 | |
| 174 | |
| 175 | glNewList( 1, GL_COMPILE ); |
| 176 | glBegin(GL_QUADS); |
| 177 | glColor3f( 0.9, 0.0, 0.0 ); |
| 178 | glMultiTexCoord2f( GL_TEXTURE0, 0.0, 0.0 ); |
| 179 | glMultiTexCoord2f( GL_TEXTURE1, 0.0, 0.0 ); |
| 180 | glVertex2f(-1, -1); |
| 181 | glMultiTexCoord2f( GL_TEXTURE0, 1.0, 0.0 ); |
| 182 | glMultiTexCoord2f( GL_TEXTURE1, 1.0, 0.0 ); |
| 183 | glVertex2f( 1, -1); |
| 184 | glMultiTexCoord2f( GL_TEXTURE0, 1.0, 1.0 ); |
| 185 | glMultiTexCoord2f( GL_TEXTURE1, 1.0, 1.0 ); |
| 186 | glVertex2f( 1, 1); |
| 187 | glMultiTexCoord2f( GL_TEXTURE0, 0.0, 1.0 ); |
| 188 | glMultiTexCoord2f( GL_TEXTURE1, 0.0, 1.0 ); |
| 189 | glVertex2f(-1, 1); |
| 190 | glEnd(); |
| 191 | glEndList(); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | int main( int argc, char *argv[] ) |
| 196 | { |
| 197 | glutInit( &argc, argv ); |
| 198 | glutInitWindowPosition( 0, 0 ); |
| 199 | glutInitWindowSize( 400, 400 ); |
| 200 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 201 | glutCreateWindow( "GL_ATI_envmap_bumpmap test" ); |
| 202 | glutReshapeFunc( Reshape ); |
| 203 | glutKeyboardFunc( Key ); |
| 204 | glutDisplayFunc( Display ); |
| 205 | Init(); |
| 206 | glutMainLoop(); |
| 207 | return 0; |
| 208 | } |