Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8526639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:19:44+00:00 2026-06-11T08:19:44+00:00

Im trying to render a texture keeping its ratio in openGL es 2.0 .

  • 0

Im trying to render a texture keeping its ratio in openGL es 2.0 .

I suppose I need to calculate the ratio and change the frustum dimensions, is that the right way?
Right now Im rendering with frustum -1,1 for width and height so the texture gets stretched when it doesnt have the screen size.

How do I render the texture lets say width = 400 height = 800 at that ratio?

This is my code :

@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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:19:45+00:00Added an answer on June 11, 2026 at 8:19 am

    I would leave the frustum and rather use scale:

    You need 2 factors, first a screenRatio = screenWidth/screenHeight, then imageRatio = imageWidth/imageHeight.

    Now if imageRatio < screenRatio you will need to scale it down in width:

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

    Or if imageRatio > screenRatio you will need to scale it down in height:

    if (imageHeight>imageWidth) scaleM(mMMatrix, 1, 1/(imageHeight/imageWidth), 1)
    else scaleM(mMMatrix, 1, 1/(imageWidth/imageHeight), 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to render to a texture, then draw that texture to the screen
I am trying to render to a texture using openGL ES (for iPhone) and
I'm trying to write some code that will render a texture onto a simple
I'm trying to write a C++ windows service that can render to a texture.
I'm trying to render to a texture (really thought it would be easier than
Im trying to render emails on my site. The problem is that these emails
I'm trying to render to a texture using an FBO. When trying to do
Okay, so I am trying to render a scene to a small 32x32 texture
I am trying to render video via the NDK, to add some features that
I'm trying to use FBO to render to texture and then display the texture,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.