开发者

Position moving 2D object openGL

I have an object moving back and forth on the x axis, however i cant position it further right along the x axis.

this is my code, how do i do it?

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 cre开发者_C百科ates the object like so:

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();


I have the feeling you suffer from a misconception of how OpenGL works. You wrote "Platform.cpp creates the object like so:" and in the code snippet before I can see you're creating the instance of some Plattform class surrounded by OpenGL matrix stack operations. I suspect you assumed that OpenGL would somehow "store" this "object". This is not how OpenGL works You're thinking in terms of a scene graph. OpenGL is not a scene graph.

OpenGL is a drawing API. The calls

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();

draw a quad to the screen. Again: They draw it. After those commands have been issued they are gone and forgotten by OpenGL. The OpenGL transformation matrices are used for transforming the drawing commands' input data. But again there's no persistency. The drawing commands have to be issued for every frame drawn. I first thought I could rewrite some of your code, but it needs to be rewritten ground up, if I may say so.

The typical OpenGL program looks like this (I liberally omit all the class and type definitions and expect some common sense interpreting the variable, member and method names).

/* 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();
}

This is the most basic way use OpenGL seriously. I wrote it in this detail to give you the idea how to use it, and how it works.


Why don't you adjust the x-axis scaling in your call to glTranslatef?

glTranslatef(amplitude * sinf(moveRad), -100,0);


I have a feeling you don't know exactly what your code is doing (correct me if I'm wrong). If you want to move it to the right just add a number in here.

glTranslatef(50.0 * sinf(moveRad) + 30, -100,0);

I'll update my answer if neccesary.


I think your problem is the '50.0 * sinf(moveRad)' - that will oscilate between -50 and 50. Try adding a value instead of or as well as multiplying it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜