amir
amir

Reputation: 1332

opengl es 2.0 textures the right way

i am new to opengl es 2.0 on ios, i am trying to create few objects (made from vertices) and assign each a different texture, i found out i can do that by binding then drawing in the render function like this:

inside render():

    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);

    glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));

    glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 7));    

    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, _Texture1);
    glUniform1i(_textureUniform, 0); 

    glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);

i repeat the code above for every texture with only changing _Texture1 in glBindTexture to _Texture2 etc..

before all this i setup the textures as follows:

     glGenTextures(1, &texName);
     glBindTexture(GL_TEXTURE_2D, texName);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);

i would like to know what is the right way to give textures to different vertices, should it even be done inside the render function?

this doesn't seem the right way to do it.

(here is the full code if i didnt explain myself fully: http://codetidy.com/1709/ )

Upvotes: 1

Views: 276

Answers (1)

Jeshua Lacock
Jeshua Lacock

Reputation: 6658

You are doing it the correct way. OpenGL is a "state machine", so you have to set the state for the render pipe line each frame drawn. Essentially all your drawing will be done inside your render function.

Upvotes: 2

Related Questions