132001f49Smrg/**
232001f49Smrg * Test flat shading and clipping.
332001f49Smrg *
432001f49Smrg * Brian Paul
532001f49Smrg * 30 August 2007
632001f49Smrg */
732001f49Smrg
832001f49Smrg
932001f49Smrg#include <stdio.h>
1032001f49Smrg#include <stdlib.h>
1132001f49Smrg#include <math.h>
1232001f49Smrg#include "glut_wrap.h"
1332001f49Smrg
1432001f49Smrgstatic int Win;
1532001f49Smrgstatic GLfloat Scale = 2.0, Zrot = 50;
1632001f49Smrgstatic GLenum Mode = GL_LINE_LOOP;
1732001f49Smrgstatic GLboolean Smooth = 0;
1832001f49Smrgstatic GLenum PolygonMode = GL_FILL;
1932001f49Smrg
2032001f49Smrg
2132001f49Smrgstatic void
2232001f49SmrgDraw(void)
2332001f49Smrg{
2432001f49Smrg   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2532001f49Smrg
2632001f49Smrg   if (Smooth)
2732001f49Smrg      glShadeModel(GL_SMOOTH);
2832001f49Smrg   else
2932001f49Smrg      glShadeModel(GL_FLAT);
3032001f49Smrg
3132001f49Smrg   glPushMatrix();
3232001f49Smrg   glScalef(Scale, Scale, 1);
3332001f49Smrg   glRotatef(Zrot, 0, 0, 1);
3432001f49Smrg
3532001f49Smrg   glPolygonMode(GL_FRONT_AND_BACK, PolygonMode);
3632001f49Smrg
3732001f49Smrg   glBegin(Mode);
3832001f49Smrg   glColor3f(1, 0, 0);
3932001f49Smrg   glVertex2f(-1, -1);
4032001f49Smrg   glColor3f(0, 1, 0);
4132001f49Smrg   glVertex2f( 2, -1);
4232001f49Smrg   glColor3f(0, 0, 1);
4332001f49Smrg   glVertex2f( 0, 1);
4432001f49Smrg   glEnd();
4532001f49Smrg
4632001f49Smrg   glPushMatrix();
4732001f49Smrg   glScalef(0.9, 0.9, 1);
4832001f49Smrg   glBegin(Mode);
4932001f49Smrg   glColor3f(1, 0, 0);
5032001f49Smrg   glVertex2f( 0, 1);
5132001f49Smrg
5232001f49Smrg   glColor3f(0, 0, 1);
5332001f49Smrg   glVertex2f( 2, -1);
5432001f49Smrg
5532001f49Smrg   glColor3f(0, 1, 0);
5632001f49Smrg   glVertex2f(-1, -1);
5732001f49Smrg
5832001f49Smrg   glEnd();
5932001f49Smrg   glPopMatrix();
6032001f49Smrg
6132001f49Smrg   glPopMatrix();
6232001f49Smrg
6332001f49Smrg   glutSwapBuffers();
6432001f49Smrg}
6532001f49Smrg
6632001f49Smrg
6732001f49Smrgstatic void
6832001f49SmrgReshape(int width, int height)
6932001f49Smrg{
7032001f49Smrg   glViewport(0, 0, width, height);
7132001f49Smrg   glMatrixMode(GL_PROJECTION);
7232001f49Smrg   glLoadIdentity();
7332001f49Smrg   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
7432001f49Smrg   glMatrixMode(GL_MODELVIEW);
7532001f49Smrg   glLoadIdentity();
7632001f49Smrg   glTranslatef(0.0, 0.0, -15.0);
7732001f49Smrg}
7832001f49Smrg
7932001f49Smrg
8032001f49Smrgstatic void
8132001f49SmrgKey(unsigned char key, int x, int y)
8232001f49Smrg{
8332001f49Smrg   (void) x;
8432001f49Smrg   (void) y;
8532001f49Smrg   switch (key) {
8632001f49Smrg      case 'p':
8732001f49Smrg         if (Mode == GL_TRIANGLES)
8832001f49Smrg            Mode = GL_LINE_LOOP;
8932001f49Smrg         else
9032001f49Smrg            Mode = GL_TRIANGLES;
9132001f49Smrg         break;
9232001f49Smrg      case 'f':
9332001f49Smrg         if (PolygonMode == GL_POINT)
9432001f49Smrg            PolygonMode = GL_LINE;
9532001f49Smrg         else if (PolygonMode == GL_LINE)
9632001f49Smrg            PolygonMode = GL_FILL;
9732001f49Smrg         else
9832001f49Smrg            PolygonMode = GL_POINT;
9932001f49Smrg         printf("PolygonMode = 0x%x\n", PolygonMode);
10032001f49Smrg         break;
10132001f49Smrg      case 'r':
10232001f49Smrg         Zrot -= 5.0;
10332001f49Smrg         break;
10432001f49Smrg      case 'R':
10532001f49Smrg         Zrot += 5.0;
10632001f49Smrg         break;
10732001f49Smrg      case 'z':
10832001f49Smrg         Scale *= 1.1;
10932001f49Smrg         break;
11032001f49Smrg      case 'Z':
11132001f49Smrg         Scale /= 1.1;
11232001f49Smrg         break;
11332001f49Smrg      case 's':
11432001f49Smrg         Smooth = !Smooth;
11532001f49Smrg         break;
11632001f49Smrg      case 27:
11732001f49Smrg         glutDestroyWindow(Win);
11832001f49Smrg         exit(0);
11932001f49Smrg         break;
12032001f49Smrg   }
12132001f49Smrg   glutPostRedisplay();
12232001f49Smrg}
12332001f49Smrg
12432001f49Smrg
12532001f49Smrgstatic void
12632001f49SmrgInit(void)
12732001f49Smrg{
12832001f49Smrg   printf("Usage:\n");
12932001f49Smrg   printf("  z/Z:  change triangle size\n");
13032001f49Smrg   printf("  r/R:  rotate\n");
13132001f49Smrg   printf("  p:    toggle line/fill mode\n");
13232001f49Smrg   printf("  s:    toggle smooth/flat shading\n");
13332001f49Smrg   printf("  f:    switch polygon fill mode\n");
13432001f49Smrg}
13532001f49Smrg
13632001f49Smrg
13732001f49Smrgint
13832001f49Smrgmain(int argc, char *argv[])
13932001f49Smrg{
14032001f49Smrg   glutInit(&argc, argv);
14132001f49Smrg   glutInitWindowPosition(0, 0);
14232001f49Smrg   glutInitWindowSize(400, 400);
14332001f49Smrg   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
14432001f49Smrg   Win = glutCreateWindow(argv[0]);
14532001f49Smrg   glutReshapeFunc(Reshape);
14632001f49Smrg   glutKeyboardFunc(Key);
14732001f49Smrg   glutDisplayFunc(Draw);
14832001f49Smrg   Init();
14932001f49Smrg   glutMainLoop();
15032001f49Smrg   return 0;
15132001f49Smrg}
152