通过 VBO 绘制时 Tao.OpenGL 中的纹理
Texturing in Tao.OpenGL when drawing via VBO
我正在制作一个使用 VBO 缓冲区绘制模型的应用程序,而不是 gl.Vertex。我想绘制的 Parallelepiped 已正确绘制,但纹理未正确绘制。经过一个小时的搜索,我发现我还需要添加法线和顶点。但是我怎样才能得到它们呢?
我的变量:
static float x = 0;
static float y = 0;
static float z = 0;
static float width = 10;
static float height = 5;
static float depth = 3;
float[] points = new float[72] {x, y, z , x + width, y, z , x + width, y - height, z , x, y - height, z , x, y, z + depth , x + width, y, z + depth,
x + width, y - height, z + depth , x, y - height, z + depth , x, y, z , x, y, z + depth , x, y - height, z + depth , x, y - height, z, x + width, y, z,
x + width, y, z + depth , x + width, y - height, z + depth , x + width, y - height, z , x, y, z , x + width, y, z , x + width, y, z + depth , x, y, z + depth,
x, y - height, z , x + width, y - height, z , x + width, y - height, z + depth , x, y - height, z + depth};
float[] texcoords = new float[48] { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 };
int vSize;
int tSize;
uint[] vboName = new uint[1] { 0 };
uint[] texName = new uint[1] { 0 };
我加载缓冲区的方法:
Gl.glEnable(Gl.GL_ARRAY_BUFFER_ARB);
vSize = points.Length / 3;
int s = points.Length * sizeof(float);
object ss = s;
IntPtr size = new IntPtr(ss.GetHashCode());
tSize = texcoords.Length / 2;
int t = texcoords.Length * sizeof(float);
object tt = t;
IntPtr tsize = new IntPtr(tt.GetHashCode());
Gl.glGenBuffersARB(1, vboName);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, vboName[0]);
Gl.glBufferDataARB(Gl.GL_ARRAY_BUFFER_ARB, size, points, Gl.GL_STATIC_DRAW);
Gl.glGenBuffersARB(1, texName);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, texName[0]);
Gl.glBufferDataARB(Gl.GL_ARRAY_BUFFER_ARB, tsize, texcoords, Gl.GL_STATIC_DRAW);
Gl.glDisable(Gl.GL_ARRAY_BUFFER_ARB);
我画模型的方法:
Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, texName[0]);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, vboName[0]);
Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, null);
Gl.glTexCoordPointer(2, Gl.GL_FLOAT, 0, null);
Gl.glDrawArrays(Gl.GL_QUADS, 0, 24);
Gl.glDisableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);
最后,结果:
真是愚蠢的错误...我刚刚忘记在应用纹理之前绑定缓冲区:
Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, vboName[0]);
Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, null);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, texName[0]);
Gl.glTexCoordPointer(2, Gl.GL_FLOAT, 0, null);
Gl.glDrawArrays(Gl.GL_QUADS, 0, 24);
Gl.glDisableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);
我正在制作一个使用 VBO 缓冲区绘制模型的应用程序,而不是 gl.Vertex。我想绘制的 Parallelepiped 已正确绘制,但纹理未正确绘制。经过一个小时的搜索,我发现我还需要添加法线和顶点。但是我怎样才能得到它们呢?
我的变量:
static float x = 0;
static float y = 0;
static float z = 0;
static float width = 10;
static float height = 5;
static float depth = 3;
float[] points = new float[72] {x, y, z , x + width, y, z , x + width, y - height, z , x, y - height, z , x, y, z + depth , x + width, y, z + depth,
x + width, y - height, z + depth , x, y - height, z + depth , x, y, z , x, y, z + depth , x, y - height, z + depth , x, y - height, z, x + width, y, z,
x + width, y, z + depth , x + width, y - height, z + depth , x + width, y - height, z , x, y, z , x + width, y, z , x + width, y, z + depth , x, y, z + depth,
x, y - height, z , x + width, y - height, z , x + width, y - height, z + depth , x, y - height, z + depth};
float[] texcoords = new float[48] { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 };
int vSize;
int tSize;
uint[] vboName = new uint[1] { 0 };
uint[] texName = new uint[1] { 0 };
我加载缓冲区的方法:
Gl.glEnable(Gl.GL_ARRAY_BUFFER_ARB);
vSize = points.Length / 3;
int s = points.Length * sizeof(float);
object ss = s;
IntPtr size = new IntPtr(ss.GetHashCode());
tSize = texcoords.Length / 2;
int t = texcoords.Length * sizeof(float);
object tt = t;
IntPtr tsize = new IntPtr(tt.GetHashCode());
Gl.glGenBuffersARB(1, vboName);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, vboName[0]);
Gl.glBufferDataARB(Gl.GL_ARRAY_BUFFER_ARB, size, points, Gl.GL_STATIC_DRAW);
Gl.glGenBuffersARB(1, texName);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, texName[0]);
Gl.glBufferDataARB(Gl.GL_ARRAY_BUFFER_ARB, tsize, texcoords, Gl.GL_STATIC_DRAW);
Gl.glDisable(Gl.GL_ARRAY_BUFFER_ARB);
我画模型的方法:
Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, texName[0]);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, vboName[0]);
Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, null);
Gl.glTexCoordPointer(2, Gl.GL_FLOAT, 0, null);
Gl.glDrawArrays(Gl.GL_QUADS, 0, 24);
Gl.glDisableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);
最后,结果:
真是愚蠢的错误...我刚刚忘记在应用纹理之前绑定缓冲区:
Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, vboName[0]);
Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, null);
Gl.glBindBufferARB(Gl.GL_ARRAY_BUFFER_ARB, texName[0]);
Gl.glTexCoordPointer(2, Gl.GL_FLOAT, 0, null);
Gl.glDrawArrays(Gl.GL_QUADS, 0, 24);
Gl.glDisableClientState(Gl.GL_TEXTURE_COORD_ARRAY);
Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);