我需要绘制一个球体和立方体,同时
每当我按下'r'时,立方体会改变它的颜色,然后当'R'时球体会改变它的颜色。与“g”和“b”相同
r 是红色,g 是绿色,b 是蓝色。
问题是我不知道要使用哪组代码。目前在 C++ 的基础/初学者程序中
#include <conio.h>
#include <stdio.h>
#include <glut.h>
float CubeX = 0.0, CubeY = 0.0;
float Angle = 0.0l;
bool LightSwitch1 = false;
bool LightSwitch2 = false;
bool LightSwitch3 = false;
float Green[4] = {0.0, 1.0, 0.0, 1.0}; //R, G, B, A
float Blue[4] = {0.0, 0.0, 1.0, 1.0}; //R, G, B, A
float Red[4] = {1.0, 0.0, 0.0, 1.0}; //R, G, B, A
float White[4] = {1.0, 1.0, 1.0, 1.0}; //R, G, B, A
float Yellow[4] = {1.0, 1.0, 0.5, 1.0}; //R, G, B, A
float red = 1.0;
float blue = 1.0;
float green = 1.0;
void GameScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //lighswitch statement
if (!LightSwitch1)
{
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
}
else if (!LightSwitch2)
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Green);
}
else if (!LightSwitch3)
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
else
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
glPushMatrix();
glTranslatef(CubeX, CubeY, 0.0);
glRotatef(Angle, 0.0, 1.0, 0.0);
glColor3f(red, green, blue);
glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT); //lighting = on | color
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Red);
glutSolidCube(2.0);
glPopAttrib();
glPopMatrix();
//glPushMatrix();
//glColor3f(0.0, 0.0, 1.0);
//glTranslatef(2.0, 2.5, 0.0);
//glRotatef(Angle, 0.0, 1.0, 0.0);
//glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT); //lighting = on | color
// glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Red);
//glutSolidSphere(1.0, 10.0, 5.0);
//glPopAttrib();
//Angle++;
//glPopMatrix();
glEnd();
glutSwapBuffers();
}
void Keys(unsigned char key, int x, int y)
{
switch(key)
{
case 'w':
CubeY+= 0.15;
break;
case 'a':
CubeX-= 0.15;
break;
case 's':
CubeY-= 0.15;
break;
case 'd':
CubeX += 0.15;
break;
case 'r':
LightSwitch1 = !LightSwitch1; //light switch controls
break;
case 'g':
LightSwitch2 = !LightSwitch2;
break;
case 'b':
LightSwitch3 = !LightSwitch3;
break;
}
}
void SpecialKeys(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_UP:
CubeY += 0.15;
break;
case GLUT_KEY_DOWN:
CubeY -= 0.15;
break;
case GLUT_KEY_LEFT:
CubeX -= 0.15;
break;
case GLUT_KEY_RIGHT:
CubeX += 0.15 ;
break;
}
}
void ResizeWindow(int w, int h)
{
float ratio = 1.0*w/h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45.0, ratio, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 10.0, 15.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0,0);
glutInitWindowSize(1200, 700);
glutCreateWindow("Hey, its a moving cube!");
glutReshapeFunc(ResizeWindow);
glutDisplayFunc(GameScene);
glutIdleFunc(GameScene);
glutKeyboardFunc(Keys);
glutSpecialFunc(SpecialKeys);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glutMainLoop();
}