Jump to content
  • Advertisement
Sign in to follow this  
rXpSwiss

OpenGL [FIXED]Open simple obj file -> nothing

This topic is 1456 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

Hello,

 

I tried to set myself a not to hard challenge since I'm in a early stage of learning OpenGL.

My goal was to be able to load simple obj file without the texture and then add the texture etc.

I parse the file correctly but I always have nothing displayed but not error, but I can still draw manually a triangle etc.

I am going to put my simple init and draw code below and also the results of a parsed file (only the vertices position).

But this is my question : Do I need to do something "special" to display an object from a obj file ? 

 

Init :

void init(){
//---------------------------- SHADERS
GLint vertShader = createBasicShader();
GLint fragShader = createFragShader();


programHandle = glCreateProgram();
if (0 == programHandle)
{
fprintf(stderr, "Error creating program object.\n");
exit(1);
}
glAttachShader(programHandle, vertShader);
glAttachShader(programHandle, fragShader);


glLinkProgram(programHandle);
if (glVerifyStatus(programHandle, "link program")==GL_TRUE)
glUseProgram(programHandle);

//setup element array buffer
glGenBuffers(2, vboHandles);
glBindBuffer(GL_ARRAY_BUFFER, vboHandles[0]);
glBufferData(GL_ARRAY_BUFFER, obj.getVertexNumber() * 4 * sizeof(GLfloat), obj.getVertexCoords()->data(), GL_STATIC_DRAW);


glBindBuffer(GL_ARRAY_BUFFER, vboHandles[1]);
glBufferData(GL_ARRAY_BUFFER, obj.getVertexNumber()*3 * sizeof(GLfloat), obj.getNormals()->data(), GL_STATIC_DRAW);


glGenVertexArrays(1, &vaoHandle);
glBindVertexArray(vaoHandle);


glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, vboHandles[0]);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);


glBindBuffer(GL_ARRAY_BUFFER, vboHandles[1]);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);

}

draw :

void draw(){
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(vaoHandle);
glDrawArrays(GL_TRIANGLES, 0, obj.getVertexNumber());
glFlush();
}

A parsed file vertices position (cube in blender) :

1, -1, -1, 1,
1, -1, 1, 1,
-1, -1, 1, 1,
-1, -1, -1, 1,
1, 1, -1, 1,
1, 1, 1, 1,
-1, 1, 1, 1,
-1, 1, -1, 1

I hope someone will be able to help me smile.png

 

rXp

 

Share this post


Link to post
Share on other sites
Advertisement

"Do I need to do something "special" to display an object from a obj file ?"

You just need to parse the file correctly, once you got the file data into memory it doesn't matter what format the file was. With the file data you should be able to prepare the buffers you need to pass to the OpenGL functions.

 

If you have any doubt, try the same code but instead of reading the OBJ file, hard code the vertices. If you see something then your problem is on the parsing or the array construction, if you still see nothing then the problem is elsewhere. Can you draw a single triangle with your current code?

 

I've never used buffers with OpenGL so I can't follow that code, but I guess the rest of the code will be usefull for other users.

 

Also, learn how to use a debugger, and set a breakpoint to make sure that the values you read from the file are still there in the arrays when setting the buffer data.

Edited by DiegoSLTS

Share this post


Link to post
Share on other sites

The values are correct and the last "code" is a file that I parsed and printed (so 100% correct).

Outside from that, yes I can draw a simple triangle.

Share this post


Link to post
Share on other sites

debugging is the most important skill to learn:

so start by walking thru the code and see if it matches exactly with what you think it is doing. 

A few things jump out at me: 

the 4 in

glBufferData(GL_ARRAY_BUFFER, obj.getVertexNumber() * 4 * sizeof(GLfloat), obj.getVertexCoords()->data(), GL_STATIC_DRAW);

the GL_TRIANGLES in

glDrawArrays(GL_TRIANGLES, 0, obj.getVertexNumber());

 

should the 4 be 3? why should it be 3?

 

And the parsed file vert positions looks off:

 

say like what does 1, -1, -1, 1,  represent?

 

I was expecting to see something like 1,-1,1 to represent say the (xyz) coordinates of a vert.

Edited by steven katic

Share this post


Link to post
Share on other sites

Since the W is optional in obj, if there is none I add it to 1.0f myself. That's why there is X,Y,Z,W everytime.

Edited by rXpSwiss

Share this post


Link to post
Share on other sites

diegoSTLS makes a good point: "You just need to parse the file correctly, once you got the file data into memory it doesn't matter what format the file was."

ok, so W is for freeform surfaces in obj.

 

And why should the 4 be a 3? (maybe I haven't given enough hints?)

Edited by steven katic

Share this post


Link to post
Share on other sites

Okey, I see changes now but let me explain how I thought :

When I parse a obj file there can be 4 values for each vertex position (x,y,z,w), since the W is optional I decided to get it if it is specified or to set it to 1.0f if it is not specified.

This means after parsing I always have 4 values for a vertex position.

When I wrote that

glBufferData(GL_ARRAY_BUFFER, obj.getVertexNumber() * 4 * sizeof(GLfloat), obj.getVertexCoords()->data(), GL_STATIC_DRAW);

I thought : "Okey, there is X vertices with 4 floating values, hence X * 4 * sizeof(GLfloat)" and this is the reason I also set the glVertexAttribPointer to "0,4,..." because I want to give a vec4 my basic vertex shader so it can simply set it to gl_position.

 

I understand that it is wrong but I don't understand why. What is wrong in my train of thought ?

Edited by rXpSwiss

Share this post


Link to post
Share on other sites

now might be a good time to get really familiar with glDrawArrays and gldrawelements. look for as many examples as you need to.

you ultimately will end up explicitly specifiying the structure of your data in a format that is restricted by opengl function specs:

typically when you specify GL_TRIANGLES your data should conform to a "set of 3 floats in the array of floats represents a vertex of a triangle".

edit: and so every sequence of 3 verts (of 3 floats) will represent a triangle.

another useful thing to research now would be arrays of structures vs structures of arrays.

I feel I am still being a little bit vague here, so I welcome anyone to be more succinct and specific with an explanation for the OP.

Still... I hope this helps   

Edited by steven katic

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!