Objects rendered at wrong Position

Started by Stuuupiiid, September 26, 2014, 13:34:15

Previous topic - Next topic

Stuuupiiid

Hello,

first of all I'm new here, and this is my first time writing about java stuff, so don't blame me if I'm writing something wrong.

Now, I'm currently working on a Terrain Renderer. When the camera is at the same height as the terrain is, it looks like picture #1. Everything works as expected.

If I increase the camera's height by 30 it looks like picture #1. Also, everything's fine. But, if I keep the camera at 0 height and increase the terrain's height by 30, it also looks like the picture #2. So instead of having the terrain above me, it's below me. Yes, I could add a simple '-' (minus) in front of the terrain's height, but I want to know what went wrong, before going on. Now, here is some code:

public void setupOpenGL() //calling this before loop
	{
		glEnable(GL_DEPTH_TEST);
		glEnable(GL_ALPHA_TEST);
		glDisable(GL_LIGHTING);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glAlphaFunc(GL_GREATER, 0);
		glClearColor(0.5F, 0.5F, 0.5F, 0F);
	}

	public void render2D(double deltaTime) //1st call in loop
	{
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, this.displayWidth, this.displayHeight, 0, 1, -1);
		glViewport(0, 0, this.displayWidth, this.displayHeight);
		glMatrixMode(GL_MODELVIEW);
	}

	public void render3D(double deltaTime) //2nd call
	{
		if(Cubeheads.instance().world != null && Cubeheads.instance().player != null)
		{
			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			GLU.gluPerspective(45.0f, ((float) this.displayWidth / (float) this.displayHeight), 0.1f, 10000.0f);
			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();
			GL11.glRotatef(Cubeheads.instance().camera.getCameraRotX(), 1F, 0, 0);
			GL11.glRotatef(Cubeheads.instance().camera.getCameraRotY(), 0, 1F, 0);
			GL11.glRotatef(Cubeheads.instance().camera.getCameraRotZ(), 0, 0, 1F);
			GL11.glTranslatef((float) Cubeheads.instance().camera.getCameraPosX(), (float) Cubeheads.instance().camera.getCameraPosY(), (float) Cubeheads.instance().camera.getCameraPosZ());
			this.worldRenderer.renderWorld(Cubeheads.instance().world);
		}
	}


To render the terrain, I use the simple 'glBegin(GL_TRIANGLES);' method YET (I will use VAO/VBO later of course). The Y-Coordinate of every Vertex is 30.





I hope I explained my problem well. I would really appreciate some help.

Thanks for reading.
Yeah, my name is funny :P

quew8

I think you are getting confused about the camera concept in OpenGL. There is no such thing as a camera but we can create the effect of a moving camera by translating the scene in the opposite direction to the camera position.

abcdef

quew8 is right

Your camera is effectively your monitor. You can't move your monitor, you can only move the world that you are drawing. By moving the world down your monitor is effectively looking at something higher up (ie camera goes up), if you move the world to the right your monitor is looking at things to the left more (ie camera goes left)

Stuuupiiid

Thanks for your answers. I already knew that, but Im still getting confused.

The "camera" is always at [0, 0, 0] looking towards -Z. Lets say an object is rendered at [0, 0, -10]: I should be able to see it. Lets move the object to [0, 10, -10]. Now it should be above the camera, or am I wrong again?
Yeah, my name is funny :P

abcdef


Cornix

Depends on your perspective and modelview matrices. You can manipulate them to have it move in any direction if you change one of the values.