如何绘制近前剪裁平面?

时间:2013-03-29 12:45:38

标签: c opengl-es

我试图在前面裁剪平面附近的OpenGL中绘制对象。已经定义了路径,相机正沿着路径向下移动。现在,我想画一个用相机移动的物体,机器人不是直接在前剪裁平面上。

我正在使用Android上的Tux Racer游戏端口,并尝试使用hud.c中的小型测试功能:

void draw_triangle() {
    set_gl_options(FOG_PLANE);

    glPushMatrix();
    {
        glTranslatef(getparam_x_resolution() / 2.0, getparam_y_resolution() / 2.0, -5.0);
        glBegin(GL_TRIANGLES);          // Drawing Using Triangles
        glVertex3f(0.0f, 50.0f, 0.0f);      // Top
        glVertex3f(-50.0f, -50.0f, 0.0f);   // Bottom Left
        glVertex3f(50.0f, -50.0f, 0.0f);    // Bottom Right
        glEnd();                // Finished Drawing
    }
    glPopMatrix();
}

但它不会画。我哪里出错了?如果z轴为-5.0太远,我不会反感,或者我应该使用set_gl_options()的其他选项,也就是说,我不应该从这个选项中启用/禁用什么来看3D对象:

  • GL_TEXTURE_2D
  • GL_DEPTH_TEST
  • GL_CULL_FACE
  • GL_LIGHTING
  • GL_NORMALIZE
  • GL_ALPHA_TEST
  • GL_BLEND
  • GL_STENCIL_TEST
  • GL_TEXTURE_GEN_S
  • GL_TEXTURE_GEN_T
  • GL_COLOR_MATERIAL

其他功能:

  • glDepthMask
  • glShadeModel
  • glDepthFunc

以下是设置摄像机视图的代码:

void update_view(player_data_t *plyr, scalar_t dt) {
    point_t view_pt;
    vector_t view_dir, up_dir, vel_dir, view_vec;
    scalar_t ycoord;
    scalar_t course_angle;
    vector_t axis;
    matrixgl_t rot_mat;
    vector_t y_vec;
    vector_t mz_vec;
    vector_t vel_proj;
    quaternion_t rot_quat;
    scalar_t speed;
    vector_t vel_cpy;
    scalar_t time_constant_mult;

    vel_cpy = plyr->vel;
    speed = normalize_vector(&vel_cpy);

    time_constant_mult =
            1.0
                    / min( 1.0,
                            max( 0.0,
                                    ( speed - NO_INTERPOLATION_SPEED ) /
                                    ( BASELINE_INTERPOLATION_SPEED - NO_INTERPOLATION_SPEED )));

    up_dir = make_vector(0, 1, 0);

    vel_dir = plyr->vel;
    normalize_vector(&vel_dir);

    course_angle = get_course_angle();

    switch (plyr->view.mode) {
    case TUXEYE: {
        .
.
.
        break;
    }
    case BEHIND: {
        /* Camera-on-a-string mode */

        /* Construct vector from player to camera */
        view_vec = make_vector(0, sin(ANGLES_TO_RADIANS(
                course_angle -
                CAMERA_ANGLE_ABOVE_SLOPE +
                PLAYER_ANGLE_IN_CAMERA )), cos(ANGLES_TO_RADIANS(
                course_angle -
                CAMERA_ANGLE_ABOVE_SLOPE +
                PLAYER_ANGLE_IN_CAMERA )));

        view_vec = scale_vector(CAMERA_DISTANCE, view_vec);

        y_vec = make_vector(0.0, 1.0, 0.0);
        mz_vec = make_vector(0.0, 0.0, -1.0);
        vel_proj = project_into_plane(y_vec, vel_dir);

        normalize_vector(&vel_proj);

        /* Rotate view_vec so that it places the camera behind player */
        rot_quat = make_rotation_quaternion(mz_vec, vel_proj);

        view_vec = rotate_vector(rot_quat, view_vec);

        /* Construct view point */
        view_pt = move_point(plyr->pos, view_vec);

        /* Make sure view point is above terrain */
        ycoord = find_y_coord(view_pt.x, view_pt.z);

        if (view_pt.y < ycoord + MIN_CAMERA_HEIGHT) {
            view_pt.y = ycoord + MIN_CAMERA_HEIGHT;
        }

        /* Interpolate view point */
        if (plyr->view.initialized) {
            /* Interpolate twice to get a second-order filter */
            int i;
            for (i = 0; i < 2; i++) {
                view_pt = interpolate_view_pos(plyr->pos, plyr->pos,
                        MAX_CAMERA_PITCH, plyr->view.pos, view_pt,
                        CAMERA_DISTANCE, dt,
                        BEHIND_ORBIT_TIME_CONSTANT * time_constant_mult);
            }
        }

        /* Make sure interpolated view point is above terrain */
        ycoord = find_y_coord(view_pt.x, view_pt.z);

        if (view_pt.y < ycoord + ABSOLUTE_MIN_CAMERA_HEIGHT) {
            view_pt.y = ycoord + ABSOLUTE_MIN_CAMERA_HEIGHT;
        }

        /* Construct view direction */
        view_vec = subtract_points(view_pt, plyr->pos);

        axis = cross_product(y_vec, view_vec);
        normalize_vector(&axis);

        make_rotation_about_vector_matrix(rot_mat, axis,
                PLAYER_ANGLE_IN_CAMERA);
        view_dir = scale_vector(-1.0, transform_vector(rot_mat, view_vec));

        /* Interpolate orientation of camera */
        if (plyr->view.initialized) {
            /* Interpolate twice to get a second-order filter */
            int i;
            for (i = 0; i < 2; i++) {
                interpolate_view_frame(plyr->view.up, plyr->view.dir, &up_dir,
                        &view_dir, dt, BEHIND_ORIENT_TIME_CONSTANT);
                up_dir = make_vector(0.0, 1.0, 0.0);
            }
        }

        break;
    }

    case FOLLOW: {
        /* Camera follows player (above and behind) */

        up_dir = make_vector(0, 1, 0);

        /* Construct vector from player to camera */
        view_vec = make_vector(0, sin(ANGLES_TO_RADIANS(
                course_angle -
                CAMERA_ANGLE_ABOVE_SLOPE +
                PLAYER_ANGLE_IN_CAMERA )), cos(ANGLES_TO_RADIANS(
                course_angle -
                CAMERA_ANGLE_ABOVE_SLOPE +
                PLAYER_ANGLE_IN_CAMERA )));
        view_vec = scale_vector(CAMERA_DISTANCE, view_vec);

        y_vec = make_vector(0.0, 1.0, 0.0);
        mz_vec = make_vector(0.0, 0.0, -1.0);
        vel_proj = project_into_plane(y_vec, vel_dir);

        normalize_vector(&vel_proj);

        /* Rotate view_vec so that it places the camera behind player */
        rot_quat = make_rotation_quaternion(mz_vec, vel_proj);

        view_vec = rotate_vector(rot_quat, view_vec);

        /* Construct view point */
        view_pt = move_point(plyr->pos, view_vec);

        /* Make sure view point is above terrain */
        ycoord = find_y_coord(view_pt.x, view_pt.z);

        if (view_pt.y < ycoord + MIN_CAMERA_HEIGHT) {
            view_pt.y = ycoord + MIN_CAMERA_HEIGHT;
        }

        /* Interpolate view point */
        if (plyr->view.initialized) {
            /* Interpolate twice to get a second-order filter */
            int i;
            for (i = 0; i < 2; i++) {
                view_pt = interpolate_view_pos(plyr->view.plyr_pos, plyr->pos,
                        MAX_CAMERA_PITCH, plyr->view.pos, view_pt,
                        CAMERA_DISTANCE, dt,
                        FOLLOW_ORBIT_TIME_CONSTANT * time_constant_mult);
            }
        }

        /* Make sure interpolate view point is above terrain */
        ycoord = find_y_coord(view_pt.x, view_pt.z);

        if (view_pt.y < ycoord + ABSOLUTE_MIN_CAMERA_HEIGHT) {
            view_pt.y = ycoord + ABSOLUTE_MIN_CAMERA_HEIGHT;
        }

        /* Construct view direction */
        view_vec = subtract_points(view_pt, plyr->pos);

        axis = cross_product(y_vec, view_vec);
        normalize_vector(&axis);

        make_rotation_about_vector_matrix(rot_mat, axis,
                PLAYER_ANGLE_IN_CAMERA);
        view_dir = scale_vector(-1.0, transform_vector(rot_mat, view_vec));

        /* Interpolate orientation of camera */
        if (plyr->view.initialized) {
            /* Interpolate twice to get a second-order filter */
            int i;
            for (i = 0; i < 2; i++) {
                interpolate_view_frame(plyr->view.up, plyr->view.dir, &up_dir,
                        &view_dir, dt, FOLLOW_ORIENT_TIME_CONSTANT);
                up_dir = make_vector(0.0, 1.0, 0.0);
            }
        }

        break;
    }

    case ABOVE: {
        /* Camera always uphill of player */

        up_dir = make_vector(0, 1, 0);

        /* Construct vector from player to camera */
        view_vec = make_vector(0, sin(ANGLES_TO_RADIANS(
                course_angle -
                CAMERA_ANGLE_ABOVE_SLOPE+
                PLAYER_ANGLE_IN_CAMERA )), cos(ANGLES_TO_RADIANS(
                course_angle -
                CAMERA_ANGLE_ABOVE_SLOPE+
                PLAYER_ANGLE_IN_CAMERA )));
        view_vec = scale_vector(CAMERA_DISTANCE, view_vec);

        /* Construct view point */
        view_pt = move_point(plyr->pos, view_vec);

        /* Make sure view point is above terrain */
        ycoord = find_y_coord(view_pt.x, view_pt.z);

        if (view_pt.y < ycoord + MIN_CAMERA_HEIGHT) {
            view_pt.y = ycoord + MIN_CAMERA_HEIGHT;
        }

        /* Construct view direction */
        view_vec = subtract_points(view_pt, plyr->pos);

        make_rotation_matrix(rot_mat, PLAYER_ANGLE_IN_CAMERA, 'x');
        view_dir = scale_vector(-1.0, transform_vector(rot_mat, view_vec));

        break;
    }

    case BIRDEYE: {
        /* Camera always uphill of player */

        up_dir = make_vector(0, 1, 0);

        /* Construct vector from player to camera */
        /*sin(ANGLES_TO_RADIANS(
                                        course_angle -
                                        CAMERA_ANGLE_ABOVE_SLOPE+
                                        PLAYER_ANGLE_IN_CAMERA ))
                                        */
        /*cos(ANGLES_TO_RADIANS(
                                                course_angle -
                                                CAMERA_ANGLE_ABOVE_SLOPE+
                                                PLAYER_ANGLE_IN_CAMERA ))
                                                */
        view_vec = make_vector(0, 3, -0.5);
        view_vec = scale_vector(CAMERA_DISTANCE, view_vec);

        /* Construct view point */
        view_pt = move_point(plyr->pos, view_vec);

        /* Make sure view point is above terrain */
        ycoord = find_y_coord(view_pt.x, view_pt.z);

        if (view_pt.y < ycoord + MIN_CAMERA_HEIGHT) {
            view_pt.y = ycoord + MIN_CAMERA_HEIGHT;
        }

        /* Construct view direction */
        view_vec = subtract_points(view_pt, plyr->pos);

        make_rotation_matrix(rot_mat, PLAYER_ANGLE_IN_CAMERA, 'x');
        view_dir = scale_vector(-1.0, transform_vector(rot_mat, view_vec));

        break;
    }
    default:
        code_not_reached();
        break;
    }

    /* Create view matrix */
    plyr->view.pos = view_pt;
    plyr->view.dir = view_dir;
    plyr->view.up = up_dir;
    plyr->view.plyr_pos = plyr->pos;
    plyr->view.initialized = True;

    setup_view_matrix(plyr);
}

更新

解决了!问题在于使用OpenGL而不是OpenGL ES。

2 个答案:

答案 0 :(得分:2)

Android使用OpenGL ES,它不支持glBegin()和glEnd(),所以你的代码不会绘制任何东西。您需要使用“顶点缓冲区对象”来绘制对象。

维也纳各组织的经典指南如下:http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=45

但你可能会发现谷歌搜索“Android OpenGL教程”将提供一些更简单的指南。

答案 1 :(得分:1)

好吧,如果你想在相机前面显示一些东西,那么所有的相机设置代码都无关紧要;此外,你需要确保它不适用于你的三角形,也就是说,你从矩阵堆栈为空的某个地方调用drawTriangle()。从现在开始你在哪里打电话?我去GitHub看TuxRider并迷路了,抱歉。

之后,如果你在屏幕坐标中渲染一个三角形,可能值得考虑它们的范围从-1到+1 - 你在5的距离下50到50的几何形状太大而且太远了。

祝你好运