Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  [Solved] [Help] mouse 2D coords to 3D coordinates, with a twist  (Read 1339 times)
0 Members and 1 Guest are viewing this topic.
Offline Graftwijgje

Senior Newbie


Medals: 1



« Posted 2013-05-20 22:48:01 »

I've rendered a textured 3D cube on screen with glDrawElements. Pretty much built upon the OpenGL tuts on the lwjgl wiki.

Now I want to add cubes to the sides of an existing cube by clicking on the sides, I figured I'd at least need some 3D coordinates for that. Now there's some great posts to find on the internet about using gluUnProject, but they seem to use matrices for immediate rendering, and not the matrices I've been using. (Stuff about Uniform, shader programs, location ints, etc. I only grasp half of it, but keep learning bit by bit)

Getting the 3D position
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  
public static FloatBuffer getOGLPos(int x, int y){
     
      IntBuffer viewport = BufferUtils.createIntBuffer(16);
      FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
      FloatBuffer projection = BufferUtils.createFloatBuffer(16);
      FloatBuffer winZ = BufferUtils.createFloatBuffer(3);
      float winX, winY;
      FloatBuffer position = BufferUtils.createFloatBuffer(16);
     
      /* This doesn't seem to be the right thing to do, since I use newer openGL and all...
      GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
      GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
      GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
      */

     

      GL20.glGetUniform(renderer.pId, renderer.modelMatrixLocation, modelview); // This is what I came up with after some google-ing am I doing this right?
     GL20.glGetUniform(renderer.pId, renderer.projectionMatrixLocation, projection);
      GL20.glGetUniform(renderer.pId, renderer.viewMatrixLocation, viewport);
     
      winX = (float)x;
      winY = (float)viewport.get(3) - (float)y;
     
      GL11.glReadPixels(x, (int)winY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, winZ);
     
      if (winZ.get(0) == 1) {
           return null;
       }
     
      GLU.gluUnProject(winX, winY, winZ.get(), modelview, projection, viewport, position);
     
      return position;
   }


Printing the info in the console
1  
2  
3  
4  
5  
6  
while (Mouse.next()) {
         if (Mouse.isButtonDown(1)) {
            FloatBuffer posBuffer = Utils.getOGLPos(Mouse.getX(), Mouse.getY());
            System.out.println("Clicked @ x: " + posBuffer.get(0) + " y: " + posBuffer.get(1) + " z: " + posBuffer.get(2));
         }
      }


As output I get NaN's for the x, y and z. I'm doing something wrong, somewhere. Do I really need to use glGetUniform? Because using glGetFloat seemed to f**k with my matrices (I get the 3D position in the console, but an openGL error right after that). I figured that was because I was using GL_MODELVIEW_MATRIX, something I only used in openGL examples/tuts using immediate mode.

Please note, I've only used Java (and serious programming altogether) since October and OpenGL since a few weeks. I'm rushing head over heels into game programming to get a portfolio set up before/during the summer break. I'll make loads of mistakes, sorry. But at least I'm learning, and fast too.

Long story short: Look at code, facepalm, cuss, give link/corrected code/stuff I need to look up on.

Offline quew8

JGO Coder


Medals: 23



« Reply #1 - Posted 2013-05-21 10:43:55 »

For getting the matrices, you are doing the right thing (provided those are the names of your uniforms matrices) But the viewport isn't really a matrix, and you don't have a custom implementation for that in your shader, it's a fixed function thing. So get rid of glGetUniform for the viewport and replace it with the commented getGetInteger.

However, there isn't much point to a tutorial if you come away from it not understanding the material. Reread it and if you still don't understand (which I doubt - it's a good tutorial) then find another one.
Offline Graftwijgje

Senior Newbie


Medals: 1



« Reply #2 - Posted 2013-05-21 11:12:04 »

Thanks! It seems to work now, apart from that I keep getting the same z value... I'll wrestle with it some more!

About the tutorials, it isn't so much that I'm not learning from them, it's just that understanding all the new stuff each tutorial introduces takes some time to grasp. I.e. I read about something new in tut 2, but only get to fully understanding it in tutorial 4, after using it a couple times.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline quew8

JGO Coder


Medals: 23



« Reply #3 - Posted 2013-05-21 11:29:06 »

I know exactly what you mean - I'm the same. But some people read the words but don't take them in then just copy out the code and feel they understand because it works.
Offline Graftwijgje

Senior Newbie


Medals: 1



« Reply #4 - Posted 2013-05-26 11:40:59 »

I got it to work!
When initialising OpenGL you need to be sure to add this line:
1  
GL11.glEnable(GL11.GL_DEPTH_TEST)

It allows for testing a pixel's depth value from the depth buffer with glReadPixels()

And for heaven's sake, don't forget to clear your depth buffer! (you do this just before rendering)
1  
GL11.glClear(GL11.COLOR_BUFFER_BIT | GL11.DEPTH_BUFFER_BIT)
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.

xsi3rr4x (57 views)
2014-04-15 18:08:23

BurntPizza (55 views)
2014-04-15 03:46:01

UprightPath (68 views)
2014-04-14 17:39:50

UprightPath (51 views)
2014-04-14 17:35:47

Porlus (68 views)
2014-04-14 15:48:38

tom_mai78101 (92 views)
2014-04-10 04:04:31

BurntPizza (153 views)
2014-04-08 23:06:04

tom_mai78101 (249 views)
2014-04-05 13:34:39

trollwarrior1 (205 views)
2014-04-04 12:06:45

CJLetsGame (212 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!