Reputation: 63357
I'm learning SharpGL and there is a strange problem I can't understand, as far as I know when some object such as a cube is drawn by some gl.Color
and gl.Vertex
placed between gl.Begin()
and gl.End()
all other commands outside that pair of methods can't change the cube, but in my case it made some changes, more specifically, the gl.Color() called after the gl.End()
(which ends the drawing of the cube) still be able to affect that cube, the cube was drawn with some texture binding.
Here is my code, I want to say that, this code was modified from an article in CodeProject, I'm not the original writer, but I modified it not to simply draw a rotating cube (as the code writer did) but a cube with a lip being able to be controlled (opened and closed using some keys), and I also want to draw a pyramid inside the cube, when closing the cube you can't see the pyramid and when pressing a key to open the cube's lip, it shows you the pyramid inside. That's very cute to me, this is a good beginning I've made before going deeper with the so-called OpenGL for C# (SharpGL):
private void openGLControl1_OpenGLDraw(object sender, PaintEventArgs e) {
// This is for pausing the scene
if (pause) return;
// Get the OpenGL object, for quick access.
SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
// --------------------
gl.Clear(OpenGL.COLOR_BUFFER_BIT | OpenGL.DEPTH_BUFFER_BIT);
gl.Enable(OpenGL.TEXTURE_2D);
gl.LoadIdentity();
gl.Translate(x, y, z);
gl.Rotate(rtri, 0.0f, 1.0f, 1.0f);
gl.BindTexture(OpenGL.TEXTURE_2D, textures[0]);
// Start drawing the cube (or box)
gl.Begin(OpenGL.QUADS);
// Front Face
gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1f); // Bottom Left Of The Texture and Quad
gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
gl.TexCoord(1.0f, 1.0f); gl.Vertex(1,(float)2*Math.Cos(alpha)- 1, 1+(float)2*Math.Sin(alpha)); // Top Right Of The Texture and Quad
gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1,(float)2*Math.Cos(alpha) - 1, 1+(float)2*Math.Sin(alpha)); // Top Left Of The Texture and Quad
// Back Face
gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture
// Top Face
gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
// End drawing the box, but the code below still affect the cube?
// Why??? How to prevent this unexpected behavior?
gl.End();
// Increase The Rotation Variable For The Triangle
rtri += 1.0f;// 0.2f;
// This is my method to add controlling features to the box such as
// moving up, down, left, right, near, far and opening, closing the
// lip of the box, this is very simple and I think you don't need to care.
ChangeCoord(null, EventArgs.Empty);
// Try drawing a triangle in the box
// Here is the starting point the strange thing comes, if I end this
// method here, all will go well but if so I won't be able to create
// a triangle inside the box.
gl.Disable(OpenGL.TEXTURE_2D);
gl.LoadIdentity();
gl.Translate(x, y, z - 0.5);
gl.Scale(0.3f, 0.3f, 0.3f);
gl.Rotate(r, 0.0f, 1.0f, 0.0f);
gl.Begin(OpenGL.TRIANGLES);
gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0f, 1f, 0f);
gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(-1f, -1f, 1f);
gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(1f, -1f, 1f);
gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0f, 1f, 0f);
gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(1f, -1f, 1f);
gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(1f, -1f, -1f);
gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0f, 1f, 0f);
gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(1f, -1f, -1f);
gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(-1f, -1f, -1f);
gl.Color(1.0f, 0.0f, 0.0f); gl.Vertex(0f, 1f, 0f);
gl.Color(0.0f, 1.0f, 0.0f); gl.Vertex(-1f, -1f, -1f);
gl.Color(0.0f, 0.0f, 1.0f); gl.Vertex(-1f, -1f, 1f);
gl.End();
r += 1.0f; //Just changing the rotating angle of the triangle.
}
The gl.Color()
s which I think are used for drawing the triangle still affects the box?
I also downloaded another article in which the writer draws both a triangle and a cube in the same logic and all goes well but the cube in that code is drawn using gl.Color()
without texture binding, in this code the cube is drawn with some texture from a Bitmap image.
Please help me out, the cube and triangle are drawn, I can control the cube as I want (move left, right, up, down, open and close the lip and pause the scene) but the whole cube turns in blue overlapping the texture image, it's not I want, because it doesn't look real, the texture image is enough. As I mentioned at the beginning, if I don't use the code drawing the additional triangle, it will render well as I expect.
Upvotes: 0
Views: 1647
Reputation: 16612
If you submit things to be drawn which require a colour, and you don't specify one, GL just uses the last colour you gave it.
If you don't even specify a colour, the default will be white.
Better to just give it the colour you want rather than relying on the right colour to be magically there when you need it.
Just put gl.Color(1.0f, 1.0f, 1.0f);
after your first gl.Begin()
.
(And as others noted in your comments, the fixed-function pipeline is no longer recommended. Consider moving to the new programmable pipeline.)
Upvotes: 1