Isometric view with gluPerspective?

Started by Droa, April 09, 2012, 07:03:14

Previous topic - Next topic

Droa

Hey forum
i've been busy playing with lwjgl 2d, and found it really fun, however as i progressed with my coding, i figured i should do my project in 3D instead, as i could add so much more dynamic to what i wanted..

now i still wanted to make the view "isometric" as it is a nice perspective for a RTS, i was planning to make, and its more easy to mix 2D and 3D together that way.

now i made a easy grass-tiles plane in a simple rendering model, however i can't seem to get it to show like an isometric view, i know its something to do with i have to add glRotated somewhere, however i got no idea how to add it proberly.. anyone that can show me in my demo code?
or maby a good tutorial or guide to how to use it proberly?

Texture2D
package dk.minsk.lwjgl.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.newdawn.slick.opengl.*;

public class Texture2D {
	private Texture texture;
	
	public Texture2D() {
		
	}
	
	public Texture2D(File file) {
		load(file);
	}
	
	public void load(File file){
		FileInputStream in;
		try {
			in = new FileInputStream(file);
			this.texture = TextureLoader.getTexture("PNG", in);
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void bind(){
		this.texture.bind();
	}
}


Boot
package dk.minsk.dungeonvillage3d;

import java.io.File;
import java.util.Random;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.*;

import dk.minsk.lwjgl.tools.Texture2D;

public class Boot {
	float zoom = -20;
	int last1 = 0;
	int last2 = 0;
	Texture2D t2d_grass, t2d_water;
	
	public static void main(String[] args) {
		Boot b = new Boot();
	}
	
	public Boot() {
		try {
			Display.setDisplayMode(new DisplayMode(1600, 1000));
			Display.create();
		} catch (LWJGLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		t2d_grass = new Texture2D(new File("res/tiles/grass-ngrid.png"));
		t2d_water = new Texture2D(new File("res/tiles/water-ngrid.png"));
		
		initGL();
		while(!Display.isCloseRequested()){
			renderGL();
			Display.update();
			Display.sync(60);
		}
	}
	
	public void initGL(){
		 GL11.glMatrixMode(GL11.GL_PROJECTION);
		 GL11.glLoadIdentity();
		 GLU.gluPerspective(90f, 1600f/1000f, 1f, 2000f);
		 GL11.glMatrixMode(GL11.GL_MODELVIEW);
		 //GL11.glOrtho(0, 1600, 1000, 0, 10, -10);
		 //GL11.glMatrixMode(GL11.GL_MODELVIEW);
		 //GL11.glEnable(GL11.GL_BLEND);
		 //GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		 
	 }
	
	 public void renderGL(){
		 zoom+=0.5;
		 Random r = new Random();
		 GL11.glEnable(GL11.GL_TEXTURE_2D);
		 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		 GL11.glLoadIdentity();
		 GLU.gluLookAt(
				 10+zoom, -40+zoom*2,180, 
				 10+zoom, 40+zoom*2,0, 
				 0, 1, 1);
		 GL11.glPushMatrix();
		 //GL11.glRotated(45, 1, 0, 1);
		 //GL11.glTranslatef(0, 0, 80);
		 GL11.glColor3f(1f, 1f, 1f);
		 t2d_grass.bind();
		 for(int x = 1;x<100;x++)
		 {
			 for(int y = 1;y<100;y++)
			 {
			 int cur1 = 0;
			 int cur2 = 0;
			 GL11.glBegin(GL11.GL_QUADS);
			 	GL11.glTexCoord2f(0,0);
		 		GL11.glVertex3f(x*20, y*20+0,last1);
		 		GL11.glTexCoord2f(1,0);
		 		GL11.glVertex3f(x*20+20,y*20+0,cur1);
		 		GL11.glTexCoord2f(1,1);
		 		GL11.glVertex3f(x*20+20, y*20+20,cur2);
		 		GL11.glTexCoord2f(0,1);
		 		GL11.glVertex3f(x*20+0, y*20+20,last2);
			GL11.glEnd();
			last1 = cur1;
			last2 = cur2;
			 }
		}
		GL11.glDisable(GL11.GL_TEXTURE_2D);	
		GL11.glPopMatrix();
	 }
}

matheus23

Nope, gluPerspective is not, what you want. gluPerspective will give you a ... yeah... perspective view... That means: Far objects are smaller than near Objects. That means: you want to get a normal glOrtho view and rotate, as if you would look from top. But that acctually depends... Do you want to create 2D planes, which will *look* like they would be 2D, but acctually have 3D coordinates? or do you want to give openGL 2D coords at the end, but compute them from a "height" yourself?
My github account and currently active project: https://github.com/matheus23/UniverseEngine

Droa

Quote from: matheus23 on April 09, 2012, 10:36:22
Nope, gluPerspective is not, what you want. gluPerspective will give you a ... yeah... perspective view... That means: Far objects are smaller than near Objects. That means: you want to get a normal glOrtho view and rotate, as if you would look from top. But that acctually depends... Do you want to create 2D planes, which will *look* like they would be 2D, but acctually have 3D coordinates? or do you want to give openGL 2D coords at the end, but compute them from a "height" yourself?

i really want to use 3D environment, so i have to use another view method? what type else does there exists?

matheus23

Okey, you really want 3D environment, but you can also archive that with glOrtho. If you want to look at your environment from the top, you need to do that yourself with glTranslate and glRotate.. And if you really want a 3D-looking environment, you can use gluPerspective, which will give you a perspective projection. The thing I would suggest: Try out both. It works with both.
My github account and currently active project: https://github.com/matheus23/UniverseEngine

Droa

thanks.. i've been trying both.. however i really like the perspective best.. been trying to use the glrotation.. but really mess up the way i can use gluLookat or translate