Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Mipmapping for OpenGL texture creation?  (Read 24654 times)

0 Members and 1 Guest are viewing this topic.

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Mipmapping for OpenGL texture creation?
« on: September 18, 2010, 06:47:29 pm »
Hello,

Can we add in the ability to generate mipmapping.
Using

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

We can use the glGenerateMipmap() if everyone is on board for that vs. the above code...

I am thinking of something like this?

Code: [Select]

bool Image::CreateTexture()
{
 // Create the OpenGL texture
 if (!myTexture)
    {
       GLint PreviousTexture;
       GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &PreviousTexture));
 
        GLuint Texture = 0;
        GLCheck(glGenTextures(1, &Texture));
        GLCheck(glBindTexture(GL_TEXTURE_2D, Texture));
        GLCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, myTextureWidth, myTextureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));
        GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
        GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, myIsSmooth ? GL_LINEAR : GL_NEAREST));
if(myIsMipmapped)
GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, myIsSmooth ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST));
else
GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, myIsSmooth ? GL_LINEAR : GL_NEAREST));
GLCheck(glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE));
        myTexture = static_cast<unsigned int>(Texture);
        GLCheck(glBindTexture(GL_TEXTURE_2D, PreviousTexture));
   }
}


Mipmapping would be on by default and you would need to add bool IsMipmapped() and SetMipmap(bool)

Please advise!

Thanks,
Dave

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mipmapping for OpenGL texture creation?
« Reply #1 on: September 18, 2010, 11:02:32 pm »
Mipmapping for 2D?? It wouldn't be used at all in SFML, and if you manage your textures externally... well, you manage them externally :)

If you're using sf::Image for your texture, you can still do it:
Code: [Select]
image.Bind();
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
Laurent Gomila - SFML developer

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Mipmapping for OpenGL texture creation?
« Reply #2 on: September 19, 2010, 03:48:13 am »
Yeah I am doing that already, I figured since SFML seems to be somewhat tied to OpenGL it would be nice to have that built in, so one has less code to deal with and would look cleaner sending a flag vs. calling more GL functions for each bind...

Anyway thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mipmapping for OpenGL texture creation?
« Reply #3 on: September 19, 2010, 10:04:47 am »
Quote
Yeah I am doing that already, I figured since SFML seems to be somewhat tied to OpenGL it would be nice to have that built in, so one has less code to deal with and would look cleaner sending a flag vs. calling more GL functions for each bind...

I understand your point of view, but SFML is not a general purpose OpenGL wrapper, it's not meant to make your OpenGL code nicer.
Laurent Gomila - SFML developer

Malcolm

  • Newbie
  • *
  • Posts: 1
    • View Profile
Mipmapping for OpenGL texture creation?
« Reply #4 on: April 06, 2011, 06:41:21 pm »
+1 for this feature, it would be very useful!

Quote from: "Laurent"
Mipmapping for 2D?? It wouldn't be used at all in SFML,

Why not? Any image I load from disk has a fixed resolution obviously, but the sizes at which it will be drawn on screen may vary greatly (depending on the sprite's scales at which I blit it, and to some extent the user's screen resolution).

When loading a large image (e.g. 800x600) and blitting it to a smaller area (say 80x60) the quality will *greatly* benefit from mipmapping (as opposed to only bilinear filtering, which happens by default).

Quote
If you're using sf::Image for your texture, you can still do it:
Code: [Select]
image.Bind();
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

Not quite sure where/how to do this, could you elaborate please?

I'm doing this:
Code: [Select]
sf::Image myImage;
myImage.LoadFromFile("example.png");

Now what? Doing the code above didn't seem to make any difference.

Do I have to assign this image to a sprite in any special way, or do I have to Draw the sprite differently?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mipmapping for OpenGL texture creation?
« Reply #5 on: April 06, 2011, 07:31:54 pm »
It's true that mipmaps could improve the 2D quality, I'll do some tests and add it if it's worth.
Laurent Gomila - SFML developer

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Mipmapping for OpenGL texture creation?
« Reply #6 on: April 07, 2011, 04:23:31 am »
;) I say add it, simple flag should suffice!

BTW what is the status of flipping the image for OpenGL textures? Meaning the image is not lower left but upper left? I am guessing this is setup for DX and should be for GL... Please inform me as I need to figure out what I am to do wait for a update or 2.0 or move on...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mipmapping for OpenGL texture creation?
« Reply #7 on: April 07, 2011, 07:55:49 am »
Quote
I say add it, simple flag should suffice!

I'm even thinking about nothing at all in the public API (and removing SetSmooth as well): generating mipmaps + using NEAREST_MIPMAP_LINEAR should be ok for all situations (pixel perfect for scale 1, smoothed for scale < 1, no "big blur" for scale > 1).
That's why I was talking about tests ;)

Quote
BTW what is the status of flipping the image for OpenGL textures? Meaning the image is not lower left but upper left?

Well, I'm not convinced that there is a real problem with that. UV (0, 0) correctly maps to the pixel at (0, 0), not (0, height-1). So I'm not sure why people always complain about this stuff.
Laurent Gomila - SFML developer

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Mipmapping for OpenGL texture creation?
« Reply #8 on: April 08, 2011, 01:30:03 am »
I would use GL_LINEAR_MIPMAP_LINEAR IMO....

THe issue with the image isn't the 0,0 origin, but the pixels are not correct. The image itself needs to be flipped on the Y axis. I am not using your texture coordinates for anything in my engine, and I still have to flip the image either myself, or use a paint program to flip it or else my model mesh will not be mapped correctly! So I beg you please flip the pixels after the image data is loaded with SOIL with a flag parameter that is set to default your way, but can be changed to meet others needs...

Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mipmapping for OpenGL texture creation?
« Reply #9 on: April 08, 2011, 07:48:39 am »
Quote
I would use GL_LINEAR_MIPMAP_LINEAR IMO....

Why?

Quote
THe issue with the image isn't the 0,0 origin, but the pixels are not correct. The image itself needs to be flipped on the Y axis.

That's what I meant: if the image is flipped on Y it means that bottom pixels are mapped to top texture coordinates.
But in all the tests that I did, it was not true. So instead of just saying "it doesn't work for me so please fix it", let's try to understand what's really wrong and find the real solution ;)

Quote
I am not using your texture coordinates for anything in my engine, and I still have to flip the image either myself

How do you create/load your mesh? Which file format?
Laurent Gomila - SFML developer

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Mipmapping for OpenGL texture creation?
« Reply #10 on: April 09, 2011, 01:10:26 am »
I use ASSIMP SDK to load my meshes...

http://assimp.sourceforge.net/

My not use Trilinear mipmapping?

Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mipmapping for OpenGL texture creation?
« Reply #11 on: April 09, 2011, 09:29:28 am »
Quote
I use ASSIMP SDK to load my meshes...

http://assimp.sourceforge.net/

My not use Trilinear mipmapping?

Sorry, I don't understand. What do you mean?
Laurent Gomila - SFML developer

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Mipmapping for OpenGL texture creation?
« Reply #12 on: April 10, 2011, 04:36:28 am »
Quote from: "Laurent"
Quote
I use ASSIMP SDK to load my meshes...

http://assimp.sourceforge.net/

My not use Trilinear mipmapping?

Sorry, I don't understand. What do you mean?


Lost me?

ASSIMP is the SDK to load my mesh files.... link shows that

And the mipmapping filtering I was stating use the best Image Quality possible...

Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Mipmapping for OpenGL texture creation?
« Reply #13 on: April 10, 2011, 09:56:48 am »
Quote
Lost me?

Kind of. Sorry I had forgotten about the flipped texture thing, I thought your previous answer was all about mipmapping and I didn't understand what ASSIMP was doing here :D

Quote
And the mipmapping filtering I was stating use the best Image Quality possible...

Don't forget that in 2D we sometimes want 1:1 rendering (pixel perfect) compared to the original images. Using the "biggest" smooth filter available may not be the best option.
Laurent Gomila - SFML developer

etam

  • Newbie
  • *
  • Posts: 5
    • View Profile
Mipmapping for OpenGL texture creation?
« Reply #14 on: April 22, 2011, 11:02:09 pm »
+1 :D

How is your testing? Any results?