user13353974
user13353974

Reputation: 145

Set color for a 3d Cone in OpenGL

I'm learning about OpenGL and I use C# for it. 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:

Cone

So it really is a cone but the color is just white (alpha = 1 when ran).

How to achieve the red color with an ability to make it transparent?

Upvotes: 0

Views: 385

Answers (2)

John Alexiou
John Alexiou

Reputation: 29254

I hope this answer helps also.

fig1

The main render class, sets up the camera view and renders all the lights, and then renders all the objects in the scene:

   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();
   }

Upvotes: 0

user13353974
user13353974

Reputation: 145

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(1.0f, 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.

Upvotes: 1

Related Questions