我正在考虑用C#学习OpenGL,我打算使用OpenTK.我想知道OpenTK和"真正的OpenGL"的代码是否相同?我是否必须阅读有关OpenTK的书籍/教程,或者我是否可以阅读有关OpenGL的内容并仍然学会用C#编写代码?
我用我的OpenGL程序观察一个奇怪的现象,该程序是用C#/ OpenTK/core-profile编写的.当从具有~1M顶点的高度图显示mandelbrot数据时,性能根据我的视图矩阵的比例值而不同(它是正交的,所以我需要比例).使用VBO渲染数据.渲染过程包括光照和阴影贴图.
我唯一的猜测是着色器中的某些内容在低比例值上出现"错误"并且存在一些错误处理.有什么提示吗?
例子:

我试图让一个简单的Cloo程序运行,但它不起作用,谁能告诉我为什么?
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 Cloo;
using System.Runtime.InteropServices;
namespace HelloWorld
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ComputeContextPropertyList cpl = new ComputeContextPropertyList(ComputePlatform.Platforms[0]);
ComputeContext context = new ComputeContext(ComputeDeviceTypes.Gpu, cpl, null, IntPtr.Zero);
string kernelSource = @"
kernel void VectorAdd(
global read_only float* a,
global read_only float* b,
global write_only float* c )
{
int index = …Run Code Online (Sandbox Code Playgroud) 请考虑以下简单代码:
using System;
class Test
{
delegate int FooDelegate(int i);
FooDelegate Foo = FooImplementation;
static int FooImplementation(int i)
{
return i + 1;
}
public static void Main()
{
Foo(1);
}
}
Run Code Online (Sandbox Code Playgroud)
我想做的是将一些调试代码注入到Foo委托中,这相当于:
FooDelegate Foo = delegate(int i)
{
try
{
DebugPrologue();
return FooImplementation(i);
}
finally
{
DebugEpilogue();
}
};
Run Code Online (Sandbox Code Playgroud)
扭曲的是我必须能够在运行时执行此操作,因此编译时和后处理方法是不可能的.
我的初始方法使用Delegate.Combine()将序言和结尾方法添加到Foo委托.唉,这不会起作用,因为它会回报价值.
我目前的想法是使用System.Reflection.Emit和DynamicMethod作为潜在的解决方案.据我所知,我需要为FooImplementation获取MethodInfo,获取它的MethodBody,将其转换为DynamicMethod并将try-finally块注入其中.
不幸的是,我完全不知道该怎么做.谁愿意伸出援助之手?或者你有另一个(最好更简单)的想法?
编辑:这里的用例是调试OpenGL绑定(http://www.opentk.com).我们必须注入2226种具有完全不同参数的方法,因此需要采用一般方法.
我正在开发一个新游戏,并试图根据玩家相对于斜坡坐标的坐标来检测玩家(在斜坡上)是否与给定的网格发生碰撞。我正在使用这个功能,它似乎不起作用(斜率似乎太小或什么)
//Slopes
float slopeY = max.Y-min.Y;
float slopeZ = max.Z-min.Z;
float slopeX = max.X-min.X;
float angle = (float)Math.Atan(slopeZ/slopeY);
//Console.WriteLine(OpenTK.Math.Functions.RadiansToDegrees((float)Math.Atan(slopeZ/slopeY)).ToString()+" degrees incline");
slopeY = slopeY/slopeZ;
float slopeZX = slopeY/slopeX;
//End slopes
float surfaceposX = max.X-coord.X;
float surfaceposY = max.Y-coord.Y;
float surfaceposZ = min.Z-coord.Z;
min-=sval;
max+=sval;
//Surface coords
//End surface coords
//Y SHOULD = mx+b, where M = slope and X = surfacepos, and B = surfaceposZ
if(coord.X<max.X& coord.X>min.X&coord.Y>min.Y&coord.Y<max.Y&coord.Z>min.Z&coord.Z<max.Z) {
if(slopeY !=0) {
Console.WriteLine("Slope = "+slopeY.ToString()+"SlopeZX="+slopeZX.ToString()+" surfaceposZ="+surfaceposZ.ToString());
Console.WriteLine(surfaceposY-(surfaceposY*slopeY));
//System.Threading.Thread.Sleep(40000);
if(surfaceposY-(surfaceposZ*slopeY)<3 || surfaceposY-(surfaceposX*slopeZX)<3) …Run Code Online (Sandbox Code Playgroud) 我有以下图片:

窗口显示两个纹理,左边纹理通过将其绑定到FBO并渲染到FBO,50个随机三角形来生成.
正确的纹理由glTexImage2D生成,带有蒙娜丽莎的全彩色图像,但是你可以看到图像缺少红色,我认为问题出在OpenGL部分,因为我已经研究了传递给glTexImage2D的字节,他们确实包含红色......
这是C#/ Mono/OpenTK源代码
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Platform;
using OpenTK.Platform.X11;
using OpenTK.Input;
using ManOCL;
namespace GlTest
{
public class MainClass: GameWindow
{
public MainClass()
: base(800, 600, new GraphicsMode(new ColorFormat(0, 0, 0, 0), 32, 0))
{
this.VSync = VSyncMode.Off;
}
public uint fbo;
public uint textureA;
public uint textureB;
public int textureAWidth = 1024;
public int textureAHeight = 1024;
public int textureBWidth;
public int textureBHeight; …Run Code Online (Sandbox Code Playgroud) 我正在将Gleed2D从XNA 转换为MonoGame.
Gleed2D是一个实例化的Windows窗体应用程序XnaGame.在window由创建Game然后被隐藏和DeviceWindowHandle被设定为的Canvas主窗体上.
我知道这有点拗口; 代码说明了一切:在这里它是完整的.
相关位在构造函数中:
_drawSurface = mainForm.GetHandleToCanvas();
_graphics = new GraphicsDeviceManager( this )
{
PreferredBackBufferWidth = 800,
PreferredBackBufferHeight = 600
} ;
_graphics.PreparingDeviceSettings += graphics_PreparingDeviceSettings;
_winform = (Form)Form.FromHandle(Window.Handle);
_winform.VisibleChanged += game1VisibleChanged;
_winform.Size = new Size(10, 10);
Mouse.WindowHandle = _drawSurface;
Size pictureBoxSize = _mainForm.CanvasSize ;
ResizeBackBuffer( pictureBoxSize.Width, pictureBoxSize.Height ) ;
... some other stuff....
void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = …Run Code Online (Sandbox Code Playgroud) 我想从gDEBugger切换到CodeXL,因为CodeXL是gdeb的继承者,甚至还有Visual Studio集成.
但是,对我来说,集成存在一些问题.我正在使用VS2013,这是最新的CodeXL版本,并针对.NET 4.5进行编译.
是否有人成功使用CodeXL和OpenTK,并告诉我如何继续?它归结为以下两个问题:
尝试制作游戏,但纹理从未到达屏幕。
仅对四边形使用颜色是有效的,但是当我尝试对四边形进行纹理处理时,它仅显示为黑色方块。
基本片段着色器:
#version 300 es
precision highp float;
uniform sampler2D SS;
in vec4 Color;
in vec2 UVtrans;
out vec4 color;
void main()
{
color = texture(SS, UVtrans);
//color = Color;
}
Run Code Online (Sandbox Code Playgroud)
顶点着色器:
#version 300 es
layout(location = 0) in vec2 Vertex;
layout(location = 1) in vec2 UV;
uniform vec4 ColorIn;
uniform mat4 QuadMatrix;
out vec2 UVtrans;
out vec4 Color;
void main()
{
gl_Position = QuadMatrix* vec4(Vertex, 0.0, 1.0);
gl_Position.w = 1.0;
UVtrans = UV;
Color = ColorIn;
}
Run Code Online (Sandbox Code Playgroud)
用于将纹理绑定到四边形的代码部分: …
我正在使用 ImageSharp 学习教程(https://opentk.net/learn/chapter1/4-textures.html)。如何将“ImageSharp.Image”类型转换为“ImageSharp.PixelFormats.Rgba32”?
要加载图像,我正在使用
Image<Rgba32> image = Image.Load(path);
但我不断收到错误消息:
Cannot implicitly convert type 'SixLabors.ImageSharp.Image' to 'SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgba32>'. An explicit conversion exists (are you missing a cast?).