简体   繁体   中英

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. Right now I can successfully load a PNG file and display it on the screen.

I used GLKViewController in my program, and did some initialization in viewDidLoad . Here's the code in my glkView:drawInRect method:

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. 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. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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