Jump to content
  • Advertisement
Sign in to follow this  
nightcracker

OpenGL Opengl still tries to blur even with GL_NEAREST (GL_TEXTURE_2D)

This topic is 2542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

An image says a thousand words, so what about two? I have this map art:

Rd9zv.png

In order to actually use this as a map I scale this texture 6 times. This however didn't go as expected:

PA6Kr.png

All the OpenGL code is in my homebrew 2D OpenGL rendering library, and since OpenGL is a state machine it is hard to actually document the whole rendering process. But here is +/- what I do (the code is Python):


width, height = window.get_size()
glViewport(0, 0, width, height)

glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()

glOrtho(0.0, width, height, 0.0, 0.0, 1.0)

glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()

# displacement trick for exact pixelization
glTranslatef(0.375, 0.375, 0.0)

glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

glEnable(self.texture.target) # GL_TEXTURE_2D
glBindTexture(self.texture.target, self.texture.id)

glTexParameteri(self.texture.target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(self.texture.target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)

glPushMatrix()

glTranslatef(self.x, self.y, 0.0) # self.x and self.y are two negative integer offsets
glScalef(self.scale_x, self.scale_y, 1.0) # scale_x and scale_y are both 6

glBegin(GL_QUADS)
glTexCoord2i(0, 0)
glVertex2i(0, 0)
glTexCoord2i(0, 1)
glVertex2i(0, self.texture.height)
glTexCoord2i(1, 1)
glVertex2i(self.texture.width, self.texture.height)
glTexCoord2i(self.texture.width, 0)
glVertex2i(self.texture.width, 0)
glEnd()

glPopMatrix()
glDisable(self.texture.target)


However, this "blurring" bug doesn't occur when I use GL_TEXTURE_RECTANGLE_ARB. I'd like to also be able to use GL_TEXTURE_2D, so can someone please point out how to stop this from happening?

Share this post


Link to post
Share on other sites
Advertisement
Why don't you use FreeImage? These two functions load any image (except BMP and closed formats like PSD) into an OpenGL texture. I tried JPEG, PNG, TIFF, GIF all works fine.

#include <FreeImage.h>
struct image_data
{
BYTE* raw;
int width;
int height;
};
void load_image( std::string filename, image_data* data )
{
std::cout << "-Loading: " << filename << "\n";
// Get the image file type from FreeImage.
FREE_IMAGE_FORMAT file_format = FreeImage_GetFileType(( filename ).c_str(), 0 );
// Actually load the image file.
FIBITMAP* dib = FreeImage_Load( file_format, ( filename ).c_str(), 0 );
// Now, there is no guarantee that the image file
// loaded will be GL_RGBA, so we force FreeImage to
// convert the image to GL_RGBA.
dib = FreeImage_ConvertTo32Bits( dib );
if ( dib != NULL )
{
// Firstly, allocate the new bit data doe the image.
BYTE* bits = new BYTE[FreeImage_GetWidth( dib ) * FreeImage_GetHeight( dib ) * 4];
// get a pointer to FreeImage's data.
BYTE* pixels = FreeImage_GetBits( dib );
// Iterate through the pixels, copying the data
// from 'pixels' to 'bits' except in RGBA format.
for ( unsigned int pix = 0; pix < FreeImage_GetWidth( dib ) * FreeImage_GetHeight( dib ); pix++ )
{
bits[pix * 4 + 0] = pixels[pix * 4 + 3]; //r = 3
bits[pix * 4 + 1] = pixels[pix * 4 + 0]; //g = 0
bits[pix * 4 + 2] = pixels[pix * 4 + 1]; //b = 1
bits[pix * 4 + 3] = pixels[pix * 4 + 2]; //a = 2
}

//store data
data->raw = bits;
data->width = FreeImage_GetWidth( dib );
data->height = FreeImage_GetHeight( dib );
}
}
void texture_loader( image_data* data, texture* tex )
{
tex->create();
tex->bind();
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); //replace with nearest if needed
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, data->width, data->height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, data->raw );
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!