C# OpenGL不能同时渲染未加纹理和纹理的形状
我有两个VBO。其中一个有纹理,另一个没有。我没有成功地将它们同时绘制到屏幕上。DrawQuad绘制未纹理的,DrawQuadTex绘制纹理的,两者的代码类似于以下代码:C# OpenGL不能同时渲染未加纹理和纹理的形状,c#,c++,opengl,opentk,C#,C++,Opengl,Opentk,我有两个VBO。其中一个有纹理,另一个没有。我没有成功地将它们同时绘制到屏幕上。DrawQuad绘制未纹理的,DrawQuadTex绘制纹理的,两者的代码类似于以下代码: // Enable/Disable GL_TEXTURE_2D depending on which I'm drawing // Bind/Unbind the texture GL.EnableClientState(ArrayCap.VertexArray); GL.BindBuffer(BufferTarget.Ar
// Enable/Disable GL_TEXTURE_2D depending on which I'm drawing
// Bind/Unbind the texture
GL.EnableClientState(ArrayCap.VertexArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferId);
GL.VertexPointer(2, VertexPointerType.Float, Vector2.SizeInBytes, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBufferId);
GL.DrawElements(BeginMode.Triangles, _indices.Length, DrawElementsType.UnsignedByte, 0);
无论这两个函数中的哪一个先被调用,该形状都是可见的(即,如果我调用DrawQuad,那么一个未设置纹理的四边形是可见的)
我在GL_TEXTURE_2D上设置了glDisable,并尝试关闭TexCoordArray,尝试将纹理绑定到0,但没有任何区别。有什么是我忽略的吗
SSCCE
更新
当我说无论哪一个函数首先被调用,即显示的是谁的形状,都是不准确的,实际上只有untextured取代了textured(即当DrawQuad存在时,只有untextured可见)您发布的示例代码工作得很好。因此,我无法重现您描述的问题。显然,从图中可以看出,有纹理和无纹理的四边形都存在。然而,纹理的四边形由颜色混合而成,形成彩虹外观。要修复此问题,请禁用颜色指针或用白色填充颜色缓冲区 如果您机器上的结果与我在下面发布的图片不匹配,则可能表明其他因素在起作用。一个可能相关的例子可能是视频卡(-drivers)的选择
发布一个。完成。你知道怎么回事吗?在你的
*指针()
调用中,当你的VBO被压缩时,为什么要使用非零步长?为什么DrawQuadTex()
根本不引用纹理坐标缓冲区对象?或者为此启用GL\u纹理\u协调\u数组
客户端状态?我无法回答第一个问题,我一定在什么地方见过这样做的。将其更改为0没有任何区别。它应该如何引用纹理坐标缓冲区?只要不调用DrawQuad,它就可以毫无问题地绘制纹理……启用GL_纹理_坐标_数组没有任何效果。
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace E04
{
internal class Renderer
{
private readonly Int32 _vertexBufferId;
private readonly Int32 _textureBufferId;
private readonly Int32 _indexBufferId;
private readonly Int32 _colorBufferId;
private readonly Int32 _normalBufferId;
private readonly Vector2[] _vertices;
private readonly Vector2[] _textureCoordinates;
private readonly Byte[] _indices;
private readonly Vector4[] _colors;
private readonly Vector3[] _normals;
public Renderer()
{
// Create a vertex array
_vertices = new Vector2[4];
// Set the coordinate data
_textureCoordinates = new Vector2[4];
_textureCoordinates[0].X = 0.0f;
_textureCoordinates[0].Y = 0.0f;
_textureCoordinates[1].X = 1.0f;
_textureCoordinates[1].Y = 0.0f;
_textureCoordinates[2].X = 0.0f;
_textureCoordinates[2].Y = 1.0f;
_textureCoordinates[3].X = 1.0f;
_textureCoordinates[3].Y = 1.0f;
// Set the index data
_indices = new Byte[6];
_indices[0] = 0;
_indices[1] = 1;
_indices[2] = 3;
_indices[3] = 3;
_indices[4] = 2;
_indices[5] = 0;
// Set the color data
_colors = new Vector4[4];
_colors[0] = new Vector4(1.0f, 0.0f, 0.0f, 1.0f);
_colors[1] = new Vector4(1.0f, 1.0f, 0.0f, 1.0f);
_colors[2] = new Vector4(0.0f, 1.0f, 0.0f, 1.0f);
_colors[3] = new Vector4(0.0f, 0.0f, 1.0f, 1.0f);
// Set the normal data
_normals = new Vector3[4];
_normals[0] = new Vector3(0.0f, 0.0f, 1.0f);
_normals[1] = new Vector3(0.0f, 0.0f, 1.0f);
_normals[2] = new Vector3(0.0f, 0.0f, 1.0f);
_normals[3] = new Vector3(0.0f, 0.0f, 1.0f);
// Bind the texture array buffer
GL.GenBuffers(1, out _textureBufferId);
GL.BindBuffer(BufferTarget.ArrayBuffer, _textureBufferId);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr) (_textureCoordinates.Length*Vector2.SizeInBytes),
_textureCoordinates, BufferUsageHint.DynamicDraw);
GL.TexCoordPointer(2, TexCoordPointerType.Float, 8, IntPtr.Zero);
GL.EnableClientState(ArrayCap.TextureCoordArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
// Bind the normal array buffer
GL.GenBuffers(1, out _normalBufferId);
GL.BindBuffer(BufferTarget.ArrayBuffer, _normalBufferId);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr) (_normals.Length*Vector3.SizeInBytes), _normals,
BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
// Bind the color array buffer
GL.GenBuffers(1, out _colorBufferId);
GL.BindBuffer(BufferTarget.ArrayBuffer, _colorBufferId);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr) (_colors.Length*Vector4.SizeInBytes), _colors,
BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
// Bind vertex array buffer
GL.GenBuffers(1, out _vertexBufferId);
GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferId);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr) (_vertices.Length*Vector2.SizeInBytes), _vertices,
BufferUsageHint.StreamDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
// Bind index array buffer
GL.GenBuffers(1, out _indexBufferId);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBufferId);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr) (_indices.Length*sizeof (Byte)), _indices,
BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
}
public void DrawQuad(Int32 x, Int32 y, Int32 width, Int32 height)
{
UpdateQuadVertices(x, y, width, height);
GL.Disable(EnableCap.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.ColorArray);
GL.EnableClientState(ArrayCap.NormalArray);
GL.EnableClientState(ArrayCap.TextureCoordArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferId);
GL.VertexPointer(2, VertexPointerType.Float, 0, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ArrayBuffer, _colorBufferId);
GL.ColorPointer(4, ColorPointerType.Float, 0, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ArrayBuffer, _normalBufferId);
GL.NormalPointer(NormalPointerType.Float, 0, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ArrayBuffer, _textureBufferId);
GL.TexCoordPointer(2, TexCoordPointerType.Float, 0, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBufferId);
GL.DrawElements(BeginMode.Triangles, _indices.Length, DrawElementsType.UnsignedByte, 0);
}
public void DrawQuadTex(Int32 x, Int32 y, Int32 width, Int32 height, Int32 textureId)
{
UpdateQuadVertices(x, y, width, height);
GL.Enable(EnableCap.Texture2D);
GL.BindTexture(TextureTarget.Texture2D, textureId);
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.ColorArray);
GL.EnableClientState(ArrayCap.NormalArray);
GL.EnableClientState(ArrayCap.TextureCoordArray);
GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferId);
GL.VertexPointer(2, VertexPointerType.Float, 0, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ArrayBuffer, _colorBufferId);
GL.ColorPointer(4, ColorPointerType.Float, 0, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ArrayBuffer, _normalBufferId);
GL.NormalPointer(NormalPointerType.Float, 0, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ArrayBuffer, _textureBufferId);
GL.TexCoordPointer(2, TexCoordPointerType.Float, 0, IntPtr.Zero);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBufferId);
GL.DrawElements(BeginMode.Triangles, _indices.Length, DrawElementsType.UnsignedByte, 0);
// Removing this has no effect
GL.DisableClientState(ArrayCap.TextureCoordArray);
}
internal void UpdateQuadVertices(Int32 x, Int32 y, Int32 width, Int32 height)
{
_vertices[0].X = x;
_vertices[0].Y = y;
_vertices[1].X = x + width;
_vertices[1].Y = y;
_vertices[2].X = x;
_vertices[2].Y = y + height;
_vertices[3].X = x + width;
_vertices[3].Y = y + height;
GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBufferId);
GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, (IntPtr) (_vertices.Length*Vector2.SizeInBytes),
_vertices);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
}
public Int32 LoadSimpleTexture()
{
Int32 textureObjectId;
GL.GenTextures(1, out textureObjectId);
GL.BindTexture(TextureTarget.Texture2D, textureObjectId);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
(Int32) TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
(Int32)TextureMagFilter.Linear);
var bitmap = new Bitmap("simple.png");
BitmapData data = bitmap.LockBits(new 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);
return textureObjectId;
}
}
class Program
{
static void Main()
{
Int32 textureObjectId = 0;
using (var gameWindow = new GameWindow(800, 600))
{
var renderer = new Renderer();
gameWindow.Load += (s, e) =>
{
GL.ClearColor(Color.CornflowerBlue);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
GL.AlphaFunc(AlphaFunction.Greater, 1.0f);
textureObjectId = renderer.LoadSimpleTexture();
};
gameWindow.Resize += (s, e) =>
{
GL.Viewport(0, 0, 800, 600);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, 800, 600, 0, -1, 1);
};
gameWindow.RenderFrame += (s, e) =>
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
renderer.DrawQuad(0, 0, 128, 128);
renderer.DrawQuadTex(128, 0, 128, 128, textureObjectId);
gameWindow.SwapBuffers();
};
gameWindow.Run();
}
}
}
}