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 7812907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:40:02+00:00 2026-06-02T04:40:02+00:00

This is probably a very novice question, but I could not find an answer.

  • 0

This is probably a very novice question, but I could not find an answer.

Using the simple tutorial from google (http://developer.android.com/resources/tutorials/opengl/opengl-es20.html)

I was trying to draw a square (errm, using common sense?), but getting funny results. It would be grateful if somebody can tell me what is wrong.

   private void initShapes(){    
    float triangleCoords[] = {
        // X, Y, Z
        -0.5f, -0.25f, 0,
         0.5f, -0.25f, 0,
         0.0f,  0.559016994f, 0,
         0.0f,  0f, 0
    }; 

    // initialize vertex Buffer for triangle  
    ByteBuffer vbb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            triangleCoords.length * 4); 
    vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
    triangleVB = vbb.asFloatBuffer();  // create a floating point buffer from the ByteBuffer
    triangleVB.put(triangleCoords);    // add the coordinates to the FloatBuffer
    triangleVB.position(0);            // set the buffer to read the first coordinate    
}


public void onDrawFrame(GL10 unused) {    
    // Redraw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

 // Add program to OpenGL environment
    GLES20.glUseProgram(mProgram);

    GLES20.glVertexAttribPointer(maPositionHandle, 4, GLES20.GL_FLOAT, false, 12, triangleVB);
    GLES20.glEnableVertexAttribArray(maPositionHandle);
    GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 0, 4);

}

Everything else same from tutorial.

private final String vertexShaderCode = 
    "attribute vec4 vPosition; \n" +
    "void main(){              \n" +
    " gl_Position = vPosition; \n" +
    "}                         \n";

private final String fragmentShaderCode = 
    "precision mediump float;  \n" +
    "void main(){              \n" +
    " gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n" +
    "}                         \n";

private int mProgram;
private int maPositionHandle;

private int loadShader(int type, String shaderCode){

    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type); 

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    return shader;
}

public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    initShapes();

    // Set the background frame color
    GLES20.glClearColor(0f, 0f, 0f, 1.0f);

    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
    GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram);                  // creates OpenGL program executables

    // get handle to the vertex shader's vPosition member
    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
}

Thank you!

  • 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-02T04:40:04+00:00Added an answer on June 2, 2026 at 4:40 am

    As I posted in the comments above:

    it seems like you might want to change the size parameter from the glVertexAttribPointer call from 4 to 3, since you aren’t using a 4d vector (XYZW).

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably very simple, but I simply cannot find the answer myself :(
I searched a bit, but couldn't find an answer to this (probably very simple)
This is probably very simple and i am suffering from firday afternoon syndrome but
I know this is probably a very simple question but how would I do
I'm new to Xcode. This is probably a very simple question to you, but
This is probably very simple but I couldn't find anything about in the usual
The solution to this is probably very simple, but I'm not sure what I'm
This is probably a simple one but I'm very much a regex novice. I'm
This is probably very simple but it's really confusing me. When I implement the
So this one is probably very simple, but I'm having a bit of trouble

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.