2k1Toaster's picture

Using TexLib to draw textures, and it works. Now I cannot draw coloured quads

I am using the TexLib source to import a texture, and it works great. I can then draw the code and all is well.

Now I want to draw a quad that has no texture, but instead uses coloured vertecies to create a gradient. Problem is the quad itself doesnt show, except for where the texture is. So the parts of the texture that is white, turns the colour of the gradient. The part that is transparent, stays the background colour. So basically it is "shading" my texture with a gradient overlay. It is cool but not what I am after.

What am I doing wrong?

 base.OnRenderFrame(e);
            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
 
            GL.Begin(BeginMode.Triangles);
 
            GL.Color3(1.0f, 1.0f, 0.0f); GL.Vertex3(-1.0f, -1.0f, 4.0f);
            GL.Color3(1.0f, 0.0f, 0.0f); GL.Vertex3(1000.0f, -1.0f, 4.0f);
            GL.Color3(0.2f, 0.9f, 1.0f); GL.Vertex3(500.0f, 1000.0f, 4.0f);
 
            GL.End();
 
            GL.Begin(BeginMode.Quads);
            GL.BindTexture(TextureTarget.Texture2D, texture);
            Point internalLocation = new Point(location.X, Height - location.Y);
            GL.TexCoord2(0, 0); GL.Vertex2(internalLocation.X, internalLocation.Y);
            GL.TexCoord2(1, 0); GL.Vertex2(internalLocation.X + image_size.Width, internalLocation.Y);
            GL.TexCoord2(1, 1); GL.Vertex2(internalLocation.X + image_size.Width, internalLocation.Y + image_size.Height);
            GL.TexCoord2(0, 1); GL.Vertex2(internalLocation.X, internalLocation.Y + image_size.Height);
            GL.End();
 
            SwapBuffers();

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
longjoel's picture

if you just want to paint a polygon, not do texturing, you have to disable texture_2d.

2k1Toaster's picture

So, is there no way to draw both simultaneously? Or can I disable texture2d, draw my regular shapes, enable texture2d, draw my textured shapes, then draw the screen?

Mincus's picture

You've two choices either unbind all textures or disable texturing midway through the render.
I think (but I'm not sure) unbinding all textures will be quicker if you're only working with one.

Something like:

GL.BindTexture(TextureTarget.Texture2D, 0);
// Render non-textured triangles
 
GL.BindTexture(TextureTarget.Texture2D, texture);
// Render textured quads.

Or

GL.Disable(EnableCap.Texture2D);
// Render non-textured triangles
GL.Enable(EnableCap.Texture2D);
 
// Render textured quads.
2k1Toaster's picture

Enabling and Disabling the EnableCap.Texture2D, works somewhat. I now can see the polygon being drawn. However there is still a problem where every quad is being shaded by the same colouring. Is there a colour clear that I should do after drawing the coloured non-textured polygons, or should I set the Color3 to white? I guess I am not understanding how the colour from one call is being blended into the texture from another call.

Mincus's picture

Colours are persistent between the polygons, so yes, you need a GL.Color3f(Color.White); call before the textured polygons.
Most things (shaders, texture co-ordinates etc) are persistent in the same way, so keep tabs on what is currently active between polygons.

2k1Toaster's picture

perfect!

Thankyou all for the help. I wasn't sure if setting the color to white was the actual method or a rude hack, but it works and I know why (superficially) that it works.

So does OpenGL work as if there is one static "drawing brush" and you just change its properties such as which colour it draws, or which texture it draws, and tell it where? GL.Begin and GL.End dont actually "spawn" new instances of this brush with default settings and only modified for what you are doing, but begin and end individual polygons drawn by the same brush-thing. Is that somewhat close?

So you could bind a texture to the brush, and make n quads with it, all of which would have the same texture? And once you do a bindtexture, is it bound forever until the program closes, for instance if I were to call it in the instanciation of the program, if I were to never change it, everything I drew would have that texture applied? And same with colour if I were to call a Color3.(Color.Whatever), everything from now until the end of the program would be shaded with that colour?

Interesting if it is the case, and I will have to play around with it some more. It opens up a lot of possibilities!

Thanks again.

Mincus's picture

Essentially, yes, it works as you describe.

It's technically described as a state machine (at least according to the openGL wiki: http://www.opengl.org/wiki/OpenGL_Objects).

What it means in practice is that you set states on the machine and they'll remain like that until you change them or the OpenGL context is destroyed.
It applies to a number of things, colours and textures being two. Position in the "world" through GL.Translate is another (GL.LoadIdentity resets that, which is why you see it at the beginning of most render cycles).
It also applies to things you enable/disable, and you can use the Push and Pop functions to easily store and return to states of some things when necessary.