PDA

View Full Version : Can't draw to the alpha component of a single channel texture after creation



Silverlan
08-28-2014, 01:27 PM
My texture has a single channel only, however I can't seem to change its contents other than at the time of creation:


glDisable(GL_DEPTH_TEST);
unsigned int frameBuffer;
glGenFramebuffers(1,&frameBuffer);
unsigned int texture;
glGenTextures(1,&texture);

glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer);
int w = 256;
int h = 256;
unsigned char *pixels = new unsigned char[w *h];
for(unsigned int i=0;i<(w *h);i++)
pixels[i] = 128;
glTexImage2D(
GL_TEXTURE_2D,0,
GL_ALPHA16,w,h,
0,GL_ALPHA,
GL_UNSIGNED_BYTE,
&pixels[0] // Works fine
);
delete[] pixels;
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATT ACHMENT0,GL_TEXTURE_2D,texture,0);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
glDeleteTextures(1,&texture);
return;
}
glClearColor(1,1,1,1);
glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT); // Has no effect
glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);


Even after the glClear-Call the content of the frame buffer is still the same as what I've initialized it with (128 for each pixel). And yes, the frame buffer status reports as complete. Trying to write anything to it via shader has no effect either:



// Fragment shader
#version 330 core
in vec2 uv;

out float color;

uniform sampler2D tex; // The generated alpha-texture from above

void main()
{
color = 1.f;
}


glGetError doesn't report any errors anywhere. I'm using OpenGL 4.4.
Am I missing something here?

Yandersen
08-28-2014, 01:51 PM
glViewport, glScissor, glDrawBuffers, e.t.c....?

Silverlan
08-28-2014, 02:12 PM
Well I'll be damned. The scissor test was the problem, I still had it active from a different test.
Thank you.

// Edit:
Well, the glClear-Call works now, but still no luck when trying to render to it via shader. Is it correct to use a single float for the out-value?

Yandersen
08-28-2014, 02:21 PM
Nope. You must output 4 values. Only the one will be taken - the last one as you draw into the 4th channel. :)

Silverlan
08-29-2014, 12:46 AM
Nope. You must output 4 values. Only the one will be taken - the last one as you draw into the 4th channel. :)

Hm... Still having a bit of trouble with the shader.
Using glClear works as expected now:


glClearColor(0.f,0.f,0.f,0.75f);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.f,0.f,0.f,1.f);
unsigned char *pixels = new unsigned char[w *h];
for(unsigned int i=0;i<(w *h);i++)
pixels[i] = 0;
glReadPixels(0,0,w,h,GL_ALPHA,GL_UNSIGNED_BYTE,&pixels[0]);
for(unsigned int i=0;i<(w *h);i++)
std::cout<<i<<": "<<(unsigned int(pixels[i]))<<std::endl; // Outputs '191' for each pixel


The shader however has some odd results:


// Vertex shader
#version 330 core

layout(location = 0) in vec3 vertPos;

out vec2 UV;

void main()
{
gl_Position = vec4(vertPos,1);
UV = (vertPos.xy +vec2(1,1)) /2.0;
}

// Fragment shader
#version 330 core
in vec2 UV;

out vec4 color;

void main()
{
color = vec4(0.f,0.f,0.f,0.75f);
}




unsigned int shader = LoadShader("screen/vs_screen.gls","wgui/fs_wgui_text.gls"); // Shader is loading fine, no errors
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER,WGUI::GetQuadVertexBu ffer()); // Just some vertices that fill out the entire area

glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(voi d*)0);
glDrawArrays(GL_TRIANGLES,0,6);
glDisableVertexAttribArray(0);

After running this (Again, glGetError reports no errors), the alpha of each pixel in the texture is set to 64. In fact it doesn't matter what color I output in the fragment shader, it always ends up as 64?

Yandersen
08-29-2014, 05:21 AM
No one likes VAO nowadays as I see. Too bad, because without it nothing will be drawn. :)
Aside from that, set the different alpha in a shader rather than the same 0.75 you use in glClearColor. And btw, in glsl the fp numbers do not have "f" at the end (similar to the definition of double precision fp in C) - nVidia is OK with that, others may follow the specs directly.

Silverlan
08-29-2014, 11:36 AM
Aside from that, set the different alpha in a shader rather than the same 0.75 you use in glClearColor.
I've tried it with various different values, 0.25, 0.75, 0.18, etc, the result is always '64' for all pixels.


Aside from that, set the different alpha in a shader rather than the same 0.75 you use in glClearColor. And btw, in glsl the fp numbers do not have "f" at the end (similar to the definition of double precision fp in C) - nVidia is OK with that, others may follow the specs directly.
I have an AMD card and it's working fine, but thanks, I'll remember that.