/* backface.c:
Draw a cube that can run with backface() turned on or off. Turn backface() on with the F key. Turn backface() off with the B key. Cube is moved when LEFTMOUSE is pressed and mouse itself is moved. */
#include <gl/gl.h> #include <gl/device.h>
#define CUBE_SIZE 200 #define CUBE_OBJ 1
main () {
Device dev; int moveit; short val, x = 30,y = 30;
initialize();
while (TRUE) {
while (qtest()) {
dev = qread(&val);
if (dev == ESCKEY) { backface(FALSE); gexit(); exit(); } else if (dev == REDRAW) { reshapeviewport(); drawcube(x,y); } else if (dev == LEFTMOUSE) moveit = val; /* left mouse is down */ else if (dev == BKEY) { backface(TRUE); /* turn back facing off */ drawcube(x,y); } else if (dev == FKEY) { backface(FALSE); /* turn back facing on */ drawcube(x,y); }
}
if (moveit) { x = getvaluator(MOUSEX); y = getvaluator(MOUSEY); drawcube(x,y); }
}
}
initialize() {
int gid;
prefposition(XMAXSCREEN/4, XMAXSCREEN*3/4, YMAXSCREEN/4, YMAXSCREEN*3/4); gid = winopen("backface");
doublebuffer(); gconfig(); shademodel(FLAT);
ortho((float)-XMAXSCREEN, (float)XMAXSCREEN, (float)-YMAXSCREEN, (float)YMAXSCREEN, (float)-YMAXSCREEN, (float)YMAXSCREEN);
qdevice(ESCKEY); qdevice(REDRAW); qdevice(LEFTMOUSE); qdevice(BKEY); qdevice(FKEY); qenter(REDRAW,gid);
backface(TRUE); /* turn on back facing polygon removal */
}
/* define a cube */ cube() {
/* front face */ pushmatrix(); translate(0.0,0.0,CUBE_SIZE); color(RED); rectfi(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE); popmatrix(); /* right face */ pushmatrix();
translate(CUBE_SIZE, 0.0, 0.0); rotate(900, 'y'); color(GREEN); rectfi(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE); popmatrix(); /* back face */ pushmatrix();
translate(0.0, 0.0, -CUBE_SIZE); rotate(1800, 'y'); color(BLUE); rectfi(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE); popmatrix(); /* left face */ pushmatrix();
translate(-CUBE_SIZE, 0.0, 0.0); rotate(-900, 'y'); color(CYAN); rectfi(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE); popmatrix(); /* top face */ pushmatrix();
translate(0.0, CUBE_SIZE, 0.0); rotate(-900, 'x'); color(MAGENTA); rectfi(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE); popmatrix(); /* bottom face */ pushmatrix();
translate(0.0, -CUBE_SIZE, 0.0); rotate(900, 'x'); color(YELLOW); rectfi(-CUBE_SIZE,-CUBE_SIZE,CUBE_SIZE,CUBE_SIZE); popmatrix();
}
drawcube(x,y) short x,y; {
pushmatrix(); rotate(2*x, 'x'); rotate(2*y, 'y'); color(BLACK); clear(); cube(); popmatrix(); swapbuffers();
} /*Changes -The constant CUBE_SIZE had a .0 append to the end of it */
The backface subroutine, ortho or ortho2 subroutine, popmatrix subroutine, pushmatrix subroutine, rotate subroutine, shademodel subroutine, swapbuffers subroutine, translate subroutine.
Removing Hidden Surfaces in GL3.2 Version 4 for AIX: Programming Concepts.