Merge branch 'master' of git+ssh://znh@git.freedesktop.org/git/mesa/mesa into 965...
[mesa.git] / progs / demos / winpos.c
1
2 /*
3 * Example of how to use the GL_MESA_window_pos extension.
4 * Brian Paul This file is in the public domain.
5 */
6
7 #include <math.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #ifdef _WIN32
12 #include <windows.h>
13 #endif
14 #define GL_GLEXT_PROTOTYPES
15 #include "GL/glut.h"
16
17 #include "readtex.h"
18
19 #define IMAGE_FILE "../images/girl.rgb"
20
21
22 #ifndef M_PI
23 # define M_PI 3.14159265
24 #endif
25
26
27
28 static GLubyte *Image;
29 static int ImgWidth, ImgHeight;
30 static GLenum ImgFormat;
31
32 typedef void (APIENTRY * PFNWINDOWPOSFUNC)(GLfloat x, GLfloat y);
33 static PFNWINDOWPOSFUNC WindowPosFunc;
34
35 static void draw( void )
36 {
37 GLfloat angle;
38
39 glClear( GL_COLOR_BUFFER_BIT );
40
41 for (angle = -45.0; angle <= 135.0; angle += 10.0) {
42 GLfloat x = 50.0 + 200.0 * cos( angle * M_PI / 180.0 );
43 GLfloat y = 50.0 + 200.0 * sin( angle * M_PI / 180.0 );
44
45 /* Don't need to worry about the modelview or projection matrices!!! */
46 (*WindowPosFunc)( x, y );
47
48 glDrawPixels( ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image );
49 }
50 glFinish();
51 }
52
53
54 static void key( unsigned char key, int x, int y )
55 {
56 (void) x;
57 (void) y;
58 switch (key) {
59 case 27:
60 exit(0);
61 }
62 }
63
64
65 /* new window size or exposure */
66 static void reshape( int width, int height )
67 {
68 glViewport(0, 0, (GLint)width, (GLint)height);
69 }
70
71
72 static void init( void )
73 {
74 #ifdef GL_ARB_window_pos
75 if (glutExtensionSupported("GL_ARB_window_pos")) {
76 printf("Using GL_ARB_window_pos\n");
77 WindowPosFunc = &glWindowPos2fARB;
78 }
79 else
80 #elif defined(GL_MESA_window_pos)
81 if (glutExtensionSupported("GL_MESA_window_pos")) {
82 printf("Using GL_MESA_window_pos\n");
83 WindowPosFunc = &glWindowPos2fMESA;
84 }
85 else
86 #endif
87 {
88 printf("Sorry, GL_ARB/MESA_window_pos extension not available.\n");
89 exit(1);
90 }
91
92 Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
93 if (!Image) {
94 printf("Couldn't read %s\n", IMAGE_FILE);
95 exit(0);
96 }
97 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
98 }
99
100
101 int main( int argc, char *argv[] )
102 {
103 glutInit(&argc, argv);
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 }