[英]OpenGL strange issue : Screen fit on 480x854 device, crop image having 1 px diff, what's up?
我正在使用OpenGL開發的2D游戲在android上工作,並在320x480設備上運行是正常的。
不幸的是,在我的Motorola defy(480x854)上出現的問題似乎是調整UV點對象的差異為1像素(上下偏移1像素)。
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裁剪)
http://i1105.photobucket.com/albums/h355/ch_mac/device_str5.png
我想要的結果:
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();
}
將視口和投影設置放置在圖形處理程序中。 這是它們真正有意義的唯一地方(就像大多數其他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,
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.