So I am trying to display a texture in OpenGl that is beeing roated to the mouse, but i can't get it to running the way I want to. First i tried to display the texture via 2 Triangles that build a Quad. Like this:
Matrix4 modelViewMatrix =
Matrix4.CreateScale((float)Width/Txture.Width, (float)Height/Txture.Height, 1f) *
Matrix4.CreateRotationZ(Convert.ToSingle(LookingDir/(180/Math.PI))) *
Matrix4.CreateTranslation((float)X_PixPos, (float)Y_PixPos, 0f);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelViewMatrix);
GL.BindTexture(TextureTarget.Texture2D, Txture.ID);
GL.Begin(PrimitiveType.Triangles);
GL.Color4(1f, 1f, 1f, 1f);
GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
GL.TexCoord2(1, 1); GL.Vertex2(Txture.Width, Txture.Height);
GL.TexCoord2(0, 1); GL.Vertex2(0, Txture.Height);
GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
GL.TexCoord2(1, 0); GL.Vertex2(Txture.Width, 0);
GL.TexCoord2(1, 1); GL.Vertex2(Txture.Width, Txture.Height);
GL.End();
But since it rotates around the origin (0,0) Point which isn't in the center it is not working as intended. After that i tried to put the origin in to the center of the Quad like this:
GL.BindTexture(TextureTarget.Texture2D, Txture.ID);
GL.Begin(PrimitiveType.Triangles);
GL.Color4(1f, 1f, 1f, 1f);
GL.TexCoord2(-1f, 1f); GL.Vertex2(-Txture.Width / 2.0f, Txture.Height / 2.0f);
GL.TexCoord2(1f, 1f); GL.Vertex2(Txture.Width / 2.0f, Txture.Height / 2.0f);
GL.TexCoord2(1f, -1f); GL.Vertex2(Txture.Width / 2.0f, -Txture.Height / 2.0f);
GL.TexCoord2(-1f, 1f); GL.Vertex2(-Txture.Width / 2.0f, Txture.Height / 2.0f);
GL.TexCoord2(1f, -1f); GL.Vertex2(Txture.Width / 2.0f, -Txture.Height / 2.0f);
GL.TexCoord2(-1f, -1f); GL.Vertex2(-Txture.Width / 2.0f, -Txture.Height / 2.0f);
GL.End();
Since the Code for the matrix stays the same I left it out on this code snippet. This works nearly as intended. The origin is in the center and the texture rotates around it but Instead of displaying me the texture ones it displays it 4 Times. Hard to explain so I got a picture:Texture beeing displayed 4 times instead of once
So i tried to draw it with a quad but came to the same result. The akward thing is that, the first try is getting displayed correctly with and without the modelViewMatrix. But if i run the second code or the quad without the modelViewMatrix it gets displayed correctly. Since I just started with OpenGl i can't really find my mistake.
If anyone wants to know what i am trying to build:
I am trying to build a Topdown game but I dont want to use all these fancy engines where you just drag and drop all you stuff togther and get something out. I wanted to write my one engine, so I came across opentk for graphics. But i didn't thought that rendering a texture that rotates to the position of the mouse would be so hard.
Texture Coordinates (TexCoord2
) are more or less independent of the actual geometric position. They range from [0,0] (top-left) to [1,1] (bottom-right). You see 4 textures because you tell OpenGL that it should map the texture from -1 to 1 on the quad. With wrap-mode set to repeat, -1 to 1 means exactly two textures beside each other along each axis.
If you want to have just one texture, then use texture coordinates from 0 to 1:
GL.TexCoord2(0, 1); GL.Vertex2(-Txture.Width / 2.0f, Txture.Height / 2.0f);
GL.TexCoord2(1, 1); GL.Vertex2(Txture.Width / 2.0f, Txture.Height / 2.0f);
GL.TexCoord2(1, 0); GL.Vertex2(Txture.Width / 2.0f, -Txture.Height / 2.0f);
GL.TexCoord2(0, 1); GL.Vertex2(-Txture.Width / 2.0f, Txture.Height / 2.0f);
GL.TexCoord2(1, 0); GL.Vertex2(Txture.Width / 2.0f, -Txture.Height / 2.0f);
GL.TexCoord2(0, 0); GL.Vertex2(-Txture.Width / 2.0f, -Txture.Height / 2.0f);