I can set my fragment shader to showing colors of the object or the texture color of the object. My question is, how can I combine those two so that I can have a transparrent picture with lines for bricks, and then show the different colors underneath, so that by changing the color of the object, you change the color of the bricks.
I tried using mix() for that in the fragment shader, but it only shows me the glClearColor where it is transparent insteath of the red color I have assigned it!
My fragment shader:
#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;
}
EDIT: Added texture loader func:
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);
}
GL_RGBA
or better yet,GL_RGBA8
. Your shader is GLSL 1.20, so your implementation may not supportGL_RGBA8
but it will definitely understandGL_RGBA
. – Andon M. ColemanSDL_CreateRGBSurface (...)
are to blame for your constant alpha value. On a little-endian machine (e.g. x86), I think you need an alpha mask (last parameter) of 0xff000000. For all other color channels, you can use a mask of 0, but alpha needs an explicit mask. – Andon M. Coleman