Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenGL C#(OpenTK)加载和绘制图像函数不工作_C#_Opengl_Textures_Opentk - Fatal编程技术网

OpenGL C#(OpenTK)加载和绘制图像函数不工作

OpenGL C#(OpenTK)加载和绘制图像函数不工作,c#,opengl,textures,opentk,C#,Opengl,Textures,Opentk,我试图在屏幕上加载和显示一个纹理,但是我收到一个黑色的框,它是用来显示的 两种主要的方法是“LoadTexture”和“Draw Image”,我假设其中一种方法是错误的 使用系统; 使用系统诊断; 使用OpenTK; 使用OpenTK.Graphics; 使用OpenTK.Graphics.OpenGL; 使用系统图; 使用系统、绘图、成像; 命名空间失败呈现 { 公共类FailRender:GameWindow { public FailRender():base(800600,Graphi

我试图在屏幕上加载和显示一个纹理,但是我收到一个黑色的框,它是用来显示的

两种主要的方法是“LoadTexture”和“Draw Image”,我假设其中一种方法是错误的

使用系统;
使用系统诊断;
使用OpenTK;
使用OpenTK.Graphics;
使用OpenTK.Graphics.OpenGL;
使用系统图;
使用系统、绘图、成像;
命名空间失败呈现
{
公共类FailRender:GameWindow
{
public FailRender():base(800600,GraphicsMode.Default,“囤积升级”)
{
GL.ClearColor(0,0.1f,0.4f,1);
纹理=加载纹理(“sand.jpg”);
}
私有int纹理;
公共int LoadTexture(字符串文件)
{
位图位图=新位图(文件);
int-tex;
GL.Hint(HintTarget.PerspectiveCorrectionHint,HintMode.Nicest);
总目:织物(1件,外特克斯);
GL.BindTexture(TextureTarget.Texture2D,tex);
BitmapData数据=位图.LockBits(新系统.绘图.矩形(0,0,位图.宽度,位图.高度),
ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format32bppRgb);
GL.TexImage2D(TextureTarget.Texture2D,0,PixelInternalFormat.Rgba,data.Width,data.Height,0,
OpenTK.Graphics.OpenGL.PixelFormat.Rgba,PixelType.UnsignedByte,data.Scan0);
位图。解锁位(数据);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMinFilter,(int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMapFilter,(int)TextureMapFilter.Linear);
返回tex;
}
公共静态图像(int图像)
{
GL.MatrixMode(MatrixMode.Projection);
GL.PushMatrix();
GL.LoadIdentity();
GL.Ortho(0800,0600,-1,1);
GL.MatrixMode(MatrixMode.Modelview);
GL.PushMatrix();
GL.LoadIdentity();
总账禁用(启用上限照明);
总账启用(EnableCap.Texture2D);
GL.Color4(1,0,0,1);
GL.BindTexture(TextureTarget.Texture2D,图像);
总账开始(BeginMode.Quads);
特克斯库尔德2(0,0);
顶点3(0,0,0);
德国劳埃德公司2(1,0);
顶点3(256,0,0);
德国劳埃德船级社2(1,1);
顶点3(256,256,0);
德克斯库尔德总合2(0,1);
顶点3(0,256,0);
GL.End();
总账禁用(启用CAP.Texture2D);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Modelview);
} 
RenderFrame上受保护的覆盖无效(FrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
图像(纹理);
SwapBuffers();
}
}
公共课程
{
[状态线程]
公共静态void Main()
{
使用(FailRender win=new FailRender())
{
赢。跑();
}
}
}
}
删除行“GL.Color4(1,0,0,1);”

并将“加载纹理”功能更改为:

public int LoadTexture(string file)
{
    Bitmap bitmap = new Bitmap(file);

    int tex;
    GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

    GL.GenTextures(1, out tex);
    GL.BindTexture(TextureTarget.Texture2D, tex);

    BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
        ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
        OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
    bitmap.UnlockBits(data);


    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Linear);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Linear);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) TextureWrapMode.Repeat);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) TextureWrapMode.Repeat);

    return tex;
}

解决了这个问题
System.Drawing.PixelFormat.Argb
与OpenGL格式
Bgra

配合使用,如果您确保按正确的顺序推送/弹出矩阵,则使用Argb也有效。这也让我很恼火,但也给了我一些自由