I want to load a default colour vertColor if the sampler2D wasn't loaded with an image, to achieve that I have tried the following:
Fragment Shader:
#version 410
uniform sampler2D tex0;
in vec4 vertColor;
in vec2 TexCoords;
out vec4 fragColor;
void main( void )
{
vec4 texColor = texture(tex0, TexCoords);
if(tex0 == -1) fragColor = blinnPhongModel(vertColor);
else{
texColor.a = 1.0;
fragColor = texColor;
}//end of else
} // End of main
but it didn't work, how can I do that?
How to load a default colour if the sampler2D was empty?
Why do you want to do that? I recommend making sure that the sampler is never "empty".
If you want to draw with a uniform color using a sampler, create a 1x1 texture with a single pixel. With this trick, you don't need to branch in the shader.
All you have to do is to create a 1x1 texture. This texture has 1 pixel and 1 color.
The fragment shader does just the texture lookup and has not branch at all:
#version 410
uniform sampler2D tex0;
in vec2 TexCoords;
out vec4 fragColor;
void main( void )
{
vec4 texColor = texture(tex0, TexCoords);
fragColor = texColor;
}
Related
I am Having an issue where the blending to get transparency in OpenGL isn't working. The alpha channel is effectively just turning the color to black(might be the same color as my clear color). I have enabled blending and the blendfunc. I also have tested that I am submitting my color data correctly as the transparency isn't showing up even if I hard code it in. The geometry is just overriding all color behind it. Any tips on how to debug?
// Blending function for Alpha channels
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Allows for depth testing between z-values
glEnable(GL_DEPTH_TEST);
#type vertex
#version 330 core
layout(location = 0) in vec3 a_Position;
layout(location = 1) in vec4 a_Color;
layout(location = 2) in vec2 a_TexCoord;
layout(location = 3) in float a_TexIndex;
uniform mat4 u_VPMatrix;
out vec4 v_Color;
out vec2 v_TexCoord;
out float v_TexIndex;
void main() {
v_Color = a_Color;
v_TexCoord = a_TexCoord;
v_TexIndex = a_TexIndex;
gl_Position = u_VPMatrix * vec4(a_Position, 1.0);
}
#type fragment
#version 330 core
in vec4 v_Color;
in vec2 v_TexCoord;
in float v_TexIndex;
uniform sampler2D u_Textures[32];
//uniform float u_TilingFactor;
void main() {
gl_FragColor = texture(u_Textures[int(v_TexIndex)], v_TexCoord) * v_Color;
}
When loading the jpg image using SOIL, the image is displayed slanted and the colors are not right (i don't know how to describe it, because it is in color, but it looks black and white)
The intended version
However, it is displayed like this
The shaders are:
vertex shader
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 texCoord;
out vec3 ourColor;
out vec2 TexCoord;
void main(){
gl_Position = vec4(position, 1.0f);
ourColor = color;
TexCoord = texCoord;
}
fragment shader
#version 330 core
in vec3 ourColor;
in vec2 TexCoord;
out vec4 color;
uniform sampler2D ourTexture;
void main(){
color = texture(ourTexture, TexCoord);
}
How to display it correctly, the intended way?
By default OpenGL assumes that the start of each row of an image is aligned with 4 bytes. This is because the GL_UNPACK_ALIGNMENT parameter by default is 4. If the format of the image is RGB and width*3 is not divisibly by 4, you must change the parameter before specifying the two-dimensional texture image (glTexImage2D):
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
I'm trying to texture a quad in OpenGL and keep getting -1 when getting the uniform location.
EDIT: Without the Uniform code, it renders just fine(minus the texture ofc).
here's the code:
texture binding code;
m_renderer.prepare();
m_colorProgram.start();
// Sets the active texture to '0'.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_texture.id);
GLint samplerLoc = m_colorProgram.getUniformLocation("mySampler");
// Tells OGl that we are using the active texture '0' as stated above.
glUniform1i(samplerLoc, 0);
m_renderer.render(m_model);
// Always unbind
glBindTexture(GL_TEXTURE_2D, 0);
m_colorProgram.stop();
m_colorProgram.getUniformLocation:
GLint GLSLProgram::getUniformLocation ( const std::string uniformName )
{
GLint location = glGetUniformLocation ( m_programID, "size" );
if ( location == (GLint)GL_INVALID_INDEX ) {
fatal_error ( "Uniform '" + uniformName + "' could not be found!" );
}
return location;
}
vert shader:
#version 130
// super simple shader program
in vec3 vertexPositon;
in vec4 vertexColor;
in vec2 vertexUV;
out vec4 fragColor;
out vec2 fragUV;
void main(){
gl_Position = vec4(vertexPositon,1.0);
fragColor = vertexColor;
fragUV = vertexUV;
}
frag shader:
#version 130
in vec4 fragColor;
in vec2 fragUV;
uniform sampler2D mySampler;
out vec4 color;
void main()
{
vec4 texColor = texture( mySampler, fragUV );
color = texColor * fragColor;
}
Most of the solutions I've found online seem to indicate that the Uniform is is being cut out because it is not being used, however, as you can see, it IS being used, and yet still is returning a -1.
Can anyone tell me why this is happening?
Turns out I had hardcoded a string without realizing it in glGetUniform
I want to darken the corners of my little quad in my program. I have the following vertex shader:
#version 130
varying vec4 v_color;
varying vec2 v_texcoord;
void main()
{
v_color = gl_Color.rgba;
v_texcoord = gl_MultiTexCoord0.xy;
gl_FrontColor = vec4(v_color.r, v_color.g, v_color.b, 1.0f);
gl_Position = ftransform();
}
And my fragment shader:
#version 130
uniform sampler2D u_texture;
varying vec4 v_color;
varying vec2 v_texcoord;
void main()
{
gl_FragColor = v_color * texture2D(u_texture, v_texcoord);
}
I read somewhere that gl_FrontColor could be used to "color" vertices, but no matter what I change the values to, it always seems to stay the same.
My question is, what function can I use to set the color of my vertices? I want the vertices to be slightly darker than the rest of the quad so it looks a little "nicer".
You output to both v_color (your varying), and gl_FrontColor (GLSL builtin). But, in fragment shader, you only use v_color, so anything that is in gl_FrontColor is being ignored.
You should use only one of these. Either
// vertex
#version 130
#define SCALE_FACTOR 0.5
varying vec4 v_color;
varying vec2 v_texcoord;
void main()
{
v_color = vec4(gl_Color.rgb * SCALE_FACTOR, 1.0);
v_texcoord = gl_MultiTexCoord0.xy;
gl_Position = ftransform();
}
// fragment
#version 130
uniform sampler2D u_texture;
varying vec4 v_color;
varying vec2 v_texcoord;
void main()
{
gl_FragColor = v_color * texture2D(u_texture, v_texcoord);
}
Or use gl_FrontColor in vertex and gl_Color in fragment shader, instead of your v_color (and remove this varying as it no longer needed).
Of course vertex gl_Color attribute comes from glColorPointer, - if you changed that colors, it would be changed in shader too.
How to scrolling a texture on a plane?
So I have a plane with a texture, can I use a shader to scroll left from right (infinite) the texture on it?
Setup the texture wrapping mode using
glTexParameteri(TextureID, L_TEXTURE_WRAP_S, GL_REPEAT)
Add the float uniform named Time to your texturing shader
Use something like texture2D(sampler, u + Time, v) while fetching texture sample.
Update the Time uniform using some timer in your code.
Here's a GLSL shader:
/*VERTEX_PROGRAM*/
in vec4 in_Vertex;
in vec4 in_TexCoord;
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix;
out vec2 TexCoord;
void main()
{
gl_Position = ProjectionMatrix * ModelViewMatrix * in_Vertex;
TexCoord = vec2( in_TexCoord );
}
/*FRAGMENT_PROGRAM*/
in vec2 TexCoord;
uniform sampler2D Texture0;
/// Updated in external code
uniform float Time;
out vec4 out_FragColor;
void main()
{
/// "u" coordinate is altered
out_FragColor = texture( Texture0, vec2(TexCoord.x + Time, TexCoord.y) );
}