SDL2+OpenGL 混合图像产生动态效果
Posted CodeMIRACLE
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SDL2+OpenGL 混合图像产生动态效果相关的知识,希望对你有一定的参考价值。
利用OpenGL的glBlendFunc函数将下面这张图作为纹理产生动态效果
1 #include <GL/glew.h>
2 #include <SDL2/SDL.h>
3 #include <SDL2/SDL_opengl.h>
4 #include <SDL2/SDL_image.h>
5 bool quit;
6 SDL_Window* window;
7 SDL_GLContext glContext;
8 SDL_Event sdlEvent;
9 GLuint texture;
10 float x=0,y=0;
11 void Init()
12 {
13 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
14 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
15 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,32);
16 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
17 SDL_Init(SDL_INIT_EVERYTHING);
18 window = SDL_CreateWindow("SDL+OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,600,600,SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
19 glContext = SDL_GL_CreateContext(window);
20 SDL_GL_SetSwapInterval(1);
21 glewInit();
22 glEnable(GL_TEXTURE_2D);
23 glClearDepth(1.0f);
24 glDepthFunc(GL_LEQUAL);
25 glEnable(GL_DEPTH_TEST);
26 glShadeModel(GL_SMOOTH);
27 }
28 void LoadImage()
29 {
30 SDL_Surface *tmp=IMG_Load("water.png");
31 tmp = SDL_ConvertSurfaceFormat(tmp,SDL_PIXELFORMAT_ABGR8888,0);
32 texture=gluBuild2DMipmaps(GL_TEXTURE_2D,4,tmp->w,tmp->h,GL_RGBA,GL_UNSIGNED_BYTE,tmp->pixels);
33 SDL_FreeSurface(tmp);
34 }
35 void Draw()
36 {
37 if(x>=5) x=0;
38 if(y>=5) y=0;
39 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
40 glLoadIdentity();
41 glScaled(1.0f/10,1.0f/10,1.0f/10);
42
43 glPushMatrix();
44 glEnable(GL_BLEND);
45 glColor4f(1.0f,1.0f,1.0f,0.5f);
46 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
47 glTranslatef(0.0f, 0.0f, 1.0f);
48 glBindTexture(GL_TEXTURE_2D,texture);
49 glBegin(GL_QUADS);
50 glTexCoord2f(5.0f+x, 5.0f+y); glVertex3f(-10.0f, 10.0f, 0.0f);
51 glTexCoord2f(5.0f+x, 0.0f+y); glVertex3f( 10.0f, 10.0f, 0.0f);
52 glTexCoord2f(0.0f+x, 0.0f+y); glVertex3f( 10.0f, -10.0f, 0.0f);
53 glTexCoord2f(0.0f+x, 5.0f+y); glVertex3f(-10.0f, -10.0f, 0.0f);
54 glEnd();
55 glDisable(GL_BLEND);
56 glPopMatrix();
57
58 glPushMatrix();
59 glEnable(GL_BLEND);
60 glColor4f(1.0f,1.0f,1.0f,0.6f);
61 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_CONSTANT_ALPHA);
62 glTranslatef(0.0f, 0.0f, 0.0f);
63 glBindTexture(GL_TEXTURE_2D,texture);
64 glBegin(GL_QUADS);
65 glTexCoord2f(5.0f-x, 5.0f-y); glVertex3f(-10.0f, 10.0f, 0.0f);
66 glTexCoord2f(5.0f-x, -5.0f-y); glVertex3f( 10.0f, 10.0f, 0.0f);
67 glTexCoord2f(0.0f-x, -5.0f-y); glVertex3f( 10.0f, -10.0f, 0.0f);
68 glTexCoord2f(0.0f-x, 5.0f-y); glVertex3f(-10.0f, -10.0f, 0.0f);
69 glEnd();
70 glDisable(GL_BLEND);
71 glPopMatrix();
72
73 x+=0.005;
74 y+=0.005;
75 SDL_GL_SwapWindow(window);
76 }
77 int main(int argc, char *argv[])
78 {
79 quit = false;
80 Init();
81 LoadImage();
82 while (!quit)
83 {
84 while(SDL_PollEvent(&sdlEvent))
85 {
86 if(sdlEvent.type == SDL_QUIT)
87 {
88 quit = true;
89 }
90 }
91 Draw();
92 SDL_Delay(16);
93 }
94 SDL_DestroyWindow(window);
95 window = NULL;
96 SDL_Quit();
97 return 0;
98 }
效果(gif文件录制的太短了,效果不是很明显)
以上是关于SDL2+OpenGL 混合图像产生动态效果的主要内容,如果未能解决你的问题,请参考以下文章
IOS OpenGL ES GPUImage 图像透明混合 GPUImageAlphaBlendFilter