Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Ray Casting Tutorial  (Read 1226 times)
0 Members and 1 Guest are viewing this topic.
Offline zFollette

Junior Member


Exp: 2 years


I like jokes


« Posted 2014-01-26 14:02:15 »

The method of Ray Casting I will be going over is the UnProject method, it works by getting the coordinates of your mouse in 3D space at the nearest and farthest plane on the Z axis. I think everyone will be glad to know that there is very little math involved for just casting the ray. Lets get started.

For the sake of time, I have created two convenience methods to return FloatBuffers and IntBuffers which will be used in the program.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
private FloatBuffer compileBuffer(float[] floats) {
        FloatBuffer buffer = BufferUtils.createFloatBuffer(floats.length);
        buffer.put(floats);
        buffer.flip();
        return buffer;
    }

private IntBuffer compileBuffer(int[] ints) {
        IntBuffer buffer = BufferUtils.createIntBuffer(ints.length);
        buffer.put(ints);
        buffer.flip();
        return buffer;
    }


Ok, now to the fun part.

First off, we have to make 2 float arrays, I will call them matModelView and matProjView. Make the size of the array 16 (this is the most amount of information that can be returned from an openGL get method).

1  
float[] matModelView = new float[16], matProjView = new float[16];

These float arrays will represent the Model View Matrix and the Projection Matrix.
Now we create an Integer array of size 16 to represent the view port, I will call mine view.
1  
int[] view = new int[16];


At this point, we have to grab our mouse coordinates, if you have your mouse grabbed, your coordinates will just be the middle of the Display.
1  
2  
float mouseX = width / 2;
float mouseY = height / 2;

If your mouse is not grabbed, you will have to get the mouse x and y:
1  
2  
float mouseX = Mouse.getX();
float mouseY = Mouse.getY();

You may have to mess around with inverting the mouseY coordinate, I don't know for sure because I grab my mouse.
Now we can make 2 Vector3f variables to later hold the coordinates for the ray.
1  
2  
Vector3f start = new Vector3f();
Vector3f end = new Vector3f();


Time to start using those convenience methods Smiley

1  
2  
3  
4  
5  
FloatBuffer modelBuffer = compileBuffer(matModelView);
FloatBuffer projBuffer = compileBuffer(matProjView);
FloatBuffer startBuffer = compileBuffer(new float[]{start.x, start.y, start.z, 1});
FloatBuffer endBuffer = compileBuffer(new float[]{end.x, end.y, end.z, 1});
IntBuffer viewBuffer = compileBuffer(view);


This just makes FloatBuffers with the size of the inputted through the parameters. I guess you didn't really have to make those variables in the beggining, I just think it looks neater.

The reason that the start and end buffers have a size 4 float array is because that is the minimum allowed by openGL. The 3rd index (4th number) doesn't really matter and can be anything.

Now we can grab our values from openGL.
1  
2  
3  
glGetFloat(GL_MODELVIEW_MATRIX, modelBuffer);
glGetFloat(GL_PROJECTION_MATRIX, projBuffer);
glGetInteger(GL_VIEWPORT, viewBuffer);

This, very simply, just gets the value of the modelview, projection view, and view port values and stores them into the corresponding objects.

Here is where we actually call the gluUnProjcect method, we will call it twice to get the near and far coordinates.
1  
2  
gluUnProject(mouseX, mouseY, 0.0f, modelBuffer, projBuffer, viewBuffer, startBuffer);
gluUnProject(mouseX, mouseY, 1.0f, modelBuffer, projBuffer, viewBuffer, endBuffer);

As you can see, we input the 2D mouse coords, the requested Z distance, and the 3 values we just grabbed. The last parameter is the object in which to store the output of gluUnProject.

That is basically it, other than assigning the start and end Vectors using the startBuffer and endBuffer objects:
1  
2  
start = new Vector3f(startBuffer.get(0), startBuffer.get(1), startBuffer.get(2));
 end = new Vector3f(endBuffer.get(0), endBuffer.get(1), endBuffer.get(2));


See, very simple. If you did it right, you should get a method that looks like this (except for the mouse coords, if your mouse is not grabbed) :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
float[] matModelView = new float[16], matProjView = new float[16];
        int[] view = new int[16];
        float mouseX = width / 2;
        float mouseY = height / 2;
        Vector3f start = new Vector3f();
        Vector3f end = new Vector3f();

        FloatBuffer modelBuffer = compileBuffer(matModelView);
        FloatBuffer projBuffer = compileBuffer(matProjView);
        FloatBuffer startBuffer = compileBuffer(new float[]{start.x, start.y, start.z, 1});
        FloatBuffer endBuffer = compileBuffer(new float[]{end.x, end.y, end.z, 1});
        IntBuffer viewBuffer = compileBuffer(view);

        glGetFloat(GL_MODELVIEW_MATRIX, modelBuffer);
        glGetFloat(GL_PROJECTION_MATRIX, projBuffer);
        glGetInteger(GL_VIEWPORT, viewBuffer);

        gluUnProject(mouseX, mouseY, 0.0f, modelBuffer, projBuffer, viewBuffer, startBuffer);
        gluUnProject(mouseX, mouseY, 1.0f, modelBuffer, projBuffer, viewBuffer, endBuffer);

        start = new Vector3f(startBuffer.get(0), startBuffer.get(1), startBuffer.get(2));
        end = new Vector3f(endBuffer.get(0), endBuffer.get(1), endBuffer.get(2));


I hoped I could help someone. My next tutorial will be Ray Picking, so actually getting the intersections and what not.

Humor will keep you alive.
Offline SHC
« Reply #1 - Posted 2014-01-26 14:21:44 »

Quote
Now we create an Integer array of size 16 to represent the view port, I will call mine view.

I think Viewport is different from View Matrix. The first one is the rectangle covering the display area and the latter is the matrix which we use to change the view of the camera.

Offline zFollette

Junior Member


Exp: 2 years


I like jokes


« Reply #2 - Posted 2014-01-26 14:33:19 »

Right, good catch. I am calling it view port because that is what the value is called in openGL.

Humor will keep you alive.
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.

pw (37 views)
2014-07-24 01:59:36

Riven (38 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (58 views)
2014-07-18 06:55:21

Zero Volt (49 views)
2014-07-17 23:47:54

danieldean (41 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!