340 likes | 402 Views
Learn to generate, load, and manipulate textures for parametric surfaces and solid meshes using OpenGL techniques. This guide covers texture format, loading methods, and filtering options for optimal rendering. Get hands-on practice with code examples and troubleshooting strategies.
E N D
Texture generating textures using textures repeating clamping filtering texturing a parametric surface
where to put the Textures folder • VS: usually in the same folder as the .cpp file. If project is graphics, then in folder graphics/graphics • Xcode: XCodeHowTo link. (class 1) • Unix: in file with executable
Try it now. • First try • loadTextures.cpp from the 1st edition • If the computer complains about glext, just comment out that include in the code. • If you get a white square, call me over. • More errors, see the next two slides, only if I tell you to!
fixing getbmp.cpp • Get the files getbmp.cpp and getbmp.h from the lecture page • in getbmp.cpp, in getbmp.cpp comment out old return and replace with new return: //return bmpRGBA; //mjb return bmpRGB;
fixing loadTextures.cpp • in loadTextures.cpp • comment out the Bitmap struct • comment out the function BitMapFile *getBMPData(string filename) • comment out the function call //image[0] = getBMPData("Textures/launch.bmp"); • and replace with the function call image[0] = getbmp("Textures/launch.bmp");
Format of Texture Big 1-D array: R G B A R G B A R G B A 1st pixel 3rd pixel 2nd pixel
This data is in many photos, but has to be properly retrieved
Book examples use uncom-pressed 24 bit BMP www.tinaja.com/glib/expbmp.pdf
useful function to read texture data from a .BMPFrom 1st edition // Routine to read a bitmap file. // Works only for uncompressed bmp files of 24-bit color. BitMapFile *getBMPData(string filename) • Go through the function • notice BGR vs RGB
If you can't get this version working, there is a new version, with a separate .h and .cpp file, from the 2nd edition. • Links are on the webpage, only if you need it.
Changes to make to loadTextures.cpp from 2nd edition //image[0] = getbmp("../../Textures/launch.bmp"); //mjb image[0] = getbmp("Textures/launch.bmp");
Changes to make to loadTextures.cpp from 2nd edition In main, comment out: glutInitContextVersion(4, 3); glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE); glewExperimental = GL_TRUE; glewInit();
globals and setup staticunsignedint texture[2]; // Array of texture indices. // Create texture index array. glGenTextures(2, texture); // Struct of bitmap file. struct BitMapFile{ int sizeX; int sizeY; unsignedchar *data;};
loadExternalTextures() // Local storage for bmp image data. BitMapFile *image[1]; // Load the texture. image[0] = getBMPData("Textures/launch.bmp"); // Activate texture index texture[0]. glBindTexture(GL_TEXTURE_2D, texture[0]); ... // Specify an image as the texture to be bound with the currently active texture index. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image[0]->sizeX, image[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image[0]->data);
loadProceduralTextures() // Activate texture index texture[1]. glBindTexture(GL_TEXTURE_2D, texture[1]); ... • // Specify an image as the texture to be bound with the currently active texture index. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, chessboard);
createChessboard(void) • look at function
texture space • texel : pixel of a texture • note: s, t, intervals [0,1]
mapping a polygon to texture space // Map the texture onto a square polygon. glBegin(GL_POLYGON); glTexCoord2f(0.0, 0.0); glVertex3f(-10.0, -10.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(10.0, -10.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(10.0, 10.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(-10.0, 10.0, 0.0); glEnd(); • Interpolate
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
fieldAndSky.cpp • run • notice shimmer • try clamping grass in T direction • do graph paper experiment.
GL_NEAREST glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
GL_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR); • Average the surrounding points
mipmap • make other versions of texture, various sizes • speed vs storage • book has good examples • if you try mipmaplevels.cpp change to d+=10.0 (not .2, also for -=) Nothing happens till box gets very small
fieldAndSkyFiltered.cpp • run and observe differences
texturedTorus.cpp • run • note code for parametrization • // Specify how texture values combine with current surface color values. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); • run also DoubletexturedTorusMJB.cpp