Jump to content
  • Advertisement
Sign in to follow this  
DominicHughes

OpenGL OpenGL .obj Parser/Rendering Problem Really Really Need Help!

This topic is 2381 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 will start out by explaining my setup/problem

Introduction

I'm using :

OpenGL 3.3
GLSL 330
C++


I'm parsing a .obj file like so :


while (!file.eof() )
{

char modelbuffer[2000000];
file.getline(modelbuffer, 2000000);
switch(modelbuffer[0])
{
cout << " " << endl;
case 'v' :
Point p;
sscanf(modelbuffer, "v %f %f %f", &p.x, &p.y, &p.z);
points.push_back(p);
cout << " p.x = " << p.x << " p.y = " << p.y << " p.z = " << p.x << endl;
break;
cout << " " << endl;
case 'f':
int read_count = sscanf(modelbuffer, "f %i %i %i %i", &face[0], &face[1], &face[2], &face[3]);
cout << "face[0] = " << face[0] << " face[1] = " << face[1] << " face[2] = " << face[2] << " face[3] = " << face[3] << "\n";
if(read_count !=4)
{
cout << "bad/n"; throw std::exception();
}
faces.push_back(face[0] - 1);
faces.push_back(face[1] - 1);
faces.push_back(face[2] - 1);
faces.push_back(face[3] - 1);
cout << face[0] - 1 << face[1] - 1 << face[2] - 1 << face[3] - 1 << endl;
}



And the .obj is being rendered to the screen like so :

[size=2]ammCh.png



This is the .obj file:


# Blender v2.61 (sub 0) OBJ File: ''
# www.blender.org
mtllib cabnet01.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl Material
s off
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8


As you can see it should be a cube rather then a stretched mess.

I've looked into why this could be happening by outputing the data of p.x,p.y,p.z to see if the data was not getting in correctly and found that the data was fine.

I was hoping someone could tell me why this stretchyness could be happening I will provide a link to the full source code below:

http://pastebin.com/ynF8w21D

Share this post


Link to post
Share on other sites
Advertisement
Hi,

Not entirely sure (there was quite a lot of code in that pastebin) but...


glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

//Model Vertices cordinates
glVertexAttribPointer(0, 0, GL_INT, GL_FALSE,0,0);

//Model Texture cordinates
glVertexAttribPointer(1, 0, GL_FLOAT, GL_FALSE,0,0);


I think you need to bind the buffer containing the texture before setting glVertexAttribPointer for the texture coordinates.

Share this post


Link to post
Share on other sites
tried doing that no luck what so ever but thanks for trying smile.png




glBindTexture(GL_TEXTURE_2D, ModelTexture);
//Enable Attribute for "index 0"
glEnableVertexAttribArray(0);
//Enable Attribute for "index 1"
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, texcoordbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
//Model Vertices cordinates
glVertexAttribPointer(0, 0, GL_INT, GL_FALSE,0,0);
//Model Texture cordinates
glVertexAttribPointer(1, 0, GL_FLOAT, GL_FALSE,0,0);

GLfloat RotationMatrix[] = {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
//Translation Process
GLfloat TranslationMatrix[] = {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 0.0, 1.0
};
//Send Translation Matrix up to the vertex shader
glUniformMatrix4fv(translation, 1, TRUE, TranslationMatrix);

//Send Rotation Matrix up to the vertex shader
glUniformMatrix4fv(Rotation, 1, TRUE, RotationMatrix);
glDrawElements( GL_QUADS, faces.size(), GL_UNSIGNED_INT, reinterpret_cast<GLvoid*>(&faces[0]));
}

Share this post


Link to post
Share on other sites
It should be:-


//Model Vertices cordinates
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 0, GL_INT, GL_FALSE,0,0);

//Model Texture cordinates
glBindBuffer(GL_ARRAY_BUFFER, texcoordbuffer);
glVertexAttribPointer(1, 0, GL_FLOAT, GL_FALSE,0,0);



Also... I see another issue.

glDrawElements( GL_QUADS, faces.size(), GL_UNSIGNED_INT, reinterpret_cast<GLvoid*>(&faces[0]));


should be glDrawArrays.

Afterall, you have added your data to a VBO, so why are you now drawing the data from faces[]?

That is also why it is displaying incorrectly, because faces[] contains the data for vertexes and tex coords merged into one.

So yeah, have a look at:

http://www.opengl.or...lDrawArrays.xml
(cant remember the exact code myself, but let me know if you have any issues) smile.png

Share this post


Link to post
Share on other sites
this is the code now smile.png



glBindTexture(GL_TEXTURE_2D, ModelTexture);
//Enable Attribute for "index 0"
glEnableVertexAttribArray(0);
//Enable Attribute for "index 1"
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, texcoordbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
//Model Vertices cordinates
glVertexAttribPointer(0, 0, GL_INT, GL_FALSE,0,0);
//Model Texture cordinates
glVertexAttribPointer(1, 0, GL_FLOAT, GL_FALSE,0,0);
//Translation Process
GLfloat RotationMatrix[] = {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};

//Translation Process
GLfloat TranslationMatrix[] = {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 0.0, 1.0
};
//Send Translation Matrix up to the vertex shader
glUniformMatrix4fv(translation, 1, TRUE, TranslationMatrix);

//Send Rotation Matrix up to the vertex shader
glUniformMatrix4fv(Rotation, 1, TRUE, RotationMatrix);
glDrawArrays(GL_QUADS, 0, 4);


//glDrawElements( GL_QUADS, faces.size(), GL_UNSIGNED_INT, reinterpret_cast<GLvoid*>(&faces[0]));


and this is what it looks like :

XMRoR.png

Share this post


Link to post
Share on other sites
Hmm, I dont think the code is quite right yet...

The following is basically what I use.


glGenBuffersARB(1, &texCoordBuffer);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, texCoordBuffer);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, texCoords.size()*sizeof(float), &texCoords[0], GL_STATIC_DRAW_ARB);
glVertexAttribPointerARB(inTexCoord, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArrayARB(inTexCoord);

glGenBuffersARB(1, &colorBuffer);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, colorBuffer);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, colors.size()*sizeof(float), &colors[0], GL_STATIC_DRAW_ARB);
glVertexAttribPointerARB(inVColor, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArrayARB(inVColor);

glGenBuffersARB(1, &normalBuffer);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, normalBuffer);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, normals.size()*sizeof(float), &normals[0], GL_STATIC_DRAW_ARB);
glVertexAttribPointerARB(inNormal, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArrayARB(inNormal);

glGenBuffersARB(1, &vertexBuffer);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBuffer);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertices.size()*sizeof(float), &vertices[0], GL_STATIC_DRAW_ARB);
glVertexAttribPointerARB(inVertex, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArrayARB(inVertex);

glDrawArrays(GL_TRIANGLES, 0, vertices.size() / 3);


So..

1) Generate the buffer
2) Bind the buffer
3) Set the bound buffer data
4) Set the shader attribute to point to the bound buffer
5) Enable the shader attribute rather than using the default value (i.e the one set with glVertexAttrib3f)

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!