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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:25:56+00:00 2026-05-29T20:25:56+00:00

This is the code I wrote in order to get 2D drawing to work.

  • 0

This is the code I wrote in order to get 2D drawing to work. The activity and the view and renderer are setup the correct way. I get a completely black screen, but with no polygons on it. Nothing is drawn. This is about to drive me crazy…

public class OpenGLActivity extends Activity {

    private GLSurfaceView surfaceView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN
                );

        this.surfaceView = new OpenGLSurfaceView(this);
        setContentView(this.surfaceView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        surfaceView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        surfaceView.onResume();
    }
}

public class OpenGLSurfaceView extends GLSurfaceView {

    private OpenGLSurfaceRenderer renderer;

    public OpenGLSurfaceView(Context context) {
        super(context);
        this.renderer = new OpenGLSurfaceRenderer();
        setRenderer(renderer);
    }

}

public class OpenGLSurfaceRenderer implements Renderer {

    private FloatBuffer vertexBuffer;
    private float[] vertices = {
            0.0f, 1.0f, 0.0f,
            0.0f, 0.0f, 0.0f,
            1.0f, 1.0f, 0.0f,
            1.0f, 0.0f, 0.0f,
            0.0f, 10.0f, 0.0f,
            0.0f, 0.0f, 0.0f,
            10.0f, 10.0f, 0.0f,
            10.0f, 0.0f, 0.0f
    };

    public OpenGLSurfaceRenderer() {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * (Float.SIZE >> 3));
        vertexBuffer = byteBuffer.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

        gl.glClearColor(0.0f, 0.0f, 0.0f, 1);
        gl.glShadeModel(GL10.GL_FLAT);
        gl.glDisable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_TEXTURE_2D);

        gl.glDisable(GL10.GL_DITHER);
        gl.glDisable(GL10.GL_LIGHTING);

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    }

    public void onDrawFrame(GL10 gl) {

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

        gl.glTranslatef(0.0f, 0.0f, 0.5f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {

        gl.glViewport(0, 0, width, height);

        // Clear the projection matrix
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();

        // Set up orthographic projection mode (2D drawing)
        gl.glOrthof(0, width, height, 0, 0, 1);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

}
  • 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-05-29T20:25:57+00:00Added an answer on May 29, 2026 at 8:25 pm

    I see four problems:

    1. The last parameter to glDrawArrays() should be 8, not 4. It’s the number of vertices you are providing, not the number of triangles in your triangle strip.

    2. Your orthographic projection makes your world units equivalent to pixels. Your first 4 vertexes describe triangles that are 1-pixel wide. Maybe you are getting a single white pixel in the bottom left corner of the screen?

    3. You advance the model projection along the Z-axis every frame. After 2 frames it will be out of the view frustrum.

    4. Most importantly, you do not set the right ordering on your ByteBuffer. Insert this line into your OpenGlRenderer() constructor:

      public OpenGLSurfaceRenderer() {
      ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
      byteBuffer.order(ByteOrder.nativeOrder()); <——
      vertexBuffer = byteBuffer.asFloatBuffer();
      vertexBuffer.put(vertices);
      vertexBuffer.position(0);
      }

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

Sidebar

Related Questions

I wrote this code. The constructor works normally, but in the destructor I get
I wrote some code to get the files in a directory. In order to
Okay, so i have this code i wrote: class Connection { public static StreamWriter
If I wrote this code: typeof(myType).TypeHandle Would it use reflection? How much different from:
Hi guys I wrote this code and i have two errors. Invalid rank specifier:
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I wrote this code in C# to encrypt a string with a key: private
I was trying to understand something with pointers, so I wrote this code: #include
I want to convert function object to function. I wrote this code, but it
I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =

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.