Coolant
Coolant

Reputation: 448

Create MipMap image manually (iOS OpenGL)

Currently I'm loading a texture with this code :

GLKTextureInfo * t = [GLKTextureLoader textureWithContentsOfFile:path options:@{GLKTextureLoaderGenerateMipmaps: [NSNumber numberWithBool:YES]} error:&error];

But the result is not that good when I scaled down the image (jagged edged).

Can I create my own mipmap using image software like Adobe Illustrator? But what is the rule to do that? And how do I load this image using the code ?

Thanks!

-- Edited --

Thanks for the answer, I got it using :

GLuint texName;
glGenTextures(1, &texName);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// load image data here
...

// set up mipmap
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData0);
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 128,128, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData1);
...
glTexImage2D(GL_TEXTURE_2D, 8, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData8);

Upvotes: 1

Views: 4513

Answers (1)

user1118321
user1118321

Reputation: 26375

Yes, you can manually make the mipmaps and upload them yourself. If you're using Illustrator, presumably it has some method to output an image at a particular resolution. I'm not that familiar with Illustrator, so I don't know how that part works.

Once you have the various resolutions, you can upload them as part of your main image. You can use glTexImage2D() to upload the original image to a texture. Then you can upload additional mipmap levels using glTexImage2D(), but setting the level parameter to other values. For example:

glTexImage2D (GL_TEXTURE_2D, level, etc...);

where level is the mipmap level for this particular image. Note that you will probably have to set the various texture parameters appropriately for mipmaps, such as:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, <whatever the max is here>);

See the section on mipmaps on this page for details.

Upvotes: 4

Related Questions