0

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?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
user13353974
  • 125
  • 7
  • 3
    Not familiar with the `C#` API but I'd expect `glColor4f` to take parameters in the range [0, 1]. – G.M. Jan 11 '22 at 20:03
  • 2
    also if you have lighting enabled then in order to make `glColor` not be ignored you have to `glEnable(GL_COLOR_MATERIAL);` otherwise you have to change light and or material properties to affect color... Transparency is depending on your `glBlendFunc` settings and it must be also enabled ... see https://stackoverflow.com/a/37783085/2521214 – Spektre Jan 12 '22 at 10:55

2 Answers2

1

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
user13353974
  • 125
  • 7
  • 2
    most likely your 255.0 got clamped to 1.0 the range is `<0.0,1.0>` for `glColor?f` once you want less saturated color you have to go below 1.0 ... like 0.75 ,... – Spektre Jan 12 '22 at 16:33
0

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();
   }
John Alexiou
  • 28,472
  • 11
  • 77
  • 133