我正在创建一个使用VBO缓冲区绘制模型的应用程序,而不是 gl.Vertex 。我想绘制的平行六面体是正确绘制的,但纹理不是。经过一个小时的搜索,我发现,我还需要添加法线和顶点。但我怎么能得到它们呢?
我的变数:
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);
答案 0 :(得分:0)
如此愚蠢的错误......我在应用纹理之前忘记了绑定缓冲区:
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);