Jump to content
  • Advertisement
Sign in to follow this  
gdevmon

Multiple Texture Coords for a vertex

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

can anyone tell me how am i suppose to pass multiple texture coords to a single vertex in OpenGL?

right now i am using glDrawElements to draw my mesh. but when it comes to texturing something like a simple cube the image is either distorted or some faces arent even textured.

 

opengl expects me to pass in the vbos like: v1v2...vM n1n2...nM t1t2...tM or v1t1n1 v2t2n2 ... vMtMnM , so the normal and UVs must be one for each vertex correct? so how am i suppose to pass in 2 texcoord per vertex?

 

im passing only vertices and uv coords for now like this:

glBufferSubData(GL_ARRAY_BUFFER, 0,                    3 * sizeof(GLfloat) * loader.vertices.size(), loader.vertices.data());
glBufferSubData(GL_ARRAY_BUFFER, 3 * sizeof(GLfloat) * loader.vertices.size(), 2 * sizeof(GLfloat) * loader.vertices.size(), loader.textures.data());

for example, suppose i have a cube like so in this image:

http://imageshack.com/a/img922/329/fsdEBO.png

note: the vnumber refers to the vertices. its not exact though, just numbered them in for an example purposes.

 

if u can see in the image it has 14 different uv coords. but i have only 8 vertices. you can clearly see that left side 6 uv coords point to the same vertices on the right.

so do i have to like clone the vertex or something?

isnt drawing with indices supposed to reduce the number of vertices to save memory? i know it wouldnt be too much problem with basic shapes like a pyramid, cube, cylinder etc to duplicate some vertices. but what if i have more complex models like human and whatnot

 

maybe im there is some function? if so, then can anyone tell what that function is and how can i fix this duplicate vertex problem?

 

i've seen other posts and searched the internet for this but nothing seemed to explain the problem well. i've also read somewhere that opengl allows 8 uv coords per vertex, so, someone please explain me or suggest me a good modern opengl book.

 

im using my own loader for obj file too. heres the source:

void WFOLoader::load(std::string path) {
	std::ifstream file(path);
	
	std::vector<GLfloat> vertices;
	std::vector<GLfloat> textures;
	std::vector<GLuint> indices;

	std::vector<GLfloat> tex_ordered;

	std::vector<GLuint> tex_order;

	float *textures_ord;

	int num_verts;

	if (!file.is_open()) {
		std::cout << "Error" << std::endl;
	} else {

		std::string cline;

		while (getline(file, cline)) {	
			std::istringstream is;
			if (cline.substr(0, 2) == "v ") {
				is.str(cline);
				std::string c;
				GLfloat x, y, z;
				is >> c; is >> x; is >> y; is >> z;
				vertices.push_back(x);
				vertices.push_back(y);
				vertices.push_back(z);
			}
			if (cline.substr(0, 2) == "vt") {
				std::string c;
				GLfloat x, y;
				is.str(cline);
				is >> c; is >> x; is >> y;
				textures.push_back(x);
				textures.push_back(y);
			}
			if (cline.substr(0, 2) == "f ") {
				GLuint i1, i2, i3, t1, t2, t3;
				std::string ec;
				char sc;
				int ee;
				is.str(cline);
				is >> ec;
				is >> i1; is >> sc; is >> t1; is >> sc; is >> ee;
				is >> i2; is >> sc; is >> t2; is >> sc; is >> ee;
				is >> i3; is >> sc; is >> t3; is >> sc; is >> ee;

				indices.push_back(--i1);
				indices.push_back(--i2);
				indices.push_back(--i3);

				tex_order.push_back(--t1);
				tex_order.push_back(--t2);
				tex_order.push_back(--t3);
			}
		}

		// re-order textures in places
		for (int i = 0; i < vertices.size()/3; i++) {
			int ind = findIndex(i, &indices);
			//std::cout << ind << std::endl;
			tex_ordered.push_back(textures[tex_order[ind] * 2]);
			tex_ordered.push_back(1 - textures[tex_order[ind] * 2 + 1]);
		}
		// ==========================================
		// re-order normals in places, havent programmed it yet

		//===============================================
		
		this->vertices = vertices;
		this->textures = tex_ordered;
		this->indices = indices;
	}
}

int WFOLoader::findIndex(int value, std::vector<GLuint> *r) {
	for (int i = 0; i < r->size(); i++) {
		if (r->at(i) == value) return i;
		//std::cout << r->at(i) << std::endl;
	}
	return 0;
}

Note: i purposely programmed the loader to ignore duplicates in my loader.

 

p.s: im a beginner-to-intermediate, both in opengl and c++. I apologize if this "problem" is duplicate.

Share this post


Link to post
Share on other sites
Advertisement

Yes, you have to duplicate the vertices along a shared edge if the two faces sharing that edge require different UV layouts.

 

Mathematically, a vertex is a unique position, but...

According to a GPU, a vertex is a unique tuple of attributes. If any of the attributes is different (e.g. the UV is different), then that's a whole new vertex.

 

Yes, indices are very useful in reducing the number of vertices required.

Complex shapes such as a human tend to have a lot of shared vertices (where all the attributes are identical / thus mergable). It's usually only "hard edges", like on simple shapes such as a cube, where you're forced to duplicate verts.

 

Having multiple UV's per vertex is a completely different topic. You use this when you want to apply multiple textures to an object with different projections. The most common example is a regular coloured texture that repeats around your cube, plus a "baked" lightmap that contains pre-computed information about lighting and shadows in your scene.

Edited by Hodgman

Share this post


Link to post
Share on other sites

Thank you Hodgeman. i guess i have no other choice to clone it in the modelling program. Though, if you can recommend some modern opengl books, it would be great too. Thanks again.

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!