c_cpp C ++ 3D城市建筑飞行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C ++ 3D城市建筑飞行相关的知识,希望对你有一定的参考价值。
#include GLUT/glut.h
#include iostream
#include stdlib.h
#include math.h
#include cmath
using namespace std;
class wcPt2D // class to represent a 2D point
{
public:
GLfloat x, y, z;
};
wcPt2D look;
wcPt2D eye;
GLfloat yScale;
GLfloat xScale;
GLfloat yTranslate;
GLfloat xTranslate;
GLfloat zTranslate;
GLfloat color1, color2, color3;
/* Lights */
GLfloat globalAmbient[]={.0, .0, .0, 1.0}; // Global lighting
/* Light 0 is the overhead light */
GLfloat light0_ambient[]={.1, .1, .1, 1.0};
GLfloat light0_diffuse[]={.5, .5, .5, 1.0};
GLfloat light0_specular[]={.1, .1, .1, 1.0};
GLfloat light0_position[]={0.0, 1.5 , 0.0, 0.0}; // change last value to 1.0 to make this positional
/* Light 1 is the bottom light */
GLfloat light1_ambient[]={.1, .1, .1, 1.0};
GLfloat light1_diffuse[]={.5, .5, .5, 1.0};
GLfloat light1_specular[]={.1, .1, .1, 1.0};
GLfloat light1_position[]={0.0, -1.5 , 0.0, 0.0}; // change last value to 1.0 to make this positional
/* Light 3 is right spotlight */
GLfloat light3_ambient[]={.1, .1, .1, 1.0};
GLfloat light3_diffuse[]={.5, .5, .5, 1.0};
GLfloat light3_specular[]={.1, .1, .1, 1.0};
GLfloat dirVectorLight3[]={0.0, 0.0, 0.0};
GLfloat light3_position[]={0.0, 0.0, 0.0, 1.0};
/* Light 4 is right spotlight */
GLfloat light4_ambient[]={.5, .5, .5, 1.0};
GLfloat light4_diffuse[]={.5, .5, .5, 1.0};
GLfloat light4_specular[]={.1, .1,.1, 1.0};
GLfloat dirVectorLight4[]={0.0, 0.0, 0.0};
GLfloat light4_position[]={0.0, 0.0, 0.0, 1.0};
/* Materials */
//http://www.opengl.org/archives/resources/code/samples/sig99/advanced99/notes/node153.html
//Brass
GLfloat matBrass_specular[]={.992157, .941176, .807843, 1.0};
GLfloat matBrass_diffuse[]={.780392, .568627, .113725, 1.0};
GLfloat matBrass_ambient[]={.329412, .223529, .027451, 1.0};
GLfloat matBrass_shininess={27.8974};
//Chrome
GLfloat matChrome_specular[]={.774597, .458561, .200621, 1.0};
GLfloat matChrome_diffuse[]={.4, .4, .4, 1.0};
GLfloat matChrome_ambient[]={.25, .25, .25, 1.0};
GLfloat matChrome_shininess={76.8};
//Jade
GLfloat matJade_specular[]={.316228, .316228, .316228, .95};
GLfloat matJade_diffuse[]={.54, .89, .63, .95};
GLfloat matJade_ambient[]={.135, .2225, .1575, .95};
GLfloat matJade_shininess={12.8};
//Polished
GLfloat matPolished_specular[]={.774597, .774597, .774597, 1.0};
GLfloat matPolished_diffuse[]={.4, .4, .4, 1.0};
GLfloat matPolished_ambient[]={.25, .25, .25, 1.0};
GLfloat matPolished_shininess={76.8};
//Gold
GLfloat matGold_specular[]={.628281, .555802, .366065, 1.0};
GLfloat matGold_diffuse[]={.75164, .60648, .22648, 1.0};
GLfloat matGold_ambient[]={.24725, .1995, .0745, 1.0};
GLfloat matGold_shininess={51.2};
//Ruby
GLfloat matRuby_specular[]={.727811, .626959, .626959, .55};
GLfloat matRuby_diffuse[]={.61424, .04136, .04136, .55};
GLfloat matRuby_ambient[]={.1745, .01175, .01175, .55};
GLfloat matRuby_shininess={76.8};
bool start, initial;
void init(void)
{
glEnable(GL_CULL_FACE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_BACK, GL_FILL);
glPolygonMode(GL_FRONT, GL_FILL);
//glEnable(GL_COLOR_MATERIAL);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient);
glShadeModel(GL_SMOOTH); /* enable smooth shading */
glClearColor (0.0, 0.0, 0.0, 1.0);
glColor3f (1.0, 1.0, 1.0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
light4_position[0]=2.0;
light4_position[1]=0.0;
light4_position[2]=1.0;
light4_position[3]=1.0;
glLightf (GL_LIGHT4, GL_SPOT_CUTOFF, 40.0);
glLightf(GL_LIGHT4, GL_SPOT_EXPONENT, 10.5);
glLightfv(GL_LIGHT4, GL_AMBIENT, light4_ambient);
glLightfv(GL_LIGHT4, GL_DIFFUSE, light4_diffuse);
glLightfv(GL_LIGHT4, GL_SPECULAR, light4_specular);
glLightfv(GL_LIGHT4, GL_POSITION, light4_position);
light3_position[0]=2.0;
light3_position[1]=0.0;
light3_position[2]=1.0;
light3_position[3]=1.0;
glLightf (GL_LIGHT3, GL_SPOT_CUTOFF, 40.0);
glLightf(GL_LIGHT3, GL_SPOT_EXPONENT, 10.5);
glLightfv(GL_LIGHT3, GL_AMBIENT, light3_ambient);
glLightfv(GL_LIGHT3, GL_DIFFUSE, light3_diffuse);
glLightfv(GL_LIGHT3, GL_SPECULAR, light3_specular);
glLightfv(GL_LIGHT3, GL_POSITION, light3_position);
start = true;
look.x = 3;
look.y = 14;
look.z = 20;
eye.z = 20;
eye.x = 0;
zTranslate = 0;
xTranslate = 0;
initial = true;
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
//glEnable(GL_DEPTH_TEST);
}
void aCube(string color) // draw the cube
{
glPolygonMode(GL_FRONT, GL_FILL); // front is solid fill
glPolygonMode(GL_BACK, GL_LINE); // back is wireframe
GLfloat x1[] = {-1, 1, -1}; // left back top
GLfloat x2[] = {-1, 1, 1}; // left front top
GLfloat x3[] = {1, 1, 1}; // right front top
GLfloat x4[] = {1, 1, -1}; // right back top
GLfloat x5[] = {-1, -1, -1}; // left back bottom
GLfloat x6[] = {-1, -1, 1}; // left front bottom
GLfloat x7[] = {1, -1, 1}; // right front bottom
GLfloat x8[] = {1, -1, -1}; // right back bottom
glBegin(GL_QUADS);
//Top
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(0,1,0);
glVertex3fv(x1);
glVertex3fv(x2);
glVertex3fv(x3);
glVertex3fv(x4);
//Bottom
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(0,-1,0);
glVertex3fv(x5);
glVertex3fv(x8);
glVertex3fv(x7);
glVertex3fv(x6);
//Back
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(0,0,-1);
glVertex3fv(x1);
glVertex3fv(x4);
glVertex3fv(x8);
glVertex3fv(x5);
//Front
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(0,0,1);
glVertex3fv(x2);
glVertex3fv(x6);
glVertex3fv(x7);
glVertex3fv(x3);
//Left
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(-1,0,0);
glVertex3fv(x2);
glVertex3fv(x1);
glVertex3fv(x5);
glVertex3fv(x6);
//Right
if(color == "aComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
} else if(color == "bComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
color3 = .2;
} else if(color == "cComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matRuby_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matRuby_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matRuby_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matRuby_shininess);
} else if(color == "dComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matPolished_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matPolished_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matPolished_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matPolished_shininess);
} else if(color == "eComplex"){
glMaterialfv(GL_FRONT, GL_SPECULAR, matJade_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matJade_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matJade_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matJade_shininess);
}
glNormal3f(1,0,0);
glVertex3fv(x4);
glVertex3fv(x3);
glVertex3fv(x7);
glVertex3fv(x8);
glEnd();
}
void B_Complex(){
glTranslatef(2, 0, -8);
glScalef(1, 2, 1);
aCube("bComplex");
}
void A_Complex(){
glTranslatef(-3, 0, 0);
glScalef(1, 3, 2);
aCube("aComplex");
}
void C_Complex(){
glTranslatef(-5, 0, -10);
glScalef(1, 5, 1);
aCube("cComplex");
}
void D_Complex(){
glTranslatef(1, 0, -14);
glScalef(2, 4, 1);
aCube("dComplex");
}
void complex(){
glTranslatef(5, 0, -3);
glScalef(1, 3, 2);
glRotated(2, 0, 0, 0);
aCube("eComplex");
}
void DrawCube(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING); /* turn lights on */
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_LIGHT0);
//glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHT3);
glEnable(GL_LIGHT4);
//glMatrixMode(GL_MODELVIEW);
//glutSolidSphere (1.0, 20, 16);
A_Complex();
glColor3f(.0, .0, .0);
glLoadIdentity();
gluLookAt(look.x, look.y, look.z, eye.x, 0, -20, 0, 1, 0);
glPushMatrix();
glBegin(GL_QUADS);
glMaterialfv(GL_FRONT, GL_SPECULAR, matBrass_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, matBrass_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matBrass_diffuse);
glMaterialf(GL_FRONT, GL_SHININESS, matBrass_shininess);
glNormal3f(10, 0, 1);
glVertex3f(-10, -3, -15);
glVertex3f(-10, -3, 11);
glVertex3f(10, -3, 11);
glVertex3f(10, -3, -15);
glEnd();
glPopMatrix();
glPushMatrix();
A_Complex();
glPopMatrix();
glPushMatrix();
B_Complex();
glPopMatrix();
glPushMatrix();
C_Complex();
glPopMatrix();
glPushMatrix();
D_Complex();
glPopMatrix();
glPushMatrix();
complex();
glPopMatrix();
glutSwapBuffers();
}
void reshape(int x, int y)
{
if (y == 0 || x == 0) return; //Nothing is visible then, so return
//Set a new projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,200);
glMatrixMode(GL_MODELVIEW);
//glViewport(0,0,x,y); //Use the whole window for rendering
glLoadIdentity();
}
void timer(int input){
if(start){
if(look.z > -12){
look.z -= .05;
if(look.z < 9){
if(look.x > 0){
look.x -= .05;
}
}
if(look.y > 4){
look.y -= .05;
}
}
if(look.z < -9){
eye.x += .5;
look.x += .2;
if(look.x > 7){
start = false;
}
}
} else {
eye.x = 0;
eye.z = 0;
eye.y = 0;
look.y = 10;
look.z = 20;
look.x = 0;
}
cout << "z - " << look.z << endl;
cout << "x - " << look.x << endl;
glutTimerFunc(30, timer, 1);
glutPostRedisplay();
}
void key(unsigned char key, int x, int y){
switch(key){
case 'g': {
start = true;
glutPostRedisplay();
break;
}
}
DrawCube();
}
void animation(void)
{
DrawCube();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(10, 10);
glutInitWindowSize(1000, 800);
glutCreateWindow(argv[0]);
init();
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutIdleFunc(animation);
glutTimerFunc(50, timer, 1);
glutMainLoop();
return 0;
}
以上是关于c_cpp C ++ 3D城市建筑飞行的主要内容,如果未能解决你的问题,请参考以下文章