本文主要是介绍opengl 蝴蝶飞,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本人一直在学习opengl, 技术仍然有很大的发展空间 以下程序是蕴酿很长时间才写出来的,可能在有些高手大牛面前时小case 但是我用了大约一个星期做出来 这些博客记录了我一步步学习opengl的过程 希望有些代码能给其他新手一些idea。本程序读取图片用了freeimage库,这样可以读很多不同种的格式图片,真的很好用,所以大家在运行一下程序的时候要配置一下属性。另外我在蝴蝶后面加了一些花的纹理,可能看上去并不美观,如果你觉得不好看,可以将“贴一束花”那段代码删除,这样看上去比较好看。
以下程序:
#include<GL/glut.h>
#include "FreeImage.h"#include <Windows.h>
float angle;const int a=5;const int number=50;struct JSnow
{float x,y;int b;
};
JSnow Snow[number];
char* bmpfile[a]={("Data/2.jpg"),("Data/3.jpg"),("Data/5.jpg"),("Data/6.jpg"),("Data/hua.jpg")};
int w=500,h=500;
GLuint texture[a];
GLenum image_format = GL_BGR_EXT; //format the image is in
GLint internal_format = GL_RGB; //format to store the image in
GLint level = 0; //mipmapping level
GLint border = 0;
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
FIBITMAP *dib(0);
//BYTE* bits(0);
unsigned char *imageData;
unsigned int width(0), height(0);bool LoadImage(const char*filename)
{//unsigned int texID;unsigned int index1;unsigned char temp;
#ifdef FREEIMAGE_LIBFreeImage_Initalise();
#endifFIBITMAP *dib(0);fif = FreeImage_GetFileType(filename, 0);if(fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(filename);if(fif == FIF_UNKNOWN)return false;if(FreeImage_FIFSupportsReading(fif))dib = FreeImage_Load(fif, filename);if(!dib)return false;//bool flipResult = FreeImage_FlipVertical(dib);dib = FreeImage_ConvertTo24Bits(dib);imageData = (unsigned char *)FreeImage_GetBits(dib);//获取图像位信息;/*bits = FreeImage_GetBits(dib);*/width = FreeImage_GetWidth(dib);height = FreeImage_GetHeight(dib);int p=FreeImage_GetBPP(dib);if((imageData == 0) || (width == 0) || (height == 0))return false;//for(index1=0;index1<width*height*3;index1+=3)//BGR -> RGB 转换 //{//temp = imageData[index1];//imageData[index1] = imageData[index1+2];//imageData[index1+2] = temp;//}
}
void reshape(int width,int height)//调整和初始化GL的窗口
{if (height==0) // Prevent A Divide By Zero By{height=1; // Making Height Equal One}glViewport(0,0,width,height); // Reset The Current ViewportglMatrixMode(GL_PROJECTION); // Select The Projection MatrixglLoadIdentity(); // Reset The Projection Matrix// Calculate The Aspect Ratio Of The Window//gluPerspective(120.0f,(GLfloat)width/(GLfloat)height,1.0f,300.0f);glOrtho(0,w,0,h,-10.1,100);glMatrixMode(GL_MODELVIEW); // Select The Modelview MatrixglLoadIdentity(); // Reset The Modelview Matrix// gluLookAt(0.0,0.0,40.0,0.0,0.0,0.0,0.0,1.0,0.0);w=width;h=height;
}
int Init(void)
{glClearColor(1.0,1.0,1.0,1.0);glShadeModel(GL_SMOOTH);
// glEnable(GL_DEPTH_TEST);for (int j=0;j<a;j++){LoadImage(bmpfile[j]);glGenTextures(1,&texture[j]);glBindTexture(GL_TEXTURE_2D,texture[j]);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);glTexImage2D(GL_TEXTURE_2D, level, internal_format, width, height,border, image_format, GL_UNSIGNED_BYTE, imageData);}return true;
}
void InitSnow()
{for (int i=0;i<number;i++){Snow[i].b=rand()%4;Snow[i].x=rand()%w-10;Snow[i].y=rand()%h-10;}
}void display()
{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth BufferglEnable(GL_TEXTURE_2D);glColor3f(1.0,1.0,1.0);glMatrixMode( GL_MODELVIEW );
//接下来贴背景纹理 一束花glLoadIdentity();// Reset The ViewglBindTexture(GL_TEXTURE_2D,texture[4]);glBegin(GL_QUADS);glNormal3f( 0.0f, 1.0f, 0.0f);glTexCoord2f(0.0f, 0.0f);glVertex3f(0,0,0); //glVertex3f(-w/2.0,-h/2.0, 0.0f);glTexCoord2f(1.0f, 0.0f); glVertex3f(w,0,0); //glVertex3f(w/2.0,-h/2.0, 0.0f);glTexCoord2f(1.0f, 1.0f); glVertex3f(w,h,0); //glVertex3f( w/2, h/2, 0.0f);glTexCoord2f(0.0f, 1.0f);glVertex3f(0,h,0); //glVertex3f(-w/2, h/2, 0.0f);glEnd();glEnable(GL_DEPTH_TEST);//glMatrixMode(GL_MODELVIEW);//glColor3f(1.0,0.0,0.0);glPushMatrix();//glEnable(GL_BLEND); //glEnable(GL_ALPHA_TEST); //glAlphaFunc(GL_GREATER ,0.0);//0.5可以换成任安在0~1之间的数for (int i=0;i<number;i++){glPushMatrix();glTranslated(Snow[i].x,Snow[i].y,0.0);glRotated(angle,0.0,0.0,1.0); glBindTexture(GL_TEXTURE_2D, texture[Snow[i].b]);glBegin(GL_QUADS);glTexCoord2f(0.0f, 0.0f);glVertex3f(-w/50.0f,-h/50.0f, -1.0f);glTexCoord2f(1.0f, 0.0f); glVertex3f( w/50.0f,-h/50.0f, -1.0f);glTexCoord2f(1.0f, 1.0f);glVertex3f( w/50.0f, h/50.0f, -1.0f);glTexCoord2f(0.0f, 1.0f);glVertex3f(-w/50.0f, h/50.0f, -1.0f);glEnd();glPopMatrix();//angle+=0.05;//glRasterPos2f(Snow[i].x,Snow[i].y);}glutSwapBuffers();//glFlush();glDisable(GL_TEXTURE_2D);glDisable(GL_DEPTH_TEST);
}void idle()
{InitSnow();display();angle+=0.05f;Sleep(300);
}int main(int argc,char **argv)
{glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);glutInitWindowSize(500,500);glutCreateWindow(argv[0]);Init();InitSnow();glutReshapeFunc(reshape);glutDisplayFunc(display);glutIdleFunc(idle);glutMainLoop();return 0;
}
如果背景为空,运行程序如下:
如果加上背景:
这篇关于opengl 蝴蝶飞的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!