support transparency now

This commit is contained in:
Anakin 2017-01-15 15:51:12 +01:00
parent b8f8a5c2ad
commit 454ed45fa1
5 changed files with 103 additions and 23 deletions

View File

@ -6,6 +6,8 @@ precision mediump float;
uniform sampler2D texture; uniform sampler2D texture;
uniform bool b_transparent;
varying vec2 v_texcoord; varying vec2 v_texcoord;
void main() void main()
@ -13,5 +15,10 @@ void main()
// Set fragment color from texture // Set fragment color from texture
vec4 finalColor = vec4(texture2D(texture, v_texcoord)); vec4 finalColor = vec4(texture2D(texture, v_texcoord));
if(!b_transparent)
{
finalColor.a = 1.0f;
}
gl_FragColor = finalColor; gl_FragColor = finalColor;
} }

View File

@ -71,7 +71,6 @@ void GeometryEngine::loadFile(QString filePath)
m_boundings = file.getBoundingBox(); m_boundings = file.getBoundingBox();
// collect data // collect data
//TODO: sort transparent faces
unsigned int indexOffset(0); unsigned int indexOffset(0);
unsigned int vertexOffset(0); unsigned int vertexOffset(0);
for (auto& modelIterator : *models) for (auto& modelIterator : *models)
@ -94,7 +93,11 @@ void GeometryEngine::loadFile(QString filePath)
// save data // save data
vertexData += segmentIterator->vertices; vertexData += segmentIterator->vertices;
indexData += segmentIterator->indices; indexData += segmentIterator->indices;
m_drawList.push_back(new_info);
if (segmentIterator->textureIndex < m_materials->size() && m_materials->at(segmentIterator->textureIndex).transparent)
m_drawList.push_back(new_info);
else
m_drawList.push_front(new_info);
// update offset // update offset
indexOffset += new_info.size; indexOffset += new_info.size;
@ -172,15 +175,25 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
for (auto& it : m_drawList) for (auto& it : m_drawList)
{ {
bool tmp_transparent(false);
// bind the correct texture // bind the correct texture
if (it.textureIndex < m_materials->size()) if (it.textureIndex < m_materials->size())
{
m_materials->at(it.textureIndex).texture->bind(); m_materials->at(it.textureIndex).texture->bind();
tmp_transparent = m_materials->at(it.textureIndex).transparent;
}
else else
{
m_materials->last().texture->bind(); m_materials->last().texture->bind();
tmp_transparent = m_materials->last().transparent;
}
// Set model matrix // Set model matrix
program->setUniformValue("m_matrix", it.modelMatrix); program->setUniformValue("m_matrix", it.modelMatrix);
// decide if this is transparent
program->setUniformValue("b_transparent", tmp_transparent);
// Draw cube geometry using indices from VBO 1 // Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint))); glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint)));
} }

View File

@ -160,6 +160,7 @@ void MshFile::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
std::list<ChunkHeader*> tmp_matdChunks; std::list<ChunkHeader*> tmp_matdChunks;
loadChunks(tmp_matdChunks, it->position, it->size); loadChunks(tmp_matdChunks, it->position, it->size);
//TODO: materialy without texture have null pointer need to fix that
m_materials->push_back(Material()); m_materials->push_back(Material());
// analyse MATD subchunks // analyse MATD subchunks
@ -216,7 +217,67 @@ void MshFile::analyseMatdChunks(std::list<ChunkHeader*>& chunkList)
{ {
for (auto& it : chunkList) for (auto& it : chunkList)
{ {
if (!strcmp("TX0D", it->name)) //TODO: get information from flags
// attributes
if (!strcmp("ATRB", it->name))
{
// read the attributes
m_file.seekg(it->position);
std::uint8_t flag, render, data[2];
m_file.read(F2V(flag), sizeof(flag));
m_file.read(F2V(render), sizeof(render));
m_file.read(F2V(data[0]), sizeof(data[0]));
m_file.read(F2V(data[1]), sizeof(data[1]));
// specular
if (flag >> 7)
{
std::cout << "specular" << std::endl;
}
// additive transparency
if ((flag << 1) >> 7)
{
std::cout << "additive transparency" << std::endl;
m_materials->back().transparent = true;
}
// per-pixel lighting
if ((flag << 2) >> 7)
{
std::cout << "per-pixel lighting" << std::endl;
}
// hard-edged transparency
if ((flag << 3) >> 7)
{
std::cout << "hard-edged transparency" << std::endl;
m_materials->back().transparent = true;
}
// double-sided transparency
if ((flag << 4) >> 7)
{
std::cout << "double-sided transparency" << std::endl;
m_materials->back().transparent = true;
}
// single-sided transparency
if ((flag << 5) >> 7)
{
std::cout << "single-sided transparency" << std::endl;
m_materials->back().transparent = true;
}
// glow
if ((flag << 6) >> 7)
{
std::cout << "glow" << std::endl;
}
// emissive
if ((flag << 7) >> 7)
{
std::cout << "emissive" << std::endl;
}
}
// texture name
else if (!strcmp("TX0D", it->name))
{ {
// get the texture name // get the texture name
m_file.seekg(it->position); m_file.seekg(it->position);
@ -397,27 +458,27 @@ void MshFile::analyseSegmChunks(Model * dataDestination, std::list<ChunkHeader*>
// normals // normals
else if (!strcmp("NRML", it->name)) else if (!strcmp("NRML", it->name))
{ {
std::uint32_t tmp_size; std::uint32_t tmp_size;
m_file.seekg(it->position); m_file.seekg(it->position);
m_file.read(F2V(tmp_size), sizeof(tmp_size)); m_file.read(F2V(tmp_size), sizeof(tmp_size));
if (tmp_size < new_segment->vertices.size()) if (tmp_size < new_segment->vertices.size())
{ {
emit sendMessage("WARNING: too less normals " + QString::number(tmp_size) + " < " + QString::number(new_segment->vertices.size()), 1); emit sendMessage("WARNING: too less normals " + QString::number(tmp_size) + " < " + QString::number(new_segment->vertices.size()), 1);
for (unsigned int i = new_segment->vertices.size(); i != tmp_size; i--) for (unsigned int i = new_segment->vertices.size(); i != tmp_size; i--)
for (unsigned int j = 0; j < 3; j++)
new_segment->vertices[i - 1].normal[j] = 0;
}
else if (tmp_size > new_segment->vertices.size())
{
emit sendMessage("WARNING: too many normals " + QString::number(tmp_size) + " > " + QString::number(new_segment->vertices.size()), 1);
tmp_size = new_segment->vertices.size();
}
for (unsigned int i = 0; i < tmp_size; i++)
for (unsigned int j = 0; j < 3; j++) for (unsigned int j = 0; j < 3; j++)
new_segment->vertices[i - 1].normal[j] = 0; m_file.read(F2V(new_segment->vertices[i].normal[j]), sizeof(float));
}
else if (tmp_size > new_segment->vertices.size())
{
emit sendMessage("WARNING: too many normals " + QString::number(tmp_size) + " > " + QString::number(new_segment->vertices.size()), 1);
tmp_size = new_segment->vertices.size();
}
for (unsigned int i = 0; i < tmp_size; i++)
for (unsigned int j = 0; j < 3; j++)
m_file.read(F2V(new_segment->vertices[i].normal[j]), sizeof(float));
} }
@ -536,7 +597,6 @@ void MshFile::analyseClthChunks(Model * dataDestination, std::list<ChunkHeader*>
m_materials->push_back(Material()); m_materials->push_back(Material());
m_materials->back().name = QString(buffer); m_materials->back().name = QString(buffer);
//TODO: load texture;
loadTexture(m_materials->back().texture, m_filepath + "/" + m_materials->back().name); loadTexture(m_materials->back().texture, m_filepath + "/" + m_materials->back().name);
new_segment->textureIndex = m_materials->size() - 1; new_segment->textureIndex = m_materials->size() - 1;
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 48 KiB

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.