Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

OpenGL: mix 2D and 3D rendering question

$
0
0
Hi all, a little background, our current opengl based system uses 2D orthographic projection to draw 2D elements into screen like adding images, texts, etc. When adding objects, coordinates are specified in screen coordinates (top left is 0,0 and bottom right is width, height), for the upcoming upgrade, we want to add rotation feature to these objects, however we cant do that using orthographic projection, so what i did is to add a perspective as well as a camera matrix to draw the object in 3D and add rotation, but if we do it in 3D then it takes a 3D coordinate and not the screen space coordinate which is not the specification, so what i did is, accept the 2D X,Y coordinate as input, unProject it and use the result as my new base X, Y coordinate for my object, my problem is i cannot seem to make to make it work here is the code (stripped down for simplicty): // Projection matrix 4:3 ratio glm::mat4 projectionMatrix = glm::perspective(glm::radians(45.0f), 4.0f / 3.0f, 0.1f, 100.0f); // Camera matrix glm::mat4 viewMatrix = glm::lookAt( glm::vec3(0, 0, 5), // Camera is at 0,0,5 in World Space, our triangle is at 0 z axis glm::vec3(0, 0, 0), // and looks at the origin glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down) ); glm::mat4 model = glm::mat4(1.0f); //identity // sample Rectangle input (X, Y, X2,Y2) in screen space is (0,0,100,100) i set 0 for Z as screen space does not have a Z glm::vec3 un = glm::unProject(glm::vec3(0, 0, 0), viewMatrix, projectionMatrix, viewport); glm::vec3 un_2 = glm::unProject(glm::vec3(100, 100, 0), viewMatrix, projectionMatrix, viewport); // Result // un : un = {x=-0.0552284457 y=-0.0414213315 z=4.90000010 ...} // un_2: un_2 = {x=-0.0414213352 y=-0.0276142210 z=4.90000010 ...} So the plan here is to use the unprojected result to construct a triangle and apply transformation such as rotation without breaking the existing code that accepts 2D space as coordinate. so in summary is accept 2D coordinates and draw a 3D object on that area with transformation. on the above code, it looks like the result is in the near clipping plane and when i draw a rectangle using those points, it is not visible on the screen (camera is at Z=5 position). any idea on how to do this? i know that Z is a player here somewhere but i dont know how. like the Z on the above result is on the near clipping plane, i just want it to be visible on the screen and appear on the specific 2D spot. let me know if you have any ideas on how to work on this black magic of a problem, Thank you in advance

Viewing all articles
Browse latest Browse all 17825

Trending Articles