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

  • Home
  • SEARCH
  • 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 7011805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:10:25+00:00 2026-05-27T22:10:25+00:00

I’ve created a scene with a rotating cube in Java/lwjgl. If I enable lighting,

  • 0

I’ve created a scene with a rotating cube in Java/lwjgl.
If I enable lighting, I get a realistic behavior as if the light source were at the camera position, no matter where I place my light source.

Here is my method to initialize openGL:

private void initGL() throws LWJGLException {
    Display.create();
    Display.setFullscreen(true);
    Display.setVSyncEnabled(true);

    GL11.glViewport(0, 0, WIDTH, HEIGHT);
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GLU.gluPerspective(70, 4f / 3f, 1, 10000);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL11.glDepthMask(true);

    GL11.glEnable(GL11.GL_CULL_FACE);

    // ----IMPORTANT PART----
    GL11.glEnable(GL11.GL_LIGHTING);
    GL11.glEnable(GL11.GL_LIGHT0);

    FloatBuffer position = ByteBuffer.allocateDirect(16).asFloatBuffer();
    position.mark();
    position.put(new float[] { -5f, 5f, 10f, 0f }); // even values about 10e3 for the first three parameters aren't changing anything
    position.reset();

    GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, position);

    FloatBuffer ambient = ByteBuffer.allocateDirect(16).asFloatBuffer();
    ambient.mark();
    ambient.put(new float[] { 0.5f, 0.5f, 0.5f, 1f });
    ambient.reset();

    GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, ambient);

    // Textures...
}

With this piece of code is called, when I start rendering:

private void renderGL() {
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

    for (RenderObject ro : renderObjects) {
        ro.render();
    }
}

And this draws the cube. The normals are correct, I think, reversing them would prevent the face to be drawn, at least in my tests.

public void render() {
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texID);
    GL11.glColor3f(1f, 1f, 1f);

    GL11.glPushMatrix();

    GL11.glTranslatef(x, y, z);
    GL11.glRotatef(rotation, 1, 1, 1);
    GL11.glTranslatef(-x, -y, -z);

    GL11.glBegin(GL11.GL_QUADS);

    GL11.glNormal3f(0, 0, -1f);
    GL11.glTexCoord2f(0, 0);
    GL11.glVertex3f(x - dx, y - dy, z - dz);
    GL11.glTexCoord2f(0, 1);
    GL11.glVertex3f(x - dx, y + dy, z - dz);
    GL11.glTexCoord2f(1, 1);
    GL11.glVertex3f(x + dx, y + dy, z - dz);
    GL11.glTexCoord2f(1, 0);
    GL11.glVertex3f(x + dx, y - dy, z - dz);

    // the other five faces

    GL11.glEnd();

    GL11.glPopMatrix();
}

What am I doing wrong with the lighting stuff?

EDIT: Additional information about the error

When I set the last value of the light position array to 0, the whole scene is black. Setting it to any other value, the light comes out of the viewport as described. Ambient light does not change anything, the cube I want to draw stays dark, when the light source is not working. I tried to put the lighting stuff at other positions in the code, e.g. directly after the GLU.gluPerspective(70, 4f / 3f, 1, 10000); line, or into the loop, not changing any of the described effects.

  • 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-27T22:10:26+00:00Added an answer on May 27, 2026 at 10:10 pm

    What am I doing wrong with the lighting stuff?

    The lighting position is multiplied with the modelview matrix. So you must call glLightfv(GL_LIGHT_, GL_POSITION, …); right when your modelview matrix contains the viewing transformation.

    Technically everything you’ve written in that initGL function belongs into the drawing function. OpenGL is not a scene graph you initialize.

    Update:

    Change your code like this:

    public void render() {
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texID);
        GL11.glColor3f(1f, 1f, 1f);
    
        GL11.glPushMatrix();
    
        // this is where you set up your view:
        GL11.glTranslatef(x, y, z);
        GL11.glRotatef(rotation, 1, 1, 1);
        GL11.glTranslatef(-x, -y, -z);
    
        // and now it's time to set the light position:
        GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, position);
    
        GL11.glBegin(GL11.GL_QUADS);
    
        GL11.glNormal3f(0, 0, -1f);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex3f(x - dx, y - dy, z - dz);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(x - dx, y + dy, z - dz);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex3f(x + dx, y + dy, z - dz);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(x + dx, y - dy, z - dz);
    
        // the other five faces
    
        GL11.glEnd();
    
        GL11.glPopMatrix();
    }
    

    Then you should drop initGL completely, move it’s code into the drawing function, turning it into:

    public void render() {
    
        GL11.glViewport(0, 0, WIDTH, HEIGHT);
    
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GLU.gluPerspective(70, 4f / 3f, 1, 10000);
    
        GL11.glMatrixMode(GL11.GL_MODELVIEW);   
        glLoadIdentity();
        GL11.glPushMatrix();
    
        // this is where you set up your view:
        GL11.glTranslatef(x, y, z);
        GL11.glRotatef(rotation, 1, 1, 1);
        GL11.glTranslatef(-x, -y, -z);
    
        // and now it's time to set the light position:
        GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, position);
    
        // we do the rest of the light here as well
        GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, ambient);
    
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glDepthMask(true);
        GL11.glEnable(GL11.GL_CULL_FACE);
    
        // we enable lighting right before rendering
        GL11.glEnable(GL11.GL_LIGHTING);
        GL11.glEnable(GL11.GL_LIGHT0);    
    
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texID);
    
        GL11.glColor3f(1f, 1f, 1f);
    
        GL11.glBegin(GL11.GL_QUADS);
    
        GL11.glNormal3f(0, 0, -1f);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex3f(x - dx, y - dy, z - dz);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex3f(x - dx, y + dy, z - dz);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex3f(x + dx, y + dy, z - dz);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex3f(x + dx, y - dy, z - dz);
    
        // the other five faces
    
        GL11.glEnd();
    
        GL11.glPopMatrix();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.