Coloring in SharpGL - c#

I have a sphere and I want to color it and add a light effect on it, so when I shed a light on it, the original color of the sphere is gone, replaced by the color of the illuminating light.
I don't know what is the problem, help me.
Here is my code:
enter code here
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using SharpGL;
using SharpGL.SceneGraph;
namespace SharpGLWinformsApplication1
{
public partial class SharpGLForm : Form
{
public SharpGLForm()
{
InitializeComponent();
}
private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e)
{
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;
IntPtr quad=gl.NewQuadric();
// Clear the color and depth buffer.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gl.LoadIdentity();
gl.Translate(0.0f, 0.0f, 12.0f);
gl.Scale(0.1,0.1,0.1);
//*
gl.PushMatrix();
gl.Color(1.0f, 1.0f, 0.0f);
gl.Sphere(quad, 15, 20, 20);
gl.PopMatrix();
//------------------------------------//
gl.PushMatrix();
gl.Color(0.0f, 1.0f, 0.0f);
gl.Rotate(rotation, 0.0f, 1.0f, 0.0f);
gl.Translate(0, 1, 30);
gl.Sphere(quad, 12, 20, 20);
gl.PopMatrix();
rotation += 3.0f;
}
private void openGLControl_OpenGLInitialized(object sender, EventArgs e)
{
// TODO: Initialise OpenGL here.
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;
// Set the clear color.
gl.ClearColor(0, 0, 0, 0);
gl.Enable(OpenGL.GL_LIGHTING);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_AMBIENT, ambient);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_DIFFUSE, diffuse);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_SPECULAR, specular);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_POSITION, new float[] { 0.0f, 2f, -30f, 0 });
gl.Enable(OpenGL.GL_LIGHT1);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_SPOT_CUTOFF, 180.0f);
}
private void openGLControl_Resized(object sender, EventArgs e)
{
// TODO: Set the projection matrix here.
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;
// Set the projection matrix.
gl.MatrixMode(OpenGL.GL_PROJECTION);
// Load the identity.
gl.LoadIdentity();
gl.Viewport(0, 0, (int)Width, (int)Height);
// Create a perspective transformation.
gl.Perspective(45.0f, (double)Width / (double)Height, 1, 200.0);
// Use the 'look at' helper function to position and aim the camera.
gl.LookAt(0, -3, -10, 0, 0, 0, 0, 1, 0);
// Set the modelview matrix.
gl.MatrixMode(OpenGL.GL_MODELVIEW);
}
private float rotation = 0.0f;
private GLColor ambient = new GLColor(1, 1, 1, 1f);
private GLColor diffuse = new GLColor(1, 1, 1, 1);
private GLColor specular = new GLColor(1, 1, 1, 1);
private GLColor shadowColor = new GLColor(0, 0, 0, 0.4f);
}
}

I think you need to setup materials using gl.Materials(). otherwise opengl will take the sphere with a surface that reflects no light.

Related

Rendering triangles with OpenTK from VBO

I cannot render triangles for the life of me with a VBO in OpenTK. I am loading my data to the VBO in glControl_Load() event. I get a background screen with no triangles when running. The data is a from a mesh m.OpenGLArrays(out data, out indices) outputs a list of floats and ints. The list of floats for the vertices T1v1, T1v2, T1v3, T2v1, T2v2, T2v3, .... , all three vertices for each triangle back to back.
However given a blank screen with the code when I comment the "intermediate" rendering code everything renders fine....??? What am I doing wrong?
private void glControl_Load(object sender, EventArgs e)
{
loaded = true;
glControl.MouseMove += new MouseEventHandler(glControl_MouseMove);
glControl.MouseWheel += new MouseEventHandler(glControl_MouseWheel);
GL.ClearColor(Color.DarkSlateGray);
GL.Color3(1f, 1f, 1f);
m.OpenGLArrays(out data, out indices);
this.indicesSize = (uint)indices.Length;
GL.GenBuffers(1, out VBOid[0]);
GL.GenBuffers(1, out VBOid[1]);
SetupViewport();
}
private void SetupViewport()
{
if (this.WindowState == FormWindowState.Minimized) return;
glControl.Width = this.Width - 32;
glControl.Height = this.Height - 80;
Frame_label.Location = new System.Drawing.Point(glControl.Width / 2, glControl.Height + 25);
GL.MatrixMode(MatrixMode.Projection);
//GL.LoadIdentity();
GL.Ortho(0, glControl.Width, 0, glControl.Height, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
GL.Viewport(0, 0, glControl.Width, glControl.Height); // Use all of the glControl painting area
GL.Enable(EnableCap.DepthTest);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid[0]);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(data.Length * sizeof(float)), data, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
float aspect_ratio = this.Width / (float)this.Height;
projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect_ratio, 1, 1024);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);
}
private void glControl_Paint(object sender, PaintEventArgs e)
{
if (loaded)
{
GL.Clear(ClearBufferMask.ColorBufferBit |
ClearBufferMask.DepthBufferBit |
ClearBufferMask.StencilBufferBit);
modelview = Matrix4.LookAt(0f, 0f, -200f + zoomFactor, 0, 0, 0, 0.0f, 1.0f, 0.0f);
var aspect_ratio = Width / (float)Height;
projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect_ratio, 1, 512);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref projection);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelview);
GL.Rotate(angleY, 1.0f, 0, 0);
GL.Rotate(angleX, 0, 1.0f, 0);
GL.EnableClientState(ArrayCap.VertexArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid[0]);
GL.Color3(Color.Yellow);
GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, new IntPtr(0));
GL.DrawArrays(PrimitiveType.Triangles, 0, data.Length);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.DisableClientState(ArrayCap.VertexArray);
//GL.Color3(Color.Yellow);
//GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
//GL.Begin(PrimitiveType.Triangles);
//for (int i = 0; i < this.md.mesh.Count; i++)
//{
// GL.Normal3(this.md.mesh[i].normal);
// GL.Vertex3(this.md.mesh[i].vertices[0]);
// GL.Vertex3(this.md.mesh[i].vertices[1]);
// GL.Vertex3(this.md.mesh[i].vertices[2]);
//}
//GL.End();
//GL.EndList();
glControl.SwapBuffers();
Frame_label.Text = "Frame: " + frameNum++;
}
}
If something doesn't seem right, then it probably isn't. I seriously questioned my understanding of opengl and spent hours looking at this. However it was just a simple error of forgetting to iterate a count variable in a for loop to transfer the mesh from one object to another. Each triangle had identical vertices! Always expect the unexpected when it comes to debugging!

OpenTK - VertexBufferObject doesn't draw anything

I'm trying to learn how to draw with VBOs in C# OpenTK - following examples like http://www.opentk.com/node/2292 and VBOs Using Interleaved Vertices in C#.
I'm pretty sure I want the interleaved single array method like this, with a neat struct for each vertex. I got the code to compile with no errors, but it simply draws a blank brown screen, no white triangle. I'm sure I've made a stupid error, please help me learn from it!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System.Drawing;
using System.Runtime.InteropServices;
namespace VBOTest2
{
[StructLayout(LayoutKind.Sequential)]
public struct Vertex
{
public Vector3 Position;
public byte[] Colour;
public Vertex(byte[] colour, Vector3 position)
{
Colour = colour;
Position = position;
}
public static readonly int Stride = Marshal.SizeOf(default(Vertex));
}
public class VBOTest2 : GameWindow
{
uint vbo;
public VBOTest2() :
base(1, 1, new GraphicsMode(32, 24, 8, 0), "Test")
{
Width = 1500;
Height = 800;
VSync = VSyncMode.On;
ClientSize = new Size(1500, 800);
this.Location = new System.Drawing.Point(100, 300);
GL.Viewport(0, 0, Width, Height);
}
void CreateVertexBuffer()
{
Vertex[] vertices = new Vertex[3];
vertices[0] = new Vertex(new byte[]{255,255,255,255}, new Vector3(-1f, -1f, 0f));
vertices[1] = new Vertex(new byte[] { 255, 255, 255, 255 }, new Vector3(1f, -1f, 0f));
vertices[2] = new Vertex(new byte[] { 255, 255, 255, 255 }, new Vector3(0f, 1f, 0f));
GL.GenBuffers(1, out vbo);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData<Vertex>(BufferTarget.ArrayBuffer, (IntPtr)Vertex.Stride, vertices, BufferUsageHint.StaticDraw);
}
protected override void OnLoad(EventArgs e)
{
GL.ClearColor(Color.Brown);
CreateVertexBuffer();
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.ColorArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.VertexPointer(3, VertexPointerType.Float, Vertex.Stride, (IntPtr)(0));
GL.ColorPointer(4, ColorPointerType.UnsignedByte, Vertex.Stride, (IntPtr)(3 * sizeof(float)));
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
//release buffer
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.DisableClientState(ArrayCap.VertexArray);
GL.DisableClientState(ArrayCap.ColorArray);
SwapBuffers();
}
}
}

How to render image properly

My image appears too large when it is rendered using SharpGL. How do I load it properly? The image's dimension is only 313 x 79 pixels but it almost occupy the rest of the screen when it renders.
I got this code from codeplex. The example given is how to render images in 3D (name of project is NativeTexturesSample).
https://sharpgl.codeplex.com/downloads/get/614989. I manage to make the rendering in 2D but I think I'm not doing it correctly.
private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs e)
{
const int screenWidth = 1920;
const int screenHeight = 1080;
SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
gl.MatrixMode(OpenGL.GL_PROJECTION);
gl.Ortho2D(0, screenWidth , screenHeight , 0);
gl.Disable(OpenGL.GL_DEPTH_TEST);
gl.LoadIdentity();
texture.Create(gl, #"C:\image\footerlogo.bmp");
texture.Bind(gl);
gl.Begin(OpenGL.GL_QUADS);
gl.TexCoord(0.0f, 1.0f); gl.Vertex(-2.0f, 0.0f);
gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 0.0f);
gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, 1.0f);
gl.TexCoord(0.0f, 0.0f); gl.Vertex(-2.0f, 1.0f);
gl.End();
gl.Flush();
}
Try this code:
public partial class SharpGLForm : Form
{
private bool TexturesInitialised = false;
private float rotation = 0.0f;
private float Scale = 1;
private Bitmap gImage1;
private System.Drawing.Imaging.BitmapData gbitmapdata;
private uint[] gtexture = new uint[1];
/// <summary>
/// Initializes a new instance of the <see cref="SharpGLForm"/> class.
/// </summary>
public SharpGLForm()
{
InitializeComponent();
}
private void InitialiseTexture(ref OpenGL gl)
{
gImage1 = new Bitmap(#"C:\Jess1.bmp");// Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
Rectangle rect = new Rectangle(0, 0, gImage1.Width, gImage1.Height);
gbitmapdata = gImage1.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
gImage1.UnlockBits(gbitmapdata);
gl.GenTextures(1, gtexture);
gl.BindTexture(OpenGL.GL_TEXTURE_2D, gtexture[0]);
gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGB8, gImage1.Width, gImage1.Height, 0, OpenGL.GL_BGR_EXT, OpenGL.GL_UNSIGNED_BYTE, gbitmapdata.Scan0);
uint[] array = new uint[] { OpenGL.GL_NEAREST };
gl.TexParameterI(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, array);
gl.TexParameterI(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, array);
TexturesInitialised = true;
}
private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e)
{
// Get the OpenGL object.
OpenGL gl = openGLControl.OpenGL;
// Clear the color and depth buffer.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
// Load the identity matrix.
gl.LoadIdentity();
if (!TexturesInitialised)
{
InitialiseTexture(ref gl);
}
gl.Enable(OpenGL.GL_TEXTURE_2D);
gl.BindTexture(OpenGL.GL_TEXTURE_2D, gtexture[0]);
gl.Color(1.0f, 1.0f, 1.0f, 0.1f); //Must have, weirdness!
gl.Begin(OpenGL.GL_QUADS);
gl.TexCoord(1.0f, 1.0f);
gl.Vertex(gImage1.Width, gImage1.Height, 1.0f);
gl.TexCoord(0.0f, 1.0f);
gl.Vertex(0.0f, gImage1.Height, 1.0f);
gl.TexCoord(0.0f, 0.0f);
gl.Vertex(0.0f, 0.0f, 1.0f);
gl.TexCoord(1.0f, 0.0f);
gl.Vertex(gImage1.Width, 0.0f, 1.0f);
gl.End();
gl.Disable(OpenGL.GL_TEXTURE_2D);
}
private void openGLControl_OpenGLInitialized(object sender, EventArgs e)
{
OpenGL gl = openGLControl.OpenGL;
gl.ClearColor(0, 0, 0, 0);
}
private void openGLControl_Resized(object sender, EventArgs e)
{
OpenGL gl = openGLControl.OpenGL;
gl.MatrixMode(OpenGL.GL_PROJECTION);
gl.LoadIdentity();
gl.Perspective(60.0f, (double)Width / (double)Height, 0.01, 10000.0);
gl.LookAt(0, 0, -500, 0, 0, 0, 0, 1, 0);
gl.MatrixMode(OpenGL.GL_MODELVIEW);
}
}

OpenTK basic triangle not drawing as it should

I am trying to draw the triangle with the colours and verticies specified but currently it seems like its picking some colour numbers for the positions and is not doing what its supposed to do
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
namespace newTriangle
{
class Program
{
static void Main(string[] args)
{
MyWindow myWindow = new MyWindow();
myWindow.Run();
}
}
class MyWindow : GameWindow
{
private uint[] vertexBufferObjectIDs = new uint[2];
private int vertexArrayID, vertexShaderID, fragmentShaderID, shaderProgramID;
public MyWindow()
: base(800, // Width
600, // Height
GraphicsMode.Default,
"My OpenTK Window",
GameWindowFlags.Default,
DisplayDevice.Default,
3, // major
0, // minor
GraphicsContextFlags.ForwardCompatible) { }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GL.ClearColor(Color4.CornflowerBlue);
GL.GenVertexArrays(1, out vertexArrayID);
GL.BindVertexArray(vertexArrayID);
ushort[] indices = new ushort[] { 0, 1, 2 };
float[] vertices = new float[] {-1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.0f, -1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 0.0f, 1.0f };
GL.GenBuffers(vertexBufferObjectIDs.Length, vertexBufferObjectIDs);
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferObjectIDs[0]);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, vertexBufferObjectIDs[1]);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(ushort)), indices, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, true, 5 * sizeof(float), 0);
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, 5 * sizeof(float), 2 * sizeof(float));
GL.EnableVertexAttribArray(1);
vertexShaderID = GL.CreateShader(ShaderType.VertexShader);
string vertShaderText =
#"
#version 150
in vec3 position;
in vec3 colour;
out vec3 Colour;
void main()
{
Colour = colour;
gl_Position = vec4(position, 1) ;
}";
GL.ShaderSource(vertexShaderID, vertShaderText);
GL.CompileShader(vertexShaderID);
fragmentShaderID = GL.CreateShader(ShaderType.FragmentShader);
string fragShaderText =
#"
#version 150
in vec3 Colour;
out vec4 outputF;
void main()
{
outputF = vec4(Colour, 1.0);
}";
GL.ShaderSource(fragmentShaderID, fragShaderText);
GL.CompileShader(fragmentShaderID);
shaderProgramID = GL.CreateProgram();
GL.AttachShader(shaderProgramID, fragmentShaderID);
GL.AttachShader(shaderProgramID, vertexShaderID);
GL.LinkProgram(shaderProgramID);
GL.UseProgram(shaderProgramID);
}
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
GL.DeleteBuffers(vertexBufferObjectIDs.Length, vertexBufferObjectIDs);
GL.DeleteVertexArrays(1, ref vertexArrayID);
GL.UseProgram(0); GL.DetachShader(shaderProgramID, vertexShaderID);
GL.DetachShader(shaderProgramID, fragmentShaderID);
GL.DeleteShader(fragmentShaderID);
GL.DeleteShader(vertexShaderID);
GL.DeleteProgram(shaderProgramID);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.DrawElements(BeginMode.Triangles, 3, DrawElementsType.UnsignedShort, IntPtr.Zero);
this.SwapBuffers();
}
}
}
can anyone see my mistake?
You didn't link up the locations of the attributes. You can fix it by using the appropriate calls to BindAttribLocation​, or use layout qualifiers with locations. Also, position is a vec3 but you give it only 2 floats.
Using layout qualifiers is an easy fix:
layout(location = 0) in vec2 position;
layout(location = 1) in vec3 colour;
That gives me this picture: http://i.imgur.com/H9FEXZ0.png which looks like it's probably what you had in mind.
vec4(position, 1)
In GLSL integers are not automatically type-promoted.
Kinda weird, because you got it right in your fragment shader.
Try this:
vec4(position, 1.0)

Using OpenTK (z-indexing)

I'm doing a cross between following some NeHe tutorials (translating it to OpenTK) and an OpenTK sample I found online to draw a triangle (for a simple initial setup):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.IO;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.Windows.Forms;
namespace GravSimBasic
{
class GLMain : GameWindow
{
int vbo;
Vector3[,] vertices;
float time = 0.01f;
void CreateVertexBuffer()
{
vertices = new Vector3[2,3];
vertices[0,0] = new Vector3(-1f, -1f, (float)Math.Sin(time));
vertices[0, 1] = new Vector3(0.5f, -1f, (float)Math.Sin(time));
vertices[0, 2] = new Vector3(-0.25f, 1f, -(float)Math.Sin(time));
vertices[1, 0] = new Vector3(-0.5f, -1f, (float)Math.Cos(time));
vertices[1, 1] = new Vector3(1f, -1f, (float)Math.Cos(time));
vertices[1, 2] = new Vector3(0.25f, 1f, -(float)Math.Cos(time));
//MessageBox.Show("Length: " + vertices.Length.ToString());
GL.GenBuffers(1, out vbo);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
new IntPtr(vertices.Length * Vector3.SizeInBytes),
vertices, BufferUsageHint.StaticDraw);
}
protected override void OnLoad(EventArgs e)
{
//set the window area
GL.Viewport(0, 0, 400, 400);
//background color
GL.ClearColor(Color.Black);
//set the view area
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(-2, 2, -2, 2, 2, -2);
//now back to 'scene editing' mode
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
//make things look nice
GL.ShadeModel(ShadingModel.Smooth);
//set up our z-rendering logic
GL.ClearDepth(2.0000f);
GL.Enable(EnableCap.DepthTest);
GL.DepthFunc(DepthFunction.Lequal);
//other improvements to quality
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
//initialize our scene data
CreateVertexBuffer();
}
protected override void OnRenderFrame(FrameEventArgs e)
{
time += 0.01f;
vertices[0, 0].Z = (float)Math.Sin(time);
vertices[0, 1].Z = (float)Math.Sin(time);
vertices[0, 2].Z = -(float)Math.Sin(time);
vertices[1, 0].Z = (float)Math.Cos(time);
vertices[1, 1].Z = (float)Math.Cos(time);
vertices[1, 2].Z = -(float)Math.Cos(time);
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
new IntPtr(vertices.Length * Vector3.SizeInBytes),
vertices, BufferUsageHint.StaticDraw);
System.Threading.Thread.Sleep(10);
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.EnableVertexAttribArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.Color4(0.75f,0.0f,0.0f,0.25f);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.DrawArrays(BeginMode.Triangles, 0, 3);
GL.Color4(0.0f, 0.75f, 0.0f, 0.55f);
GL.VertexAttribPointer(3, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.DrawArrays(BeginMode.Triangles, 3, 3);
GL.DisableVertexAttribArray(0);
SwapBuffers();
}
}
}
I suspect the issue is in lines 58-60, but I've changed the value in line 58 between -2.0, 0.00001, and 2.0, none changed the results. It could be the perspective setup a few lines earlier, though.
I've tried almost all the functions available as a parameter for line 60 - Lequal seems to be the best option by what I would expect, and it does produce the closest result to what I want, but it isn't quite correct.
Setup: There is a green and red triangle. They partially overlap on the x-y axis. The top z-axis of one is mapped by a -sin(time) function, and the bottom, a sin(time) function. The other uses cos() instead of sin, but is otherwise the same. The 'time' value changes each rendering.
What I want/expect: Two overlapping triangles - one red, one green. As that rotate back and forth, the non-overlapping portion of each should always be visible, and the overlapping portions should only show the foremost triangle.
What I get:
(a) nothing
(b) a display of both triangles, one on top of other.
(c) A changing image of bits of one, both or none of the triangles - even if one or both are showing, the are bits of either triangle, that should be visible, that are missing (background).
If I remove time, it shows a correct snapshot - the red triangle in front in the bottom half, and the green in front on the top.
Can anyone help diagnose this?
On line 86 you're only clearing the color buffer, not the depth buffer, so any changes to 58 won't have any effect:
GL.Clear(ClearBufferMask.ColorBufferBit);
I've never used OpenTK, but I'd guess it needs to go something along these lines:
GL.Clear(ClearBufferMask.ColorBufferBit|ClearBufferMask.DepthBufferBit);

Categories

Resources