Hello Guest

Terrain with OPENGL

  • 1 Replies
  • 4395 Views
Terrain with OPENGL
« on: February 27, 2017, 15:36:05 »
Hi,

at the Moment im learning OPENGL and after creating different Applikations with (transformed) Geometric Primitives just like Cubes,Quads and Triangles,
im figuring out to get deeper in this 3D thang.

So, i thought about a Terrain in 3D. To do this I created some Triangles, just imagine as a 2D-Field of triangles. After that Creation i thought about rotating this whole field arround the x-axis lets say 160 degrees. In my mind that is absolut logically, but when im trying to Code it i dont get the wished results.

My Problem is only the drawying, cause if i translate the field to the Center of the window and then im rotating it finally i translate it back just as its said in some books, result shows me no depth. I expectect the triangle field to become smaller in the Background and larger in the foreground, but it seems there is no depth in it.

Code: [Select]
private void initGL(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,WIDTH,HEIGHT,0,10,-10);
glMatrixMode(GL_MODELVIEW);

}

public void renderGL(){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPolygonMode(GL11.GL_FRONT_AND_BACK,GL11.GL_LINE);

glPushMatrix();

glTranslatef(WIDTH/2, HEIGHT/2, 0);
glRotated(160, 1.0, 0, 0);
glTranslatef(-WIDTH/2,-HEIGHT/2, 0);

for(int y = 0;y<rows;y++){
glBegin(GL_TRIANGLE_STRIP);
for(int x = 0;x<cols;x++){
vertex(x*scl,y*scl);
vertex(x*scl,(y+1)*scl);
}
glEnd();
}

glPopMatrix();
}

public void vertex(int x,int y){
glVertex2i(x,y);
glVertex2i(x,y);
glVertex2i(x,y);
}

This is my relevant Code i got, but as i said im new to OpenGL and i dont know all the tricks yet.

Tomm9080

*

Kai

Re: Terrain with OPENGL
« Reply #1 on: February 27, 2017, 16:56:55 »
That is because you do not use a _perspective_ projection, but instead an orthogonal one:
Code: [Select]
glOrtho(0,WIDTH,HEIGHT,0,10,-10);

You should do something like this:
Code: [Select]
GLU.gluPerspective(45.0f, (float)WIDTH/HEIGHT, 0.1f, 100.0f);
You can use the class `org.lwjgl.util.glu.GLU` for this if you are using LWJGL 2.x. It comes with the lwjgl_util.jar.

However, if you are using LWJGL 3, then you probably want to use JOML (https://github.com/JOML-CI/JOML) and do something like this:
Code: [Select]
private void initGL(){
  glMatrixMode(GL_PROJECTION);
  Matrix4f projection = new Matrix4f().perspective((float)Math.toRadians(45), (float)WIDTH/HEIGHT, 0.1f, 100.0f);
  glLoadMatrixf(projection.get(BufferUtils.createFloatBuffer(16)));
  glMatrixMode(GL_MODELVIEW);
}

There are better ways to do memory management than using the BufferUtils class in LWJGL. Have a look at: https://github.com/LWJGL/lwjgl3-wiki/wiki/1.3.-Memory-FAQ

In order to understand everything you need to right now about OpenGL, you'll probably gonna have to do quite some reading. First, I recommend these:
- http://www.songho.ca/opengl/gl_transform.html
- http://www.songho.ca/opengl/gl_projectionmatrix.html
- (generally all articles on songho.ca are great)
- http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/

I recommend getting familiar with these transformations and matrices stuff, in order to get away from the OpenGL fixed-function pipeline, and if not that, then at least for a better understanding of what glTranslatef(), glRotatef(), etc. are actually doing in the background.