In this recipe, we will learn how to apply a texture to the cloth simulation we created in the Creating a cloth simulation recipe of the current chapter.
We will be using the cloth simulation developed in the recipe Creating a cloth Simulation as the base for this recipe.
You will also need an image to use as texture; place it inside your assets
folder. In this recipe we will name our image texture.jpg
.
We will calculate the correspondent texture coordinate to each particle in the cloth simulation and apply a texture.
#include "cinder/gl/Texture.h" #include "cinder/ImageIo.h"
ci::gl::Texture
object in your application's class declaration.gl::Texture mTexture;
setup
method load the image.mTexture = loadImage( loadAsset( "image.jpg" ) );
draw
method. So we'll erase everything in it which was changed in the Creating a cloth simulation recipe and apply the clear
method. Your draw
method should be like the following:void MyApp::draw(){ gl::clear( Color( 0, 0, 0 ) ); }
clear
method call, enable the VERTEX
and TEXTURE COORD
arrays and bind the texture. Add the following to the draw
method:glEnableClientState( GL_VERTEX_ARRAY ); glEnableClientState( GL_TEXTURE_COORD_ARRAY ); mTexture.enableAndBind();
for
loop with mNumRows-1
iterations and create two std::vector<Vec2f>
containers to store vertex and texture coordinates.for( int i=0; i<mNumRows-1; i++ ){ vector<Vec2f>vertexCoords, textureCoords; }
textureCoords
and vertexCoords
. Type the following code into the loop that we created in the previous step:or( int j=0; j<mNumLines; j++ ){ int indexTopLeft = i * mNumLines + j; int indexTopRight = ( i+1) * mNumLines + j; Particle *left = mParticleSystem.particles[ indexTopLeft ]; Particle *right = mParticleSystem.particles[indexTopRight ]; float texX = ( (float)i / (float)(mNumRows-1) ) * mTexture.getRight(); float texY = ( (float)j / (float)(mNumLines-1) ) * mTexture.getBottom(); textureCoords.push_back( Vec2f( texX, texY ) ); vertexCoords.push_back( left->position ); texX = ( (float)(i+1) / (float)(mNumRows-1) ) * mTexture.getRight(); textureCoords.push_back( Vec2f( texX, texY ) ); vertexCoords.push_back( right->position ); }
Now that the vertex
and texture
coordinates are calculated and placed inside vertexCoords
and textureCoords
we will draw them. Here is the complete nested loop:
for( int i=0; i<mNumRows-1; i++ ){ vector<Vec2f> vertexCoords, textureCoords; for( int j=0; j<mNumLines; j++ ){ int indexTopLeft = i * mNumLines + j; int indexTopRight = ( i+1) * mNumLines + j; Particle *left = mParticleSystem.particles[ indexTopLeft ]; Particle *right = mParticleSystem.particles[ indexTopRight ]; float texX = ( (float)i / (float)(mNumRows-1) ) * mTexture.getRight(); float texY = ( (float)j / (float)(mNumLines-1) ) * mTexture.getBottom(); textureCoords.push_back( Vec2f( texX, texY ) ); vertexCoords.push_back( left->position ); texX = ( (float)(i+1) / (float)(mNumRows-1) ) * mTexture.getRight(); textureCoords.push_back( Vec2f( texX, texY ) ); vertexCoords.push_back( right->position ); } glVertexPointer 2, GL_FLOAT, 0, &vertexCoords[0] ); glTexCoordPointer( 2, GL_FLOAT, 0, &textureCoords[0] ); glDrawArrays( GL_TRIANGLE_STRIP, 0, vertexCoords.size() ); }
mTexture
by adding the following:mTexture.unbind();
3.138.34.80