我已经使用此加载了一个对象(.obj)模型(来自此tutorial):
void Model::loadOBJ(const char * path, std::vector < vector3 > & out_vertices, std::vector < vector2 > & out_uvs, std::vector < vector3 > & out_normals){
std::vector<unsigned int> vertexIndices, uvIndices, normalIndices;
std::vector<vector3> temp_vertices;
std::vector<vector2> temp_uvs;
std::vector<vector3> temp_normals;
FILE * file = fopen(path, "r");
if (file == NULL)
return;
while (1){
char lineHeader[128];
int res = fscanf(file, "%s", lineHeader);
if (res == EOF)
break;
if (strcmp(lineHeader, "v") == 0){
vector3 vertex;
fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
temp_vertices.push_back(vertex);
}else if (strcmp(lineHeader, "vt") == 0){
vector2 uv;
fscanf(file, "%f %f\n", &uv.x, &uv.y);
temp_uvs.push_back(uv);
}else if (strcmp(lineHeader, "f") == 0){
std::string vertex1, vertex2, vertex3;
unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
if (matches != 9){
exit(matches);
return;}
vertexIndices.push_back(vertexIndex[0]);
vertexIndices.push_back(vertexIndex[1]);
vertexIndices.push_back(vertexIndex[2]);
uvIndices.push_back(uvIndex[0]);
uvIndices.push_back(uvIndex[1]);
uvIndices.push_back(uvIndex[2]);
normalIndices.push_back(normalIndex[0]);
normalIndices.push_back(normalIndex[1]);
normalIndices.push_back(normalIndex[2]);
}
}
for (unsigned int i = 0; i < vertexIndices.size(); i++){
unsigned int vertexIndex = vertexIndices[i];
vector3 vertex = temp_vertices[vertexIndex - 1];
out_vertices.push_back(vertex);
}
for (unsigned int i = 0; i < uvIndices.size(); i++){
unsigned int uvIndex = uvIndices[i];
vector2 uv = temp_uvs[uvIndex - 1];
//vertex.normalise();
out_uvs.push_back(uv);
}
}
我用
绘制它void Model::draw()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_TRIANGLES);
glPushMatrix();
glLoadIdentity();
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1, 1, 1);
for (int l_index = 0; l_index < vertices.size(); l_index++)
{
glTexCoord2f(uvs[l_index].x, uvs[l_index].y);
glVertex3f(vertices[l_index].x * 5, vertices[l_index].y * 5, vertices[l_index].z * 5);
}
glDisable(GL_TEXTURE_2D);
glEnd();
}
模型正确加载并保存.obj文件中的法线,uv和顶点。
我如何为此制作着色器?
答案 0 :(得分:3)
您将需要阅读有关GLSL基础知识的一些教程。由于模型包含xyz位置,uv纹理坐标和xyz法线,因此这些将是您的顶点着色器的输入。顶点着色器将使用您的世界/视图/投影矩阵将顶点转换为剪辑空间,将纹理坐标传递给像素着色器,并且像素着色器将从纹理贴图中查找颜色。