Brian Paul | 66fa33e | 2002-12-03 03:13:17 +0000 | [diff] [blame^] | 1 | /* $Id: winpos.c,v 1.6 2002/12/03 03:13:17 brianp Exp $ */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Example of how to use the GL_MESA_window_pos extension. |
| 5 | * Brian Paul This file is in the public domain. |
| 6 | */ |
| 7 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 8 | #include <math.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <stdio.h> |
Karl Schultz | 164ce12 | 2002-01-16 00:48:43 +0000 | [diff] [blame] | 12 | #ifdef _WIN32 |
| 13 | #include <windows.h> |
| 14 | #endif |
Brian Paul | 66fa33e | 2002-12-03 03:13:17 +0000 | [diff] [blame^] | 15 | #define GL_GLEXT_PROTOTYPES |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 16 | #include "GL/glut.h" |
| 17 | |
pesco | d1ff1f6 | 2000-12-24 22:53:54 +0000 | [diff] [blame] | 18 | #include "readtex.c" /* a hack, I know */ |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 19 | |
| 20 | #define IMAGE_FILE "../images/girl.rgb" |
| 21 | |
| 22 | |
| 23 | #ifndef M_PI |
| 24 | # define M_PI 3.14159265 |
| 25 | #endif |
| 26 | |
| 27 | |
| 28 | |
| 29 | static GLubyte *Image; |
| 30 | static int ImgWidth, ImgHeight; |
| 31 | static GLenum ImgFormat; |
| 32 | |
Brian Paul | 66fa33e | 2002-12-03 03:13:17 +0000 | [diff] [blame^] | 33 | static void (*WindowPosFunc)(GLfloat x, GLfloat y); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | static void draw( void ) |
| 37 | { |
| 38 | GLfloat angle; |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 39 | |
| 40 | glClear( GL_COLOR_BUFFER_BIT ); |
| 41 | |
| 42 | for (angle = -45.0; angle <= 135.0; angle += 10.0) { |
| 43 | GLfloat x = 50.0 + 200.0 * cos( angle * M_PI / 180.0 ); |
| 44 | GLfloat y = 50.0 + 200.0 * sin( angle * M_PI / 180.0 ); |
| 45 | |
| 46 | /* Don't need to worry about the modelview or projection matrices!!! */ |
Brian Paul | 66fa33e | 2002-12-03 03:13:17 +0000 | [diff] [blame^] | 47 | (*WindowPosFunc)( x, y ); |
| 48 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 49 | glDrawPixels( ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image ); |
| 50 | } |
Brian Paul | 66fa33e | 2002-12-03 03:13:17 +0000 | [diff] [blame^] | 51 | glFinish(); |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 55 | static void key( unsigned char key, int x, int y ) |
| 56 | { |
| 57 | (void) x; |
| 58 | (void) y; |
| 59 | switch (key) { |
| 60 | case 27: |
| 61 | exit(0); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 66 | /* new window size or exposure */ |
| 67 | static void reshape( int width, int height ) |
| 68 | { |
| 69 | glViewport(0, 0, (GLint)width, (GLint)height); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static void init( void ) |
| 74 | { |
Brian Paul | 66fa33e | 2002-12-03 03:13:17 +0000 | [diff] [blame^] | 75 | #ifdef GL_ARB_window_pos |
| 76 | if (glutExtensionSupported("GL_ARB_window_pos")) { |
| 77 | printf("Using GL_ARB_window_pos\n"); |
| 78 | WindowPosFunc = &glWindowPos2fARB; |
| 79 | } |
| 80 | else |
| 81 | #elif defined(GL_ARB_window_pos) |
| 82 | if (glutExtensionSupported("GL_MESA_window_pos")) { |
| 83 | printf("Using GL_MESA_window_pos\n"); |
| 84 | WindowPosFunc = &glWindowPos2fMESA; |
| 85 | } |
| 86 | else |
| 87 | #endif |
| 88 | { |
| 89 | printf("Sorry, GL_ARB/MESA_window_pos extension not available.\n"); |
| 90 | exit(1); |
| 91 | } |
| 92 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 93 | Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat ); |
| 94 | if (!Image) { |
| 95 | printf("Couldn't read %s\n", IMAGE_FILE); |
| 96 | exit(0); |
| 97 | } |
| 98 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 99 | } |
| 100 | |
| 101 | |
jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 102 | int main( int argc, char *argv[] ) |
| 103 | { |
| 104 | glutInitWindowPosition(0, 0); |
| 105 | glutInitWindowSize(500, 500); |
| 106 | glutInitDisplayMode( GLUT_RGB ); |
| 107 | |
| 108 | if (glutCreateWindow("winpos") <= 0) { |
| 109 | exit(0); |
| 110 | } |
| 111 | |
| 112 | init(); |
| 113 | |
| 114 | glutReshapeFunc( reshape ); |
| 115 | glutKeyboardFunc( key ); |
| 116 | glutDisplayFunc( draw ); |
| 117 | glutMainLoop(); |
| 118 | return 0; |
| 119 | } |