I've created a plane at OpenGL with this code:
glBegin(GL_TRIANGLE_STRIP);
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);
glEnd();
Now I want to apply a texture to this plane.
I've read this tutorial here but I've stopped at the "Using the texture in OpenGL" part because I don't have the UV coordinates.
I know that we have to import the texture file, in my case a bitmap file.
The loader returns a Glint, which is the textureID, is that global? In example, if I need to load that texture I simply need to call the bind texture function with the ID given?
Anyway, how can I apply the texture to the plane?
EDIT #1:
The texture at the example mosquito posted works now.
What I found out was that I was loading the texture into the constructor of the class and I shouldn't because the object was public and it was constructed before OpenGL's initialization.
So the texture now loads fine at the quad example :)
The problem now is that I've tried to use it at my plane and it looks distorted. The solution to this is probably enabling the texture to be loaded several times but keep it's original size (although I don't know how to do this).
Also, I've created another plane next to the current one with a cream colour but after the texturing it seems like this:
So there are 2 problems now:
Edit #2:
2nd problem has been fixed now, thanks to mosquito! But the tiling still doesn't work. This is how it looks now:
And this is the texture I've used:
Its dimensions are 256x256 (I've used an image editor to crop it)
Edit #3:
These are the glTexCoord2f coordinates with their matching vertices (for the inner margin):
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);
Select the material, press F2, and rename the new material to Textures. Now, double click on the textures, open the brick wall image, and drag and drop the image into Textures. Right-click on the assets, select Create, and pick the Material option.
The Vertex Animation Texture (VAT) technique, also known as Morphing Animation, consists of baking animations into a texture map for use later in the vertex shader. This technique enables us to keep the mesh completely static on the CPU side (changing the SkinnedMeshRenderer component to MeshRenderer component).
Texture coordinates define how an image (or portion of an image) gets mapped to a geometry. A texture coordinate is associated with each vertex on the geometry, and it indicates what point within the texture image should be mapped to that vertex.
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:
glEnable(GL_TEXTURE_2D);
You don't have to call it every time you draw object with texture. It may be called just once before your drawing.
EDIT
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
on.
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);
EDIT2
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With