Reputation: 153
I am referring to the OpenGL Programming Guide Chapter 13 (https://www.glprogramming.com/red/chapter13.html) which is about selection. I want to translate the code in Example 13.2 into C# using OpenTK.
Here are my code. When I run it, I cannot see those triangle on the window. Do you know what is wrong in my code? One thing to note is that I commend out the create perspective field of view (in drawScene function), because it throws an argument out of range exception.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace OpenTKSelectTry2
{
public class SelectWindow
{
GameWindow window;
public SelectWindow(GameWindow wd)
{
this.window = wd;
init();
display();
window.Run(1.0 / 60.0);
}
private void drawTriangle(double x1,double y1,double x2,double y2,double x3,double y3,double z)
{
GL.Begin(BeginMode.Triangles);
GL.Vertex3(x1,y1,z);
GL.Vertex3(x2, y2, z);
GL.Vertex3(x3, y3, z);
GL.End();
}
private void drawViewVolume(double x1, double x2, double y1, double y2, double z1, double z2)
{
GL.Color3(1.0, 1.0, 1.0);
GL.Begin(BeginMode.LineLoop);
GL.Vertex3(x1, y1, -z1);
GL.Vertex3(x2, y1, -z1);
GL.Vertex3(x2, y2, -z1);
GL.Vertex3(x1, y2, -z1);
GL.End();
GL.Begin(BeginMode.LineLoop);
GL.Vertex3(x1, y1, -z2);
GL.Vertex3(x2, y1, -z2);
GL.Vertex3(x2, y2, -z2);
GL.Vertex3(x1, y2, -z2);
GL.End();
GL.Begin(BeginMode.Lines);
GL.Vertex3(x1, y1, -z1);
GL.Vertex3(x1, y1, -z2);
GL.Vertex3(x1, y2, -z1);
GL.Vertex3(x1, y2, -z2);
GL.Vertex3(x2, y1, -z1);
GL.Vertex3(x2, y1, -z2);
GL.Vertex3(x2, y2, -z1);
GL.Vertex3(x2, y2, -z2);
GL.End();
}
public void drawScene()
{
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
//Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(40.0f, 4.0f/3.0f, 1.0f, 100.0f);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
Matrix4 view = Matrix4.LookAt(new Vector3(7.5f,7.5f,12.5f), new Vector3(2.5f,2.5f,-5.0f), new Vector3(0.0f,1.0f,0.0f));
GL.Color3(0.0, 1.0, 0.0);
drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0);
GL.Color3(1.0, 0.0, 0.0);
drawTriangle(2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0);
GL.Color3(1.0, 1.0, 0.0);
drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, 0.0);
drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -10.0);
drawViewVolume(0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
}
public void processHits(int hits,int[] buffer)
{
int ptr;
Console.WriteLine("hit={0}", hits);
for (int i = 0; i < hits; i++)
{
Console.WriteLine("number of names for hit={0}", buffer[0]);
Console.WriteLine("z1={0}", buffer[1]);
Console.WriteLine("z2={0}", buffer[2]);
}
}
public void selectObjects()
{
var selectBuf = new int[512];
int hits;
GL.SelectBuffer(512, selectBuf);
GL.RenderMode(RenderingMode.Select);
GL.InitNames();
GL.PushName(0);
GL.PushMatrix();
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.LoadName(1);
drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -5.0);
GL.LoadName(2);
drawTriangle(2.0, 7.0, 3.0, 7.0, 2.5, 8.0, -5.0);
GL.LoadName(3);
drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, 0.0);
drawTriangle(2.0, 2.0, 3.0, 2.0, 2.5, 3.0, -10.0);
GL.PopMatrix();
GL.Flush();
hits = GL.RenderMode(RenderingMode.Render);
processHits(hits, selectBuf);
}
public void init()
{
GL.Enable(EnableCap.DepthTest);
GL.ShadeModel(ShadingModel.Flat);
}
public void display()
{
GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
drawScene();
selectObjects();
GL.Flush();
}
}
}
The main function:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
namespace OpenTKSelectTry2
{
class Program
{
static void Main(string[] args)
{
var window = new GameWindow(200, 200);
var gm = new SelectWindow(window);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press enter to finish...");
Console.ReadLine();
}
}
}
Upvotes: 1
Views: 242
Reputation: 210877
Use GL.LoadMatrix
to load the projection and view matrix:
float angle_rad = 40.0f * (float)Math.PI / 180.0f;
Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(
angle_rad, 4.0f/3.0f, 1.0f, 100.0f);
Matrix4 view = Matrix4.LookAt(
new Vector3(7.5f, 7.5f, 12.5f),
new Vector3(2.5f, 2.5f, -5.0f),
new Vector3(0.0f, 1.0f, 0.0f));
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref perspective);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref view);
In contrast to gluPerspective
,with
Matrix4.CreatePerspectiveFieldOfView` the angle has to specified in radians instead of degrees.
Upvotes: 1