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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:56:53+00:00 2026-06-05T02:56:53+00:00

I am learning OpenGL ES for Android through this website: http://www.learnopengles.com/android-lesson-one-getting-started/ There are a

  • 0

I am learning OpenGL ES for Android through this website: http://www.learnopengles.com/android-lesson-one-getting-started/

There are a couple of things I don’t understand…

What exactly do this code do:

mTriangle1Vertices = ByteBuffer.allocateDirect(triangle1VerticesData.length * mBytesPerFloat)
    .order(ByteOrder.nativeOrder()).asFloatBuffer();
    mTriangle2Vertices = ByteBuffer.allocateDirect(triangle2VerticesData.length * mBytesPerFloat)
    .order(ByteOrder.nativeOrder()).asFloatBuffer();
    mTriangle3Vertices = ByteBuffer.allocateDirect(triangle3VerticesData.length * mBytesPerFloat)
    .order(ByteOrder.nativeOrder()).asFloatBuffer();

    mTriangle1Vertices.put(triangle1VerticesData).position(0);
    mTriangle2Vertices.put(triangle2VerticesData).position(0);
    mTriangle3Vertices.put(triangle3VerticesData).position(0);

What are these “buffer” things and are there always four bytes to a float? I’m not really sure why the code above is needed. The author explained it, but I’m not sure what he means.

Thanks!

  • 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-05T02:56:54+00:00Added an answer on June 5, 2026 at 2:56 am

    Why do we even need to do this at all?

    If we were coding in C, we wouldn’t need the above code — we’d just skip the whole ByteBuffer.allocateDirect stuff and pass a pointer to our array when we call drawArrays or drawElements. However, we can’t do that because Dalvik, the Java virtual machine running inside Android, has a garbage collector, and it might move the data around on us. It might even scrap the memory, erroneously thinking that we don’t need it anymore. The safe way to do it is to use ByteBuffer.allocateDirect to create a block of memory in native memory, copy the data to that, and then OpenGL can use that directly without us having to worry about the virtual machine.

    Each buffer needs to be big enough to hold the data we’re going to copy; when we create the buffer, it wants a size in bytes. Our Java array is made of floats, so to figure out the size in bytes, we pass the array length and multiply it by 4. A floating point value is a 32-bit, and a byte is an 8-bit value, so there are always 4 bytes to a float.

    Hope this helps out. 🙂

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

Sidebar

Related Questions

I'm learning OpenGL from this tutorial: http://openglbook.com/the-book/ . I have problem with shaders. I'm
I'm currently learning OpenGL ES programming on Android (2.1). I started with the obligatory
I've recently started learning OpenGl through the SB fifth edition, unfortunately got stuck right
I am learning OpenGL and just started getting into lighting. I enable lighting and
I just started learning opengl and writing a first person shooter but I'm getting
I've just started learning LWJGL (Java OpenGL), and i'm encountering an enormous amount of
I need to visualize 3D point clouds using C++, I started learning OpenGL but
I have a question - I am learning OpenGL ES 2.0 from this tutorial
I'm looking into learning OpenGL ES for making a little graphics engine in Android
I'm new to opengl. As a learning exercise I started manually building some basic

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.