我被要求制作一个盒子,上面有4个三角形,按下o和c,盒子顶部的4个三角形应逐个打开和关闭.
我用它上面的三角形制作了盒子,它们一下子移动,我试图操纵每个三角形的步骤,希望一个接一个地打开和关闭,但我陷入了算法和代码的困境.
这是完整的代码:
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159265
static int step = 0, step1, step2, step3, step4;
/* camera location in longitude and latitude */
static float alpha = 0.0;
static float beta = PI / 6.0;
/* position of camera */
static GLdouble cpos[3];
/* position of light source */
static GLfloat lpos[] = { -2.5, 2.5, 3.0, 1.0 };
/* vectors to specify material properties */
static GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
static GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
static GLfloat gray[] = { 0.5, 0.5, 0.5, 1.0 };
static GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
static GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
static GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
static GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
static GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
static GLfloat darkcyan[] = { 0.0, 0.4, 0.4, 1.0 };
static bool topFinishedOpening = false;
static float stepBefore = step;
void writemessage()
{
printf("HW-1");
}
void init(void)
{
writemessage();
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
/* initially GL_FILL mode (default), later GL_LINE to show wireframe */
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_LIGHTING);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_LIGHT0);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
static int i;
static double angle1, angle2, angle3, angle4, Y1, Z1, normalY1, normalZ1, Y2, Z2, normalY2, normalZ2, X3, Y3, normalX3, normalY3, X4, Y4, normalX4, normalY4;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* initialize (modelview) matrix */
glLoadIdentity();
/* update camera position */
cpos[0] = 15.0 * cos(beta) * sin(alpha);
cpos[1] = 15.0 * sin(beta);
cpos[2] = 15.0 * cos(beta) * cos(alpha);
gluLookAt(cpos[0], cpos[1], cpos[2], 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);
/* set current light source position */
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
/* draw sphere to show light source */
glMaterialfv(GL_FRONT, GL_EMISSION, white);
glPushMatrix();
glTranslatef(lpos[0], lpos[1], lpos[2]);
glutSolidSphere(0.1, 10, 8);
glPopMatrix();
/* remaining objects do not look as if they emit light */
glMaterialfv(GL_FRONT, GL_EMISSION, black);
/* draw a tabletop */
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, gray);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64);
glNormal3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(-5.0, -0.001, -3.0);
glVertex3f(-5.0, -0.001, 5.0);
glVertex3f(5.0, -0.001, 5.0);
glVertex3f(5.0, -0.001, -3.0);
glEnd();
printf("Angle Number 1: %f\n", (angle1 * (180.0 / PI)));
printf("Angle Number 2: %f\n", (angle2 * (180.0 / PI)));
printf("Angle Number 3: %f\n", (angle3 * (180.0 / PI)));
printf("Angle Number 4: %f\n", (angle4 * (180.0 / PI)));
printf("\n");
if ((step - stepBefore) > 0){
step1 = step; step2 = step;
step3 = step;
step4 = step;
}
else if ((step - stepBefore) < 0){
step1 = step; step2 = step;
step3 = step;
step4 = step;
}
else{
step1 = step; step2 = step;
step3 = step;
step4 = step;
}
// first
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
angle1 = ((float)step1 / 180) * PI;
Y1 = 2.0 + 2.0 * sin(angle1);
Z1 = -1.0 + 2.0 * cos(angle1);
normalY1 = cos(angle1);
normalZ1 = -sin(angle1);
glNormal3f(0, normalY1, normalZ1);
glBegin(GL_POLYGON);
glVertex3f(-3.0, 2.0, -1.0);
glVertex3f(3.0, 2.0, -1.0);
glVertex3f(0, Y1, Z1);
glEnd();
// second
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
angle2 = ((float)step2 / 180) * PI;
Y2 = 2.0 + 2.0 * sin(angle2);
Z2 = 3.0 + -2.0 * cos(angle2);
normalY2 = cos(angle2);
normalZ2 = sin(angle2);
glNormal3f(0, normalY2, normalZ2);
glBegin(GL_POLYGON);
glVertex3f(-3.0, 2.0, 3.0);
glVertex3f(3.0, 2.0, 3.0);
glVertex3f(0, Y2, Z2);
glEnd();
// third
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
angle3 = ((float)step3 / 180) * PI;
X3 = 3.0 - 3.0 * cos(angle3);
Y3 = 2.0 + 2.0 * sin(angle3);
normalX3 = sin(angle3);
normalY3 = cos(angle3);
glNormal3f(normalX3, normalY3, 0);
glBegin(GL_POLYGON);
glVertex3f(3.0, 2.0, 3.0);
glVertex3f(3.0, 2.0, -1.0);
glVertex3f(X3, Y3, 1);
glEnd();
// fourth
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
angle4 = ((float)step4 / 180) * PI;
X4 = -3.0 + 3.0 * cos(angle4);
Y4 = 2.0 + 2.0 * sin(angle4);
normalX4 = sin(angle4);
normalY4 = cos(angle4);
glNormal3f(normalX4, normalY4, 0);
glBegin(GL_POLYGON);
glVertex3f(-3.0, 2.0, 3.0);
glVertex3f(-3.0, 2.0, -1.0);
glVertex3f(X4, Y4, 1);
glEnd();
// back
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
glBegin(GL_POLYGON);
glVertex3f(3.0, 0.0, -1.0);
glVertex3f(3.0, 2.0, -1.0);
glVertex3f(-3.0, 2.0, -1.0);
glVertex3f(-3.0, 0.0, -1.0);
glEnd();
// front
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
glBegin(GL_POLYGON);
glVertex3f(3.0, 0.0, 3.0);
glVertex3f(3.0, 2.0, 3.0);
glVertex3f(-3.0, 2.0, 3.0);
glVertex3f(-3.0, 0.0, 3.0);
glEnd();
// right
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
glBegin(GL_POLYGON);
glVertex3f(3.0, 2.0, -1.0);
glVertex3f(3.0, 2.0, 3.0);
glVertex3f(3.0, 0.0, 3.0);
glVertex3f(3.0, 0.0, -1.0);
glEnd();
// left
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
glBegin(GL_POLYGON);
glVertex3f(-3.0, 2.0, -1.0);
glVertex3f(-3.0, 2.0, 3.0);
glVertex3f(-3.0, 0.0, 3.0);
glVertex3f(-3.0, 0.0, -1.0);
glEnd();
// buttom
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
glBegin(GL_POLYGON);
glVertex3f(-3.0, 0.0, -1.0);
glVertex3f(-3.0, 0.0, 3.0);
glVertex3f(3.0, 0.0, 3.0);
glVertex3f(3.0, 0.0, -1.0);
glEnd();
glFlush();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
static int polygonmode[2];
switch (key) {
case 27:
exit(0);
break;
/* open and close the top */
case 'o':
if (step < 90) { stepBefore = step*1.0; step = step + 2; }
glutPostRedisplay();
break;
case 'c':
if (step > 0) {
stepBefore = step*1.0; step = step - 2;
}
glutPostRedisplay();
break;
/* w for switching between GL_FILL and GL_LINE
* Using a query command glGetIntegerv() we obtain two integers
* indicating the current GL_POLYGON_MODE for FRONT and BACK faces.
* Look up glIsEnabled() that may be found useful. */
case 'w':
glGetIntegerv(GL_POLYGON_MODE, polygonmode);
if (polygonmode[0] == GL_FILL)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glutPostRedisplay();
break;
/* control lpos */
case 'x':
lpos[0] = lpos[0] + 0.2;
glutPostRedisplay();
break;
case 'X':
lpos[0] = lpos[0] - 0.2;
glutPostRedisplay();
break;
case 'y':
lpos[1] = lpos[1] + 0.2;
glutPostRedisplay();
break;
case 'Y':
lpos[1] = lpos[1] - 0.2;
glutPostRedisplay();
break;
case 'z':
lpos[2] = lpos[2] + 0.2;
glutPostRedisplay();
break;
case 'Z':
lpos[2] = lpos[2] - 0.2;
glutPostRedisplay();
break;
default:
break;
}
}
void specialkey(GLint key, int x, int y)
{
switch (key) {
case GLUT_KEY_RIGHT:
alpha = alpha + PI / 180;
if (alpha > 2 * PI) alpha = alpha - 2 * PI;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
alpha = alpha - PI / 180;
if (alpha < 0) alpha = alpha + 2 * PI;
glutPostRedisplay();
break;
case GLUT_KEY_UP:
if (beta < 0.45*PI) beta = beta + PI / 180;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
if (beta > 0.05*PI) beta = beta - PI / 180;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 800);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialkey);
glutMainLoop();
return 0;
}
截图:
最佳答案 我通过逐步表达4个三角形的角度状态,为您的代码添加了一个简单的技巧.具体来说:代替[0,90]的范围,我将范围变为你的变量步[0,360],其中[0,90]部分用于step1,[90,180]用于step2,等等.
请参阅下面的代码.搜索/ *修改代码* /查看我添加的内容.
代码已经过测试并按预期工作.
请注意,我不认为我的技巧是解决这个问题的好设计.我只是提供某种提示,你应该自己编写代码. 🙂
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159265
static int step = 0, step1, step2, step3, step4;
/* camera location in longitude and latitude */
static float alpha = 0.0;
static float beta = PI / 6.0;
/* position of camera */
static GLdouble cpos[3];
/* position of light source */
static GLfloat lpos[] = { -2.5, 2.5, 3.0, 1.0 };
/* vectors to specify material properties */
static GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
static GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
static GLfloat gray[] = { 0.5, 0.5, 0.5, 1.0 };
static GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
static GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
static GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
static GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
static GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
static GLfloat darkcyan[] = { 0.0, 0.4, 0.4, 1.0 };
static bool topFinishedOpening = false;
static float stepBefore = step;
void writemessage()
{
printf("HW-1");
}
void init(void)
{
writemessage();
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
/* initially GL_FILL mode (default), later GL_LINE to show wireframe */
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_LIGHTING);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_LIGHT0);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
static int i;
static double angle1, angle2, angle3, angle4, Y1, Z1, normalY1, normalZ1, Y2, Z2, normalY2, normalZ2, X3, Y3, normalX3, normalY3, X4, Y4, normalX4, normalY4;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* initialize (modelview) matrix */
glLoadIdentity();
/* update camera position */
cpos[0] = 15.0 * cos(beta) * sin(alpha);
cpos[1] = 15.0 * sin(beta);
cpos[2] = 15.0 * cos(beta) * cos(alpha);
gluLookAt(cpos[0], cpos[1], cpos[2], 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);
/* set current light source position */
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
/* draw sphere to show light source */
glMaterialfv(GL_FRONT, GL_EMISSION, white);
glPushMatrix();
glTranslatef(lpos[0], lpos[1], lpos[2]);
glutSolidSphere(0.1, 10, 8);
glPopMatrix();
/* remaining objects do not look as if they emit light */
glMaterialfv(GL_FRONT, GL_EMISSION, black);
/* draw a tabletop */
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, gray);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64);
glNormal3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(-5.0, -0.001, -3.0);
glVertex3f(-5.0, -0.001, 5.0);
glVertex3f(5.0, -0.001, 5.0);
glVertex3f(5.0, -0.001, -3.0);
glEnd();
printf("Angle Number 1: %f\n", (angle1 * (180.0 / PI)));
printf("Angle Number 2: %f\n", (angle2 * (180.0 / PI)));
printf("Angle Number 3: %f\n", (angle3 * (180.0 / PI)));
printf("Angle Number 4: %f\n", (angle4 * (180.0 / PI)));
printf("\n");
#if 0
if ((step - stepBefore) > 0){
step1 = step; step2 = step;
step3 = step;
step4 = step;
}
else if ((step - stepBefore) < 0){
step1 = step; step2 = step;
step3 = step;
step4 = step;
}
else{
step1 = step; step2 = step;
step3 = step;
step4 = step;
}
#else
/* Modifications to your code */
if (step >= 270)
{
step1 = 90;
step2 = 90;
step3 = 90;
step4 = step - 270;
}
else if (step >= 180) // implies step < 270
{
step1 = 90;
step2 = 90;
step3 = step - 180;
step4 = 0;
}
else if (step >= 90) // implies step < 180
{
step1 = 90;
step2 = step - 90;
step3 = 0;
step4 = 0;
}
else if (step >= 0) // implies step < 90
{
step1 = step;
step2 = 0;
step3 = 0;
step4 = 0;
}
else
; // error handling since this shall not happen
#endif
// first
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
angle1 = ((float)step1 / 180) * PI;
Y1 = 2.0 + 2.0 * sin(angle1);
Z1 = -1.0 + 2.0 * cos(angle1);
normalY1 = cos(angle1);
normalZ1 = -sin(angle1);
glNormal3f(0, normalY1, normalZ1);
glBegin(GL_POLYGON);
glVertex3f(-3.0, 2.0, -1.0);
glVertex3f(3.0, 2.0, -1.0);
glVertex3f(0, Y1, Z1);
glEnd();
// second
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
angle2 = ((float)step2 / 180) * PI;
Y2 = 2.0 + 2.0 * sin(angle2);
Z2 = 3.0 + -2.0 * cos(angle2);
normalY2 = cos(angle2);
normalZ2 = sin(angle2);
glNormal3f(0, normalY2, normalZ2);
glBegin(GL_POLYGON);
glVertex3f(-3.0, 2.0, 3.0);
glVertex3f(3.0, 2.0, 3.0);
glVertex3f(0, Y2, Z2);
glEnd();
// third
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
angle3 = ((float)step3 / 180) * PI;
X3 = 3.0 - 3.0 * cos(angle3);
Y3 = 2.0 + 2.0 * sin(angle3);
normalX3 = sin(angle3);
normalY3 = cos(angle3);
glNormal3f(normalX3, normalY3, 0);
glBegin(GL_POLYGON);
glVertex3f(3.0, 2.0, 3.0);
glVertex3f(3.0, 2.0, -1.0);
glVertex3f(X3, Y3, 1);
glEnd();
// fourth
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
angle4 = ((float)step4 / 180) * PI;
X4 = -3.0 + 3.0 * cos(angle4);
Y4 = 2.0 + 2.0 * sin(angle4);
normalX4 = sin(angle4);
normalY4 = cos(angle4);
glNormal3f(normalX4, normalY4, 0);
glBegin(GL_POLYGON);
glVertex3f(-3.0, 2.0, 3.0);
glVertex3f(-3.0, 2.0, -1.0);
glVertex3f(X4, Y4, 1);
glEnd();
// back
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
glBegin(GL_POLYGON);
glVertex3f(3.0, 0.0, -1.0);
glVertex3f(3.0, 2.0, -1.0);
glVertex3f(-3.0, 2.0, -1.0);
glVertex3f(-3.0, 0.0, -1.0);
glEnd();
// front
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
glBegin(GL_POLYGON);
glVertex3f(3.0, 0.0, 3.0);
glVertex3f(3.0, 2.0, 3.0);
glVertex3f(-3.0, 2.0, 3.0);
glVertex3f(-3.0, 0.0, 3.0);
glEnd();
// right
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
glBegin(GL_POLYGON);
glVertex3f(3.0, 2.0, -1.0);
glVertex3f(3.0, 2.0, 3.0);
glVertex3f(3.0, 0.0, 3.0);
glVertex3f(3.0, 0.0, -1.0);
glEnd();
// left
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
glBegin(GL_POLYGON);
glVertex3f(-3.0, 2.0, -1.0);
glVertex3f(-3.0, 2.0, 3.0);
glVertex3f(-3.0, 0.0, 3.0);
glVertex3f(-3.0, 0.0, -1.0);
glEnd();
// buttom
glMaterialfv(GL_FRONT, GL_EMISSION, black);
glMaterialfv(GL_BACK, GL_EMISSION, black);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
glBegin(GL_POLYGON);
glVertex3f(-3.0, 0.0, -1.0);
glVertex3f(-3.0, 0.0, 3.0);
glVertex3f(3.0, 0.0, 3.0);
glVertex3f(3.0, 0.0, -1.0);
glEnd();
glFlush();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
static int polygonmode[2];
switch (key) {
case 27:
exit(0);
break;
/* open and close the top */
case 'o':
#if 0
if (step < 90) { stepBefore = step*1.0; step = step + 2; }
#else
/* Modifications to your code */
if (step < 360) { stepBefore = step*1.0; step = step + 2; }
#endif
glutPostRedisplay();
break;
case 'c':
if (step > 0) {
stepBefore = step*1.0; step = step - 2;
}
glutPostRedisplay();
break;
/* w for switching between GL_FILL and GL_LINE
* Using a query command glGetIntegerv() we obtain two integers
* indicating the current GL_POLYGON_MODE for FRONT and BACK faces.
* Look up glIsEnabled() that may be found useful. */
case 'w':
glGetIntegerv(GL_POLYGON_MODE, polygonmode);
if (polygonmode[0] == GL_FILL)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glutPostRedisplay();
break;
/* control lpos */
case 'x':
lpos[0] = lpos[0] + 0.2;
glutPostRedisplay();
break;
case 'X':
lpos[0] = lpos[0] - 0.2;
glutPostRedisplay();
break;
case 'y':
lpos[1] = lpos[1] + 0.2;
glutPostRedisplay();
break;
case 'Y':
lpos[1] = lpos[1] - 0.2;
glutPostRedisplay();
break;
case 'z':
lpos[2] = lpos[2] + 0.2;
glutPostRedisplay();
break;
case 'Z':
lpos[2] = lpos[2] - 0.2;
glutPostRedisplay();
break;
default:
break;
}
}
void specialkey(GLint key, int x, int y)
{
switch (key) {
case GLUT_KEY_RIGHT:
alpha = alpha + PI / 180;
if (alpha > 2 * PI) alpha = alpha - 2 * PI;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
alpha = alpha - PI / 180;
if (alpha < 0) alpha = alpha + 2 * PI;
glutPostRedisplay();
break;
case GLUT_KEY_UP:
if (beta < 0.45*PI) beta = beta + PI / 180;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
if (beta > 0.05*PI) beta = beta - PI / 180;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 800);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialkey);
glutMainLoop();
return 0;
}