Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (710)
Games in Android Showcase (212)
games submitted by our members
Games in WIP (785)
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 2712 times)
0 Members and 1 Guest are viewing this topic.
Offline Graftwijgje

Senior Newbie

Medals: 1

« Posted 2013-05-20 20: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
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
while ( {
         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 Knight

Medals: 53

« Reply #1 - Posted 2013-05-21 08: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 09: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 Knight

Medals: 53

« Reply #3 - Posted 2013-05-21 09: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 09:40:59 »

I got it to work!
When initialising OpenGL you need to be sure to add this line:

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)
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

numerical (133 views)
2017-02-21 07:32:16

numerical (132 views)
2017-02-21 07:31:46

theagentd (240 views)
2017-02-18 13:42:33

theagentd (241 views)
2017-02-18 13:35:16

h.pernpeintner (1403 views)
2017-01-24 22:39:11

h.pernpeintner (1391 views)
2017-01-24 22:38:32

Galdo (1953 views)
2017-01-12 13:44:09

Archive (2019 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2556 views)
2016-12-16 03:58:39

0AndrewShepherd0 (2321 views)
2016-12-15 21:50:57
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 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‑
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!