0
\$\begingroup\$

Recently, I have been making a 3D game (and asking alot of questions about it), and yet again, I've run into another problem.

I'm trying to rotate a cube across the "camera" origin, which is easily set by doing this in the gameloop:

//Wrapping a push/pop matrix around the main push/pops from the object for rotating around origin.

            GL11.glPushMatrix();
            
            GL11.glRotatef(ROTDX += Mouse.getDX(), 0, 1, 0);
            GL11.glRotatef(ROTDY -= Mouse.getDY(), 1, 0, 0);
            
            GL11.glTranslatef(0, 0, -50.0f);

            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);  
            
            GL11.glClearColor(1.0f, 1.0f, 0.2f, 1.0f);
             
            shader.start();
            
            a+=0.001f;
            
            if (a > 360) {
                a = 0;
            }
            
            int uniform1 = GL20.glGetUniformLocation(shader.getProgID(), "u_time");
            
            GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "u_time"), a);
            
            int res = GL20.glGetUniformLocation(shader.getProgID(), "u_resolution");
            
            GL20.glUniform2f(GL20.glGetUniformLocation(shader.getProgID(), "u_resolution"), 640.0f, 480.0f);
            
            GL11.glPushMatrix();
            
            GL11.glRotatef(DX += Mouse.getDX(), 0, 1, 0);
            GL11.glRotatef(DY -= Mouse.getDY(), 1, 0, 0);

            cube.drawCube();
            
            GL11.glPopMatrix();
            
            GL11.glPopMatrix();

But, I cannot seem to update the "center" of the screen or the origin of the "camera".

Is this even possible without a custom Camera class?

Here are two images to show you what I mean:

Rotating on "viewport" or "camera" origin:

Rotating on camera origin

Still rotating on center of the original "viewport" or "camera" origin:

Still rotating on origin

And this is what I want to happen:

Rotate on "origin"

Rot1

Updates and rotates on updated "origin"

Rot2

Here is my whole main loop class:

package me.pale.main;

import java.io.File;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.util.glu.GLU;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

import me.pale.input.Input;
import me.pale.model.Model;
import me.pale.model.RGB;
import me.pale.shader.RegShader;

public class MainLoop extends RGB{
    
    boolean forward = false;
    boolean backward = false;
    boolean left = false;
    boolean right = false;
    
    boolean mouseVisible = false;
    
    float posx = 0;
    float posx2 = 0;
    float posy = 0;
    float posz = 0;
    float posz2 = 0;
    int b = 640;
    int c = 480;
    
    float a = 0;
    
    String vertex = "res/vertex.txt";
    String frag = "res/fragment.txt";
    
    File vertexShad = new File(this.getClass().getResource("/vertex.glsl").toString());
    File fragShad = new File(this.getClass().getResource("/fragment.glsl").toString());
    
    float rot = 0;
    float DX = 0;
    float DY = 0;
    
    float ROTDY = 0;
    float ROTDX = 0;
    
    Input i = new Input();
    
    RegShader shader;
    
    Model cube = new Model();
    
    Texture tex;
    
    public void init() throws LWJGLException, Exception {
        

        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.create();
        
        shader = new RegShader();
        
        // tex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/test.png"));
        
        Display.sync(60);
        
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        // GL11.glLoadIdentity();
        // GL11.glOrtho(-640/2, 640/2, -480/2, 480/2, -200, 500);
        
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        
        GL11.glClearColor(0, 0, 0, 0);
        
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glEnable(GL11.GL_LIGHTING);
        
        GL11.glEnable(GL11.GL_NORMALIZE);
        //GL11.glEnable(GL11.GL_BLEND);
        //GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        
        GLU.gluPerspective(90.0f, 1.333f, 1.0f, 650.0f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glDepthMask(true);
        GL11.glDepthFunc(GL11.GL_LESS);
        GL11.glLoadIdentity();
        
        Mouse.setGrabbed(true);
        
        // tex.bind();
        
        while (!Display.isCloseRequested()) {
            
            // GL11.glViewport(0, 0, b -= 1, c - 1);
            
            input();
            
            GL11.glPushMatrix();
            
            GL11.glRotatef(ROTDX += Mouse.getDX(), 0, 1, 0);
            GL11.glRotatef(ROTDY -= Mouse.getDY(), 1, 0, 0);
            
            GL11.glTranslatef(0, 0, -50.0f);
            // GL11.glPushMatrix();
            
            // GL11.glRotatef(ROTDX += Mouse.getDX(), 0, 1, 0);
            // GL11.glRotatef(ROTDY -= Mouse.getDY(), 1, 0, 0);
            
            // GL11.glTranslatef(0.0f, 0.0f, -50.0f);
            
            // GL11.glPopMatrix();
            
            // Clear the screen and depth buffer
            // GL11.glEnable(GL11.GL_DEPTH_TEST);
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);  
            
            GL11.glClearColor(1.0f, 1.0f, 0.2f, 1.0f);
             
            // set the color of the quad (R,G,B,A)
            // GL11.glColor3f(toRatio(12), toRatio(202), toRatio(222));
            
            shader.start();
            
            a+=0.001f;
            
            if (a > 360) {
                a = 0;
            }
            
            int uniform1 = GL20.glGetUniformLocation(shader.getProgID(), "u_time");
            
            GL20.glUniform1f(GL20.glGetUniformLocation(shader.getProgID(), "u_time"), a);
            
            int res = GL20.glGetUniformLocation(shader.getProgID(), "u_resolution");
            
            GL20.glUniform2f(GL20.glGetUniformLocation(shader.getProgID(), "u_resolution"), 640.0f, 480.0f);
            
            GL11.glPushMatrix();
            
            GL11.glRotatef(DX += Mouse.getDX(), 0, 1, 0);
            GL11.glRotatef(DY -= Mouse.getDY(), 1, 0, 0);
            
            // GL11.glRotatef(rot += 0.05f, 1, 1, 1);
            // GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex.getTextureID());
           
            cube.drawCube();
            
            // GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
            
            GL11.glPopMatrix();
            
            GL11.glPopMatrix();
            
            shader.halt();
            
            Display.update();
            
        }
        
        shader.wash();
        tex.release();
        Display.destroy();
        
    }
    
    public void input() {
        
        if (forward) {
            
            posz = 0.1f;
            GL11.glTranslatef(posx, posy, posz);
            
        }
        
        if (backward) {
            
            posz = -0.1f;
            GL11.glTranslatef(posx, posy, posz);
            
        }
        
        if (left) {
            
            posx = 0.1f;
            GL11.glTranslatef(posx, posy, posz);
            
        }
        
        if (right) {
            
            posx = -0.1f;
            GL11.glTranslatef(posx, posy, posz);
            
        }
        
        if (!left && !right) {
            
            posx = 0;
            
        }
        
        if (!forward && !backward) {
            
            posz = 0;
            
        }
        
        if (!Keyboard.getEventKeyState()) { 
            
            forward = false;
            backward = false;
            left = false;
            right = false;
            
        }
            
        if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
                
            left = true;
                
        }
            
        if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
                
            right = true;
                
        }
            
        if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
                
            forward = true;
                
        }
            
        if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
                
            backward = true;
                
        }
        
        if (i.isLMBDown()) {
            
            System.out.println("MOUSEX: " + i.getMouseX() + ", MOUSEY: " + i.getMouseY());
            
        }
        
        while (Keyboard.next()) {
            
            if (Keyboard.getEventKey() == Keyboard.KEY_M) {
                
                if (Keyboard.getEventKeyState()) {
                    
                    if (!mouseVisible) {
            
                        Mouse.setGrabbed(false);
                        mouseVisible = true;
                        
                    } else {
                    
                        Mouse.setGrabbed(true);
                        mouseVisible = false;
                        
                    }
                    
                }
                            
            }
            
            if (Keyboard.getEventKey() == Keyboard.KEY_B) {
                
                if (Keyboard.getEventKeyState()) {
                    
                    System.out.println("Key B Pressed.");
                    
                }
                
            }
            
            if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
                
                Display.destroy();
                System.exit(0);
                
            }
            
        }
        
    }
    
    public static void main(String[] args) throws Exception {
        
        MainLoop ml = new MainLoop();
        ml.init();
        
    }

}

Any help much appreciated!

\$\endgroup\$
1
  • \$\begingroup\$ How do I do this? \$\endgroup\$ – Pale_Gray May 17 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.