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

  • Home
  • SEARCH
  • 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 9083757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:50:54+00:00 2026-06-16T20:50:54+00:00

EDIT 2: Take a look on the Triangle2d sample of this GitHub project for

  • 0

EDIT 2: Take a look on the Triangle2d sample of this GitHub project for a complete, working sample.

EDIT: See the accepted answer for a link with a good explanation on how the ortographic matrix works. In the end, I tweaked the provided code a little:

  float[] mvp = {
     2f/width,   0f,         0f, 0f,
     0f,        -2f/height,  0f, 0f,
     0f,         0f,         0f, 0f,
    -1f,         1f,         0f, 1f
   };

Please note that my z is fixed in 0 and w fixed in 1. This matrix make the origin (0,0) at the bottom-left of the screen; if you want the origin at top-left, try:

  float[] mvp = {
     2f/width,   0f,         0f, 0f,
     0f,         2f/height,  0f, 0f,
     0f,         0f,         0f, 0f,
    -1f,        -1f,         0f, 1f
   };

Another problem was the call to GLES20.glUniformMatrix4fv which I changed to:

FloatBuffer b = ByteBuffer.allocateDirect(mvp.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
b.put(mvp).position(0);
GLES20.glUniformMatrix4fv(uMvpPos, b.limit() / mvp.length, false, b);

If you want to mess with this a bit, try this online calculator. Just remember that the rows in your sorce file will be columns in the calculator.

Original Problem:
I’m trying to draw a 2d triangle using OpenGLES 2.0 on android, but so far, not much success. These are my shaders:

(Vertex shader)
uniform mat4    uMvp;
attribute vec3 aPosition;
attribute vec3 aColor;
varying vec4 vColor;
void main() {
  vColor = vec4(aColor.xyz, 1.0);
  vec4 position = vec4(aPosition.xyz, 1.0);
  gl_Position = uMvp * position;
};

(Fragment shader)
precision mediump float;
varying vec4 vColor;
void main(void)
{
  gl_FragColor = vColor;
};

Then, in the onSurfaceChanged method of GLSurfaceView.Renderer, I put the following:

public void onSurfaceChanged(GL10 gl, int width, int height)
{
  // here I load and compile the shaders and link the program.
  // in the end, I use GLES20.glUseProgram(programHandle);
  // (code omitted)

  // create the matrix for the uniform
  int uMvpPos = GLES20.glGetUniformLocation(programHandle, "uMvp");
  float[] mvp = {width, 0f, 0f, 0f,
                 0f, -height, 0f, 0f,
                 0f, 0f, -2f, 0f,
                 -1f, 1, -1f, 1};
  GLES20.glUniformMatrix4fv(uMvpPos, mvp.length * 4, false, mvp, 0);

  // set viewport and clear color to white
  GLES20.glViewport(0, 0, width, height);
  GLES20.glClearColor(1f, 1f, 1f,1.0f);
}

I used the values of the matrix showed on this question. My intent here is to work with coordinates in the same way as a canvas works: (0,0) on top-left of screen and (width, height) on bottom-right.

And last but not least, this is the code of onDrawFrame:

public void onDrawFrame(GL10 gl)
{
  int aPos = GLES20.glGetAttribLocation(programHandle,"aPosition");
  int aColor = GLES20.glGetAttribLocation(programHandle,"aColor");

  // assuming I correctly set up my coordinate system,
  // these are the triangle coordinates and color
  float[] data =
      {
        // XYZ, RGB
        100f, 100f, 0f,
        1f, 0f, 0f,

        50f, 50f, 0f,
        1f, 0f, 0f,

        150f, 50f, 0f,
        1f, 0f, 0f,
      };

  // put all my data into a float buffer
  // the '* 4' is because a float has 4 bytes
  FloatBuffer dataVertex = ByteBuffer.allocateDirect(data.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
  dataVertex.put(data).position(0);

  // set the POSITION values
  // attribute, dataSize(number of elements), data type, normalized?, size(bytes), data
  GLES20.glEnableVertexAttribArray(aPos);
  GLES20.glVertexAttribPointer(aPos, 3, GLES20.GL_FLOAT, false, 6 * 4, dataVertex);

  // set the COLOR values
  dataVertex.position(3); // offset
  GLES20.glEnableVertexAttribArray(aColor);
  GLES20.glVertexAttribPointer(aColor, 3, GLES20.GL_FLOAT, false, 6 * 4, dataVertex);

  // put a white background
  GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

  // and finally draw the triangle!
  GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}

The end result is… a boring white screen, without the triangle. I guess I’m committing a very simple mistake, but I just can’t spot it. Any thoughts?

  • 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-16T20:50:55+00:00Added an answer on June 16, 2026 at 8:50 pm

    Your orthographic projection matrix is wrong.
    Orthographic projection is defined as (in column major order):

    2/(r-l), 0, 0, 0,
    0, 2/(t-b), 0, 0,
    0, 0, 2/(f-n), 0,
    (r+l)/(l-r), (t+b)/(b-t), (f+n)/(n-f), 1
    

    In your case r=0, l=width, t=height, b=0, f=1, n=0 and you get:

    -2/width, 0, 0, 0,
    0, 2/height, 0, 0,
    0, 0, 2, 0,
    1, -1, -1, 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take a look at this sample: http://jsbin.com/imivek/1/edit Clicking the text field, which is part
EDIT 23-06-2012 10:24 (CET) : Found the answer Take a look at the bottom
EDIT To simplify this further. Let's take a look at this structure: Table1: idProduct
Take a look at the this link . Scroll down a bit and you
If you take a look at this fiddle in Chrome: http://jsfiddle.net/up4Fa/ You will see
EDIT: I solved my issue...take a look at my answer. Correct if it's wrong.
MAIN EDIT: This will help to explain the question better, take a look of
Take a look at this fiddle which shows a sample menu that I've written.
Take a look at my code example at js bin: http://jsbin.com/iritep/3/edit I'd like to
** EDIT ** Nevermind, just needed to take out the parens... I get this

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.