// my generic vector class
template <class T>
struct Vector3D
{
T X;
T Y;
T Z;
Vector3D(){
X = 0;
Y = 0;
Z = 0;
}
Vector3D(T x, T y, T z){
X = x;
Y = y;
Z = z;
}
Vector3D(T x, T y){
X = x;
Y = y;
Z = 0;
}
.......
}
//here where I init the model data and send it to the opengl
void Model::Init(std::vector<Vector3D<float>> vertices, std::vector<Vector3D<float>> uvs, std::vector<Vector3D<float>> normals, std::vector<Face> faces)
{
std::vector<Vector3D<float>> drawVertices;
if (!faces.empty()){
for (size_t i = 0; i < faces.size(); i++)
{
drawVertices.push_back(vertices[faces[i].VerticesIndex[0]]);
drawVertices.push_back(vertices[faces[i].VerticesIndex[1]]);
drawVertices.push_back(vertices[faces[i].VerticesIndex[2]]);
if (faces[i].IsQuad)
drawVertices.push_back(vertices[faces[i].VerticesIndex[3]]);
}
}
else
{
drawVertices = vertices;
}
verticesSize = drawVertices.size();
glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);
glGenBuffers(1, &generatedBuffer);
glBindBuffer(GL_ARRAY_BUFFER, generatedBuffer);
glBufferData(GL_ARRAY_BUFFER, drawVertices.size() * sizeof(drawVertices[0]), &drawVertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindVertexArray(0);
}
//here is how am drawing it
void Model::Draw()
{
if (!isInited)
{
Init(tempVertices, tempUVs, tempNormals, tempFaces);
isInited = true;
tempVertices.clear();
tempUVs.clear();
tempNormals.clear();
tempFaces.clear();
}
glBindVertexArray(vertexArray);
glDrawArrays(GL_TRIANGLES, 0, verticesSize);
glBindVertexArray(0);
}
//the fragmentshader
#version 120
void main()
{
gl_FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}
//the obj file data
v -0.5 -0.5 0.0
v 0.0 0.5 0.0
v 0.5 -0.5 0.0
result is a wrong colored flickering triangle where it should be red triangle without flickering!
http://i.stack.imgur.com/KXKwW.jpgjpg