openGL纹理绑定拉伸和扭曲

时间:2013-07-04 13:53:36

标签: java opengl textures

我正在尝试将纹理绑定到完美的多边形像素(绘制部分纹理但不比多边形本身大)

例如

(编辑:那些是图片中的多边形而不是四边形) example

我的代码如下

//example usage
// drawRectangle((1D * 100D) + 110D, 5, 0D, 25);
//draws a 100px width, 20px height, 0px from left most screen and 5px from top most screen
public static void drawRectangle(double par1, double par2, double par3, double par4)
    {
        //glRectd(par1, par2, par3, par4);
        glBegin(GL_POLYGON);
        {
            glVertex2d(par1, par2);
            glTexCoord2d(0, 0);
            glVertex2d(par3, par2);
            glTexCoord2d(1, 0);
            glVertex2d(par3, par4);
            glTexCoord2d(1, 1);
            glVertex2d(par1, par4);
            glTexCoord2d(0, 1);
        }
        glEnd();
    }

openGL启动代码

glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, finals.GAME_WIDTH, finals.GAME_HEIGHT, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);

1 个答案:

答案 0 :(得分:0)

东西是纹理坐标被标准化为[0..1]范围,你应该使tex坐标取决于多边形大小和tex大小,如下所示:

public static void drawRectangle(double par1, double par2, double par3, double par4,
    double width, double height)
{
    //glRectd(par1, par2, par3, par4);
    glBegin(GL_POLYGON);
    {
        glVertex2d(par1, par2);
        glTexCoord2d(par1/width, par2/height);
        glVertex2d(par3, par2);
        glTexCoord2d(par2/width, par2/height);
        glVertex2d(par3, par4);
        glTexCoord2d(par3/width, par4/height);
        glVertex2d(par1, par4);
        glTexCoord2d(par1/width, par4/height);
    }
    glEnd();
}