What would be the appropriate blend function?

Started by
18 comments, last by Axesor 18 years ago
I saw that anist had beat me.
Advertisement
I cannot get Anist's code to work either :(
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward
Here's my BMP loading module:

/*  Texture class  Relsoft (Richard Eric M. Lope BSN RN)  Uses SDL to load the image  Transparency is supported*/#ifndef TEXTURE_CPP#define TEXTURE_CPP#include <stdio.h>#include <stdlib.h>#include <math.h>#include <sdl\sdl.h>#include <gl\gl.h>#include <gl\glu.h>#include "ctexture.h"CTexture::CTexture(){}CTexture::CTexture(char *file_name){    if ( load(file_name) == 0)      {        fprintf( stderr, "Can't load texture!!!%s\n",             SDL_GetError( ) );        SDL_Quit( );        exit (3);    }    }CTexture::~CTexture(){}GLuint CTexture::texID(){    return texname;}int CTexture::load(char *file_name){    glGenTextures(1,&texname);    glBindTexture(GL_TEXTURE_2D,texname);    SDL_Surface *image = SDL_LoadBMP(file_name);    if (image == NULL)    {        SDL_FreeSurface(image);        return 0;    }        invert_sdl_image(image);	unsigned char *tex_RGBA;	tex_RGBA = convert_to_rgba(image);        glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_RGBA);    gluBuild2DMipmaps (GL_TEXTURE_2D, GL_RGBA,                      image->w, image->h, GL_RGBA,                      GL_UNSIGNED_BYTE, tex_RGBA);    //Free the surface since we don't have use for it anymore	SDL_FreeSurface (image);	delete [] tex_RGBA;	tex_RGBA = NULL; }int CTexture::set_trans_color(char sr=0, char sg=0, char sb=0){    r = sr;    g = sg;    b = sb;}int CTexture::destroy(){    }void CTexture::invert_sdl_image(SDL_Surface *image){    unsigned char *imgptr;    unsigned char *imgptr_below;    int texture_size;    int image_format;    image_format = image->format->BytesPerPixel;    texture_size = (image->w * image->h * image_format);    imgptr = (unsigned char*)image->pixels;    imgptr_below =  (imgptr + texture_size);    imgptr_below = imgptr_below - ((image_format * image->w));    int i,j,x,y;    //Flip the image    for (y = 0; y < (image->h / 2); y++)    {      for (x = 0; x < image->w; x++)      {          for (j = 0; j < image_format; j++)          {              unsigned char c = *(imgptr + j);              *(imgptr + j) = *(imgptr_below + j);              *(imgptr_below + j) = c;          }          imgptr_below = imgptr_below + image_format;          imgptr = imgptr + image_format;      }      imgptr_below = imgptr_below - ((image_format*image->w) * 2);    }    //invert channels        imgptr = (unsigned char*)image->pixels;    for (y = 0; y < image->h; y++)    {      for (x = 0; x < image->w; x++)      {          unsigned char c = *(imgptr);          *(imgptr)=*(imgptr+2);          *(imgptr+2) = c;          imgptr = imgptr + image_format;      }    }    }unsigned char* CTexture::convert_to_rgba(SDL_Surface *image){        unsigned char *imgptr;    int texture_size;    int image_format;    image_format = image->format->BytesPerPixel;    texture_size = (image->w * image->h * image_format);        unsigned char *texture_RGBA = new unsigned char[image->w * image->h * 4];    int i, j, x, y;    unsigned char ir, ig, ib;    imgptr = (unsigned char*)image->pixels;	i = 0;    for (y = 0; y < image->h; y++)    {      for (x = 0; x < image->w; x++)      {      	  ir = *(imgptr);      	  ig = *(imgptr + 1);      	  ib = *(imgptr + 2);      	  texture_RGBA = ir;<br>      	  texture_RGBA = ig;<br>      	  texture_RGBA = ib;<br>          <span class="cpp-keyword">if</span>  ( (ir == <span class="cpp-number">0</span>) &amp;&amp;  (ig == <span class="cpp-number">0</span>) &amp;&amp; (ib == <span class="cpp-number">0</span>) )<br>		  	  texture_RGBA = <span class="cpp-number">0</span>;     <span class="cpp-comment">//Transparent</span><br>          <span class="cpp-keyword">else</span><br>		  	  texture_RGBA = <span class="cpp-number">255</span>;   <span class="cpp-comment">//Opaque</span><br>          <br>          i = i + <span class="cpp-number">4</span>;<br>          imgptr = imgptr + image_format;<br>      }<br>    }<br><br>    <span class="cpp-keyword">return</span> texture_RGBA;<br>}<br><br><br><span class="cpp-directive">#endif</span><br><br><br></pre></div><!–ENDSCRIPT–><br><br><br>Here's the header:<br><br><br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br><br><span class="cpp-comment">/*<br>  Texture class<br>  Relsoft (Richard Eric M. Lope BSN RN)<br>  Uses SDL to load the image<br>  Transparency is supported<br>*/</span><br><span class="cpp-directive">#ifndef</span> TEXTURE_H<br><span class="cpp-directive">#define</span> TEXTURE_H<br><br><span class="cpp-directive">#include</span> &lt;stdio.h&gt;<br><span class="cpp-directive">#include</span> &lt;stdlib.h&gt;<br><span class="cpp-directive">#include</span> &lt;math.h&gt;<br><span class="cpp-directive">#include</span> &lt;sdl\sdl.h&gt;<br><br><br><br><span class="cpp-keyword">class</span> CTexture<br>{<br>    <span class="cpp-keyword">public</span>:<br>        CTexture::CTexture();<br>        CTexture::CTexture(<span class="cpp-keyword">char</span> *file_name);<br>        CTexture::~CTexture();<br>        <span class="cpp-keyword">int</span> load(<span class="cpp-keyword">char</span> *file_name);<br>        <span class="cpp-keyword">int</span> set_trans_color(<span class="cpp-keyword">char</span> sr, <span class="cpp-keyword">char</span> sg, <span class="cpp-keyword">char</span> sb);<br>        <span class="cpp-keyword">int</span> destroy();<br>        GLuint texID();<br>    <span class="cpp-keyword">private</span>:<br>    <span class="cpp-keyword">char</span> r,g,b;        <span class="cpp-comment">//mask color</span><br>    GLuint texname;          <span class="cpp-comment">//id</span><br>    <span class="cpp-keyword">void</span> invert_sdl_image(SDL_Surface *image);<br>    <span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">char</span> *convert_to_rgba(SDL_Surface *image);<br>};<br><br><br><span class="cpp-directive">#endif</span><br><br><br><br></pre></div><!–ENDSCRIPT–><br><br>Hope this helps. It uses SDL.<br><br>
Hi.
Axesor, instead of blindly copying and pasting, perhaps you should try to actually DEBUG the code.
You think I'd try that already?

PS: I am doing it in OpenGL, not SDL.
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward
Quote:Original post by Axesor
You think I'd try that already?

Uh... yes, I think you should consider it at this point. (?)
I have been on this forever and what I meant was that I have tried denugging it before I camplained <_<
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward
Actually that's for opengl, I just used SDL to load the BMP.
Hi.
I liked Anist's method but when I put down 64L for long int* width, it said it could compare a const long with a long*

What does that mean??
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward
I was able to debug Anist's code but it shows nothing but a white square...
.::WARNING!::. Axesor is a total newb or n00b! Beware his lack ofintellegence of OpenGL. Feel sorry for him and keep him in your thoughts.~Leader of the phsychoward

This topic is closed to new replies.

Advertisement