I've created a plane at OpenGL with this code:
glColor4f(0.8, 0.8, 0.5, 1.0);
glVertex3f(startlen, height, startwid);
glVertex3f(startlen, height, startwid + width);
glVertex3f(startlen + length, height, startwid);
glVertex3f(startlen + length, height, startwid + width);
glTexCoord2f(0.0f, 0.0f); glVertex3f(startlen, height, startwid);
glTexCoord2f(0.0f, 1.0f); glVertex3f(startlen, height, startwid - width/2);
glTexCoord2f(1.0f, 0.0f); glVertex3f(startlen + length, height, startwid);
glTexCoord2f(1.0f, 1.0f); glVertex3f(startlen + length, height, startwid - width/2);
If you have loader which returns textureID then it probably already takes care of loading image data, setting properties and storing it as a real texture for further use. So I'll just move on to binding texture to your texturing target.
If you have this id, you basically have to call
glBindTexture() function before drawing your triangle strip:
glBindTexture(GL_TEXTURE_2D, id); // where id is GLuint returned by your loader
now for each vertex you draw, you should also specify coordinates of texture for those vertices. To do so, you use
glTexCoord2f() function. For simple quad, it could look like this:
glBegin(GL_QUADS); // Bottom left glTexCoord2f(0.0f, 1.0f); glVertex2i(0.0f, 10.0f); // Top left glTexCoord2f(0.0f, 0.0f); glVertex2i(0.0f, 0.0f); // Top right glTexCoord2f(1.0f, 0.0f); glVertex2i(10.0f, 0.0f); // Bottom right glTexCoord2f(1.0f, 1.0f); glVertex2i(10.0f, 10.0f); glEnd();
As you can see, each vertex has texture 'point' attached to it. It will tell OpenGL how to display your texture.
Now it's up to you to choose how you want your texture to be distributed on a plane by specifying proper coordinates for vertices.
What you have to remember is that texture coordinates starts with 0.0 and ends with 1.0. Increasing this value to more than 1.0 will result in behaviour depending on settings for your texture (e.g. tiling).
In your tutorial, you have really nice picture presenting that texture coordinates are from 0.0 to 1.0 range and what texture coordinates are being set for specific vertices:
PS. In order to make textures being displayed, you have to enable this capability first by calling:
You don't have to call it every time you draw object with texture. It may be called just once before your drawing.
According to your next problem: What you want is to achieve tiling effect, which can be easily done during creating a texture. In your loader, after this line:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
you should set next texture parameters like this:
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
In this case
GL_REPEAT is being used to repeat texture whenever
it's coordinates are higher than 1.0. For example if you specify
texture coordinate to be 2.0 it is the same as you'd put 1.0, and so
Your second problem exist because you have still your texture bound
and you have texturing enabled (remember that you used
glEnable(GL_TEXTURE_2D);), so basically everything you draw now,
will be covered with texture.
However if you want now to draw something without texturing it, you basically have to disable texturing before drawing your second plane and enable it again after drawing:
glDisable(GL_TEXTURE_2D); // Draw your second plane here glEnable(GL_TEXTURE_2D);
Well, I think you misunderstood me a bit. In this case tiling cannot work, because you specify texture coordinates to maximum of 1.0 which is the end of your texture. I have prepared an image that could show you what I mean:
In those pictures in the middle with red color I marked texture coordinates and with black - vertices coordinates.
So basically if you set your texture coordinates like you do it right now (max of 1.0), you make your texture stretched around your plane. Now imagine that this plane is very large - it can cause nothing else but ugly stretch effect if texture is of low resolution.
In order to achieve tiling effect, you have to set values higher than 1.0, like in my example - 2.0 causes texture to be repeated two times.
As there is no magic method to make your texture look 'nice' on your plane, you have to use some math to achieve it. Now this I leave to you.