gpt4 book ai didi

c - 如何靠近前剪裁平面?

转载 作者:太空宇宙 更新时间:2023-11-04 07:28:53 26 4
gpt4 key购买 nike

我正在尝试在前裁剪平面附近的 OpenGL 中绘制对象。已经定义了路径,并且相机正在沿着路径移动。现在,我想绘制随相机移动的物体,机器人不直接在前裁剪平面上。

我正在使用 Tux Racer Android 上的游戏端口并尝试在 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

这是设置相机 View 的代码:

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。

最佳答案

Android 使用不支持 glBegin() 和 glEnd() 的 OpenGL ES,因此您的代码不会绘制任何内容。您需要使用 Vertex Buffer Objects 来绘制对象。

VBO 的经典指南在这里:http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=45

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

关于c - 如何靠近前剪裁平面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15703523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com