我有一个物体在x轴上来回移动,但我不能沿x轴进一步定位。
这是我的代码,我该怎么做?
float moveRad = 0.0;
moveRad = moveBee * (PI/180.0);
moveBee += 0.1;
glPushMatrix();
glTranslatef(50.0 * sinf(moveRad), -100,0);
e[0] = new Platform(0, 0, 0, 40, 33, 40, 33, 00, textures[23], (50.0 * sinf(moveRad)), -100);
glPopMatrix();
Platform.cpp像这样创建对象:
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(x1,y1);
glTexCoord2f(0.0, 1.0); glVertex2f(x2,y2);
glTexCoord2f(1.0, 1.0); glVertex2f(x3,y3);
glTexCoord2f(1.0, 0.0); glVertex2f(x4,y4);
glEnd();
答案 0 :(得分:3)
我觉得您对OpenGL的工作方式存在误解。您编写了“Platform.cpp创建对象,就像这样:”并在代码片段中,我可以看到您正在创建由OpenGL矩阵堆栈操作包围的某些Plattform类的实例。我怀疑你认为OpenGL会以某种方式“存储”这个“对象”。 这不是OpenGL的工作方式您正在考虑场景图。 OpenGL不是场景图。
OpenGL是一个绘图API。电话
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(x1,y1);
glTexCoord2f(0.0, 1.0); glVertex2f(x2,y2);
glTexCoord2f(1.0, 1.0); glVertex2f(x3,y3);
glTexCoord2f(1.0, 0.0); glVertex2f(x4,y4);
glEnd();
在屏幕上绘制四边形。再次:他们绘制它。在发出这些命令之后,它们就被OpenGL遗忘了。 OpenGL变换矩阵用于变换绘图命令的输入数据。但同样没有持久性。必须为绘制的每个帧发出绘图命令。我首先想到我可以重写你的一些代码,但是如果可以的话,它需要重新编写。
典型的OpenGL程序看起来像这样(我自由地省略了所有类和类型定义,并期望解释变量,成员和方法名称的一些常识)。
/* draw_scene is called on every iteration of the program main loop or
the drawing callback handler to update the screen */
void Scene::draw_scene(ScreenInfo si)
{
glViewport(si.viewport.x, si.viewport.y, si.viewport.width, si.viewport.height);
glClearColor(this->clear.r, this->clear.g, this->clear.b, this->clear.a);
glClearDepth(this->clear.d);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
glClear( (this->clear.color ? GL_COLOR_BUFFER_BIT) |
(this->clear.depth ? GL_DEPTH_BUFFER_BTT) );
std::list<SceneObjects*> objects_by_distance =
sort_objects_by_direction(scene->objects,
scene->active_camera->position
scene->active_camera->direction);
SceneObjects *closest_object = objects_by_distance.front();
SceneObjects *farthest_object = objects_by_distance.back();
float near_clip = max(NEAR_CLIP_LIMIT,
length(closest_object->position - scene->active_camera->position)
- closest_object->bounding_sphere.radius );
float far_clip = min(FAR_CLIP_LIMIT,
length(farthest_object->position - scene->active_camera->position)
+ farthest_object->bounding_sphere.radius );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
switch( scene->projection.type ) {
case Projection::perspective: {
gluPerspective( scene->projection.fov,
(float)si.viewport.width/(float)si.viewport.height,
near_clip, far_clip);
} break;
case Projection::orthographic: {
float aspect = (float)si.viewport.width/(float)si.viewport.height;
glOrtho( -0.5 * scene->projection.size * aspect, 0.5 * scene->projection.size * aspect
-0.5 * scene->projection.size 0.5 * scene->projection.size );
} break;
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* I normally disregard using gluLookAt, but in this case I use it
to show as much as possible! */
gluLookAt( scene->active_camera->position.x, scene->active_camera->position.y, scene->active_camera->position.z,
scene->active_camera->position.x + scene->active_camera->direction.x,
scene->active_camera->position.y + scene->active_camera->direction.y,
scene->active_camera->position.z + scene->active_camera->direction.z,
scene->active_camera->up.x, scene->active_camera->up.y, scene->active_camera->up.z );
for_each(scene->objects.begin(), scene->objects.end(), draw_object)
}
void draw_object(SceneObject *object)
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(object->position.x, object->position.y, object->position.z);
glRotatef(object->rotation.axis.angle, object->rotation.axis.x, object->rotation.axis.y, object->rotation.axis.z);
GLfloat *(vertex_ptr[3][3]) = object->mesh->vertices;
GLuint *vertex_indices = object->mesh->face_vertex_indices;
#ifdef USE_IMMEDIATE_MODE
glBegin(GL_TRIANGLES);
for(int i = 0; i < object->mesh->face_count; i++) {
glNormalfv(&vertex_ptr[vertex_indices[i]][0]);
glTexCoord3fv(&vertex_ptr[vertex_indices[i]][1]);
glVertex3fv(&vertex_ptr[vertex_indices[i]][2]);
glNormalfv(&vertex_ptr[vertex_indices[i+1]][0]);
glTexCoord3fv(&vertex_ptr[vertex_indices[i+1]][1]);
glVertex3fv(&vertex_ptr[vertex_indices[i+1]][2]);
glNormalfv(&vertex_ptr[vertex_indices[i+2]][0]);
glTexCoord3fv(&vertex_ptr[vertex_indices[i+2]][1]);
glVertex3fv(&vertex_ptr[vertex_indices[i+2]][2]);
}
glEnd();
#else
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
/* This is direct vertex array mode.
A more modern approach is using Vertex Buffer Objects, which reused this
API, but adds further function calls. */
glNormalPointer(GL_FLOAT, 3*3*sizeof(GLfloat), &vertex_ptr[0][0]);
glTexCoordPointer(3, GL_FLOAT, 3*3*sizeof(GLfloat), &vertex_ptr[0][1]);
glVertexPointer(3, GL_FLOAT, 3*3*sizeof(GLfloat), &vertex_ptr[0][2]);
glDrawElements(GL_TRIANGLES, object->mesh->face_count*3, GL_UNSIGNED_INT, vertex_indices);
#endif
glPopMatrix();
}
这是认真使用OpenGL的最基本方式。我在这个细节中写了这篇文章,让你了解如何使用它,以及它是如何工作的。
答案 1 :(得分:0)
为什么不在调用glTranslatef
时调整x轴缩放?
glTranslatef(amplitude * sinf(moveRad), -100,0);
答案 2 :(得分:0)
我有一种感觉,你不知道你的代码到底在做什么(如果我错了,请纠正我)。如果你想将它移到右边,只需在这里添加一个数字。
glTranslatef(50.0 * sinf(moveRad) + 30 , - 100,0);
如果有必要,我会更新我的答案。
答案 3 :(得分:0)
我认为你的问题是'50 .0 * sinf(moveRad)' - 它将在-50和50之间进行振荡。尝试添加一个值而不是或者乘以它。