Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

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

  1. #1
    Intern Newbie
    Join Date
    Dec 2012
    Posts
    40

    Question Can't draw to the alpha component of a single channel texture after creation

    My texture has a single channel only, however I can't seem to change its contents other than at the time of creation:
    Code :
    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_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,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:

    Code :
    // 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?

  2. #2
    Junior Member Regular Contributor
    Join Date
    Dec 2010
    Location
    Oakville, ON, CA
    Posts
    165
    glViewport, glScissor, glDrawBuffers, e.t.c....?

  3. #3
    Intern Newbie
    Join Date
    Dec 2012
    Posts
    40
    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?
    Last edited by Silverlan; 08-28-2014 at 02:18 PM.

  4. #4
    Junior Member Regular Contributor
    Join Date
    Dec 2010
    Location
    Oakville, ON, CA
    Posts
    165
    Nope. You must output 4 values. Only the one will be taken - the last one as you draw into the 4th channel.

  5. #5
    Intern Newbie
    Join Date
    Dec 2012
    Posts
    40
    Quote Originally Posted by Yandersen View Post
    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:
    Code :
    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:
    Code :
    // 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);
    }

    Code :
    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::GetQuadVertexBuffer()); // Just some vertices that fill out the entire area
     
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)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?

  6. #6
    Junior Member Regular Contributor
    Join Date
    Dec 2010
    Location
    Oakville, ON, CA
    Posts
    165
    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.
    Last edited by Yandersen; 08-29-2014 at 05:27 AM.

  7. #7
    Intern Newbie
    Join Date
    Dec 2012
    Posts
    40
    Quote Originally Posted by Yandersen View Post
    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.

    Quote Originally Posted by Yandersen View Post
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •