132001f49Smrg/*
232001f49Smrg * glRead/DrawPixels test
332001f49Smrg */
432001f49Smrg
532001f49Smrg
632001f49Smrg#include <GL/glew.h>
732001f49Smrg#include <stdio.h>
832001f49Smrg#include <string.h>
932001f49Smrg#include <stdlib.h>
1032001f49Smrg#include "glut_wrap.h"
1132001f49Smrg
1232001f49Smrgstatic int Width = 250, Height = 250;
1332001f49Smrgstatic GLfloat Zoom = 1.0;
1432001f49Smrg
1532001f49Smrgstatic void Init(void)
1632001f49Smrg{
1732001f49Smrg   fprintf(stderr, "GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));
1832001f49Smrg   fprintf(stderr, "GL_VERSION    = %s\n", (char *) glGetString(GL_VERSION));
1932001f49Smrg   fprintf(stderr, "GL_VENDOR     = %s\n", (char *) glGetString(GL_VENDOR));
2032001f49Smrg   fflush(stderr);
2132001f49Smrg   glClearColor(0.3, 0.1, 0.3, 0.0);
2232001f49Smrg}
2332001f49Smrg
2432001f49Smrgstatic void Reshape(int width, int height)
2532001f49Smrg{
2632001f49Smrg   Width = width / 2;
2732001f49Smrg   Height = height;
2832001f49Smrg   /* draw on left half (we'll read that area) */
2932001f49Smrg   glViewport(0, 0, Width, height);
3032001f49Smrg   glMatrixMode(GL_PROJECTION);
3132001f49Smrg   glLoadIdentity();
3232001f49Smrg   glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
3332001f49Smrg   glMatrixMode(GL_MODELVIEW);
3432001f49Smrg}
3532001f49Smrg
3632001f49Smrgstatic void Key(unsigned char key, int x, int y)
3732001f49Smrg{
3832001f49Smrg   switch (key) {
3932001f49Smrg   case 27:
4032001f49Smrg      exit(0);
4132001f49Smrg   default:
4232001f49Smrg      break;
4332001f49Smrg   }
4432001f49Smrg   glutPostRedisplay();
4532001f49Smrg}
4632001f49Smrg
4732001f49Smrgstatic void Draw(void)
4832001f49Smrg{
4932001f49Smrg   GLfloat *image = (GLfloat *) malloc(Width * Height * 4 * sizeof(GLfloat));
5032001f49Smrg
5132001f49Smrg   glClear(GL_COLOR_BUFFER_BIT);
5232001f49Smrg
5332001f49Smrg   glBegin(GL_TRIANGLES);
5432001f49Smrg   glColor3f(.8,0,0);
5532001f49Smrg   glVertex3f(-0.9, -0.9, -30.0);
5632001f49Smrg   glColor3f(0,.9,0);
5732001f49Smrg   glVertex3f( 0.9, -0.9, -30.0);
5832001f49Smrg   glColor3f(0,0,.7);
5932001f49Smrg   glVertex3f( 0.0,  0.9, -30.0);
6032001f49Smrg   glEnd();
6132001f49Smrg
6232001f49Smrg   glBegin(GL_QUADS);
6332001f49Smrg   glColor3f(1, 1, 1);
6432001f49Smrg   glVertex2f(-1.0, -1.0);
6532001f49Smrg   glVertex2f(-0.9, -1.0);
6632001f49Smrg   glVertex2f(-0.9, -0.9);
6732001f49Smrg   glVertex2f(-1.0, -0.9);
6832001f49Smrg   glEnd();
6932001f49Smrg
7032001f49Smrg   glReadPixels(0, 0, Width, Height, GL_RGBA, GL_FLOAT, image);
7132001f49Smrg   printf("Pixel(0,0) = %f, %f, %f, %f\n",
7232001f49Smrg          image[0], image[1], image[2], image[3]);
7332001f49Smrg   /* draw to right half of window */
7432001f49Smrg   glWindowPos2iARB(Width, 0);
7532001f49Smrg   glPixelZoom(Zoom, Zoom);
7632001f49Smrg   glDrawPixels(Width, Height, GL_RGBA, GL_FLOAT, image);
7732001f49Smrg   free(image);
7832001f49Smrg
7932001f49Smrg   glutSwapBuffers();
8032001f49Smrg}
8132001f49Smrg
8232001f49Smrgint main(int argc, char **argv)
8332001f49Smrg{
8432001f49Smrg   glutInit(&argc, argv);
8532001f49Smrg   glutInitWindowPosition(0, 0);
8632001f49Smrg   glutInitWindowSize(Width*2, Height);
8732001f49Smrg   glutInitDisplayMode(GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE);
8832001f49Smrg   if (glutCreateWindow(argv[0]) == GL_FALSE) {
8932001f49Smrg      exit(1);
9032001f49Smrg   }
9132001f49Smrg
9232001f49Smrg   if (argc > 1)
9332001f49Smrg      Zoom = atof(argv[1]);
9432001f49Smrg
9532001f49Smrg   glewInit();
9632001f49Smrg
9732001f49Smrg   Init();
9832001f49Smrg
9932001f49Smrg   glutReshapeFunc(Reshape);
10032001f49Smrg   glutKeyboardFunc(Key);
10132001f49Smrg   glutDisplayFunc(Draw);
10232001f49Smrg   glutMainLoop();
10332001f49Smrg   return 0;
10432001f49Smrg}
105