Click here to Skip to main content
15,505,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get Stencil Test to work using SharpGL. The following simplified example does not work. It should have displayed only section of the second rectangle, however it displays the entire rectangle. Any help would be much appreciated.

     OpenGLControl1.OpenGL.Enable(OpenGL.GL_STENCIL_TEST);
OpenGLControl1.OpenGL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
OpenGLControl1.OpenGL.LineWidth(1.0f);

//First rectangle
float[] VertexArray1 = { -0.2f, -0.4f, +0.2f, -0.4f, +0.2f, +0.4f, -0.2f, +0.4f };
//Second rectangle
float[] VertexArray2 = { -0.4f, -0.2f, +0.4f, -0.2f, +0.4f, +0.2f, -0.4f, +0.2f };

uint[] VertexIndex = { 0, 1, 2, 3 };

uint[] VertexBuffer = new uint[VertexArray1.Length];
OpenGLControl1.OpenGL.GenBuffers(1, VertexBuffer);
OpenGLControl1.OpenGL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VertexBuffer[0]);
OpenGLControl1.OpenGL.EnableVertexAttribArray(0);
OpenGLControl1.OpenGL.VertexAttribPointer(0, 2, OpenGL.GL_FLOAT, OpenGL.GL_FALSE == 1, 0, (IntPtr)0);

OpenGLControl1.OpenGL.ColorMask(0, 0, 0, 0);
OpenGLControl1.OpenGL.StencilFunc(OpenGL.GL_ALWAYS, 0, 1);
OpenGLControl1.OpenGL.StencilOp(OpenGL.GL_KEEP, OpenGL.GL_KEEP, OpenGL.GL_REPLACE);
OpenGLControl1.OpenGL.StencilMask(1);

OpenGLControl1.OpenGL.Color(0.5f, 0.5f, 0.5f);
OpenGLControl1.OpenGL.BufferData(OpenGL.GL_ARRAY_BUFFER, VertexArray1, OpenGL.GL_STATIC_DRAW);
OpenGLControl1.OpenGL.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_FILL);
OpenGLControl1.OpenGL.DrawElements(OpenGL.GL_QUADS, VertexIndex.Length, VertexIndex);
OpenGLControl1.OpenGL.StencilMask(0);
OpenGLControl1.OpenGL.ColorMask(1, 1, 1, 1);
OpenGLControl1.OpenGL.StencilFunc(OpenGL.GL_EQUAL, 1, 1);
OpenGLControl1.OpenGL.StencilOp(OpenGL.GL_KEEP, OpenGL.GL_KEEP, OpenGL.GL_KEEP);

OpenGLControl1.OpenGL.BufferData(OpenGL.GL_ARRAY_BUFFER, VertexArray2, OpenGL.GL_STATIC_DRAW);
OpenGLControl1.OpenGL.PolygonMode(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_FILL);
OpenGLControl1.OpenGL.DrawElements(OpenGL.GL_QUADS, VertexIndex.Length, VertexIndex);

OpenGLControl1.OpenGL.DisableVertexAttribArray(0);


What I have tried:

I tried both integers and hexadecimal numbers for the masks, without effect.
Posted
Comments
Gerry Schmitz 27-Oct-20 11:57am    
OpenGL is like using crutches when you can already run (WPF and DirectX).
Roullakis 4-Nov-20 4:55am    
Thanks Gerry. I only need to do static 2D graphics. Which DirectX package would you recommend that is accessible from WPF?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900