青少年空军招飞定选拔:[code]3D空间中移动图像

来源:百度文库 编辑:九乡新闻网 时间:2024/04/25 05:40:31
#define _CRT_SECURE_NO_WARNINGS#pragma comment (lib,"glaux.lib") #include #include #include #include #include #include #include #include #define WIDTH 500#define HEIGHT 500
GLfloat rtri; // Angle For The Triangle ( NEW )GLfloat rquad; // Angle For The Quad ( NEW )
#define num 50 // Number Of Stars To Draw
typedef struct // Create A Structure For Star{ int r, g, b; // Stars Color GLfloat dist, // Stars Distance From Center angle; // Stars Current Angle}stars;
stars star[num]; // Need To Keep Track Of 'num' Stars
GLfloat zoom=-15.0f; // Distance Away From StarsGLfloat tilt=0.0f; // Tilt The ViewGLfloat spin; // Spin Stars
GLuint loop; // General Loop VariableGLuint texture[1]; // Storage for 1 textures
// Load Bitmap And Convert To A TextureGLvoid LoadGLTextures(){ // Load Texture AUX_RGBImageRec *texture1;
texture1 = auxDIBImageLoad(TEXT("Data/star.bmp")); if (!texture1) { exit(1); }
glGenTextures(1, &texture[0]); // Generate 1 Texture
// Create Linear Filtered Texture glBindTexture(GL_TEXTURE_2D, texture[0]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);};
int InitGL() // All Setup For OpenGL Goes Here{ LoadGLTextures(); // Load The Texture(s) glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black glClearDepth(1.0); // Enables Clearing Of The Depth Buffer glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f,1.0,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW); glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Set The Blending Function For Translucency glEnable(GL_BLEND);
for (loop=0; loop return 1;}
void myReshape(int w, int h){ printf("---------myReshape\n"); glViewport(0,0,w,h); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity();}
void myDisplay(void){ printf("---------myDisplay\n"); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glBindTexture(GL_TEXTURE_2D, texture[0]);
for (loop=0; loop spin+=0.01f; star[loop].angle+= (float)(loop)/num; star[loop].dist-=0.01f; if (star[loop].dist<0.0f) { star[loop].dist+=5.0f; star[loop].r=rand()%256; star[loop].g=rand()%256; star[loop].b=rand()%256; } }
glFlush();
glutSwapBuffers();}
void myIdle(void){ printf("---------myIdle\n");    myDisplay();}

int main(int argc, char* argv[]){    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);    glutInitWindowPosition(200, 200);    glutInitWindowSize(WIDTH, HEIGHT);    glutCreateWindow("OpenGL光照演示");
InitGL();
    glutDisplayFunc(&myDisplay); glutReshapeFunc(&myReshape); glutIdleFunc(&myIdle);   
    glutMainLoop();    return 0;}