1

I'm making a simple tutorial game with openGL and have a question about touch method. Please checkout my code:

My (0,0) point is in center of a screen:

void Init()
{
    glClearColor(0.3,0.3,0.3,0.0);
    glMatrixMode(GL_PROJECTION);
    glOrtho(-400.0,400.0,-300.0,300.0,0,1.0); //сетка, середина в точке 0
}

Before this i'm call mouse methods:

glutPassiveMotionFunc(Mouse);
glutMouseFunc(MousePress);

And in method MousePress when touch is coming, it's another system coordinate with (0,0) point in top left corner of a screen. Please can you tell me better approach then make something like x-300;y-400 in MousePress method.

vallentin
  • 23,478
  • 6
  • 59
  • 81
gronzzz
  • 617
  • 6
  • 15
  • 2
    Are you perhaps looking for `gluUnproject()`? I can't provide a link now, my connection is somewhat erratic :-( – Angew is no longer proud of SO Dec 18 '13 at 09:23
  • thanks, i will look at function, but for a moment i think best approach is here :/ http://stackoverflow.com/a/13299602/2204866 Thokra, i'm trying to make drag-control for texture, take a texture and move by calling mouse method. – gronzzz Dec 18 '13 at 09:39
  • You could/should also take a look at a similar question I once asked. http://stackoverflow.com/questions/18244678/3d-ray-picking-use-mouse-coordinates-when-mouse-isnt-locked – vallentin Dec 18 '13 at 10:26

1 Answers1

2

Given the simpler orthographic projection, your "x-300;y-400" is the correct approach, although you might want to do some scaling too...

float x = mouseX/(float)windowWidth;
float y = 1.0f - mouseY/(float)windowHeight; //flip since y=0 is at the top

//x and y are now 0 to 1, bottom left to top right

x = left + x * (right - left);
y = bottom + y * (top - bottom);

//x and y are now in 3D coordinates

Here, left/right/bottom/top are from glOrtho, which in your case can be substituted as follows (but of course storing in a variable is better)...

x = -400 + x * (400 - (-400));
y = -300 + y * (300 - (-300));

If you were using a perspective projection it gets a bit more complicated, as I've described here.

[EDIT]
Assuming the window size is 800x600, the above cancels to {x-400,300-y}. For example,

mouseY = 50;
windowHeight = 600;
float y = 1.0f - (50/(float)600); //1.0 - 0.08333 = 0.91667
y = -300 + y * (300 - (-300)); //-300 + 0.91667 * 600 = 250, also 300-50
Community
  • 1
  • 1
jozxyqk
  • 16,424
  • 12
  • 91
  • 180
  • Thanks!i'am using simple projection and first of all i want to try gluUnproject(); as i understand, that's method specially for this and then simple approach with numbers. btw as i understand for me it will be X = x - 400; Y = 300 - y; because i need in points, but in my coordinate system with (0,0) in center) – gronzzz Dec 18 '13 at 10:12
  • 1
    @gronzzz Assuming you have an `800x600` window then the above code actually cancels to exactly that. `gluUnproject` will work just fine, but as you say, it's pretty straight forward to do it yourself with an orthographic projection too. – jozxyqk Dec 18 '13 at 10:56
  • sorry my stupid, but why cancels ? touch y in 50, so touch in my coordinate system will 300 - 50 = 250, and that's right point, touch y in 450 will give me -150 as i need… so i needn't values between -1 to 1, i need points in my coordinate system. sry again if i stupid – gronzzz Dec 18 '13 at 11:02
  • @gronzzz see the edit. It cancels because first you divide by the window width/height and then multiply by the projection width/height, which are the same. – jozxyqk Dec 18 '13 at 11:17