使用gluProject()进行OpenGL投影

时间:2015-10-24 16:56:09

标签: c++ opengl matrix

我正在尝试获取多维数据集8个角的屏幕坐标。

以下是我的工作:

int width = 640;
int height = 480;

GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];

vector<GLdouble> testpoint_on_screen(24);
vector<GLdouble> vec(24);

然后在绘制函数中,一旦找到最小和最大3D坐标,我将它们放入一个数组中,然后将此数组复制到一个向量,如下所示:

GLfloat vertices[] = {
    fMinX - scanSize, fMaxY + scanSize, fMaxZ + scanSize,
    fMaxX + scanSize, fMaxY + scanSize, fMaxZ + scanSize,
    fMaxX + scanSize, fMinY - scanSize, fMaxZ + scanSize,
    fMinX - scanSize, fMinY - scanSize, fMaxZ + scanSize,
    fMinX - scanSize, fMaxY + scanSize, fMinZ - scanSize,
    fMaxX + scanSize, fMaxY + scanSize, fMinZ - scanSize,
    fMaxX + scanSize, fMinY - scanSize, fMinZ - scanSize,
    fMinX - scanSize, fMinY - scanSize, fMinZ - scanSize
};

vector<GLdouble> convertionV(vertices, vertices + sizeof vertices / sizeof vertices[0]);

vec = convertionV;

这是display(),我得到了投影和模型视图矩阵。然后我使用gluProject()将3D转换为2D。

void display()
{
    // clear the window
    glClear( GL_COLOR_BUFFER_BIT );

    // show the current camera frame

    //based on the way cv::Mat stores data, you need to flip it before displaying it
    cv::Mat tempimage;
    cv::flip(image, tempimage, 0);
    glDrawPixels( tempimage.size().width, tempimage.size().height, GL_RGB, GL_UNSIGNED_BYTE, tempimage.ptr() );

    //////////////////////////////////////////////////////////////////////////////////
    // Here, set up new parameters to render a scene viewed from the camera.

    //set viewport
    glViewport(0, 0, tempimage.size().width, tempimage.size().height);

    //set projection matrix using intrinsic camera params
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    float aspect = tempimage.size().width*1.0/tempimage.size().height;
    //gluPerspective is arbitrarily set, you will have to determine these values based
    //on the intrinsic camera parameters
    gluPerspective(60.0f, aspect, 0.1f, 100.0f); 

    //you will have to set modelview matrix using extrinsic camera params
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0);  

    // get the viewport, modelView matrix and projection matrix
    glGetIntegerv (GL_VIEWPORT, viewport);
    glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
    glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);

    /////////////////////////////////////////////////////////////////////////////////
    // Drawing routine
    //initialise certain number of particles
    for(int i = 0; i<number_of_particles; i++){

        // uniformly distributed for the very first generation
        if((points[i].x == NULL) || (points[i].y = NULL)){
            //randomly generate X,Y,Z and angle values
            X_object = RandomNumber(-5.0, 5.0);//rand() % 10 - 2.5;
            Y_object = RandomNumber(-5.0, 5.0);//rand() % 10 - 2.5;
            Z_object = 23;
            cube_angle = 0;//rand() % 360 + 1;
        }else{
            //printf("second generation should improve the distribution");
            //set sigma accordingly
            //use best particle coordinates for the next X and Y            
            X_object = points[i].x;
            Y_object = points[i].y;
            Z_object = points[i].z;
            cube_angle = points[i].angle;
        }

        points[i].x = X_object;
        points[i].y = Y_object;
        points[i].z = Z_object;
        points[i].angle = cube_angle;



        gluProject(X_object, Y_object, Z_object, mvmatrix, projmatrix, viewport, &point_on_screen[i].x, &point_on_screen[i].y, &point_on_screen[i].z);

        for(int opi = 0; opi < vec.size()/3; opi++){
            int currEl = opi * 3;
            gluProject(vec[currEl], vec[currEl + 1], vec[currEl + 2], mvmatrix, projmatrix, viewport, 
                &testpoint_on_screen[currEl], &testpoint_on_screen[currEl + 1], &testpoint_on_screen[currEl + 2]);
        }

        glLoadIdentity();       
        glTranslatef(X_object, Y_object, -Z_object); //this is an arbitrary position for demonstration

        glRotatef(cube_angle, 1.0f, 1.0f, 1.0f);  // Rotate about (1,1,1)-axis [NEW]
        DrawBox(4.0f, 3.5f, 5.0f, 5, 5, 5, 1.0, i);

    }

    // show the rendering on the screen
    glutSwapBuffers();

    // post the next redisplay
    glutPostRedisplay();

}

出于某种原因,我第一次将gluProject与X_object,Y_object和Z_object(前左上角的坐标)一起使用时,它给出了正确的值。然而,虽然我第二次在所有角落都这样做而且它返回废话:

enter image description here

如果有人可以向我解释或建议一个很棒的解决方案。

0 个答案:

没有答案