I’m in the middle of attempting to learn how to use the GL class in Unity.
My question is, how do you texture (say, a flat square) created by the GL class? I have looked everywhere and tried the few examples I have found to no avail.
Here is what I am using to draw a square now:
void DrawSquare( float x, float y, float z, float size )
{
size = size/2.0f;
GL.Begin( GL.LINES );
GL.Color( color1 );
GL.Vertex3( x-size, y-size, z );
GL.Vertex3( x-size, y+size, z );
GL.Vertex3( x+size, y-size, z );
GL.Vertex3( x+size, y+size, z );
GL.Color( color2 );
GL.Vertex3( x-size, y+size, z );
GL.Vertex3( x+size, y+size, z );
GL.Vertex3( x+size, y-size, z );
GL.Vertex3( x-size, y-size, z );
GL.End();
}
Yes, I have seen that documentation, thank you. The problem is, without prior knowledge of OGL, that documentation is very difficult to read. The method description is simply “Sets current texture coordinate (x,y) for all texture units.”. I had to look up openGL’s actual texture classes to get a better explanation.
I didn’t understand that after GL.begin is called, the tailing code is more or less read much like “tags”, or procedurally brought in to describe the piece of openGL. Each texture “tag” (or openGL texture call) is really just describing which corners of the texture should be anchored to the previously stated vertices.
As you can see, without explicit explanation, the documentation can be largely confusing to a pure beginner of openGL.
For starters I think you want the mode to be set to quads as texturing lines does not make much sense.
So change GL.LINES to GL.QUADS.
Secondly PaulR is right the issue is your not providing tex coords, you would do so like this
GL.Begin( GL.QUADS );
GL.Color( color1 );
GL.Vertex3( x-size, y-size, z );
GL.TexCoord2(0,0);
GL.Vertex3( x-size, y+size, z );
GL.TexCoord2(0,1);
GL.Vertex3( x+size, y+size, z );
GL.TexCoord2(1,1);
GL.Vertex3( x+size, y-size, z );
GL.TexCoord2(1,0);
GL.End();
I have only included code for the first quad you wanted to emit but it would work the same for the second
I have also swapped vert three with four to make a quad as you have positioned them for a triangle strip, not a quad.
Im guessing that you got the code from the unity GL script reference page as that example is to draw a quad out line and lines are emitted as triangle strips if I remeber correctly.