2

How i make camera in my lwjgl 3D java application? I try use glRotate and glTranslatef, but this move only objects, and i need move scene.

Same i try use

glLoadMatrixf(new float[]{
                50, 50, 50, 50,
                50, 50, 50, 50,
                -75, 50, 50, 50,
                100, 100, 100, 100
}); 

But I don’t quite understand how it works, and i see white screen

What solotion i can use for my task?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Aryesia
  • 152
  • 7
  • Depends on if you use something like GLU. If so, Google glulookat (or an equivalent in what you use) otherwise you could load the view matrix, then translate for -camera position an rotate it. – DJSchaffner Jan 24 '20 at 10:32
  • see [understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) at the end of the answer are examples of exactly that ... the order of transformations determines if you are changing object or camera position/orientation... – Spektre Jan 29 '20 at 09:06

2 Answers2

1

In OpenGL there is no camera. To "move the camera", you have to move the objects in the opposite direction instead. For example, instead of moving the camera forward, you would move the objects backward.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • How i can do this? – Aryesia Jan 24 '20 at 10:48
  • @DmitryTriangle You already know? You have `glRotate` and `glTranslatef`. I wrote "scene" instead of "objects" in this answer by mistake (they mean the same thing, but I don't think they mean the same thing to you) – user253751 Jan 24 '20 at 10:51
1

If you want to make your first view matrix, then start with gluLookAt.
With this handy Legacy OpenGL utility function, you can define a view matrix by a position (eye), a target point (target) and an up vector (up):
(See also Java Code Examples for org.lwjgl.util.glu.GLU.gluLookAt())

GLU.gluLookAt(eye.x, eye.y, eye.z, target.x, target.y, target.z, up.x, up.y, up.z);

The view space is the local system which is defined by the point of view onto the scene. The position of the view, the line of sight and the upwards direction of the view, define a coordinate system relative to the world coordinate system. The objects of a scene have to be drawn in relation to the view coordinate system, to be "seen" from the viewing position. The inverse matrix of the view coordinate system is named the view matrix. This matrix transforms from world coordinates to view coordinates.

Additionally you well need either an Orthographic or Perspective projection matrix.
The former matrix can be defined by glOrtho
(See also Java Code Examples for org.lwjgl.opengl.GL11.glOrtho())

GL11.glOrtho(0, width, height, 0, 1, -1);

and the later by gluPerspective
(See also Java Code Examples for org.lwjgl.util.glu.GLU.gluPerspective())

GLU.gluPerspective(45.0f, wRatio, (float) near, (float) far);

The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport. The projection matrix transforms from view space to the clip space. The coordinates in the clip space are transformed to the normalized device coordinates (NDC) in the range (-1, -1, -1) to (1, 1, 1) by dividing with the w component of the clip coordinates.

In legacy OpenGL there exist different current matrices. The projection matrix should be set to the current GL_PROJECTION matrix and the view matrix to the current GL_MODELVIEW matrix. See glMatrixMode.

e.g.:

float width     = ...; // width of the window
float height    = ...; // height of the window
Vector3f eye    = ...; // camera position
Vector3f target = ...; // camera target
Vector3f up     = ...; // camera up vector

float ratio = 1.0f * width / height;

GL11.glViewport(0, 0, (int) w, (int) h);

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45.0f, ratio , 0.1f, 100.0f);

GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GLU.gluLookAt(eye.x, eye.y, eye.z, target.x, target.y, target.z, up.x, up.y, up.z);

Instead of GLU.gluPerspective, a perspective projection matrix can be set as follows:

float width  = ...;    // width of the window
float height = ...;    // height of the window
float fov_y  = 45.0f;  // filed of view in degrees (y axis)
float n      = 0.1f;   // near plane
float f      = 100.0f; // far plane

float a  = width / height;
float ta = Math.tan(Math.radians(fov_y) / 2.0f);

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
glLoadMatrixf(new float[]{
    1.0f/(ta*a), 0.0f,     0.0f,            0.0f,
    0.0f,        1.0f/ta,  0.0f,            0.0f,
    0.0f,        0.0f,    -(f+n)/(f-n),    -1.0f,  
    0.0f,        0.0f,    -2.0f*f*n/(f-n),  0.0f
}); 

Instead of using a GLU.gluLookAt a view matrix can be set by glTranslate and glRoatate:

float distance = 10.0f; // distance to object
float pitch    = 0.0f;  // pitch in degrees
float yaw      = 0.0f;  // yaw in degrees

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f); 
GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
GL11.glTranslatef(0.0f, 0.0f, -distance);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174