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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:03:07+00:00 2026-05-16T23:03:07+00:00

the following text is a bit lenghty as I wanted to make sure that

  • 0

the following text is a bit lenghty as I wanted to make sure that people with similar problems that e.g. google this page can easily follow the exact problem and its solutions easily. But now on to my issue:

I recently started programming OpenGL ES on an Android smartphone and ran into problems understanding how gluPerspective and gluLookAt are used. I think I understand what they do, however I cannot see the triangle I draw. I run the following code in the onDrawFrame-method of my activity. Said activity implements GLSurfaceView.Renderer with the onDrawFrame marked with @Override. My code is:

gl.glViewport(0, 0, screen_width, screen_height);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //set background to black
gl.glClearDepthf(1.0f); 

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

gl.glMatrixMode(GL10.GL_PROJECTION); // Change PROJECTION matrix
gl.glLoadIdentity();

float aspect_ratio = (float) screen_width / screen_height;
GLU.gluPerspective(gl, 67, aspect_ratio, 1, 100);

gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glMatrixMode(GL10.GL_MODELVIEW); // Change MODELVIEW matrix
gl.glLoadIdentity();

GLU.gluLookAt(gl, 0, 0, 20, 0, 0, 0, 0, 1, 0);

// make triangle with vertices (-2,0,0), (2,0,0), and (0,.5,0)
float[] vertices = { -2f, 0, 0, 2f, 0, 0, 0, .5f, 0 };
ByteBuffer buffer = ByteBuffer.allocateDirect(vertices.length * 4);
buffer.order(ByteOrder.nativeOrder());
FloatBuffer vertex_buffer = buffer.asFloatBuffer();
for (int i = 0; i < 9; i++) {
    vertex_buffer.put(vertices[i]);
}

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex_buffer);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_DEPTH_TEST);

The next points are short notes on what I putatively know about camera settings in the above code. PLEASE correct me if I got something wrong in the following explanations, as that might already solve all of my issues!

  1. glMatrixMode(GL10.GL_PROJECTION) switches to changing the camera matrix (for which I load an identity matrix directly afterwards)
  2. GLU.gluPerspective(gl, 67, aspect_ratio, 1, 100) sets a perspective projection with a view frustrum of 67 degree view angle in the y-dimension. Using “aspect_ration=(float)width/height” OpenGL computes the view angle in the x-dimension. Further the “1” is the near clipping plane, while “100” is the value of the far clipping plane in this case.
  3. As far as I know it does not matter, where in the code gluLookAt is called, as long as it is done before the actual drawing (I’m unsure about this though). In this example I change to the MODELVIEW matrix first, load an identity matrix, and then gluLookAt is called.
    3.
  4. “GLU.gluLookAt(gl, 0, 0, 20, 0, 0, 0, 0, 1, 0)” actually consists of three 3-element vectors. The initial one ((0,0,20) in this example) sets the position of the camera in world coordinates (i.e. 20 units on the positive Z-axis, which means it is “sticking out” of the front of the phone’s screen). The second vector determines the point in the world coordinate system the camera looks at (in this example the origin of the coordinate system, i.e. the camera looks along the negative z-axis). The third vector is the up-vector that defines the direction of the y-Axis of the cameras coordinate system (and therefore also the x-axis via cross product with the vector from camera position to the point it looks at).

The triangle drawing code was copied from a tutorial (but I found similar code also in other tutorials, so I assume this is not the problem).

As far as I get it, the camera is now positioned on (0,0,20) and looks towards the origin of the coordinate system. Its drawing frustrum starts at z-value 19 and goes as far as -80 (the camera is at z-axis point 20 and looks along the negative z-axis; its viewing frustrum has near and far plane at 1 and 100 (both looking along the negative z-axis in this case), respectively). Therefore, the triangle should be visible, as its vertices are positioned around the origin on the z-axis=0 plane (i.e. the xy-plane), which is clearly within the viewing frustrum. Further the drawn vertices are arranged in counter-clockwise order; the default in OpenGL.

So after this long introduction into the problem my simple question is: What did I mess up so that this code does not do what I expect it to?

  • 1 1 Answer
  • 1 View
  • 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-16T23:03:08+00:00Added an answer on May 16, 2026 at 11:03 pm

    I found the answer after looking at further tutorials (and then also noticed it in the ones I used before)! Thank all of you for your answers, they were very helpful in diagnosing OpenGL problems in general and will for sure help me in the future!

    The solution to my problem is… that the problem is not glulookat or gluperspective, but in my case the internal position of the FloatBuffer. I did not set the position of variable vertex_buffer to 0 after the for(0 < i < 9) loop. Setting the buffer’s position to 0 can be accomplished with at least two commands:

    • vertex_buffer.rewind()
    • vertex_buffer.position(0)

    To my knowledge they do the same thing. I quickly tested where in the code the position reset belongs and in my example code I could call it after gl.glEnableClientState(GL10.GL_VERTEX_ARRAY), however I had to call it before gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertex_buffer), otherwise the initial problem returned.

    I hope this helps other people with similar issues or maybe is just a short of glulookat and gluperspective 😉

    Thanks again for your suggestions!

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

Sidebar

Related Questions

Ok. I have this little bit of code that is supposed to use text
I have the following bit of text (or some similar variation of it): Recurring
I have the following text contained within a field in my DB: [quote:5a7b87febe=mr smith]This
I have the following code: NSString *text = @http://bit.ly/111 http://bit.ly/222 http://www.www.www; NSRegularExpression *aLinkRegex =
I have the following bit of code that gives me the number of rows
I am following the tutorial http://www.bit-101.com/blog/?p=2115 . In this tutorial I found a project
Given the following bit of text, how would i go about returning just the
OK, this is driving me nuts. I'm trying to screen-scrape the following bit of
Consider the following tables: department deptid (type:INT) deptname (type: TEXT) hours (type:INT) active (type:BIT)
I have the following bit of code that reads data from the an Oracle

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.