Texture Mapping
Can you tell me easy steps of doing texture mapping in opengl.i m working on it i looked alot of examples but didnt understand it becoz some of them are 3-4 pages coding for just only texture mapping.i will really appreciate for your help.
You have to add glTexCoord2f(U, V) where U is the horizontal and V the vertical. If you wanted to put a texture on a basic quad:
I think it looks like this:
0,1
|-----------------|1,1
|-----------------|
|-----------------|
|-----------------|
|-----------------|
|-----------------|
|-----------------|
|-----------------|
|-----------------|1,0
0,0
Edit: Damned ascii art
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
I think it looks like this:
0,1
|-----------------|1,1
|-----------------|
|-----------------|
|-----------------|
|-----------------|
|-----------------|
|-----------------|
|-----------------|
|-----------------|1,0
0,0
Edit: Damned ascii art
A minimal texturing example:
Enigma
#include <algorithm>#include <vector>#include <GL/glut.h>// an id we use to identify our textureGLuint textureID;class Random{ public: unsigned char operator()() const { // just generate a random number from zero to two hundred and fifty-five return (std::rand() * 255) / RAND_MAX; }};void createTexture(){ // tell OpenGL that we're creating a new texture and get a new id for it. glGenTextures(1, &textureID); // create a 16 × 16 block of random RGB values std::vector<unsigned char> textureData(16 * 16 * 3); std::generate(textureData.begin(), textureData.end(), Random()); // tell OpenGL which texture we're operating on glBindTexture(GL_TEXTURE_2D, textureID); // tell OpenGL that we want this texture to be linearly filtered glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // create the texture // this is level 0 (the main version of the texture) // we only care that OpenGL stores red, green and blue values internally, so the internalformat is set as GL_RGB // the texture is 16 pixels wide // the texture is 16 pixels high // we don't want a border round the texture // the data we're giving OpenGL is in RGB format // the data we're giving OpenGL is made up of unsigned bytes // &textureData[0] is a pointer to our data glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, &textureData[0]); // tell OpenGL to use texturing whenever it draws anything glEnable(GL_TEXTURE_2D);}void reshape(int width, int height){ // set up the projection matrix and viewport glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, width, height); glOrtho(-256, 256, 256, -256, -1, 1); // reset the modelview matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity();}void display(){ // clear the buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw a quad glBegin(GL_QUADS); // set a texture coordinate (top left) glTexCoord2f(0, 0); // draw a vertex. This uses the last set texture coordinate glVertex2f(-100, -100); // set a texture coordinate (top right) glTexCoord2f(1, 0); // draw a vertex. This uses the last set texture coordinate glVertex2f(100, -100); // set a texture coordinate (bottom right) glTexCoord2f(1, 1); // draw a vertex. This uses the last set texture coordinate glVertex2f(100, 100); // set a texture coordinate (bottom left) glTexCoord2f(0, 1); // draw a vertex. This uses the last set texture coordinate glVertex2f(-100, 100); glEnd(); // swap the buffers so we can actually see it glutSwapBuffers();}int main(int argc, char** argv){ // initialise GLUT glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(0, 0); glutInitWindowSize(512, 512); glutCreateWindow("Minimal Textures"); glutReshapeFunc(reshape); glutDisplayFunc(display); // create the texture createTexture(); // set the darn thing going glutMainLoop(); return 0;}
Enigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement