我正在学习 SharpGL 并且有一个我无法理解的奇怪问题,据我所知,当某些对象(例如立方体)被某些对象绘制gl.Color
并gl.Vertex
放置在这对方法之外的所有其他命令之间时gl.Begin()
,我gl.End()
无法更改立方体,但在我的情况下,它做了一些改变,更具体地说,在(结束立方体的绘制)之后调用的 gl.Color( gl.End()
) 仍然能够影响那个立方体,立方体是用一些纹理绑定绘制的。
这是我的代码,我想说,这段代码是根据 CodeProject 中的一篇文章修改的,我不是原作者,但我修改它不是简单地画一个旋转的立方体(就像代码作者所做的那样)而是一个立方体可以控制嘴唇(使用一些键打开和关闭),我还想在立方体内画一个金字塔,当关闭立方体时你看不到金字塔,当按下一个键打开立方体的嘴唇时,它向您展示了里面的金字塔。这对我来说很可爱,这是我在深入使用所谓的 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.
}
gl.Color()
我认为用于绘制三角形的s 仍然会影响盒子吗?
我还下载了另一篇文章,其中作者以相同的逻辑绘制了一个三角形和一个立方体,一切顺利,但该代码中的立方体是使用gl.Color()
没有纹理绑定来绘制的,在此代码中,立方体是使用来自位图的一些纹理绘制的图片。
请帮帮我,立方体和三角形已绘制,我可以随意控制立方体(向左、向右、向上、向下移动、打开和关闭嘴唇并暂停场景)但整个立方体变成蓝色并与纹理重叠图像,这不是我想要的,因为它看起来不真实,纹理图像就足够了。正如我在开头提到的,如果我不使用绘制附加三角形的代码,它会像我预期的那样渲染得很好。