glcolor4f-绘制时无法获得完全不透明度
glColor4f - Cannot get full opacity when drawing
我正在重写渲染器(2D游戏的一部分)以使用OpenGL代替SDL。我对以下代码有问题:
void renderer_opengl::draw_rectangle_filled(int x, int y, int w, int h, SDL_Color color){
glColor4f((1.0f/255)*color.r, (1.0f/255)*color.g, (1.0f/255)*color.b, (1.0f/255)*color.a);
glBegin(GL_POLYGON);
glVertex2f(x, y);
glVertex2f(x + w, y);
glVertex2f(x + w, y + h);
glVertex2f(x, y + h);
glEnd();
glColor4f(1, 1, 1, 1);
}
(是的,我知道这是不弃用的GL,让我们现在就忽略它)这绘制了带有给定颜色和α值的矩形。问题是,即使设置这样的颜色:glColor4f(0, 0, 0, 1);
不会导致纯黑色,而是半透明的黑色(类似于将alpha设置为标准SDL渲染器中的50%)。
我启用了这样的混合:
glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
我尝试了glBlendFunc
的不同参数,但没有解决该问题。我永远无法获得纯色。矩形是在已经呈现到屏幕上的纹理上的。(这些矩形应为黑色,α大约70% - 太黑了,但仍然透明)。感谢您的任何帮助。
代码从SDL_Surface*
的std::unordered_map
加载纹理
void renderer_opengl::upload_surfaces(std::unordered_map<int, SDL_Surface*>* surfaces){
for ( auto it = surfaces->begin(); it != surfaces->end(); ++it ){
if(it->second != NULL && textures[it->first] == NULL){
glEnable(GL_TEXTURE_2D);
GLuint TextureID = new_texture_id();
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);
int Mode = GL_RGB;
if(it->second->format->BytesPerPixel == 4) {
Mode = GL_RGBA;
}
glTexImage2D(GL_TEXTURE_2D, 0, Mode, it->second->w, it->second->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, it->second->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
textures[it->first] = new texture(TextureID, Mode, it->second->w, it->second->h); //texture is just a struct that holds the GLuint, the Mode, width and height
}
}
}
绘制纹理的功能:
void renderer_opengl::draw_texture(int arg, int x, int y){
texture* Texture = textures[arg];
glBindTexture(GL_TEXTURE_2D, Texture->TextureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int Width = Texture->width;
int Height = Texture->height;
int X = x;
int Y = y - Height;
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3f(X, Y+ Height, 0);
glTexCoord2f(1, 1); glVertex3f(X + Width, Y + Height, 0);
glTexCoord2f(1, 0); glVertex3f(X + Width, Y , 0);
glTexCoord2f(0, 0); glVertex3f(X, Y , 0);
glEnd();
}
我在初始化代码中所做的工作与设置视口,宽高比等无关。
glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
整个渲染操作是这样的:
1。
glClearColor(0.0,0.0,0.0,0.0); //Screen is cleared
glClear( GL_COLOR_BUFFER_BIT );
呼叫
draw_texture
和draw_rectangle_filled
SDL_GL_SwapWindow(window);
,然后显示一切
我通过在绘制矩形之前调用glDisable(GL_TEXTURE_2D);
解决了问题。我想仍然有一个纹理绑定到OpenGL上下文,这引起了问题。现在一切都很完美。
相关文章:
- 如何从透明度计算不透明度?
- 图层不会绘制另一个图层
- OpenGL不会绘制三角形,遵循确切的格式
- SFML 对象在重新初始化其父类时不会绘制
- 在 openGL 中播放音频而不冻结绘制循环
- glcolor4f-绘制时无法获得完全不透明度
- 使用 glDepthFunc(GL_GREATER) 不会绘制任何内容
- OpenGL 3.3不能绘制任何东西
- VTK抽象选择器,用于不同不透明度值的多个参与者
- OpenGL 几何着色器三角形不会绘制而没有错误
- 在openFrameworks中,是否可以在使用ofxPiMapper时更改我的fbo源的不透明度
- FreeGrot(类似于OpenGL)正在拍摄我的桌面而不是绘制形状
- 如何使用GDI 显示具有25%不透明度的PNG图像?(MFC)
- 在 OpenGL 中,为什么除非我再次重新指定顶点属性,否则我的缓冲区对象不会绘制?
- OpenGL + SDL + glew 在成功初始化后不会绘制任何内容
- 为什么我的 openGL 顶点缓冲区对象不会绘制任何内容?
- "QGraphicsView"滚动后不调用"绘制"方法
- Opengl 3.3 不会绘制任何东西。使用 GLSL 330 内核
- VTK :如何将不透明度设置为同一演员的不同对象
- 当对象放入容器类中时,OpenGL 不会绘制