简体   繁体   English

为 OpenGL 中的 3d 锥体设置颜色

[英]Set color for a 3d Cone in OpenGL

I'm learning about OpenGL and I use C# for it.我正在学习 OpenGL 并为此使用 C#。 I want to have a red cone and an opportunity to change its transparency.我想要一个红色的锥体,并有机会改变它的透明度。

This is what I do:这就是我所做的:

Gl.glColor4f(255, 0, 0, alpha);
Glut.glutSolidCone(cone.Radius, cone.Height, cone.Slices, cone.Stacks);

As a result I get something like this:结果我得到了这样的东西:

锥体

So it really is a cone but the color is just white (alpha = 1 when ran).所以它确实是一个圆锥体,但颜色只是白色(运行时 alpha = 1)。

How to achieve the red color with an ability to make it transparent?如何通过使其透明的能力来实现红色?

Thank you for your replies, Finally: got a solution:谢谢大家的回复,终于找到解决办法了:

Gl.glEnable(Gl.GL_BLEND);
Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);
Gl.glEnable(Gl.GL_COLOR_MATERIAL);
Gl.glColor4f(255, 0, 0, alpha);
Glut.glutSolidCone(cone.Radius, cone.Height, cone.Slices, cone.Stacks);

So I have a Scroll Bar to change alpha parameter and the cone does change its transparency.所以我有一个滚动条来改变 alpha 参数,而圆锥体确实改变了它的透明度。 By the way, 255 and 1 works the same (solid red color)顺便说一句,255 和 1 工作方式相同(纯红色)

I hope this answer helps also.我希望这个答案也有帮助。

图。1

The main render class, sets up the camera view and renders all the lights, and then renders all the objects in the scene:主渲染class,设置相机视图并渲染所有灯光,然后渲染场景中的所有物体:

 public void RenderOnView(GLControl control) { control.MakeCurrent(); var camera = views[control]; GL.Clear(ClearBufferMask.ColorBufferBit|ClearBufferMask.DepthBufferBit); GL.Disable(EnableCap.CullFace); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); camera.LookThrough(); if (EnableLights) { GL.LightModel(LightModelParameter.LightModelAmbient, new[] { 0.2f, 0.2f, 0.2f, 1f }); GL.LightModel(LightModelParameter.LightModelLocalViewer, 1); GL.Enable(EnableCap.Lighting); foreach (var light in lights) { light.Render(); } } else { GL.Disable(EnableCap.Lighting); GL.ShadeModel(ShadingModel.Flat); } GL.Enable(EnableCap.LineSmooth); // This is Optional GL.Enable(EnableCap.Normalize); // These is critical to have GL.Enable(EnableCap.RescaleNormal); for (int i = 0; i<objects.Count; i++) { GL.PushMatrix(); objects[i].Render(); GL.PopMatrix(); } control.SwapBuffers(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM