OpenGL奇怪的问题:屏幕适合480x854设备,裁剪图像有1像素差异,怎么了?

时间:2011-10-23 17:31:25

标签: android opengl-es screen

我在Android上运行我的游戏,使用OpenGL开发2D游戏,运行在320x480设备上工作。(没有任何损坏)

不幸的是我的摩托罗拉无法解决(480x854)问题,看起来像调整UV点对象有1-px diff(向上或向下1 px位置偏差)。

SurfaceChanged返回宽度320,高度569 AndroidManifest anyDensity设置为false

下面两张图片有两个矩形,左边是原始图像(w = 36,h = 35),没有裁剪;中间一个是由uv剪辑,从px1绘制并且仅裁剪宽度34px,高度33px,右边一个非常长的矩形,从px0到px480宽度34px,高度1px从上到下绘制,对不起虚拟代码但是你知道我的意思。

我之前的游戏是用SurfaceView编写的,适用于320x480,480x800,480x854和1024x768。

图片看起来像工作,宽度填充但底部最大空。

如果我插入w = 320; H = 480;在SurfaceChanged中的GLUgluOrtho2D之前,一些对象工作(不使用UV裁剪)

image1 http://i1105.photobucket.com/albums/h355/ch_mac/device_str5.png

我想要的结果:

wantedresult http://i1105.photobucket.com/albums/h355/ch_mac/device_str6.png

我的纹理绘制方法:

public void draw(int from_x,int from_y,int w,int h,int pos_x,int pos_y)
{

          bind();
          float texW=(float)mPow2Width;  
          float texH=(float)mPow2Height;

          float minU= from_x/texW;
          float maxU = (from_x+w)/texW;        
          float minV= from_y/texH;        
          float maxV = (from_y+h)/texH;
           float verticleBuffer[] = {
               x,  y,
             x+w,  y,
               x, y+h,
             x+w, y+h,
           };
           float coordBuffer[] = {
            minU, minV,
            maxU, minV,
            minU, maxV,
            maxU, maxV, 
    };

FloatBuffer     m_vertexBuffer;
    FloatBuffer     m_textureBuffer;
    ByteBuffer l_byteBuf;
    l_byteBuf = ByteBuffer.allocateDirect( verticleBuffer.length * 4 );
    l_byteBuf.order( ByteOrder.nativeOrder() );
    m_vertexBuffer = l_byteBuf.asFloatBuffer();
    m_vertexBuffer.put( verticleBuffer );
    m_vertexBuffer.position( 0 );

    l_byteBuf = ByteBuffer.allocateDirect( coordBuffer.length * 4 );
    l_byteBuf.clear();
    l_byteBuf.order( ByteOrder.nativeOrder() );
    m_textureBuffer = l_byteBuf.asFloatBuffer();
    m_textureBuffer.put( coordBuffer );
    m_textureBuffer.position( 0 );

           gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, m_textureBuffer);
           gl.glVertexPointer(2, GL10.GL_FLOAT, 0, m_vertexBuffer);     
           gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,4);

}

SurfaceChanged:

public void onSurfaceChanged(GL10 gl, int w, int h) 
{
    gl.glViewport(0, 0, w, h);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    float aspect_w=(float)h/(float)w;
    float aspect_h=(float)w/(float)h;       
    //w=320;
    //h=480;
    GLU.gluOrtho2D(gl, 0, w, h, 0);

gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}

1 个答案:

答案 0 :(得分:0)

将视口和投影设置放在绘图处理程序中。它是唯一真正有意义的地方(就像大多数其他OpenGL操作一样)。在其他地方做那些事情是簿记的噩梦,没有任何表现。


编辑:另一个问题:

float verticleBuffer[] = {
           x,  y,
         x+w,  y,
           x, y+h,
         x+w, y+h,
       };
float coordBuffer[] = {
        minU, minV,
        maxU, minV,
        minU, maxV,
        maxU, maxV, 
}

你确定coordBuffer是对的吗?对我来说,看起来最后两行是重写的,即一个更有意义:

float coordBuffer[] = {
        minU, minV,
        maxU, minV,
        maxU, maxV,
        minU, maxV, 
}