132001f49Smrg/**
232001f49Smrg * Test glPolygonMode.
332001f49Smrg * A tri-strip w/ two tris is drawn so that the first tri is front-facing
432001f49Smrg * but the second tri is back-facing.
532001f49Smrg * Set glPolygonMode differently for the front/back faces
632001f49Smrg *
732001f49Smrg */
832001f49Smrg
932001f49Smrg
1032001f49Smrg#include <stdio.h>
1132001f49Smrg#include <stdlib.h>
1232001f49Smrg#include <math.h>
1332001f49Smrg#include "glut_wrap.h"
1432001f49Smrg
1532001f49Smrgstatic int Win;
1632001f49Smrgstatic GLfloat Zrot = 0;
1732001f49Smrgstatic GLboolean FrontFillBackUnfilled = GL_TRUE;
1832001f49Smrgstatic GLboolean Lines = GL_TRUE;
1932001f49Smrg
2032001f49Smrg
2132001f49Smrgstatic void
2232001f49SmrgDraw(void)
2332001f49Smrg{
2432001f49Smrg   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2532001f49Smrg
2632001f49Smrg   if (FrontFillBackUnfilled) {
2732001f49Smrg      if (Lines) {
2832001f49Smrg         printf("FrontMode = FILL, BackMode = LINE\n");
2932001f49Smrg         glPolygonMode(GL_BACK, GL_LINE);
3032001f49Smrg      }
3132001f49Smrg      else {
3232001f49Smrg         printf("FrontMode = FILL, BackMode = POINT\n");
3332001f49Smrg         glPolygonMode(GL_BACK, GL_POINT);
3432001f49Smrg      }
3532001f49Smrg      glPolygonMode(GL_FRONT, GL_FILL);
3632001f49Smrg   }
3732001f49Smrg   else {
3832001f49Smrg      if (Lines) {
3932001f49Smrg         printf("FrontMode = LINE, BackMode = FILL\n");
4032001f49Smrg         glPolygonMode(GL_FRONT, GL_LINE);
4132001f49Smrg      }
4232001f49Smrg      else {
4332001f49Smrg         printf("FrontMode = POINT, BackMode = FILL\n");
4432001f49Smrg         glPolygonMode(GL_FRONT, GL_POINT);
4532001f49Smrg      }
4632001f49Smrg      glPolygonMode(GL_BACK, GL_FILL);
4732001f49Smrg   }
4832001f49Smrg
4932001f49Smrg   glPushMatrix();
5032001f49Smrg   glRotatef(Zrot, 0, 0, 1);
5132001f49Smrg
5232001f49Smrg   glBegin(GL_TRIANGLE_STRIP);
5332001f49Smrg   glVertex2f(-1, 0);
5432001f49Smrg   glVertex2f( 1, 0);
5532001f49Smrg   glVertex2f(0,  1);
5632001f49Smrg   glVertex2f(0, -1);
5732001f49Smrg   glEnd();
5832001f49Smrg
5932001f49Smrg   glPopMatrix();
6032001f49Smrg
6132001f49Smrg   glutSwapBuffers();
6232001f49Smrg}
6332001f49Smrg
6432001f49Smrg
6532001f49Smrgstatic void
6632001f49SmrgReshape(int width, int height)
6732001f49Smrg{
6832001f49Smrg   glViewport(0, 0, width, height);
6932001f49Smrg   glMatrixMode(GL_PROJECTION);
7032001f49Smrg   glLoadIdentity();
7132001f49Smrg   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
7232001f49Smrg   glMatrixMode(GL_MODELVIEW);
7332001f49Smrg   glLoadIdentity();
7432001f49Smrg   glTranslatef(0.0, 0.0, -15.0);
7532001f49Smrg}
7632001f49Smrg
7732001f49Smrg
7832001f49Smrgstatic void
7932001f49SmrgKey(unsigned char key, int x, int y)
8032001f49Smrg{
8132001f49Smrg   const GLfloat step = 3.0;
8232001f49Smrg   (void) x;
8332001f49Smrg   (void) y;
8432001f49Smrg   switch (key) {
8532001f49Smrg   case 'p':
8632001f49Smrg      FrontFillBackUnfilled = !FrontFillBackUnfilled;
8732001f49Smrg      break;
8832001f49Smrg   case 'l':
8932001f49Smrg      Lines = !Lines;
9032001f49Smrg      break;
9132001f49Smrg   case 'z':
9232001f49Smrg      Zrot -= step;
9332001f49Smrg      break;
9432001f49Smrg   case 'Z':
9532001f49Smrg      Zrot += step;
9632001f49Smrg      break;
9732001f49Smrg   case 27:
9832001f49Smrg      glutDestroyWindow(Win);
9932001f49Smrg      exit(0);
10032001f49Smrg      break;
10132001f49Smrg   }
10232001f49Smrg   glutPostRedisplay();
10332001f49Smrg}
10432001f49Smrg
10532001f49Smrg
10632001f49Smrgstatic void
10732001f49SmrgInit(void)
10832001f49Smrg{
10932001f49Smrg   printf("GL_RENDERER = %s\n", (char*) glGetString(GL_RENDERER));
11032001f49Smrg
11132001f49Smrg   glLineWidth(3.0);
11232001f49Smrg   glPointSize(3.0);
11332001f49Smrg
11432001f49Smrg   glColor4f(1, 1, 1, 0.8);
11532001f49Smrg   glEnable(GL_BLEND);
11632001f49Smrg   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
11732001f49Smrg
11832001f49Smrg   printf("Press 'p' to toggle polygon mode\n");
11932001f49Smrg}
12032001f49Smrg
12132001f49Smrg
12232001f49Smrgint
12332001f49Smrgmain(int argc, char *argv[])
12432001f49Smrg{
12532001f49Smrg   glutInit(&argc, argv);
12632001f49Smrg   glutInitWindowPosition(0, 0);
12732001f49Smrg   glutInitWindowSize(400, 400);
12832001f49Smrg   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
12932001f49Smrg   Win = glutCreateWindow(argv[0]);
13032001f49Smrg   glutReshapeFunc(Reshape);
13132001f49Smrg   glutKeyboardFunc(Key);
13232001f49Smrg   glutDisplayFunc(Draw);
13332001f49Smrg   Init();
13432001f49Smrg   glutMainLoop();
13532001f49Smrg   return 0;
13632001f49Smrg}
137