简体   繁体   English

如何使用OpenGL ES 2.0和GLKit设置图像的不透明度?

[英]How to set the opacity of an image using OpenGL ES 2.0 and GLKit?

I am new to iOS and OpenGL programming, and I am currently writing a simple program using OpenGL ES 2.0 and GLKit for practicing. 我是iOS和OpenGL编程的新手,目前正在使用OpenGL ES 2.0和GLKit编写一个简单的程序进行练习。 Right now I can successfully load a PNG file and display it on the screen. 现在,我可以成功加载PNG文件并将其显示在屏幕上。

I used GLKViewController in my program, and did some initialization in viewDidLoad . 我在程序中使用了GLKViewController,并在viewDidLoad进行了一些初始化。 Here's the code in my glkView:drawInRect method: 这是我的glkView:drawInRect方法中的代码:

glClearColor(115.0/255.0, 171.0/255.0, 245.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

self.effect.texture2d0.name = self.textureInfo.name;
self.effect.texture2d0.enabled = YES;

[self.effect prepareToDraw];

glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);

long offset = (long)&_quad;
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImageVertex), (void*)(offset + offsetof(ImageVertex, geometryVertex)));
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(ImageVertex), (void*)(offset + offsetof(ImageVertex, textureVertex)));

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

The above code works pretty well. 上面的代码效果很好。 Now I want to set the opacity of the PNG image. 现在,我要设置PNG图像的不透明度。 This may sound simple, but I have no idea how I can change the opacity... 这听起来很简单,但是我不知道如何更改不透明度...

I suspect your fragment shader is giving a constant 1.0 value for the alpha channel to gl_FragColor. 我怀疑您的片段着色器为gl_FragColor的alpha通道提供了恒定的1.0值。 That value should vary to produce blending. 该值应变化以产生混合。 Please see the answers to this question: 请查看此问题的答案:

How can I get Alpha blending transparency working in OpenGL ES 2.0? 如何在OpenGL ES 2.0中使用Alpha混合透明度?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM