1 Replies - 978 Views - Last Post: 30 May 2014 - 08:22 AM Rate Topic: -----

Poll: Welcome! (1 member(s) have cast votes)

Welcome me to Dream in Code?

  1. Yes (1 votes [100.00%] - View)

    Percentage of vote: 100.00%

Vote Guests cannot vote

#1 Hydroque  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-May 14

Unexpected OpenGL Simple Movement

Posted 28 May 2014 - 09:04 PM

Hello! I am new to DreamInCode!

I will make my first question short and simple...

I was creating a game using OpenGL (and slick for textures).

Using Vector3f, I make coordinates to where my camera is camera'ing (and rotation).

It was working... until I modified something. I only added shaders. When I took the shaders off I think I might of accidentally deleted something. Nonetheless the code looks perfect to me.

On the method "input()" everything runs. No errors are thrown anywhere. I have syso'd the hell outa my code.
It shows no signs of breaks. I can, however, move elements I put into my program.
I am 100% sure that no other class is breaking my game (soon to be).

What you should look out for: I am not really sure about the glLoadIdentity()'s and glMatrixMode(). I am, knowing a good part of, thinking it could be gluViewPort... but who knows!

My problem: Can't move my camera even tho it was working before.

Thanks for your help!

package com.Hydroque.OpenGlTest;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

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.util.vector.Vector3f;

public class Main {

	private Model m;
	private Vector3f location, rotation;
	
	public static void main(String args[]){
		new Main().run();
	}
	
	public void run(){
		try {
			Display.setDisplayMode(new DisplayMode(640, 480));
			Display.setTitle("Model Viewer");
			Display.create();
		} catch(LWJGLException e){
			e.printStackTrace();
			Display.destroy();
			System.exit(1);
		}
		
		glEnable(GL_TEXTURE_2D);
		
		location = new Vector3f(0f, 0f, 0f);
		rotation = new Vector3f(0f, 0f, 0f);
		
		try {
			m = Model.getModel("res/Example.obj", "res/Example.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(30f, (float) (640/480), 0.3f, 100f);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glEnable(GL_DEPTH_TEST);
		
		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT0);
				
		while(!Display.isCloseRequested()){
			input();
			render();
			Display.update();
			Display.sync(60);
		}
		
		Display.destroy();
		System.exit(0);
	}
	
	public void input(){
		boolean up = Keyboard.isKeyDown(Keyboard.KEY_W);
		boolean down = Keyboard.isKeyDown(Keyboard.KEY_S);
		boolean left = Keyboard.isKeyDown(Keyboard.KEY_A);
		boolean right = Keyboard.isKeyDown(Keyboard.KEY_D);
		boolean flyup = Keyboard.isKeyDown(Keyboard.KEY_E);
		boolean flydown = Keyboard.isKeyDown(Keyboard.KEY_Q);
		boolean speedUp = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT);
		boolean slowDown = Keyboard.isKeyDown(Keyboard.KEY_LCONTROL);
		float walkspeed = 0.15f;
		
		float mx = Mouse.getDX();
		float my = Mouse.getDY();
		if(Mouse.isButtonDown(0)){
			mx *= 0.25f;
			my *= 0.25f;
			rotation.y += mx;
			if(rotation.y > 360)
				rotation.y -= 360;
			rotation.x -= my;
			if(rotation.x > 85)
				rotation.x = 85;
			if(rotation.x < -85)
				rotation.x = -85;
			
			if(speedUp && !slowDown)
				walkspeed = 0.25f;
			if(slowDown && !speedUp)
				walkspeed = 0.10f;
		}
		
		if(up && !down){
			float cz = (float) (walkspeed * 2 * Math.cos(Math.toRadians(rotation.y)));
			float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y)));
			location.z += cz;
			location.x -= cx;
		}
		if(down && !up){
			float cz = (float) (walkspeed * 2 * Math.cos(Math.toRadians(rotation.y)));
			float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y)));
			location.z -= cz;
			location.x += cx;
		}
		if(right && !left){
			float cz = (float) (walkspeed * 2 * Math.cos(Math.toRadians(rotation.y+90)));
			float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y+90)));
			location.z += cz;
			location.x -= cx;
		}
		if(left && !right){
			float cz = (float) (walkspeed * 2 * Math.cos(Math.toRadians(rotation.y+90)));
			float cx = (float) (walkspeed * Math.sin(Math.toRadians(rotation.y+90)));
			location.z -= cz;
			location.x += cx;
		}
		if(flyup && !flydown)
			location.y -= walkspeed;
		if(flydown && !flyup)
			location.y += walkspeed;
		
	}
	
	public void render(){
		glPushMatrix();
		FloatBuffer fb = ByteBuffer.allocateDirect(16).asFloatBuffer();
		fb.mark();
		fb.put(new float[]{0.5f, 0.5f, 0.5f, 0f}).reset();
		glLight(GL_LIGHT0, GL_AMBIENT, fb);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glRotatef(rotation.x, 1f, 0f, 0f);
		glRotatef(rotation.y, 0f, 1f, 0f);
		glRotatef(rotation.z, 0f, 0f, 1f);
		glTranslatef(location.x, location.y, location.z);
		m.render();
		glPopMatrix();
	}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Unexpected OpenGL Simple Movement

#2 Hydroque  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-May 14

Re: Unexpected OpenGL Simple Movement

Posted 30 May 2014 - 08:22 AM

I still have no figured it out and it's been a few days.
Every variables seems okay. Maybe I lock something so the camera can't translate?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1