• Advertisement
Sign in to follow this  

trying to draw with modern opengl but am geeting wrong result

This topic is 1241 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to draw a triangle using `OpenGL` , but it's keep flickering with wrong color , here is my code :

    // 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

Share this post


Link to post
Share on other sites
Advertisement

A small tip:

template <typename T>
class Vector3D
{
    // stuff
};
using vec3 = Vector3D<float>;

vec3 vector;

Also:

// old: for (size_t i = 0; i < faces.size(); i++)
// new:
for (auto& face : faces)
{
    v.push_back ... face[0]
    ...
}

It looks like the "faces" you have may be 3x vertices linear in memory?

If Face is sizeof(float) * 3, then your usage of vector<> may mean that you can simply memcpy directly from one place to another:

// reserve enough space (in elements)
v2.reserve(v2.size() + faces.size() * 3);
// if Face is a vector, copy everything from v1 to v2
std::copy(v1.begin(), v1.end(), std::back_inserter(v2));
// or, in the no-vector case, either use memcpy() or copy element by element (as you are already doing)

Reserving space by itself can help alot with the speed, and isn't risky in case you change things later on

 

Btw.,

glBindVertexArray(0);

is unnecessary, and only reverts you to immediate mode in a compatibility context.

 

You don't need to do much to at least use GLSL 1.3 (or 1.5, using #version 150 for OpenGL 3.3):

#version 130

out vec4 color;

void main(void)
{
    color = vec4(1.0, 0.0, 0.0, 1.0);
}

(although I recommend even newer!)

Here is a good example I found: http://www.lighthouse3d.com/cg-topics/code-samples/opengl-3-3-glsl-1-5-sample/

 

I don't see any problems other than perhaps you are rendering using wrong mode: GL_TRIANGLES

Perhaps the model requires indexes.

What is faces supposed to do?

Edited by Kaptein

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement