位置移动2D对象openGL [英] Position moving 2D object openGL

查看:75
本文介绍了位置移动2D对象openGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象在x轴上来回移动,但是我无法将其沿x轴定位得更远.

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像这样创建对象:

Platform.cpp creates 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();

推荐答案

我觉得您对OpenGL的工作方式有误解.您编写了"Platform.cpp像这样创建对象:",然后在代码段中看到了正在创建被OpenGL矩阵堆栈操作包围的某些Plattform类的实例.我怀疑您以为OpenGL将以某种方式存储"此对象". 这不是OpenGL的工作方式,您是根据场景图来考虑的. OpenGL不是场景图.

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是绘图API.通话

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

在屏幕上绘制一个四边形.再说一次:他们绘制它.发出这些命令后,OpenGL将它们遗忘了. OpenGL转换矩阵用于转换绘图命令的输入数据.但是再次没有持久性.必须为绘制的每一帧发出绘制命令.我最初以为我可以重写您的一些代码,但是如果可以这样说,则需要重新编写.

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.

典型的OpenGL程序如下所示(我完全省略了所有的类和类型定义,并希望在解释变量,成员和方法名称时有一些常识).

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

这是认真使用OpenGL的最基本方法.我详细介绍了它,以使您了解如何使用它以及如何工作.

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.

这篇关于位置移动2D对象openGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆