handle clustered models
This commit is contained in:
@@ -114,9 +114,17 @@ void OpenGLController::deleteVectors()
|
||||
Modl* cursor = vModels.back();
|
||||
vModels.pop_back();
|
||||
|
||||
delete cursor->uv;
|
||||
delete cursor->mesh;
|
||||
delete cursor->vertex;
|
||||
while (!cursor->segmLst.empty())
|
||||
{
|
||||
Segment* segmCuror = cursor->segmLst.back();
|
||||
cursor->segmLst.pop_back();
|
||||
|
||||
delete segmCuror->uv;
|
||||
delete segmCuror->mesh;
|
||||
delete segmCuror->vertex;
|
||||
delete segmCuror;
|
||||
}
|
||||
|
||||
delete cursor;
|
||||
}
|
||||
|
||||
@@ -294,29 +302,36 @@ void OpenGLController::updateScene()
|
||||
glUniform1i(gluiSamplerID, 0);
|
||||
|
||||
int instanceOffset(0);
|
||||
int textureIndex(0);
|
||||
|
||||
for (unsigned int modelIndex = 0; modelIndex < vModels.size(); modelIndex++)
|
||||
{
|
||||
// give texture to the shader
|
||||
glTexImage2D(GL_TEXTURE_2D,
|
||||
0,
|
||||
vTextures[modelIndex]->alpha ? GL_RGBA : GL_RGB,
|
||||
vTextures[modelIndex]->width,
|
||||
vTextures[modelIndex]->height,
|
||||
0,
|
||||
vTextures[modelIndex]->alpha ? GL_BGRA : GL_BGR,
|
||||
GL_UNSIGNED_BYTE,
|
||||
vTextures[modelIndex]->data->data()
|
||||
);
|
||||
for (auto& segIt : vModels[modelIndex]->segmLst)
|
||||
{
|
||||
// give texture to the shader
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D,
|
||||
0,
|
||||
vTextures[textureIndex]->alpha ? GL_RGBA : GL_RGB,
|
||||
vTextures[textureIndex]->width,
|
||||
vTextures[textureIndex]->height,
|
||||
0,
|
||||
vTextures[textureIndex]->alpha ? GL_BGRA : GL_BGR,
|
||||
GL_UNSIGNED_BYTE,
|
||||
vTextures[textureIndex]->data->data()
|
||||
);
|
||||
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
// give the MVP to the shader
|
||||
glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix(modelIndex)[0][0]);
|
||||
// give the MVP to the shader
|
||||
glUniformMatrix4fv(gluiMatrixID, 1, GL_FALSE, &getMVPMatrix(modelIndex)[0][0]);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, instanceOffset, vModels[modelIndex]->meshSize);
|
||||
glDrawArrays(GL_TRIANGLES, instanceOffset, segIt->meshSize);
|
||||
|
||||
instanceOffset += vModels[modelIndex]->meshSize;
|
||||
// recalulate counter
|
||||
instanceOffset += segIt->meshSize;
|
||||
textureIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
glfwSwapBuffers(pWindow);
|
||||
@@ -343,27 +358,30 @@ void OpenGLController::loadMsh(const char * path)
|
||||
// collect vertex data of all models
|
||||
std::vector<Vertex> tempBufferData;
|
||||
|
||||
for (auto& it : vModels)
|
||||
for (auto& modIt : vModels)
|
||||
{
|
||||
for (unsigned int i = 0; i < it->meshSize; i++)
|
||||
for (auto& segIt : modIt->segmLst)
|
||||
{
|
||||
Vertex tempVertex;
|
||||
tempVertex.position[0] = (GLfloat)it->vertex[it->mesh[i] * 3];
|
||||
tempVertex.position[1] = (GLfloat)it->vertex[it->mesh[i] * 3 + 1];
|
||||
tempVertex.position[2] = (GLfloat)it->vertex[it->mesh[i] * 3 + 2];
|
||||
|
||||
if (it->uv == NULL)
|
||||
for (unsigned int i = 0; i < segIt->meshSize; i++)
|
||||
{
|
||||
tempVertex.uv[0] = 1.0;
|
||||
tempVertex.uv[1] = 1.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
tempVertex.uv[0] = (GLfloat)it->uv[it->mesh[i] * 2];
|
||||
tempVertex.uv[1] = (GLfloat)it->uv[it->mesh[i] * 2 + 1];
|
||||
}
|
||||
Vertex tempVertex;
|
||||
tempVertex.position[0] = (GLfloat)segIt->vertex[segIt->mesh[i] * 3];
|
||||
tempVertex.position[1] = (GLfloat)segIt->vertex[segIt->mesh[i] * 3 + 1];
|
||||
tempVertex.position[2] = (GLfloat)segIt->vertex[segIt->mesh[i] * 3 + 2];
|
||||
|
||||
tempBufferData.push_back(tempVertex);
|
||||
if (segIt->uv == NULL)
|
||||
{
|
||||
tempVertex.uv[0] = 1.0;
|
||||
tempVertex.uv[1] = 1.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
tempVertex.uv[0] = (GLfloat)segIt->uv[segIt->mesh[i] * 2];
|
||||
tempVertex.uv[1] = (GLfloat)segIt->uv[segIt->mesh[i] * 2 + 1];
|
||||
}
|
||||
|
||||
tempBufferData.push_back(tempVertex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,35 +397,38 @@ void OpenGLController::loadMsh(const char * path)
|
||||
|
||||
|
||||
// get textures
|
||||
for (auto& it : vModels)
|
||||
for (auto& modIt : vModels)
|
||||
{
|
||||
textureData* tempData = new textureData;
|
||||
try
|
||||
for (auto& segIt : modIt->segmLst)
|
||||
{
|
||||
if (it->texture == "")
|
||||
throw std::invalid_argument("no texture name");
|
||||
textureData* tempData = new textureData;
|
||||
try
|
||||
{
|
||||
if (segIt->texture == "")
|
||||
throw std::invalid_argument("no texture name");
|
||||
|
||||
std::string tempPath = path;
|
||||
std::string tempPath = path;
|
||||
|
||||
while (tempPath.back() != '/' && tempPath.back() != '\\')
|
||||
tempPath.pop_back();
|
||||
while (tempPath.back() != '/' && tempPath.back() != '\\')
|
||||
tempPath.pop_back();
|
||||
|
||||
TextureTGA tempTex(std::string(tempPath + it->texture).c_str());
|
||||
TextureTGA tempTex(std::string(tempPath + segIt->texture).c_str());
|
||||
|
||||
tempData->alpha = tempTex.hasAlpha();
|
||||
tempData->width = tempTex.getWidth();
|
||||
tempData->height = tempTex.getHeight();
|
||||
tempData->data = new std::vector<std::uint8_t>(tempTex.getData());
|
||||
tempData->alpha = tempTex.hasAlpha();
|
||||
tempData->width = tempTex.getWidth();
|
||||
tempData->height = tempTex.getHeight();
|
||||
tempData->data = new std::vector<std::uint8_t>(tempTex.getData());
|
||||
}
|
||||
catch (std::invalid_argument e)
|
||||
{
|
||||
GLubyte solidColor[4] = { 0, 0, 255, 255 };
|
||||
tempData->alpha = true;
|
||||
tempData->width = 1;
|
||||
tempData->height = 1;
|
||||
tempData->data = new std::vector<std::uint8_t>({ 0, 0, 255, 255 });
|
||||
}
|
||||
|
||||
vTextures.push_back(tempData);
|
||||
}
|
||||
catch (std::invalid_argument e)
|
||||
{
|
||||
GLubyte solidColor[4] = { 0, 0, 255, 255 };
|
||||
tempData->alpha = true;
|
||||
tempData->width = 1;
|
||||
tempData->height = 1;
|
||||
tempData->data = new std::vector<std::uint8_t>({ 0, 0, 255, 255 });
|
||||
}
|
||||
|
||||
vTextures.push_back(tempData);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user