Ian Romanick | d2f18ec | 2004-05-11 17:48:33 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * (C) Copyright IBM Corporation 2004 |
| 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 blendsquare.c |
| 27 | * |
| 28 | * Simple test of GL_NV_blend_square functionality. Three squares are drawn |
| 29 | * with different blending modes, but all should be rendered with the same |
| 30 | * final color. |
| 31 | * |
| 32 | * \author Ian Romanick <idr@us.ibm.com> |
| 33 | */ |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <GL/glut.h> |
| 38 | |
| 39 | static int Width = 300; |
| 40 | static int Height = 200; |
| 41 | static const GLfloat Near = 5.0, Far = 25.0; |
| 42 | |
| 43 | |
| 44 | static void Display( void ) |
| 45 | { |
| 46 | glClearColor(0.2, 0.2, 0.8, 0); |
| 47 | glClear( GL_COLOR_BUFFER_BIT ); |
| 48 | |
| 49 | glPushMatrix(); |
| 50 | glTranslatef(-3.0, 0, 0); |
| 51 | glBlendFunc( GL_ONE, GL_ZERO ); |
| 52 | glBegin(GL_QUADS); |
| 53 | glColor3f( 0.5 * 0.5, 0.5 * 0.5, 0.5 * 0.5 ); |
| 54 | glVertex2f(-1, -1); |
| 55 | glVertex2f( 1, -1); |
| 56 | glVertex2f( 1, 1); |
| 57 | glVertex2f(-1, 1); |
| 58 | glEnd(); |
| 59 | |
| 60 | glTranslatef(3.0, 0, 0); |
| 61 | glBlendFunc( GL_SRC_COLOR, GL_ZERO ); |
| 62 | glBegin(GL_QUADS); |
| 63 | glColor3f( 0.5, 0.5, 0.5 ); |
| 64 | glVertex2f(-1, -1); |
| 65 | glVertex2f( 1, -1); |
| 66 | glVertex2f( 1, 1); |
| 67 | glVertex2f(-1, 1); |
| 68 | glEnd(); |
| 69 | |
| 70 | glTranslatef(3.0, 0, 0); |
| 71 | glBlendFunc( GL_ONE, GL_ZERO ); |
| 72 | glBegin(GL_QUADS); |
| 73 | glVertex2f(-1, -1); |
| 74 | glVertex2f( 1, -1); |
| 75 | glVertex2f( 1, 1); |
| 76 | glVertex2f(-1, 1); |
| 77 | glEnd(); |
| 78 | |
| 79 | glBlendFunc( GL_ZERO, GL_DST_COLOR ); |
| 80 | glBegin(GL_QUADS); |
| 81 | glVertex2f(-1, -1); |
| 82 | glVertex2f( 1, -1); |
| 83 | glVertex2f( 1, 1); |
| 84 | glVertex2f(-1, 1); |
| 85 | glEnd(); |
| 86 | |
| 87 | glPopMatrix(); |
| 88 | |
| 89 | glutSwapBuffers(); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static void Reshape( int width, int height ) |
| 94 | { |
| 95 | GLfloat ar = (float) width / (float) height; |
| 96 | Width = width; |
| 97 | Height = height; |
| 98 | glViewport( 0, 0, width, height ); |
| 99 | glMatrixMode( GL_PROJECTION ); |
| 100 | glLoadIdentity(); |
| 101 | glFrustum( -ar, ar, -1.0, 1.0, Near, Far ); |
| 102 | glMatrixMode( GL_MODELVIEW ); |
| 103 | glLoadIdentity(); |
| 104 | glTranslatef( 0.0, 0.0, -15.0 ); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static void Key( unsigned char key, int x, int y ) |
| 109 | { |
| 110 | (void) x; |
| 111 | (void) y; |
| 112 | switch (key) { |
| 113 | case 27: |
| 114 | exit(0); |
| 115 | break; |
| 116 | } |
| 117 | glutPostRedisplay(); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | static void Init( void ) |
| 122 | { |
| 123 | const char * const ver_string = (const char * const) glGetString( GL_VERSION ); |
| 124 | const double version = strtod( ver_string, NULL ); |
| 125 | |
| 126 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 127 | printf("GL_VERSION = %s\n", ver_string); |
| 128 | |
| 129 | if ( (version < 1.4) && !glutExtensionSupported("GL_NV_blend_square")) { |
| 130 | printf("Sorry, this program requires either OpenGL 1.4 or GL_NV_blend_square\n"); |
| 131 | exit(1); |
| 132 | } |
| 133 | |
| 134 | printf("All 3 squares should be the same color.\n"); |
| 135 | glEnable( GL_BLEND ); |
| 136 | glBlendEquation( GL_FUNC_ADD ); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | int main( int argc, char *argv[] ) |
| 141 | { |
| 142 | glutInit( &argc, argv ); |
| 143 | glutInitWindowPosition( 0, 0 ); |
| 144 | glutInitWindowSize( Width, Height ); |
| 145 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 146 | glutCreateWindow( "GL_NV_blend_square test" ); |
| 147 | glutReshapeFunc( Reshape ); |
| 148 | glutKeyboardFunc( Key ); |
| 149 | glutDisplayFunc( Display ); |
| 150 | Init(); |
| 151 | glutMainLoop(); |
| 152 | return 0; |
| 153 | } |