[英]OpenGL, blending transparent textures with object color
我可以將片段着色器設置為顯示對象的顏色或對象的紋理顏色。 我的問題是,如何將兩者結合在一起,才能獲得帶有磚塊線條的透明圖片,然后在下面顯示不同的顏色,以便通過更改對象的顏色來更改磚塊的顏色。
我嘗試在片段着色器中使用mix(),但是它只向我顯示了glClearColor,它透明地代表了我為其分配的紅色!
我的片段着色器:
#version 120
uniform sampler2D diffuse;
varying vec3 shared_colors;
varying vec2 shared_texCoords;
void main() {
vec4 color = vec4(shared_colors, 1);
vec4 texture = texture2D(diffuse, shared_texCoords);
vec4 finalColor = vec4(mix(color.rgb, texture.rgb, 1), 1);
gl_FragColor = finalColor;
}
編輯:添加了紋理加載器功能:
void Texture::createTexture(SDL_Surface *rawImage, GLenum format) {
//Convert to a texture of pure color pixels for OpenGL
SDL_Surface *image = SDL_CreateRGBSurface(NULL, rawImage->w, rawImage->h, 32, 0, 0, 0, 0);
SDL_BlitSurface(rawImage, NULL, image, NULL);
//Generate texture
glGenTextures(1, &m_texture);
//Tell OpenGL to use this texture
glBindTexture(GL_TEXTURE_2D, m_texture);
//Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Generate texture from image
glTexImage2D(GL_TEXTURE_2D, 0, 4, image->w, image->h, 0, format, GL_UNSIGNED_BYTE, image->pixels);
m_dimension = new PixelSize(image->w, image->h);
//Free loaded images
SDL_FreeSurface(rawImage);
SDL_FreeSurface(image);
}
mix (...)
以了解為什么使用1對a
有效的什么也不做有意義的( 它返回y)。 讓我們首先考慮常用的alpha混合函數: GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
。
這意味着獲取源的Alpha通道並將其乘以源顏色( 片段顏色 ),然后將其添加到Alpha通道的倒數乘以目標顏色( 幀緩沖區 )。
AlphaBlend = Src RGBA * Src A + Dst RGBA *( 1.0 - Src A )
現在,如果您看一下mix (...)
的實現,應該看起來很熟悉:
x *( 1.0 - a )+ y * a
顯然,這是為了扶養人的任何值a
= 0.5!(1.0 - 0.5 = 0.5),在這種情況下,更重要的是它完全拋出你的顏色之一,如果你使用了一個1.0的值a
(由該乘X 0.0 )。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.