Log in or register to post comments

GLUProject get 2d from 3d coordinates

October 9, 2012 - 6:57am #1

Hello,

 

I am trying to convert 3d coordinates into 2d in renderframe method using following code.

 

if (vbCounter>0)
        {
            // Render frame around button
            glUseProgram(vbShaderProgramID);

            glVertexAttribPointer(vbVertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
                (const GLvoid*) &vbVertices[0]);

            glEnableVertexAttribArray(vbVertexHandle);

            glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
                (GLfloat*)&modelViewProjection.data[0] );

            // We multiply by 8 because that's the number of vertices per button
            // The reason is that GL_LINES considers only pairs. So some vertices
            // must be repeated.
            glDrawArrays(GL_LINES, 0, target->getNumVirtualButtons()*8);

            SampleUtils::checkGlError("VirtualButtons drawButton");

            glDisableVertexAttribArray(vbVertexHandle);
            ///////////////////////////////Light Rectangle /////////////////////////////////////////////////////
            glGetDoublev(GL_MODELVIEW_MATRIX, ModelViewMat);
            glGetDoublev(GL_PROJECTION_MATRIX, ProjMat);
            glGetIntegerv(GL_VIEWPORT, Viewport);

                  gluProject(-97.68f, -28.f, 0.f, modelViewMatrix.data,projectionMatrix.data,Viewport,& blY_Lite,& blX_Lite,&winZ);
                  gluProject(-15.93.f, -28.f, 0.f, modelViewMatrix.data,projectionMatrix.data,Viewport,& brY_Lite,& brX_Lite,&winZ);
                  gluProject(-97.68f, -50.52.f, 0.f, modelViewMatrix.data,projectionMatrix.data,Viewport,& tlY_Lite,& tlX_Lite,&winZ);
                  gluProject(-15.93f, -50.52f, 0.f, modelViewMatrix.data,projectionMatrix.data,Viewport,& trY_Lite,& trX_Lite,&winZ);
}

 

Now NDK does not build and give errors that it does not recognize GL_VIEWPORT, GL_PROJECTION_MATRIX and GL_MODELVIEW_MATRIX constants. Does anybody knows solution to it? It says opengl es2/gl2 at top. Does anybody knows how to get these constants.

GLUProject get 2d from 3d coordinates

October 11, 2012 - 7:38am #11

Sounds great!

GLUProject get 2d from 3d coordinates

October 11, 2012 - 6:40am #10

Than You So Much It Worked 

as my Requirment

GLUProject get 2d from 3d coordinates

October 11, 2012 - 2:24am #9

The point I've used is just an example ;

for instance in your case you will the points that appear in the code you copy-pasted in this post originally:

Vec3F( -97.68f, -28.f, 0.f )
Vec3F( -15.93.f, -28.f, 0.f )

Vec3F(

-97.68f, -50.52.f, 0.f )
Vec3F( -15.93f, -50.52f, 0.f )

I mean you will just use whatever 3D point you want to project to screen coordinates. 

GLUProject get 2d from 3d coordinates

October 11, 2012 - 2:17am #8

How Can We get These points

QCAR::Vec3F(150.0f, 120.0f, 0.0f))


GLUProject get 2d from 3d coordinates

October 11, 2012 - 12:56am #7

Hi, concerning your second question about  how to get the cameraPoint (referred to the link https://ar.qualcomm.at/content/error-projecting-point-screen ):

you can use the function: QCAR:Tools:projectPoint( ... ), as in the sample code here below:

QCAR::Vec2F cameraPoint = QCAR::Tool::projectPoint(cameraCalibration, trackable->getPose(), QCAR::Vec3F(150.0f, 120.0f, 0.0f));

So, you basically give the pose matrix representing the reference frame of your trackable, the CameraCalibration, and the 3D point that you want to project (whose coordinates are expressed in the reference frame of the trackable), and you get as result the camera coordinates;

then you use the function QCAR::Vec2F cameraPointToScreenPoint(QCAR::Vec2F cameraPoint) as explained in the link above, and you have your point precisely in screen pixel coordinates.

This should really do what you are trying to achieve and will not require that you use gluProject / glGetInteger and all that "problematic" OpenGL code; also, with this approach, you don't need to use OpenGL_1.1, you can stick to 2.0 (but of course you can use the one you prefer).

 

 

 

GLUProject get 2d from 3d coordinates

October 11, 2012 - 12:40am #6

Hi, concerning your question on "unimplemented API" in you opengl code, I see an inconsistency in the code that starts with:

#ifndef USE_OPENGL_ES_1_1

here you're saying "if not using 1.1" (i.e. if using 2.0)", then you have some OpenGL_2.0 code (correct), which is mixed with functions like glGetInteger (a few lines after) which is instead OpenGL_1.1 code (so it should not be there together with 2.0); 

actually, if you really have enabled the use of 1.1 in android.mk, all the code above will not even be compiled (it will be ignored by the compiler), so the error must come from another code snippet somewhere else, not from the one that you copy-pasted; could you double-check please? 

also, one extra thing to keep in mind is that if you want to use GL_1.1 you should replace glGetDoublev with glGetFloatv, as glGetDoublev is usually not supported in OpenGL ES (no problem instead for glGetIntegerv...)

I know OpenGL ES can be a bit complicated sometimes ... :-) 

 

GLUProject get 2d from 3d coordinates

October 11, 2012 - 12:03am #5

The link you posted i.e. https://ar.qualcomm.at/content/error-projecting-point-screen shows a method that takes camerapoint and returns screen point. How I will fetch camerapoint using opengl es 2 or 1

GLUProject get 2d from 3d coordinates

October 10, 2012 - 11:53pm #4

i `ve updated code using open GL 1 enabled option in android.mk file and following is in the .cpp file

#ifdef USE_OPENGL_ES_1_1
#include <GLES/gl.h>
#include <GLES/glext.h>
#else
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#endif

now the edited code is

 if (vbCounter>0)
        {
            // Render frame around button
#ifndef USE_OPENGL_ES_1_1

else{
                LOG(" is it working ");
                    QCAR::Matrix44F modelViewProjection;

                ShaderUtils::multiplyMatrix(&projectionMatrix.data[0], &modelViewMatrix.data[0], &modelViewProjection.data[0]);
                glUseProgram(vbShaderProgramID);
                glVertexAttribPointer(vbVertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &vbVertices[0]);
                glEnableVertexAttribArray(vbVertexHandle);
                glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (GLfloat*)&modelViewProjection.data[0] );
                glDrawArrays(GL_LINES, 0, target->getNumVirtualButtons()*8);
                glDisableVertexAttribArray(vbVertexHandle);

                GLfloat ModelViewMat[16], ProjMat[16];

                float winX, winY, winZ;
                int Viewport[4];

                glGetDoublev(GL_MODELVIEW_MATRIX, ModelViewMat);
                glGetDoublev(GL_PROJECTION_MATRIX, ProjMat);
                glGetIntegerv(GL_VIEWPORT, Viewport);
                gluProject(-97.68f, -28.f, 0.f, modelViewMatrix.data,projectionMatrix.data,Viewport,& blY_Lite,& blX_Lite,&winZ);
                      gluProject(-15.93.f, -28.f, 0.f, modelViewMatrix.data,projectionMatrix.data,Viewport,& brY_Lite,& brX_Lite,&winZ);
                  gluProject(-97.68f, -50.52.f, 0.f, modelViewMatrix.data,projectionMatrix.data,Viewport,& tlY_Lite,& tlX_Lite,&winZ);
                  gluProject(-15.93f, -50.52f, 0.f, modelViewMatrix.data,projectionMatrix.data,Viewport,& trY_Lite,& trX_Lite,&winZ);

 

now it s giving me following error in logcat

01-08 05:23:04.311: E/libEGL(7134): called unimplemented OpenGL ES API

 

GLUProject get 2d from 3d coordinates

October 9, 2012 - 7:20am #3

One more note:

you can also find a very nice solution to what you are trying to achieve in this thread:

https://ar.qualcomm.at/content/error-projecting-point-screen

 

GLUProject get 2d from 3d coordinates

October 9, 2012 - 7:17am #2

Hi,

when using OpenGL ES 2.0, you cannot use the following lines: 

glGetDoublev(GL_MODELVIEW_MATRIX, ModelViewMat);
glGetDoublev(GL_PROJECTION_MATRIX, ProjMat);
glGetIntegerv(GL_VIEWPORT, Viewport);

First reason is that glGetDoublev is not supported, second reason is that GL_MODELVIEW_MATRIX, GL_PROJECTION_MATRIX and GL_VIEWPORT do not exist in OpenGL ES 2.0.

If you want to use those functions, you will have to switch to OpenGL ES 1.1 (instead of 2.0); to do that, you can open the android.mk file in the jni directory of your project, and change this variable to true:

USE_OPENGL_ES_1_1 := true

On the other hand, you can also keep using OpenGL ES 2.0, in this case you don't need to call glGetDouble / glGetInteger, as you already have the modelview and projection matrices in you application code (check otherwise the sample code in ImageTargets.cpp), and the viewport as well.

 

Log in or register to post comments