0

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

例如(编辑:这些是多边形的图片而不是四边形) 例子

我的代码如下

//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);
4

1 回答 1

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();
}
于 2013-07-04T15:04:30.060 回答