Commit 454ed45f authored by Anakin's avatar Anakin

support transparency now

parent b8f8a5c2
......@@ -6,6 +6,8 @@ precision mediump float;
uniform sampler2D texture;
uniform bool b_transparent;
varying vec2 v_texcoord;
void main()
......@@ -13,5 +15,10 @@ void main()
// Set fragment color from texture
vec4 finalColor = vec4(texture2D(texture, v_texcoord));
if(!b_transparent)
{
finalColor.a = 1.0f;
}
gl_FragColor = finalColor;
}
......@@ -71,7 +71,6 @@ void GeometryEngine::loadFile(QString filePath)
m_boundings = file.getBoundingBox();
// collect data
//TODO: sort transparent faces
unsigned int indexOffset(0);
unsigned int vertexOffset(0);
for (auto& modelIterator : *models)
......@@ -94,7 +93,11 @@ void GeometryEngine::loadFile(QString filePath)
// save data
vertexData += segmentIterator->vertices;
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
indexOffset += new_info.size;
......@@ -172,15 +175,25 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
for (auto& it : m_drawList)
{
bool tmp_transparent(false);
// bind the correct texture
if (it.textureIndex < m_materials->size())
{
m_materials->at(it.textureIndex).texture->bind();
tmp_transparent = m_materials->at(it.textureIndex).transparent;
}
else
{
m_materials->last().texture->bind();
tmp_transparent = m_materials->last().transparent;
}
// Set model matrix
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
glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint)));
}
......
......@@ -160,6 +160,7 @@ void MshFile::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
std::list<ChunkHeader*> tmp_matdChunks;
loadChunks(tmp_matdChunks, it->position, it->size);
//TODO: materialy without texture have null pointer need to fix that
m_materials->push_back(Material());
// analyse MATD subchunks
......@@ -216,7 +217,67 @@ void MshFile::analyseMatdChunks(std::list<ChunkHeader*>& 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
m_file.seekg(it->position);
......@@ -397,27 +458,27 @@ void MshFile::analyseSegmChunks(Model * dataDestination, std::list<ChunkHeader*>
// normals
else if (!strcmp("NRML", it->name))
{
std::uint32_t tmp_size;
m_file.seekg(it->position);
m_file.read(F2V(tmp_size), sizeof(tmp_size));
std::uint32_t tmp_size;
m_file.seekg(it->position);
m_file.read(F2V(tmp_size), sizeof(tmp_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);
if (tmp_size < new_segment->vertices.size())
{
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 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 = 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++)
m_file.read(F2V(new_segment->vertices[i].normal[j]), sizeof(float));
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->back().name = QString(buffer);
//TODO: load texture;
loadTexture(m_materials->back().texture, m_filepath + "/" + m_materials->back().name);
new_segment->textureIndex = m_materials->size() - 1;
}
......
No preview for this file type
No preview for this file type
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment