[1.2+OpenGL]-How To Disable Image Blending/Smoothing?

[1.2+OpenGL]-How To Disable Image Blending/Smoothing?

Hi,

I am trying to figure out how to disable image blending/smoothing
when using current SDL 1.2 with OpenGL.

Here Is The Original PNG Image Sprite:
[Image: http://16bitsoft.com/images/LF2-Smoothing-Issue/16BitSoft-Logo.png ]

Here Is The Output To Screen:
[Image: http://16bitsoft.com/images/LF2-Smoothing-Issue/LF2-SDL-OGL-Smooth-Issue1.png ]

Here is relevant source code:

Initialize SDL+OpenGL Window:

Code:
//-------------------------------------------------------------------------------------------------
bool Visuals::SetupGameWindow(int width, int height)
{
putenv( strdup(“SDL_VIDEO_CENTERED=1”) );

if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) != 0 )///SDL_INIT_EVERYTHING) != 0 )
{
    printf("ERROR: Unable to initialize SDL: %s\n", SDL_GetError());
    return false;
}
else  printf("SDL (Everything) initialized\n");

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

SDL_WM_SetCaption("LettersFall[TM] - (C)2010 by 16BitSoft(R)", NULL);
SDL_WM_SetIcon(SDL_LoadBMP("data/graphics/Icon.bmp"), NULL);

Screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL | SDL_RESIZABLE);
if (!Screen)
{
    printf("ERROR: SDL+OpenGL resizable window create failed\n");
    return false;
}
else fprintf(stdout, "SDL+OpenGL resizable window created\n");

ScreenWidth = width;
ScreenHeight = height;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glDisable(GL_DEPTH);
glDisable(GL_DEPTH_TEST);
printf("OpenGL initialized\n");

if ( TTF_Init() == -1 )
{
    fprintf(stderr, "ERROR: TTF_Init: %s\n", TTF_GetError());
    return false;
}
else fprintf(stdout, "SDL_TTF initialized\n");

#ifdef SDL_BIGENDIAN
    R_Mask = 0xff000000;
    G_Mask = 0x00ff0000;
    B_Mask = 0x0000ff00;
    A_Mask = 0x000000ff;
    ColorIsRGBA = true;
#else
    R_Mask = 0x00ff0000;
    G_Mask = 0x0000ff00;
    B_Mask = 0x000000ff;
    A_Mask = 0xff000000;
    ColorIsRGBA = false;
#endif

return true;

}
//-------------------------------------------------------------------------------------------------

Load & Initialize PNG Image Sprites:

Code:
//-------------------------------------------------------------------------------------------------
bool Visuals::LoadSpritesIntoMemoryAndInitialize(void)
{
SDL_Surface *tempSurface;
bool loadedImageIsOK = true;
GLenum textureFormat = 0;
GLint numberOfColors;
bool imageToTextureOK = true;
char filePath[256];

for (int index = 0; index < NumberOfSprites; index++)
    Sprites[index].OGL_Texture = (GLuint)NULL;

for (int index = 0; index < NumberOfSprites; index++)
{
    switch(index)
    {
        case 0:
            strcpy(filePath, "data/graphics/Screen-Fade-Black.png");
            break;

        case 10:
            strcpy(filePath, "data/graphics/16BitSoft-Logo.png");
            break;

        default:
            break;
    }

    if (filePath[0] != '~')
    {
        tempSurface = NULL;
        if ( (tempSurface = IMG_Load(filePath)) )
        {
            if ( (tempSurface->w & (tempSurface->w - 1)) != 0 )
            {
                printf("ERROR: Image width is not a power of 2\n");
                loadedImageIsOK = false;
            }

            if ( (tempSurface->h & (tempSurface->h - 1)) != 0 )
            {
                printf("ERROR: Image height is not a power of 2\n");
                loadedImageIsOK = false;
            }

            if (loadedImageIsOK == true)
            {
                numberOfColors = tempSurface->format->BytesPerPixel;
                if (numberOfColors == 4)
                {
                    textureFormat = GL_RGBA;
                }
                else if (numberOfColors == 3)
                {
                    textureFormat = GL_RGB;
                }
                else
                {
                    printf("ERROR: The image is not truecolor\n");
                    imageToTextureOK = false;
                }

                Sprites[index].ScreenX = 320;
                Sprites[index].ScreenY = 240;
                Sprites[index].ScaleX = 1;
                Sprites[index].ScaleY = 1;
                Sprites[index].RotationDegree = 0;
                Sprites[index].Transparency = 1;
                Sprites[index].redHue = 1;
                Sprites[index].greenHue = 1;
                Sprites[index].blueHue = 1;
                Sprites[index].Smooth = true;
                Sprites[index].FlipX = false;
                Sprites[index].Width = tempSurface->w;
                Sprites[index].Height = tempSurface->h;
                Sprites[index].CenterX = Sprites[index].Width / 2;
                Sprites[index].CenterY = Sprites[index].Height / 2;

                Sprites[index].OGL_Texture = (GLuint)NULL;
                if (imageToTextureOK == true)
                {
                    glGenTextures( 1, &Sprites[index].OGL_Texture );

                    glBindTexture( GL_TEXTURE_2D, Sprites[index].OGL_Texture );

                    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, numberOfColors, tempSurface->w, tempSurface->h, 0,
                                  textureFormat, GL_UNSIGNED_BYTE, tempSurface->pixels );

                    if ( tempSurface )
                    {
                        SDL_FreeSurface( tempSurface );
                    }
                }
            }
        }
        else
        {
            printf("ERROR: SDL could not load image %s\n", SDL_GetError());
            loadedImageIsOK = false;
            imageToTextureOK = false;
        }

        if (loadedImageIsOK == false || imageToTextureOK == false)  index = NumberOfSprites+1;
    }

    strcpy(filePath, "~");
}

if (loadedImageIsOK == true && imageToTextureOK == true)
{
    printf("All sprite images loaded and initialized\n");
    return true;
}
else
    return false;

}
//-------------------------------------------------------------------------------------------------

Display PNG Image Sprite To SDL+OpenGL Screen Buffer:

Code:
//-------------------------------------------------------------------------------------------------
void Visuals::DrawSpriteOntoScreenBuffer(Uint16 index)
{
glPushMatrix();

glTranslatef(Sprites[index].ScreenX, Sprites[index].ScreenY, 0.0f);
glScalef(Sprites[index].ScaleX, Sprites[index].ScaleY, 1.0f);
glRotatef(Sprites[index].RotationDegree, 0.0f, 0.0f, 1.0f);

glBindTexture( GL_TEXTURE_2D, Sprites[index].OGL_Texture );

//=DOES=NOT=WORK============================
if (Sprites[index].Smooth == false)
{
glDisable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
}
else
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
//=DOES=NOT=WORK============================

glColor4f(Sprites[index].redHue, Sprites[index].greenHue, Sprites[index].blueHue
          , Sprites[index].Transparency);

if (Sprites[index].FlipX == false)
{
    glBegin( GL_QUADS );
        glTexCoord2i( 0, 0 );// Top-left vertex (corner)
        glVertex3f( 0-(Sprites[index].Width/2), 0-(Sprites[index].Height/2), 0 );

        glTexCoord2i( 1, 0 );// Bottom-left vertex (corner)
        glVertex3f( 0+(Sprites[index].Width/2), 0-(Sprites[index].Height/2), 0 );

        glTexCoord2i( 1, 1 );// Bottom-right vertex (corner)
        glVertex3f( 0+(Sprites[index].Width/2), 0+(Sprites[index].Height/2), 0 );

        glTexCoord2i( 0, 1 );// Top-right vertex (corner)
        glVertex3f( 0-(Sprites[index].Width/2), 0+(Sprites[index].Height/2), 0 );
    glEnd();
}
else if (Sprites[index].FlipX == true)
{
    glBegin( GL_QUADS );
        glTexCoord2i( 0, 0 );// Top-left vertex (corner)
        glVertex3f( 0+(Sprites[index].Width/2), 0-(Sprites[index].Height/2), 0 );

        glTexCoord2i( 1, 0 );// Bottom-left vertex (corner)
        glVertex3f( 0-(Sprites[index].Width/2), 0-(Sprites[index].Height/2), 0 );

        glTexCoord2i( 1, 1 );// Bottom-right vertex (corner)
        glVertex3f( 0-(Sprites[index].Width/2), 0+(Sprites[index].Height/2), 0 );

        glTexCoord2i( 0, 1 );// Top-right vertex (corner)
        glVertex3f( 0+(Sprites[index].Width/2), 0+(Sprites[index].Height/2), 0 );
    glEnd();
}

glPopMatrix();

}
//-------------------------------------------------------------------------------------------------

main.cpp:

Code:
/*----------------------------------------------------
TM
LettersFall
Version 2.0

Cross-Platform: Linux / Windows® / Mac OS X® OSs

     (C)opyright 2010, By 16BitSoft(R)

*/

#include “SDL.h”
#include “SDL_image.h”
#include “SDL_ttf.h”
#include “SDL_mixer.h”
#include “SDL_opengl.h”

#include “game.h”
#include “visuals.h”
#include “input.h”

Game *game;
Visuals *visuals;
Input *input;

int main( int argc, char* args[] )
{
game = new Game();

visuals = new Visuals();
if (visuals->SetupGameWindow(640, 480) != true)  game->CoreFailure = true;
if (visuals->LoadSpritesIntoMemoryAndInitialize() != true)  game->CoreFailure = true;
if (visuals->LoadFontsIntoMemory() != true)  game->CoreFailure = true;

input = new Input();

printf("Main loop started...\n");

//---------------------------------------------------------------------------------------
while (game->ExitGame == false && game->CoreFailure == false)
{
game->SystemTicks = SDL_GetTicks();
game->NextFrameTicks = game->SystemTicks + 17;
game->CalculateFramesPerSecond();

    input->GetAllUserInput();
    if (input->KeyOnKeyboardPressedByUser == '~')  game->ExitGame = true;

    visuals->ClearScreenBufferWithColor(0.0f, 0.0f, 0.0f, 1.0f);

//-TEST------------------------------------------------------------------------
visuals->Sprites[10].ScreenX = 320;
visuals->Sprites[10].ScreenY = 240;
visuals->Sprites[10].ScaleX = 5.0;
visuals->Sprites[10].ScaleY = 8.0;
visuals->Sprites[10].Smooth = false;
visuals->DrawSpriteOntoScreenBuffer(10);

    visuals->DrawTextOntoScreenBuffer("www.16BitSoft.com", visuals->Font
                                      , 0, 445, JustifyCenter, 1, 255, 1, 1, 90, 1);

//------------------------------------------------------------------------TEST-

    visuals->DisplayScreenBufferOntoDisplay();

    game->SystemTicks = SDL_GetTicks();
    if (game->NextFrameTicks > game->SystemTicks)  SDL_Delay(game->NextFrameTicks-game->SystemTicks);

}

//---------------------------------------------------------------------------------------
delete input;
delete visuals;
delete game;

printf("SDL+OpenGL application successfully closed\n");
return 0;

}
// A 100% By 16BitSoft®…

Sorry for the long posting…
Any help would be appreciated!

Jesse

Try changing GL_LINEAR to GL_NEAREST, on these two lines.On 27/05/2010 11:39, JeZ-l-Lee wrote:

                     glTexParameteri( GL_TEXTURE_2D,

GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_LINEAR );

Tim Angus wrote:> On 27/05/2010 11:39, JeZ-l-Lee wrote:

glTexParameteri( GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_LINEAR );

Try changing GL_LINEAR to GL_NEAREST, on these two lines.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks! That worked perfectly!

Jesse


JeZ+Lee
SLNTHERO @ aol.com (remove spaces)
Silent Hero Productions®
Video Game Design Studio

www.SilentHeroProductions.com