Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (731)
Games in Android Showcase (217)
games submitted by our members
Games in WIP (798)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Compute view/proj matrices for head tracking rendering  (Read 369 times)
0 Members and 1 Guest are viewing this topic.
Offline KaiHH

JGO Kernel


Medals: 416



« Posted 2017-06-13 21:09:16 »

Ever wondered how you can compute a view and projection matrix given the eye position and any arbitrarily positioned and oriented near plane rectangle, such as what Johnny Lee did with his WiiRemote virtual reality hack using a simple monitor and the WiiRemote? Or do you own a TrackIR device and don't know what to do with it? Smiley
It's pretty simple. Here is how:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
/**
 * Create a view and projection matrix from a given <code>eye</code> position, a given bottom left corner position <code>p</code> of the near plane rectangle
 * and the extents of the near plane rectangle along its local <code>x</code> and <code>y</code> axes, and store the resulting matrices
 * in <code>projDest</code> and <code>viewDest</code>.
 * <p>
 * This method creates a view and perspective projection matrix assuming that there is a pinhole camera at position <code>eye</code>
 * projecting the scene onto the near plane defined by the rectangle.
 * <p>
 * All positions and lengths are in the same (world) units.
 *
 * @param eye
 *          the position of the camera
 * @param p
 *          the bottom left corner of the near plane rectangle (will map to the bottom left corner in window coordinates)
 * @param x
 *          the direction and length of the local "bottom/top" X axis/side of the near plane rectangle
 * @param y
 *          the direction and length of the local "left/right" Y axis/side of the near plane rectangle
 * @param nearFarDist
 *          the distance between the far and near plane (the near plane will be calculated by this method)
 * @param projDest
 *          will hold the resulting projection matrix
 * @param viewDest
 *          will hold the resulting view matrix
 */

public static void projViewFromRectangle(Vector3f eye, Vector3f p, Vector3f x, Vector3f y, float nearFarDist, Matrix4f projDest, Matrix4f viewDest) {
    float zx = y.y * x.z - y.z * x.y, zy = y.z * x.x - y.x * x.z, zz = y.x * x.y - y.y * x.x;
    float zd = zx * (p.x - eye.x) + zy * (p.y - eye.y) + zz * (p.z - eye.z);
    float zs = zd >= 0 ? 1 : -1; zx *= zs; zy *= zs; zz *= zs; zd *= zs;
    viewDest.setLookAt(eye.x, eye.y, eye.z, eye.x + zx, eye.y + zy, eye.z + zz, y.x, y.y, y.z);
    float px = viewDest.m00 * p.x + viewDest.m10 * p.y + viewDest.m20 * p.z + viewDest.m30;
    float py = viewDest.m01 * p.x + viewDest.m11 * p.y + viewDest.m21 * p.z + viewDest.m31;
    float tx = viewDest.m00 * x.x + viewDest.m10 * x.y + viewDest.m20 * x.z;
    float ty = viewDest.m01 * y.x + viewDest.m11 * y.y + viewDest.m21 * y.z;
    float len = (float) Math.sqrt(zx * zx + zy * zy + zz * zz);
    float near = zd / len;
    projDest.setFrustum(px, px + tx, py, py + ty, near, near + nearFarDist);
}


This is JOML code, however setLookAt() and setFrustum() are semantically exactly identical to glLoadIdentity() + gluLookAt() and glLoadIdentity() + glFrustum().
Offline orange451

JGO Kernel


Medals: 291
Projects: 5
Exp: 6 years


Your face. Your ass. What's the difference?


« Reply #1 - Posted 2017-06-14 14:23:35 »

Any sick videos to show it off? Tongue

First Recon. A java made online first person shooter!
Online theagentd

« JGO Bitwise Duke »


Medals: 983
Projects: 4
Exp: 8 years



« Reply #2 - Posted 2017-06-14 14:50:50 »

I may have something to show at some point. =P

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 

 
Archive (336 views)
2017-04-27 17:45:51

buddyBro (535 views)
2017-04-05 03:38:00

CopyableCougar4 (978 views)
2017-03-24 15:39:42

theagentd (1014 views)
2017-03-24 15:32:08

Rule (993 views)
2017-03-19 12:43:22

Rule (973 views)
2017-03-19 12:42:17

Rule (971 views)
2017-03-19 12:36:21

theagentd (1071 views)
2017-03-16 05:07:07

theagentd (990 views)
2017-03-15 22:37:06

theagentd (760 views)
2017-03-15 22:32:18
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!