132001f49Smrg
232001f49Smrg/*
332001f49Smrg * Example of how to use the GL_MESA_window_pos extension.
432001f49Smrg * Brian Paul   This file is in the public domain.
532001f49Smrg */
632001f49Smrg
732001f49Smrg#include <math.h>
832001f49Smrg#include <string.h>
932001f49Smrg#include <stdlib.h>
1032001f49Smrg#include <stdio.h>
1132001f49Smrg#ifdef _WIN32
1232001f49Smrg#include <windows.h>
1332001f49Smrg#endif
1432001f49Smrg#include "GL/glew.h"
1532001f49Smrg#include "glut_wrap.h"
1632001f49Smrg
1732001f49Smrg#include "readtex.h"
1832001f49Smrg
1932001f49Smrg#define IMAGE_FILE DEMOS_DATA_DIR "girl.rgb"
2032001f49Smrg
2132001f49Smrg
2232001f49Smrg#ifndef M_PI
2332001f49Smrg#  define M_PI 3.14159265
2432001f49Smrg#endif
2532001f49Smrg
2632001f49Smrg
2732001f49Smrg
2832001f49Smrgstatic GLubyte *Image;
2932001f49Smrgstatic int ImgWidth, ImgHeight;
3032001f49Smrgstatic GLenum ImgFormat;
3132001f49Smrg
3232001f49Smrgstatic PFNGLWINDOWPOS2FPROC WindowPosFunc;
3332001f49Smrg
3432001f49Smrgstatic void draw( void )
3532001f49Smrg{
3632001f49Smrg   GLfloat angle;
3732001f49Smrg
3832001f49Smrg   glClear( GL_COLOR_BUFFER_BIT );
3932001f49Smrg
4032001f49Smrg   for (angle = -45.0; angle <= 135.0; angle += 10.0) {
4132001f49Smrg      GLfloat x = 50.0 + 200.0 * cos( angle * M_PI / 180.0 );
4232001f49Smrg      GLfloat y = 50.0 + 200.0 * sin( angle * M_PI / 180.0 );
4332001f49Smrg
4432001f49Smrg      /* Don't need to worry about the modelview or projection matrices!!! */
4532001f49Smrg      (*WindowPosFunc)( x, y );
4632001f49Smrg
4732001f49Smrg      glDrawPixels( ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image );
4832001f49Smrg   }
4932001f49Smrg   glFinish();
5032001f49Smrg}
5132001f49Smrg
5232001f49Smrg
5332001f49Smrgstatic void key( unsigned char key, int x, int y )
5432001f49Smrg{
5532001f49Smrg   (void) x;
5632001f49Smrg   (void) y;
5732001f49Smrg   switch (key) {
5832001f49Smrg      case 27:
5932001f49Smrg         exit(0);
6032001f49Smrg   }
6132001f49Smrg}
6232001f49Smrg
6332001f49Smrg
6432001f49Smrg/* new window size or exposure */
6532001f49Smrgstatic void reshape( int width, int height )
6632001f49Smrg{
6732001f49Smrg   glViewport(0, 0, (GLint)width, (GLint)height);
6832001f49Smrg}
6932001f49Smrg
7032001f49Smrg
7132001f49Smrgstatic void init( void )
7232001f49Smrg{
7332001f49Smrg   if (GLEW_ARB_window_pos) {
7432001f49Smrg      printf("Using GL_ARB_window_pos\n");
7532001f49Smrg      WindowPosFunc = glWindowPos2fARB;
7632001f49Smrg   }
7732001f49Smrg   else
7832001f49Smrg   if (GLEW_MESA_window_pos) {
7932001f49Smrg      printf("Using GL_MESA_window_pos\n");
8032001f49Smrg      WindowPosFunc = glWindowPos2fMESA;
8132001f49Smrg   }
8232001f49Smrg   else
8332001f49Smrg   {
8432001f49Smrg      printf("Sorry, GL_ARB/MESA_window_pos extension not available.\n");
8532001f49Smrg      exit(1);
8632001f49Smrg   }
8732001f49Smrg
8832001f49Smrg   Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
8932001f49Smrg   if (!Image) {
9032001f49Smrg      printf("Couldn't read %s\n", IMAGE_FILE);
9132001f49Smrg      exit(0);
9232001f49Smrg   }
9332001f49Smrg   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
9432001f49Smrg}
9532001f49Smrg
9632001f49Smrg
9732001f49Smrgint main( int argc, char *argv[] )
9832001f49Smrg{
9932001f49Smrg   glutInitWindowSize(500, 500);
10032001f49Smrg   glutInit(&argc, argv);
10132001f49Smrg   glutInitDisplayMode( GLUT_RGB );
10232001f49Smrg
10332001f49Smrg   if (glutCreateWindow("winpos") <= 0) {
10432001f49Smrg      exit(0);
10532001f49Smrg   }
10632001f49Smrg
10732001f49Smrg   glewInit();
10832001f49Smrg
10932001f49Smrg   init();
11032001f49Smrg
11132001f49Smrg   glutReshapeFunc( reshape );
11232001f49Smrg   glutKeyboardFunc( key );
11332001f49Smrg   glutDisplayFunc( draw );
11432001f49Smrg   glutMainLoop();
11532001f49Smrg   return 0;
11632001f49Smrg}
117