Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  3D Camera with modern OpenGL  (Read 1664 times)
0 Members and 1 Guest are viewing this topic.
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Posted 2013-12-23 20:53:26 »

Hey guys.
First of all merry christmas and happy holidays to everyone on the forum! Cheesy
Currently I'm trying to write my own engine on top of LWJGL to understand modern OpenGL better and get some basics down but I'm stuck at the part of writing a 3D camera.
The only decent tutorial I managed to find on the subject is this: http://games.greggman.com/game/webgl-3d-cameras/ (it's for WebGL but it's really easy to port it to LWJGL).
I've tried to make my own camera class based on the tutorial but it produces weird effects (turns models upside down and such) so I obviously mess up somewhere, the only question is where?! Huh

Here are my classes:
Camera.java - http://pastebin.com/LkfMR4NH
TestCamera.java (an instance of the camera class) - http://pastebin.com/gmP84PGQ
In the vertex shader the vertex position get's calculated as follows: projectionMatrix*viewMatrix*vec4(in_Position, 1.0);
, where in_Position is a vec3 of the vertex's local position. Currently I'm not using model matrices but I will implement that later.

If I understand the tutorial correctly to get the forward vector (or z axis vector) I have to subtract the target vector from the camera position vector, then normalise the result.
To get the right vector (or x axis) I have to take the cross product of my forward vector and a unit vector that points upwards on the Y axis.
To get the real up vector (or y axis) I have to take the cross product of the up and the right vector and voilá.
Of course to get the backward, left and down vectors I just have to negate the appropriate vectors.
After I did this all to apply the transformations I can just fill up a 4x4 matrix with the given data and use it as my view matrix.
However, as you can see in my code I do exactly this and it just doesn't seem to work.
Any help would be very much appreciated since I'm trying to solve this problem for quite a few hours now.

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Online Slyth2727
« Reply #1 - Posted 2013-12-23 21:56:58 »

Hey, merry christmas to you too  Smiley

I suggest you check out this tutorial here, made by one our members:

http://www.java-gaming.org/topics/lwjgl-tutorial-series-creating-a-3d-camera/30871/view.html

I learned it using this article. Honestly you probably just messed up your matrices somewhere, and there's no shame in that.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #2 - Posted 2013-12-24 12:28:35 »

Thank you very much, using your link I've managed to get the camera working in just a few minutes. Cheesy
It seems like quite a basic camera, but now that at least I have something to go around with in my scene and observe the models I guess I'll have time to expand it later on. Cool

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PandaMoniumHUN

JGO Coder


Medals: 32
Exp: 3 years


White-bearded OGL wizard


« Reply #3 - Posted 2013-12-25 14:14:29 »

I've added quite a few things to the camera, however I can't seem to figure out how should I move it on the y axis as well (the tutorial you linked moves the camera only on the Z and the X axis).
Of course I'm not talking about increasing the or decreasing the Y value of the camera but when I move forward or backward with the camera I would like my height to increase/decrease depending on my rotation on the X axis. I've tried adding something like this to the move method:

1  
position.y += amount*Math.sin(Math.toRadians(rotation.x+90*direction));

but it obviously doesn't work (I have no idea what I'm doing, damn you trigonometry *sigh*  Emo).
Anyone could help me out on getting this right and hopefully understanding the math behind it? Smiley

Edit: Okay, I've got it working, so far everything seems to be okay. If anyone is interested I did it this way:
1  
2  
if(direction == 1f)
   position.y += amount*Math.sin(Math.toRadians(rotation.x));

This way it only increases the Y value if you're moving on the Z axis (so if you just want to move sideways it won't be modified).
Still pretty much have no idea about the math behind it, I'm pretty much okay with vectors in 2D but when it comes to 3D it just almost always gets too complicated for me.
Probably will pick up a book on algebra and trigonometry. Stare

My Blog | Jumpbutton Studio - INOP Programmer
Can't stress enough: Don't start game development until you haven't got the basics of programming down! Pointing
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!