没有时间写博客,参考下:
void model::InitModel(const char*modelFilePath,const char*textureImagePath) {
std::vector<float> texCoord;
std::vector<float> normal;
std::vector<float> points;
std::vector<unsigned int>vertexIndex_Indexs;
std::vector<int> vertexIndex;
std::vector<float> vertexInfo;
//load model from file
int fileSize = 0;
// char *fileContent = LoadModelFile( modelFilePath, fileSize);
char *fileContent = LoadFileContent( modelFilePath);
LOGE("/***********************model start********************************/");
// LOGE("model = %s" , fileContent);
// LOGE("/***********************model voer********************************/");
std::stringstream ssFileContent(fileContent);
char szOneLine[256];//申请一行的数据空间
while (!ssFileContent.eof())// 如果文件没有结尾
{
// 初始化 申请内存,将当前位置后面的数据 用 0代替
memset(szOneLine, 0, 256);
// 获得一行的数据
ssFileContent.getline(szOneLine, 256);
if (strlen(szOneLine) > 0) {
std::stringstream ssOneLine(szOneLine);
std::string temp;
if (szOneLine[0] == 'v') {
if (szOneLine[1] == 't') {
float a, b;
ssOneLine >> temp;//vt
ssOneLine >> a;
ssOneLine >> b;
texCoord.push_back(a);
texCoord.push_back(b);
} else if (szOneLine[1] == 'n') {
// LOGE("normal : %s\n", szOneLine);
float a, b, c;
ssOneLine >> temp;//v
ssOneLine >> a;
ssOneLine >> b;
ssOneLine >> c;
normal.push_back(a);
normal.push_back(b);
normal.push_back(c);
} else {
// LOGE("position : %s\n", szOneLine);
float a, b, c;
ssOneLine >> temp;//v
ssOneLine >> a;
ssOneLine >> b;
ssOneLine >> c;
points.push_back(a);
points.push_back(b);
points.push_back(c);
// points.push_back(point);
}
} else if (szOneLine[0] == 'f') {
ssOneLine >> temp;//去掉f
std::string vertexStr;
for (int i = 0; i < 3; ++i)
{
//ssOneLine>> 是以空格为分隔符,一次输出一个字符串
ssOneLine >> vertexStr;
// LOGE("vertexStr %s ",vertexStr.c_str());
size_t pos = vertexStr.find_first_of('/');
std::string positionIndexStr = vertexStr.substr(0, pos);
size_t pos2 = vertexStr.find_first_of('/', pos + 1);
std::string texcoordIndexStr = vertexStr.substr(pos + 1, pos2 - pos - 1);
std::string normalIndexStr = vertexStr.substr(pos2 + 1,
vertexStr.length() - pos2 - 1);
int a, b, c;
a = atoi(positionIndexStr.c_str());//转化为整数
b = atoi(texcoordIndexStr.c_str());
c = atoi(normalIndexStr.c_str());
int size = vertexIndex.size()/3;
int indexi = -1;
for(int i=0 ; i<size ; i ++){
if(a == vertexIndex[i*3] && b == vertexIndex[i*3 + 1] && c == vertexIndex[i*3 +2])
{
indexi = i;
break;
}
}
if(indexi == -1){
indexi = vertexIndex.size()/3;
vertexIndex.push_back(a);
vertexIndex.push_back(b);
vertexIndex.push_back(c);
// LOGE("a = %d,b = %d,c = %d ",a , b, c);
}
vertexIndex_Indexs.push_back(indexi);
}
}
}
}
delete fileContent;
LOGE("/***********************model end********************************/");
// // 验证face 是否 正确
// for(int i = 0 ; i < vertexIndex_Indexs.size(); i++) {
//
// int vertexInfo_id = vertexIndex_Indexs[i]* 3;
// int point_index = vertexIndex[vertexInfo_id];
// int tx_index = vertexIndex[vertexInfo_id + 1] ;
// int normal_index = vertexIndex[vertexInfo_id + 2];
// LOGE("[v = %d , tx = %d, nor = %d]", point_index, tx_index , normal_index);
// LOGE("");
// }
/***
* 根据 index_index 来获取 每个vertx的index,再根据vertex的index 获取真正的点
*/
for(int i = 0 ; i <vertexIndex.size(); i= i+3){
int point_index = vertexIndex[i] - 1;
int tx_index = vertexIndex[i + 1] - 1;
int normal_index = vertexIndex[i + 2] - 1;
vertexInfo.push_back(points[point_index*3]);
vertexInfo.push_back(points[point_index*3+1]);
vertexInfo.push_back(points[point_index*3+2]);
vertexInfo.push_back(texCoord[tx_index*2]);
vertexInfo.push_back(texCoord[tx_index*2+1]);
vertexInfo.push_back(normal[normal_index*3]);
vertexInfo.push_back(normal[normal_index*3+1]);
vertexInfo.push_back(normal[normal_index*3+2]);
}
/** 利用element绘制 ibo来控制绘制点的顺序
* **/
unsigned int *indexes = vectorIntToArray(vertexIndex_Indexs) ;
glGenBuffers(1, &mIBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER , mIBO);
int size = vertexIndex_Indexs.size();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*size, indexes,GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER , 0);
mIndexCount = vertexIndex_Indexs.size();
LOGE(" mIndexCount = %d " , mIndexCount);
//create vbo
glGenBuffers(1, &mVBO);
glBindBuffer(GL_ARRAY_BUFFER, mVBO);
attributeData = vectorIntToArray(vertexInfo);
glBufferData(GL_ARRAY_BUFFER,sizeof(float)*vertexInfo.size(),attributeData,GL_STATIC_DRAW);
// glBufferSubData(GL_ARRAY_BUFFER,0,sizeof(Vertex)*mVertexCount,mVertexes);
glBindBuffer(GL_ARRAY_BUFFER, 0);
CreateModelProgram("model/earth_vs2.glsl","model/earth_fs2.glsl");
mTexcoordLocation=glGetAttribLocation(program,"texcoord");
positionLocation = glGetAttribLocation(program, "position");
//colorLoction = glGetAttribLocation(program, "color");
modelMatrixLocation = glGetUniformLocation(program , "ModelMatrix");
U_textureLoacation = glGetUniformLocation(program , "U_texture");
viewMatrixLocation = glGetUniformLocation(program , "ViewMatrix");
projectMatrixLocation = glGetUniformLocation(program , "ProjectionMatrix");
// 光照相关
U_LightPosLocation = glGetUniformLocation(program , "U_LightPos");
U_CameraPosLocation = glGetUniformLocation(program , "U_CameraPos");
U_LightAmbientLocation = glGetUniformLocation(program , "U_LightAmbient");
U_LightDiffuseLocation = glGetUniformLocation(program , "U_LightDiffuse");
U_LightSpecularLocation = glGetUniformLocation(program , "U_LightSpecular");
U_AmbientMaterialLocation = glGetUniformLocation(program , "U_AmbientMaterial");
U_DiffuseMaterialLocation = glGetUniformLocation(program , "U_DiffuseMaterial");
U_SpecularMaterialLocation = glGetUniformLocation(program , "U_SpecularMaterial");
U_OptionLocation = glGetUniformLocation(program , "U_Option");
//法向量
mNormalLocation=glGetAttribLocation(program,"normal");
IT_ModelMatrixLocation = glGetUniformLocation(program , "IT_ModelMatrix");
if(textureImagePath != nullptr && textureImagePath != "") {
texture = CreateTextureFromBMP(textureImagePath);
}
}