Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 12 April 2012 - 09:42 AM

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 :

Posted Image



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

Sponsor:

#2 k.pedersen   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 April 2012 - 10:02 AM

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.
Wavefront Animation Studio - Create simple animations for .obj 3D models.
libwavefront - Open-source C++/OpenGL library to load animated .obj models.

#3 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 12 April 2012 - 10:10 AM

tried doing that no luck what so ever but thanks for trying Posted Image



   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]));
	}


#4 k.pedersen   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 April 2012 - 10:16 AM

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) Posted Image
Wavefront Animation Studio - Create simple animations for .obj 3D models.
libwavefront - Open-source C++/OpenGL library to load animated .obj models.

#5 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 12 April 2012 - 10:23 AM

this is the code now Posted Image

	
	  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 :

Posted Image

#6 k.pedersen   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 April 2012 - 10:52 AM

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)
Wavefront Animation Studio - Create simple animations for .obj 3D models.
libwavefront - Open-source C++/OpenGL library to load animated .obj models.

#7 DarkHorseKnight   Members   -  Reputation: 143

Like
-1Likes
Like

Posted 12 April 2012 - 12:38 PM

no luck nevermind




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS