Perspective view

Started by technik3k, September 24, 2015, 01:17:43

Previous topic - Next topic

technik3k

can someone help me use Perspective projection.


technik3k

I use glFrustum but nothing draws on the screen when I do the draw Quads.
And what do you mean sure.

Kai

> 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.

technik3k

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. ;)

Kai

Now we're getting somewhere. ;)
Please carefully read the documentation of glFrustum.
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, that allows you to use GLU-like functions, such as gluPerspective, which is by far easier than raw glFrustum.
See JOML's Matrix4f.perspective(...)

technik3k

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. ::)

Kai

Sure. Choose any of the following demos...:
- with simple OpenGL 1.1 immediate mode rendering: LwjglDemo.java
- with immediate mode rendering and OpenGL 2.0 shaders: ShaderExample.java
- with OpenGL 1.5 vertex buffer objects and no shaders: VboDemo.java
- and raytracing with OpenGL 4.3 compute shaders: raytracing/* (and a few of those, with "Hybrid" in the name, also with VAO/VBO and vertex/fragment shaders)

All those draw pretty cubes. :)

technik3k

Wait... So where do they initialize the view in the LwjglDemo.java :-\

Kai

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. :)

technik3k

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

Kai

Look at the examples in JOML's demo repository, as said above.

technik3k

Sorry I was busy doing other things  ;D

So I tried LwjglDemo.java (Downloaded all files) but it had errors.

technik3k

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.  :-\