OpenGL: Problem with meshes

Pages: 12
Hi there. I recently submitted a problem that was solved, and have found myself in the wake of another.

I am trying to load in a .obj mesh of a simple bike, which is working fine, yet when it comes to texturing said bike something very strange is happening. The image is loaded and the texture is applied, but it is only visible in triangles, not covering the whole bike, just patches of it.

Think of a backgammon board, if you will. The sharp triangle shapes on that are the same I'm seeing of texture, almost as if the poly's of my mesh have been split into 2 triangles, and only one triangle for each poly is textured.

Sorry if this is confusing. I'd like to show you my code, but I fear it won't help at all.

Oh and the texturing has worked with simpler meshes I've tried, but this one is having problems.

Anyone had a problem like this before, or know how to solve it?

Thanks in advance
Sorry if this is confusing. I'd like to show you my code, but I fear it won't help at all.


Knowing the symptoms of an illness without even having seen the patient is unlikely to lead to a useful diagnosis.
So a lot of penicillin and extremity amputation.
Blind guess, half of your triangles are oriented CW.
Fair enough. This is where I am drawing the "player" avatar along with texturing it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void Player::Load()
{
	m_playerMesh.Load("carmesh2.obj");
	GLuint texId = -1;
	glGenTextures(1, &texId);
	glBindTexture(GL_TEXTURE_2D, texId);
	SDL_Surface* surf = IMG_Load("greenTex.jpg");
	assert(surf);
	unsigned char* data = (unsigned char*) surf->pixels;
	int width = surf->w;
	int height = surf->h;
	int bpp = surf->format->BytesPerPixel;

	int format = GL_RGB;
	if (bpp == 4)
	{
		format = GL_RGBA;
	}

	glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
	gluBuild2DMipmaps(GL_TEXTURE_2D, format, width, height, format, GL_UNSIGNED_BYTE, data);

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
Assuming that your carmesh.obj doesn't have any errors.
You don't draw anything here.
Your *.Load method could be the offender as well.
Last edited on
True. Here is the draw function of my player mesh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void ObjMesh::Draw()
{
	glBegin(GL_TRIANGLES);

	for (unsigned int i = 0; i < m_faces.size(); i++)
	{

		Face& f = m_faces[i];

		int indexCoord1 = f.m_pointIndex[0];
		Vec3f coord1 = m_coords[indexCoord1];

		int indexCoord2 = f.m_pointIndex[1];
		Vec3f coord2 = m_coords[indexCoord2];

		int indexCoord3 = f.m_pointIndex[2];
		Vec3f coord3 = m_coords[indexCoord3];

		Vec3f normal1 = m_normals[f.m_normalIndex[0]];
		Vec3f normal2 = m_normals[f.m_normalIndex[1]];
		Vec3f normal3 = m_normals[f.m_normalIndex[2]];

		Vec2f uv1 = m_uvs[f.m_uvIndex[0]];
		Vec2f uv2 = m_uvs[f.m_uvIndex[1]];
		Vec2f uv3 = m_uvs[f.m_uvIndex[2]];

		glNormal3f(normal1.x, normal1.y, normal1.z);
		glVertex3f(coord1.x, coord1.y, coord1.z);

		glNormal3f(normal2.x, normal2.y, normal2.z);
		glVertex3f(coord2.x, coord2.y, coord2.z);

		glNormal3f(normal3.x, normal3.y, normal3.z);
		glVertex3f(coord3.x, coord3.y, coord3.z);
	}
	glEnd();


Looking at which I'm sure this is something simple but I can't figure it out

Edit: Just a side-note, I created the mesh myself and exported it from Maya as a .obj, so I'm hoping the mesh itself is fine
Last edited on
It's been a while since I used old-style OpenGL, but aren't you supposed to have calls to gl_TexCoord* somewhere? Do you actually bind your texture before drawing the mesh?
I'm actually not entirely sure. I've been trying to read up how to fix this problem but I don't think I've ever come across any mention of gl_TexCoord. Do you have any idea where it would go for me to give it a try?
glTexCoord was for setting texture coordinates. I don't know if there are default values for those, but I don't think so, so you have to set them somewhere I guess.
Ah right, I see what you mean. Wouldn't that be the same as my indexCoord function? I read in the faces and vertexes from a formula, and they get stored in a vec of coords.
It looks like you already have the uvs (texture coordinates) stored in the variables uv1, uv2 and uv3. You should be able to just use glTexCoord2f for each vertex with those variables

1
2
3
4
5
glTexCoord2f(uv1.x, uv1.y);
glNormal3f(normal1.x, normal1.y, normal1.z);
glVertex3f(coord1.x, coord1.y, coord1.z);

//the same for the other two vertices 
Thanks for the help with glTexCoord :)
However, it still gives me the same problem
Try building your texture like this:

1
2
3
4
5
6
7
8
9
GLuint texId = -1;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);


Also, you should store the resulting texId so you can switch it back on later. As it stands, it looks like you're binding GL_TEXTURE_2D here to texId and that's it. You're gonna want to switch that texture off at some point by doing glBindTexture(GL_TEXTURE_2D, 0); to draw something else and then you'd want to switch it back on to draw the player.

Are you sure you're calling glEnable(GL_TEXTURE_2D); before drawing the player? And like hanst suggested, the problem could be how you're loading the .obj. If the problem isn't solved yet, maybe you could post that code as well.
Still having the same problems. Thanks for all the tips so far though all :)

Here is the code in which I'm reading in the .obj:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
bool ObjMesh::Load(const std::string& filename)
{
	File f;
	if (!f.OpenRead(filename))
	{
		return false;
	}
	std::string line;
	while (f.GetString(&line))
	{
		std::cout << line << "\n";

		Strings strs = Split(line, ' ');
		if (strs[0] == "v")
		{
			m_coords.push_back(ToVec3(strs));
		}
		else if (strs[0] == "vn")
		{
			m_normals.push_back(ToVec3(strs));
		}
		else if (strs[0] == "vt")
		{
			m_uvs.push_back(ToVec2(strs));
		}
		else if (strs[0] == "f")
		{
			Face f = ToFace(strs);
			m_faces.push_back(f);
		}
	}
	return true;
}


Along with this, my file class has a function that skips over lines beginning with # or //, though I don't feel like that's relevant. If it is, let me know and I'll show you that aswell
Can you post a screencap of what exactly is happening?
I certainly can, here it is:

http://img152.imageshack.us/img152/7084/carmess.png

If you look closely you can see where each face has been split into 2 triangles, but it's only texturing one of them
Last edited on
Can you please change the clear color to black and make another screen shot?
¿why can you see the dark green triangles? ¿shouldn't they be obstructed?
I suppose that you make cull_face() and that your normals are incorrect.
Clear color black:

http://img31.imageshack.us/img31/8145/carmess2.png

Ne555: They should be obstructed by the rest of the texture, but it seems as though its only drawing half of each face
Try glDisable( GL_CULL_FACE ); and may try color instead of texture (with different for front and back)
Pages: 12