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 #include "glad/glad.h"
15 #include "glut_wrap.h"
16 
17 #include "readtex.h"
18 
19 #define IMAGE_FILE DEMOS_DATA_DIR "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 static PFNGLWINDOWPOS2FPROC WindowPosFunc;
33 
draw(void)34 static void draw( void )
35 {
36    GLfloat angle;
37 
38    glClear( GL_COLOR_BUFFER_BIT );
39 
40    for (angle = -45.0; angle <= 135.0; angle += 10.0) {
41       GLfloat x = 50.0 + 200.0 * cos( angle * M_PI / 180.0 );
42       GLfloat y = 50.0 + 200.0 * sin( angle * M_PI / 180.0 );
43 
44       /* Don't need to worry about the modelview or projection matrices!!! */
45       (*WindowPosFunc)( x, y );
46 
47       glDrawPixels( ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image );
48    }
49    glFinish();
50 }
51 
52 
key(unsigned char key,int x,int y)53 static void key( unsigned char key, int x, int y )
54 {
55    (void) x;
56    (void) y;
57    switch (key) {
58       case 27:
59          exit(0);
60    }
61 }
62 
63 
64 /* new window size or exposure */
reshape(int width,int height)65 static void reshape( int width, int height )
66 {
67    glViewport(0, 0, (GLint)width, (GLint)height);
68 }
69 
70 
init(void)71 static void init( void )
72 {
73    if (GLAD_GL_ARB_window_pos) {
74       printf("Using GL_ARB_window_pos\n");
75       WindowPosFunc = glWindowPos2fARB;
76    }
77    else
78    if (GLAD_GL_MESA_window_pos) {
79       printf("Using GL_MESA_window_pos\n");
80       WindowPosFunc = glWindowPos2fMESA;
81    }
82    else
83    {
84       printf("Sorry, GL_ARB/MESA_window_pos extension not available.\n");
85       exit(1);
86    }
87 
88    Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
89    if (!Image) {
90       printf("Couldn't read %s\n", IMAGE_FILE);
91       exit(0);
92    }
93    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
94 }
95 
96 
main(int argc,char * argv[])97 int main( int argc, char *argv[] )
98 {
99    glutInitWindowSize(500, 500);
100    glutInit(&argc, argv);
101    glutInitDisplayMode( GLUT_RGB );
102 
103    if (glutCreateWindow("winpos") <= 0) {
104       exit(0);
105    }
106 
107    gladLoadGL();
108 
109    init();
110 
111    glutReshapeFunc( reshape );
112    glutKeyboardFunc( key );
113    glutDisplayFunc( draw );
114    glutMainLoop();
115    return 0;
116 }
117