0

openGL es 2.0 で比率を維持したままテクスチャをレンダリングしようとしています。

比率を計算して錐台の寸法を変更する必要があると思いますが、それは正しい方法ですか? 現在、幅と高さの錐台 -1,1 でレンダリングしているため、画面サイズがない場合にテクスチャが引き伸ばされます。

幅 = 400 高さ = 800 の比率でテクスチャをレンダリングするにはどうすればよいですか?

これは私のコードです:

@Override
    public void onDrawFrame(GL10 glUnused) {

    GLES20.glClearColor(0.9f, 0.9f, 0.9f, .5f);
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
    GLES20.glEnable( GLES20.GL_BLEND );
    GLES20.glBlendFunc( GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA );


    Matrix.setIdentityM(mMMatrix, 0);
    Matrix.rotateM(mMMatrix, 0, 270.0f, 0, 0, 1);
    Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);

    GLES20.glUseProgram(programTextured);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);


    sqTex.getVertexBuffer().position(sqTex.VERT_OFFSET);
    GLES20.glVertexAttribPointer(
            GLES20.glGetAttribLocation(programTextured, "aPosition"), 3,
            GLES20.GL_FLOAT, false, 5 * 4, sqTex.getVertexBuffer());
    GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(programTextured, "aPosition"));

    sqTex.getVertexBuffer().position(sqTex.TEXT_OFFSET);
    GLES20.glVertexAttribPointer(
            GLES20.glGetAttribLocation(programTextured, "aTextureCoord"), 2,
            GLES20.GL_FLOAT, false, 5 * 4, sqTex.getVertexBuffer());
    GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(programTextured, "aTextureCoord"));


    GLES20.glUniformMatrix4fv(
            GLES20.glGetUniformLocation(programTextured, "uMVPMatrix"), 1, false,
            mMVPMatrix, 0);
    GLES20.glVertexAttrib4f(
            GLES20.glGetAttribLocation(programTextured, "acolor"), 
            .6f,0.3f,0.9f,.5f);
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, sqTex.getIndexBuffer());

    GLES20.glDisableVertexAttribArray(GLES20.glGetAttribLocation(programTextured, "aTextureCoord"));
}

@Override
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
    GLES20.glViewport(0, 0, width, height);
    float ratio = (float) width / height;
    //flaot ratio2 = (float)height / 
    //Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3f, 17);



Matrix.frustumM(mProjMatrix, 0, -1, 1, -1, 1, 3, 17);

}

int mTextureID;
@Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {

    program = createProgram(mVertexShader, mFragmentShader);
    programTextured = createProgram(mVertexShaderTextured, mFragmentShaderTextured);

    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);

    mTextureID = textures[0];
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);

    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
            GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
            GLES20.GL_TEXTURE_MAG_FILTER,
            GLES20.GL_LINEAR);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
            GLES20.GL_REPEAT);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_REPEAT);

    InputStream is = mContext.getResources()
        .openRawResource(R.drawable.image0003);
    Bitmap bitmap;
    try {
        bitmap = BitmapFactory.decodeStream(is);
    } finally {
        try {
            is.close();
        } catch(IOException e) {
            // Ignore.
        }
    }

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();
}
4

1 に答える 1

0

私は錐台を残し、代わりにスケールを使用します。

screenRatio = screenWidth/screenHeight最初に a 、次にの 2 つの因子が必要ですimageRatio = imageWidth/imageHeight

imageRatio < screenRatio幅を縮小する必要がある場合は、次のようにします。

if (imageWidth>imageHeight) scaleM(mMMatrix, 1/(imageWidth/imageHeight), 1, 1)
else scaleM(mMMatrix, 1/(imageHeight/imageWidth), 1, 1)

またはimageRatio > screenRatio、高さを縮小する必要がある場合:

if (imageHeight>imageWidth) scaleM(mMMatrix, 1, 1/(imageHeight/imageWidth), 1)
else scaleM(mMMatrix, 1, 1/(imageWidth/imageHeight), 1)
于 2012-09-07T11:33:44.940 に答える