LWJGL Forum

Programming => OpenGL => Topic started by: technik3k on September 24, 2015, 01:17:43

Title: Perspective view
Post by: technik3k on September 24, 2015, 01:17:43
can someone help me use Perspective projection.
Title: Re: Perspective view
Post by: Kai on September 24, 2015, 06:55:41
sure.
Title: Re: Perspective view
Post by: technik3k on September 24, 2015, 20:37:49
I use glFrustum but nothing draws on the screen when I do the draw Quads.
And what do you mean sure.
Title: Re: Perspective view
Post by: Kai on September 24, 2015, 20:41:10
> And what do you mean sure.
Sure, as in: "sure can we help you with it, if only you posed a more precise question so we can see where your problem actually is, instead of asking for general 'help using Perspective projection'" ;)
Imagine me having a very specific problem with Java programming and me visiting your Java programming forum and all I write is:
"can someone help me with Java objects."

But anyway: you still haven't given any sufficient information to actually help you.
Title: Re: Perspective view
Post by: technik3k on September 24, 2015, 22:15:53
Okay, basicly when I do this

                glfwMakeContextCurrent(windowID);

GLContext.createFromCurrent();

glMatrixMode(GL_PROJECTION);

        glOrtho(0, X_FR, 0 , Y_FR, -100, 100);


Then this :

                glBindTexture(GL_TEXTURE_2D, TextureID);
glBegin(GL_QUADS);
{
glTexCoord2f(0,1); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0,0); glVertex3f(1.0f, 100.0f, 1.0f);
glTexCoord2f(1,0); glVertex3f(100.0f, 100.0f, 1.0f);
glTexCoord2f(1,1); glVertex3f(100.0f, 1.0f, 1.0f);
}
glEnd();


It works. In the first one when I replace: glOrtho(0, X_FR, 0 , Y_FR, -100, 100); With this: glFrustum(-X_FR, X_FR, -Y_FR, Y_FR, -100, 100); It doesn't work.
By the way X_FR is the window width, and Y_FR is the window height

Note: I didn't include all the parts of the code. If you need more code just tell me. ;)
Title: Re: Perspective view
Post by: Kai on September 24, 2015, 22:22:30
Now we're getting somewhere. ;)
Please carefully read the documentation of glFrustum (https://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml).
Your call is generating a GL_INVALID_VALUE due to `nearVal` being negative.
glFrustum works fundamentally different from glOrtho.
You might want to consider using a math library, such as JOML (https://github.com/JOML-CI/JOML), that allows you to use GLU-like functions, such as gluPerspective (https://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml), which is by far easier than raw glFrustum.
See JOML's Matrix4f.perspective(...) (https://github.com/JOML-CI/JOML/blob/master/src/org/joml/Matrix4f.java#L5264)
Title: Re: Perspective view
Post by: technik3k on September 28, 2015, 02:01:25
I read everything and other threads. I try to fix my program but it didn't work. I don't realy know whats wrong. Can you post an example of mabye rendering a 3D cube or something. ::)
Title: Re: Perspective view
Post by: Kai on September 28, 2015, 08:06:35
Sure. Choose any of the following demos...:
- with simple OpenGL 1.1 immediate mode rendering: LwjglDemo.java (https://github.com/JOML-CI/joml-lwjgl3-demos/blob/master/src/org/joml/lwjgl/LwjglDemo.java)
- with immediate mode rendering and OpenGL 2.0 shaders: ShaderExample.java (https://github.com/JOML-CI/joml-lwjgl3-demos/blob/master/src/org/joml/lwjgl/ShaderExample.java)
- with OpenGL 1.5 vertex buffer objects and no shaders: VboDemo.java (https://github.com/JOML-CI/joml-lwjgl3-demos/blob/master/src/org/joml/lwjgl/VboDemo.java)
- and raytracing with OpenGL 4.3 compute shaders: raytracing/* (https://github.com/LWJGL/lwjgl3-demos/tree/master/src/org/lwjgl/demo/opengl/raytracing) (and a few of those, with "Hybrid" in the name, also with VAO/VBO and vertex/fragment shaders)

All those draw pretty cubes. :)
Title: Re: Perspective view
Post by: technik3k on September 28, 2015, 23:16:49
Wait... So where do they initialize the view in the LwjglDemo.java :-\
Title: Re: Perspective view
Post by: Kai on September 29, 2015, 07:08:41
What do you mean with "initialize" and with "view?"
If you mean the (model-)view matrix, which transforms from model-space in which your vertices are defined to view/camera/eye-space, then this is effectively done between lines 156-163.
If you mean the projection matrix, then this is between lines 147-151.
Even though the JOML library is used there, it resembles code that uses the GLU native library and its gluPerspective and gluLookAt methods.
The rotateY is then equivalent to glRotatef.
So, the matrices are being calculated via those JOML methods and are then "uploaded" to OpenGL via glLoadMatrixf.
That's probably what you mean. In the end, there's not more to "initializing a view" in OpenGL than setting some matrices, you know. :)
Title: Re: Perspective view
Post by: technik3k on October 11, 2015, 22:17:24
I got JOML
It's giveing me Errors with

public static Matrix4f projMatrix = new Matrix4f();
public static Matrix4f viewMatrix = new Matrix4f();

[Matrix4f cannot be resolved to a type]

Also in...

        projMatrix.setPerspective((float) Math.atan((screenHeight * height / screenHeightPx) / distanceToScreen),
                                  (float)width/height, 0.01f, 100.0f)
                   .get(fb);

How do you get screenHeight,screenHeightPx,and DistanceToScreen
Title: Re: Perspective view
Post by: Kai on October 12, 2015, 08:16:56
Look at the examples in JOML's demo repository, as said above.
Title: Re: Perspective view
Post by: technik3k on November 01, 2015, 20:10:15
Sorry I was busy doing other things  ;D

So I tried LwjglDemo.java (Downloaded all files) but it had errors.
Title: Re: Perspective view
Post by: technik3k on November 03, 2015, 23:30:08
Any way I got JOML to work sort of. I put in some numbers in:
        projMatrix.setPerspective((float) Math.atan((screenHeight * height / screenHeightPx) / distanceToScreen),
                                  (float)1000/800, 0.01f, 100.0f)
                   .get(fb);
        glMatrixMode(GL_PROJECTION);
        glLoadMatrixf(fb);
       
        viewMatrix.setLookAt(0.0f, 2.0f, 5.0f,
                             0.0f, 0.0f, 0.0f,
                             0.0f, 1.0f, 0.0f)
                  .rotateY(angle * (float) Math.toRadians(90))       
                  .get(fb);

and it worked. Then when I used the numbers like in LWJGLDemo.java it didn't work.  :-\