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 2 of 2

Thread: BGRA texture image showing up only as BGR no alpha channel

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2016
    Posts
    12

    BGRA texture image showing up only as BGR no alpha channel

    I am loading a BMP image to opengl as a texture and the image is an 32 bit 4 channel per pixel set of data in the format BGRA

    it loads in my window, but the Alpha channel not really doing anything because when i change the background colour with glClearColor background remains black , the parts that are normally transparent in the original image(which i posted a link to below) are black in opengl and no matter what background i set remain black always

    here is my program if somebody can advise what am i missing

    Code :
    const char* vertex_shader =
       "#version 330 core\n"
       "layout(location =  0) in vec2 vp;"
       "layout(location = 1) in vec2 tex;"
       "uniform vec2 disp;"
       "out vec2 texCoord;"
       "void main () {"
       "   texCoord = tex; "
       "   vec4 correct = vec4 (vp+disp, 0.0f, 1.0f);"
       "   gl_Position = correct;"
       "}";
     
    const char* fragment_shader =
       "#version 330 core\n"
       "uniform sampler2D s;"
       "uniform vec2 displace;"
       "in vec2 texCoord;"
       "out vec4 color;"
       "void main () {"
       "vec2 texFix;"
       "texFix = texCoord + displace;"
       "color = texture(s, texFix);"
       "}";
     
    GLuint vs;
    GLuint fs;
    GLuint shader_program;
     
     
    void createShaders(){
     
       GLint result;
       GLsizei log_length;
       GLchar data[255]; 
       GLchar data2[255];
     
       vs = glCreateShader (GL_VERTEX_SHADER);
       glShaderSource (vs, 1, &vertex_shader, NULL);
       glCompileShader (vs);
     
       glGetShaderiv(vs, GL_COMPILE_STATUS,&result);
     
       if(result == GL_FALSE){ 
          glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &log_length);  
          glGetShaderInfoLog(vs, log_length, NULL, data );
          printf("vertex shader %s\n", data);
       }
     
       fs = glCreateShader (GL_FRAGMENT_SHADER);
       glShaderSource (fs, 1, &fragment_shader, NULL);
       glCompileShader (fs);
     
       glGetShaderiv(fs, GL_COMPILE_STATUS,&result);
     
       if(result == GL_FALSE){
          glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &log_length);   
          glGetShaderInfoLog(fs, log_length, NULL, data2 );
          printf("fragment shader %s\n", data2);
       }
     
       shader_program = glCreateProgram ();
       glAttachShader (shader_program, fs);
       glAttachShader (shader_program, vs);
       glLinkProgram (shader_program);
       glUseProgram (shader_program);  
    }

    Code :
    //create Texture object from image and load data into graphics memory
    GLuint createTexture(const char * name){
     
       //read pixel data into array from image
       unsigned char header[2];
       unsigned int startData;   
       unsigned int width;
       unsigned int height;
     
       FILE *file = fopen(name, "rb");
          if(!file){printf("Failed to open\n"); return 0;}
     
       fread(header, 1, 2, file);
       if(header[0]!= 'B' && header[1] != 'M'){
          printf("file not read as BMP, header incorrect...exiting\n");
          exit(0);
       }
     
       ///moves past irrelevant data
       fseek(file,8,SEEK_CUR);
       //read pixel data offset starting position
       fread(&startData, 1, 4, file);
     
       //skip DIB header
       fseek(file, 4, SEEK_CUR);
     
       //read image width and height
       fread(&width, 1, 4, file);
       fread(&height, 1, 4, file);
     
       unsigned int imageSize = width*height*4;
     
       //set position to start of data 
       fseek(file, startData, SEEK_SET); 
       unsigned char * data = (unsigned char *)malloc(imageSize);
     
       fread(data, 1, imageSize, file );
     
       fclose(file);
     
       //create texture object and upload data
       GLuint tex;
       glGenTextures(1, &tex);
       glBindTexture(GL_TEXTURE_2D, tex);
       //glTexStorage2D(GL_TEXTURE_2D, 7, GL_RGB, width, height);
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA,
                  GL_UNSIGNED_BYTE, data);
       //glGenerateMipmap(GL_TEXTURE_2D);  //Generate num_mipmaps number of mipmaps here.
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
     
       free(data);
       return tex;
    }
     
    GLuint createVao(){
       GLuint vao;
       glGenVertexArrays(1, &vao);
       glBindVertexArray(vao);
       return vao;
    }
    GLuint createVbo(){
       GLuint vbo;
       glGenBuffers(1, &vbo);
       glBindBuffer(GL_ARRAY_BUFFER, vbo);
       return vbo;
    }  
    void bindObject(GLuint vao, GLuint tex){
       glBindTexture(GL_TEXTURE_2D, tex);
       glBindVertexArray(vao);
    }

    Code :
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    #include <GL/glew.h>
    #include <GL/glx.h>
    #include <time.h>
    #include <math.h>
     
    #include "glx2.c"
    #include "testing.hpp"
    #include "new.hpp"
     
    #define WIDTH 1080 
    #define HEIGHT 720
     
    int main(){
     
       init(WIDTH,HEIGHT);
       createShaders();
     
       glEnable(GL_LIGHTING);
     
       GLuint disp = glGetUniformLocation(shader_program, "disp");
       GLuint disp2 = glGetUniformLocation(shader_program, "displace");
     
       char name[1][25];
     
       strcpy(name[0],"ugly.bmp");
     
       //glViewport(0,0, WIDTH,HEIGHT);
     
     
       ////-----------------------------create VAO and VBO and upload all the relevant vertices and information
     
       GLfloat vertices[] = { 
     
       -1.0, -1.0, 0.0, 0.0,
        1.0, -1.0, 1.0, 0.0,
       -1.0,  1.0, 0.0, 1.0,
        1.0,  1.0, 1.0, 1.0};  
     
     
       GLuint vao1 = createVao();
       GLuint vbo1 = createVbo();
     
       glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);  
     
       GLuint tex = createTexture(name[0]);
     
       //set up data format and save in vao
       glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), 0);
       glEnableVertexAttribArray(0);
     
       glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (const GLvoid*)(2 * sizeof(GLfloat)));
       glEnableVertexAttribArray(1);
     
       int running =1;
       XEvent event;
       KeySym key;
       float value;
       float value2=0.0f;
     
     
     
       while(running){
     
          while(XPending(dpy)){
             XNextEvent(dpy, &event);
     
             switch(event.type){
                case KeyPress:
                XLookupString(&event.xkey, NULL, 1, &key, NULL);
                   //printf("%c\n",string);
                switch(key){
     
     
                //letter 'q'
                   case 0x0071: 
                   running = 0;
                   break;
                }
                break;
             }
          }   
     
          // red green blue alpha
          glClearColor(0.0f,0.0f,1.0f,0.0f);
          glClear(GL_COLOR_BUFFER_BIT);
          glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
          glXSwapBuffers(dpy, glxWin);
     
       }//end of while running loop
     
       glXMakeCurrent( dpy, 0, 0 );
       glXDestroyContext( dpy, ctx );
       glXDestroyWindow(dpy, glxWin);
       XDestroyWindow( dpy, win );
       XFreeColormap( dpy, cmap );
       XCloseDisplay( dpy );
     
       return 0;
     
    }

    and here is the original image (it is originally bmp but gets converted to png when i upload it to postimg, you can convert it back to bmp in linux with mogrify -format bmp *.png if you want to test for yourself)

    https://postimg.org/image/pxg6wvv2f/

    and here is what i get when i load it in opengl

    https://postimg.org/image/onnsgy7zl/

    so when i load it in openGL iget no transparency even when i change background color with glClearColor(0.0f,0.0f,1.0f,0.0f);
    Last edited by hopjopper; 07-07-2016 at 10:20 AM.

  2. #2
    Junior Member Newbie
    Join Date
    Feb 2016
    Posts
    12
    well i found the solution to the problem got to enable these:

    glEnable( GL_BLEND );
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Posting Permissions

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