120 likes | 333 Views
Texture Mapping. What is texture mapping? - a method for adding detail , surface texture (a bitmap or raster image ), or color to a computer-generated graphic or 3D model. Texture Mapping. We have to use two headers and cpp files. lodepng.h / lodepng.cpp t exture.h / texture.cpp.
E N D
Texture Mapping What is texture mapping? - a method for adding detail, surface texture (a bitmap or raster image), or color to a computer-generated graphic or3D model
Texture Mapping We have to use two headers and cpp files. lodepng.h / lodepng.cpp texture.h / texture.cpp
Texture Mapping • lodepng.h / lodepng.cpp • We load png files or tga files and convert those into binary files so that VS can read it just as data files we usually use. • We don’t have to know the details.
Texture Mapping • 2. texture.h / texture.cpp • What we really have to use • Inside the header file, • #ifndef JH_TEXTURE_H • #define JH_TEXTURE_H • #include <gl/glut.h> • unsigned char *loadTGA(const char* filepath, int &width, int &height); • void initTGA(GLuint *tex, const char *name, int &width, int &height); • void initPNG(GLuint *tex, const char *name, int &width, int &height); • void initTex(); • #endif
Texture Mapping • void initPNG(GLuint *tex, const char *name, int &width, int &height); • tex: the place where we get the converted png file as a binary file • name: the file name (you should write a path to the file) • width: the width of the original png file (in pixel) • height: the height of the original png file (in pixel)
Texture Mapping Usage of initpng function GLuinttex_example; GLuint w; GLuint h; initPNG(&tex_example, “example.png”, w, h); -> convert “example.png” into a binary file which will be in “tex_example”
Texture Mapping Then, What’s in initpng file? void initPNG(GLuint *tex, const char *name, int &width, int &height) { … glDeleteTextures(1, tex); glGenTextures(1, tex); … width = decoder.infoPng.width; height = decoder.infoPng.height; //printf("width: %d height: %d\n", width, height); free(buffer); free(image); } The number 1 is the length of the square where png file will be mapped
Texture Mapping Example, I will use the picture above and its pixel size is 50 times 50.
Texture Mapping After using initpng, what do we have to do? Using same examples as before, glBindTexture(GL_TEXTURE_2D, tex_example); glBegin(GL_QUADS); glTexCoord2i(0.0, 1.0); glVertex2d(Xmin, Ymin); glTexCoord2i(1.0, 1.0); glVertex2d(Xmax, Ymin); glTexCoord2i(1.0, 0.0); glVertex2d(Xmax, Ymax); glTexCoord2i(0.0, 0.0); glVertex2d(Xmin, Ymax); glEnd();
Texture Mapping glTexCoord2i(0.0, 1.0); glVertex2d(Xmin, Ymin); glTexCoord2i(1.0, 1.0); glVertex2d(Xmax, Ymin); glTexCoord2i(1.0, 0.0); glVertex2d(Xmax, Ymax); glTexCoord2i(0.0, 0.0); glVertex2d(Xmin, Ymax); (Xmax, Ymax) (0, 0) (1, 0) (0, 1) (1, 1) (Xmin, Ymin)
Texture Mapping Drawing order is also important. We have to map the points in clockwise or in counter-clockwise manner. Otherwise, the texture mapping isn’t what you expected.
Texture Mapping • Assignment 4 • Bezier Surface with Texture mapping • Good Luck