在对象上,有这些:
const char* vshader =
"attribute vec4 Position;\n"
"uniform mat4 modelView;\n"
"uniform mat4 projection;\n"
"attribute vec2 texcoord;\n"
"varying vec2 TextureCoord;\n"
"\n"
"void main(void) {\n"
" TextureCoord = texcoord;\n"
" gl_Position = projection * modelView * Position;\n"
"}\n";
const char* fshader =
"uniform sampler2D u_Texture;\n"
"varying vec2 TextureCoord;\n"
"\n"
"void main(void) {\n"
" gl_FragColor = texture2D(u_Texture, TextureCoord);\n"
"}\n";
这些是网格数据和初始化:
float (*cpts)[3]; //controlpoints
int ncpts;
unsigned int (*tris)[3]; //triangles indices
int ntris;
float (*tuv)[2]; //texture UVs
int ntuv;
int (*tuvi)[3]; //UVs indices (same count of ntris)
//bind vertices
glGenBuffers(1,&indexID);
glBindBuffer(GL_ARRAY_BUFFER, indexID);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*ncpts, cpts, GL_STATIC_DRAW);
//bind indices
glGenBuffers(1, &indexID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(float)*3*ntris, tris, GL_STATIC_DRAW);
这是纹理init:
glBindTexture(GL_TEXTURE_2D,textures[0].ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, textures[0].width, textures[0].height, 0, GL_RGB, GL_UNSIGNED_BYTE, textures[0].buf);
//bind texture
glGenBuffers(1, &textureID);
glBindBuffer(GL_ARRAY_BUFFER, textureID);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*ntuv, tuv, GL_STATIC_DRAW);
,这是程序init:
glUseProgram(texture_program_id);
texcoord_slot = glGetAttribLocation(texture_program_id, "texcoord");
texture_slot = glGetUniformLocation(texture_program_id, "u_Texture");
position_slot = glGetAttribLocation(texture_program_id, "Position");
// color_slot = glGetAttribLocation(program_id, "SourceColor");
model_slot = glGetUniformLocation(texture_program_id, "modelView");
projection_slot = glGetUniformLocation(texture_program_id, "projection");
glEnableVertexAttribArray(position_slot);
glEnableVertexAttribArray(color_slot);
glEnableVertexAttribArray(texcoord_slot);
glUniformMatrix4fv(model_slot, 1,0,mstack.mtxstack[mstack.level]);
glUniformMatrix4fv(projection_slot, 1,0,mstack.proj);
最后绘制例程:
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,textures[0].ID);
glUniform1i(texture_slot, 0);
glBindBuffer(GL_ARRAY_BUFFER, vertexID);
glVertexAttribPointer(position_slot,3,GL_FLOAT,GL_FALSE,sizeof(float)*3,0);
glBindBuffer(GL_ARRAY_BUFFER, textureID);
glVertexAttribPointer(texcoord_slot,2,GL_FLOAT,GL_FALSE,sizeof(float)*2,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexID);
glDrawElements(GL_TRIANGLES,ntris*3,GL_UNSIGNED_INT,0);
结果一无所获。
我是如何完成所有工作的?
编辑: 编辑所有这些已经向我展示了纹理UV也被索引..... 那么我如何给着色器赋予纹理UVs索引?