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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:13:33+00:00 2026-06-01T19:13:33+00:00

I’m trying to use gluProject with my game. I’ve found a way to use

  • 0

I’m trying to use gluProject with my game. I’ve found a way to use it but I don’t know how to deal with rotation.

Here is the code I made:

public static Vector2 getScreenCoordinates(float x, float y, float z, int height, int width)
{
    FloatBuffer screen_coords = GLAllocation.createDirectFloatBuffer(4);
    IntBuffer viewport = GLAllocation.createDirectIntBuffer(16);
    FloatBuffer modelview = GLAllocation.createDirectFloatBuffer(16);
    FloatBuffer projection = GLAllocation.createDirectFloatBuffer(16);

    GL11.glGetFloat(2982, modelview);
    GL11.glGetFloat(2983, projection);
    GL11.glGetInteger(2978, viewport);

    boolean result = GLU.gluProject(x, y, z, modelview, projection, viewport, screen_coords);
    if (result)
    {
        float screen_x = screen_coords.get(0);
        float screen_y = screen_coords.get(1);
        float scrren_z = screen_coords.get(2);

        screen_y -= height / 2;
        screen_y = -screen_y;
        screen_y += height / 2;

        return new Vector2(screen_x, screen_y);
    }
    else
    {
         System.out.printf("Failed to convert 3D coords to 2D screen coords");
         return null;
    }
}

So x, y and z are coords in my 3D map. height and width are my window size.

How can I modify it to deal with rotation (yaw and pitch)?

Thanks.


Here is my new code:

public Vector2 worldToScreen(double x, double y, double z, int yaw, int pitch)
{   
    // Initialize the result buffer
    FloatBuffer screen_coords = GLAllocation.createDirectFloatBuffer(4);

    // Init the OpenGL buffers
    IntBuffer viewport = GLAllocation.createDirectIntBuffer(16);
    FloatBuffer modelview = GLAllocation.createDirectFloatBuffer(16);
    FloatBuffer projection = GLAllocation.createDirectFloatBuffer(16);

    // Add the rotation
    GL11.glRotatef(yaw, 0, 0, 1);
    GL11.glRotatef(pitch, 1, 0, 0);

    // Get the OpenGL data
    GL11.glGetFloat(2982, modelview);
    GL11.glGetFloat(2983, projection);
    GL11.glGetInteger(2978, viewport);

    // Remove the rotation
    GL11.glRotatef(-yaw, 0, 0, 1);
    GL11.glRotatef(-pitch, 1, 0, 0);

    // Calculate the screen position
    boolean result = GLU.gluProject(x, y, z, modelview, projection, viewport, screen_coords);

    if (result)
    {
        if ( (screen_coords.get(0) < -1.0f) || (screen_coords.get(0) > 1.0f) || (screen_coords.get(1) < -1.0f) || (screen_coords.get(1) > 1.0f) )
            return null;

        int window_half_width = getWidth() / 2;
        int window_half_height = getHeight() / 2;

        int screen_x = window_half_width + (window_half_width * -screen_coords.get(0));
        int screen_y = window_half_height + (window_half_height * screen_coords.get(1));

        System.out.printf("(Full Screen / No bounds) [" +x+ ", " +y+ ", " +z+ "] gives [" +screen_coords.get(0)+ ", " +screen_coords.get(1)+ "]");
        System.out.printf("(Every Time) [" +x+ ", " +y+ ", " +z+ "] gives [" +screen_x+ ", " +screen_y+ "]");
        return new Vector2(screen_x, screen_y);
    }
    else
    {
        System.out.printf("Failed to convert 3D coords to 2D screen coords");
        return null;
    }
}

Is that correct?

  • 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-01T19:13:34+00:00Added an answer on June 1, 2026 at 7:13 pm

    Yaw is a rotation about the y axis (pointing upwards), and pitch is a rotation about the x axis.

    GL11.glRotatef(pitch, 1, 0, 0);
    GL11.glRotatef(yaw, 0, 1, 0);
    

    It may also be faster to push/pop the the projection matrix, and you should be in the correct matrix mode. (Both your model-view matrix and your projection matrix have to be correct).

    GL11.glPushMatrix();
    // Camera rotations
    GL11.glPopMatrix();
    

    But it’s questionable as to why you need to undo the camera tranformations. You should probably just apply them once per frame or viewport. Render your scene with that view, then get the projected coordinate and then change to your 2D rendering mode.

    Update

    I’d prefer it if you used roll, pitch and yaw of the camera to mean what they actually mean in OpenGL’s coordinate system – with respect to the camera. The point is, as long as you have the camera model-view and projection matrices setup correctly, you can project the point. You must already be setting up the camera somewhere. You will be able to see if this is correct, because you can SEE the results. Apply that transformation and you will know it’s correct.

    Is this correct? It might be? You have to apply all of the transformations. The exact same transformations that give you the camera view you see in game. This includes all translations and rotations. You will know if you are right, because you should already be applying these transformations in-game.

    However, What you need to think about is what is the camera projection matrix before hand? If you don’t know, call glLoadIdentity() to clear it, and then call your camera transformation logic. You need an accurate model-view and projection matrix – the exact same camera setup you are using in-game. If your model-view or projection matrices are in the wrong state, it just won’t work.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
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
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.