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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:41:08+00:00 2026-05-23T09:41:08+00:00

I’ve written several Android apps, but this is my first experience with 3D programming.

  • 0

I’ve written several Android apps, but this is my first experience with 3D programming.

I’ve created a room (4 walls, ceiling and floor) with a couple objects inside and am able to move the camera around it as if walking. I’ve textured all surfaces with various images and everything was working as expected.

For context, the room is 14 units wide and 16 units deep (centered at origin), 3 units high (1 above origin and 2 below). There are 2 objects in the middle of the room, a cube and an inverted pyramid on top of it.

Then I went to add a light source to shade the cube and pyramid. I had read through and followed a couple of NeHe’s ports, so I took what I had working in the lesson on lighting and applied it to my new code.

gl.glEnable(GL10.GL_LIGHTING);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, new float[] { 0.1f, 0.1f, 0.1f, 1f }, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, new float[] { 1f, 1f, 1f, 1f }, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, new float[] { -4f, 0.9f, 6f, 1f }, 0);
gl.glEnable(GL10.GL_LIGHT0);

The result is that the cube and pyramid are not shaded. They look the same on sides opposing the light as they do on the sides facing it. When the camera is pointed directly away from the light source the room looks as it did before I added the lighting code. As I rotate the camera to face the light source the entire room (including objects) becomes darker until completely black when the camera is directly facing the source.

What is going on here? I read many articles on lighting and how it works, but I have seen nothing to indicate why this wouldn’t light up all sides of the room, with the cube and pyramid shaded based on the light position. Is there some expected behavior of the light because it is “inside” the room? Am I just missing something easy because I’m new?

  • 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-23T09:41:09+00:00Added an answer on May 23, 2026 at 9:41 am

    Every object in your 3D world has a normal, where it helps OpenGL to determine how much light an object need to reflect. You’ve probably forgot to specify the normals for your surfaces. Without specifying them, OpenGL will light all objects in your world in the same way.

    In order to get a surface’s normal in 3D you need at least three vertices, which means it at least is a triangle.

    Sample stuff:

    In order to calculate a surface’s normal you need two vectors. Since you have three vertices in 3D space that means that these sample points could contain a triangle:

    // Top triangle, three points in 3D space.
    vertices = new float[] {
       -1.0f, 1.0f, -1.0f,
       1.0f, 1.0f, -1.0f,
       0.0f, 1.0f, -1.0f,
    }
    

    Given these three points, you can now define two vectors by the following:

    // Simple vector class, created by you.
    Vector3f vector1 = new Vector3f();
    Vector3f vector2 = new Vector3f();
    
    vector1.x = vertices[0] - vertices[3];
    vector1.y = vertices[1] - vertices[4];
    vector1.z = vertices[2] - vertices[5];
    
    vector2.x = vertices[3] - vertices[6];
    vector2.y = vertices[4] - vertices[7];
    vector2.z = vertices[5] - vertices[8];
    

    Now when you have two vectors, you can finally get the surface’s normal by using the Cross Product. Shortly, the cross product is an operation which results in a new vector containing an angle that is perpendicular to the input vectors. This is the normal that we need.

    To get the cross product in your code you have to write your own method that calculates it. In theory you calculate the cross product given this formula:

    A X B = (A.y * B.z – A.z * B.y, A.z * B.x – A.x * B.z, A.x * B.y – A.y * B.x)

    In code (by using the vectors above):

    public Vector3f crossProduct(Vector3f vector1, Vector3f vector2) {
        Vector3f normalVector = new Vector3f();
    
        // Cross product. The normalVector contains the normal for the
        // surface, which is perpendicular both to vector1 and vector2.
        normalVector.x = vector1.y * vector2.z - vector1.z * vector2.y;
        normalVector.y = vector1.z * vector2.x - vector1.x * vector2.z;
        normalVector.z = vector1.x * vector2.y - vector1.y * vector2.x;
    
        return normalVector;
    }
    

    Before any further comments; you can specify your normals in an array and just put them into OpenGL when needed, but your understanding of this topic will be much better if you dig into it and your code will be much more flexible.

    So now we have a normal which you can loop through, assign the vector values to your normal array (like NeHe’s ports, but dynamically) and set up OpenGL to use GL_NORMAL_ARRAY in order to get OpenGL to reflect the light on the object correctly:

    gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
    
    // I'm assuming you know how to put it into a FloatBuffer.
    gl.glNormalPointer(GL10.GL_FLOAT, 0, mNormalsBuffer);
    
    // Draw your surface...
    

    Another last comment; if you’re using other vertices values (like 5.0f, 10.0f or bigger) you might wanna normalize the vector that returns from the crossProduct() method in order to gain some performance. Otherwise OpenGL must calculate the new vector to get the unit vector and that might be a performance issue.

    Also, your new float[] {-4f, 0.9f, 6f, 1f} for GL_POSITION is not quite correct. When the fourth value is set to 1.0f it means that the light position is 0, 0, 0, no matter what the first three values are. In order to specify a vector for your light position, change the fourth value to 0.0f.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but

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.