Tommz
Tommz

Reputation: 3453

How to put a texture on object in OpenGL?

I've made sphere using base of icosahedron, so I have many triangles combined into a sphere. There are many vertices and indices, but everything gets drawn up easily with this code (java):

gl.glFrontFace(GL10.GL_CW); 
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
gl.glDrawElements(GL10.GL_TRIANGLES, size, GL10.GL_UNSIGNED_SHORT, shortBuff);

where shortBuff presents indices and vertBuff presents vertices. Is there any way to wrap image over the object's front face in order to put texture?

Upvotes: 0

Views: 4227

Answers (1)

Thomas
Thomas

Reputation: 88707

Basically you'd need texture coordinates for your vertices and then load an image into a texture and activate that texture stage.

Here's an (somewhat old but still valid) article on OpenGL texture mapping.

Another source might be the offical OpenGL FAQ.

There are also options to let OpenGL generate the texture coordinates for you, but they might not produce the result you want.

A last and more modern approach would be to use shaders for the texture mapping. That, however, might be still above your level and I'm also not sure about OpenGL ES' capabilities (I'm working with full OpenGL).

Edit:

Here's a tutorial on OpenGL tex coord generation.

Please note that you should still dig into other texture coordinate generation methods, e.g. generating them elsewhere (like modelling software) and loading them along with your vertex coordinates - or calculating them from the vertex coordinates or other data.

Texture coordinate generation might be a start but for learning purposes I'd advice to learn how to do it manually as well. That way you'll gain a better understanding of how texture coordinates and texture mapping work.

Upvotes: 1

Related Questions