1 2#include <stdio.h> 3#include <string.h> 4#include <stdlib.h> 5#include <GL/glew.h> 6#include "glut_wrap.h" 7 8 9static void Init(void) 10{ 11 static const char *modulate2D = 12 "!!ARBfp1.0\n" 13 "MUL result.depth.z, fragment.color.z, {.1}.x; \n" 14 "MOV result.color.xy, fragment.color; \n" 15 "END" 16 ; 17 GLuint modulateProg; 18 19 if (!GLEW_ARB_fragment_program) { 20 printf("Error: GL_ARB_fragment_program not supported!\n"); 21 exit(1); 22 } 23 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 24 25 /* Setup the fragment program */ 26 glGenProgramsARB(1, &modulateProg); 27 glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, modulateProg); 28 glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, 29 strlen(modulate2D), (const GLubyte *)modulate2D); 30 31 printf("glGetError = 0x%x\n", (int) glGetError()); 32 printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n", 33 (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB)); 34 35 glEnable(GL_FRAGMENT_PROGRAM_ARB); 36 37 glClearColor(.3, .3, .3, 0); 38} 39 40static void Reshape(int width, int height) 41{ 42 43 glViewport(0, 0, (GLint)width, (GLint)height); 44 45 glMatrixMode(GL_PROJECTION); 46 glLoadIdentity(); 47 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); 48 glMatrixMode(GL_MODELVIEW); 49} 50 51static void Key(unsigned char key, int x, int y) 52{ 53 54 switch (key) { 55 case 27: 56 exit(1); 57 default: 58 break; 59 } 60 61 glutPostRedisplay(); 62} 63 64static void Draw(void) 65{ 66 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 67 glEnable(GL_DEPTH_TEST); 68 69 70 glBegin(GL_TRIANGLES); 71 glColor4f(.8,0,.5,0); 72 glVertex3f( 0.9, -0.9, -30.0); 73 glVertex3f( 0.9, 0.9, -30.0); 74 glVertex3f(-0.9, 0.0, -30.0); 75 76 glColor4f(0,.8,.7,0); 77 glVertex3f(-0.9, -0.9, -40.0); 78 glColor4f(0,.8,.7,0); 79 glVertex3f(-0.9, 0.9, -40.0); 80 glColor4f(0,.8,.3,0); 81 glVertex3f( 0.9, 0.0, -40.0); 82 glEnd(); 83 84 glFlush(); 85} 86 87 88int main(int argc, char **argv) 89{ 90 glutInit(&argc, argv); 91 92 glutInitWindowPosition(0, 0); glutInitWindowSize( 300, 300); 93 94 glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE); 95 96 if (glutCreateWindow("Depth Test") == GL_FALSE) { 97 exit(1); 98 } 99 100 glewInit(); 101 102 Init(); 103 104 glutReshapeFunc(Reshape); 105 glutKeyboardFunc(Key); 106 glutDisplayFunc(Draw); 107 glutMainLoop(); 108 return 0; 109} 110