1
\$\begingroup\$

I am working in OpenGL ES 2.0.

I load two png images as my textures with the libSOIL. I need to use one of them as the texture for the background and another one as the texture of a rotating cube.

In OpenGL ES 2.0, the adding texture operation is in the shader. I don't know how to add the different textures to the different places in a shader. How do I do that?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The shader should use a single texture. Set the cube texture to TEX0 (or whatever it is called) then draw the cube. Then set the background texture and draw a quad that covers the entire screeen in the background. \$\endgroup\$
    – Gir
    Commented Oct 29, 2013 at 10:07
  • \$\begingroup\$ Thank you for the response.But I still don't know how to set the two textures at different time.Can you explain in detail? \$\endgroup\$ Commented Oct 29, 2013 at 11:17

1 Answer 1

3
\$\begingroup\$

Simplest way is multiple draw calls.

In OpenGL ES 2.0, the adding texture operation is in the shader

Not really. How does shaders know where to take texture pixels? You must bind texture outside shader too.

Basic algorithm for drawing multiple objects is:

for(each object obj)
{
    SetPipelineState(obj);
    Draw(obj);
}

In your case:

void RenderFrame()
{
    ClearBackBuffer();
    ClearDepthBuffer();

    // Draw cube
    glUseProgram(programForCube);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textureOfCube);  // Texture for cube
    glBindBuffer(...) // Vertex buffer for cube
    // other states go here
    Draw(...);

    // Draw background
    glUseProgram(programForBackground);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textureOfBackground); // Texture for background
    glBindBuffer(...) // Vertex buffer for background
    // other states go here
    Draw(...);
}

Don't forget to set up depth state and depth buffer.

There are advanced techniques to reduce draw calls number.

Even better and more general solution will be to google up complete OpenGL tutorial course. There are plenty on the web.

\$\endgroup\$
4
  • \$\begingroup\$ Thank you for your response. What's more, can I use two shaders in a OpenGL ES program? \$\endgroup\$ Commented Oct 30, 2013 at 1:30
  • \$\begingroup\$ It's true that I can write two shaders in a program, but when I use the API glUseProgram() to choose the shader program I use, it always the last one which is displayed in screen. Can the background and the cube display in screen at the same time? \$\endgroup\$ Commented Oct 30, 2013 at 7:15
  • \$\begingroup\$ You can use as much shaders as you want, and normally you can display as much objects as you want, and I wrote how to. Pay attention to this: "Don't forget to set up depth state and depth buffer". To see what exactly wrong, we need to see the code. But you can just Google-up "OpenGL tutorials" and find it out yourself. \$\endgroup\$
    – Drop
    Commented Oct 30, 2013 at 8:16
  • \$\begingroup\$ Thank you very much, Drop. I write my code in the way which you tell me, it works well now! Thank you! Because I forget to set the view matrix, it doesn't work before. I have modified my code. Thank you! \$\endgroup\$ Commented Oct 30, 2013 at 9:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .