开发者

OpenGL - How do you scroll a texture?

I am creating a texture from a pango layout and mapping the texture to the screen using OpenGL and GLUT. I want to scroll the texture in the window. I'm not concerned with the controls t开发者_如何学编程o scroll, but how do I cause map the portion of the texture I want to see onto the screen? I assume I use glTranslate, but where do I apply it?

Thanks in advance.

Here is what I currently do:

glEnable(GL_TEXTURE_2D);

{  
  glBegin(GL_QUADS);  

  {  
        glTexCoord2f(0.0f, 0.0f); glVertex2f(  0.0f+x,   0.0f+y);  
        glTexCoord2f(1.0f, 0.0f); glVertex2f(_width+x,   0.0f+y);  
        glTexCoord2f(1.0f, 1.0f); glVertex2f(_width+x, _height+y);  
        glTexCoord2f(0.0f, 1.0f); glVertex2f(  0.0f+x, _height+y);    
  }  

  glEnd();  
}

glFlush();    

glDisable(GL_TEXTURE_2D);


Another way would be to use a texture matrix (matrix mode GL_TEXTURE) or do it in a shader.

Depends on how many vertices would have to be modified. If just a few it could be more performant but for many vertices a texture matrix/shader approach could be better. Note that built in matrix operations are deprecated, i.e. it now is recommended to use textures.

Scrolling in a shader would be easy: out.tex = in.tex + offset where offset could be a uniform which is set per frame or be calculated from other input.

Since your example uses immediate mode, I'll expand it with the texture matrix mode. But please be aware that this is deprecated methodology.

glEnable(GL_TEXTURE_2D);
{
  glMatrixMode(GL_TEXTURE);
  glLoadIndentity();
  glTranslatef(x,y,0);
  glMatrixMode(GL_MODELVIEW);


  glBegin(GL_QUADS);
  {
    glTexCoord2f(0.0f, 0.0f); glVertex2f( 0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex2f(_width, 0.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex2f(_width, _height);
    glTexCoord2f(0.0f, 1.0f); glVertex2f( 0.0f, _height);
  }
  glEnd();
} 
glFlush();
glDisable(GL_TEXTURE_2D);


Unless you also want to tile I would just modify the UV coordinates of the vertices you are outputting. Then you just change the coordinates you feed to glTexCoord2f to specify a crop.

There is also a nice example of texture transformations here http://potatoland.org/glart/week5/GLART_5_texture_samples.java (it's java but you can easily translate it to C)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜