繁体   English   中英

Opengl ES顶点批处理,C ++,oop帮助

[英]Help with Opengl es vertex batching, c++, oop

我正在尝试游戏编程,并且正在寻找有关我的c ++ iphone opengl es代码的反馈。 我将介绍适用于我的代码,并讨论如何更改它。 我想要做的是使用oop作为子弹,通过一次抽奖来渲染我所有的英雄子弹。 以下仅使用一次平局,但还没有项目符号。

这是我的渲染引擎定义:

class RenderingEngine {
public:
    void Initialize(int width, int height);
    void Render();
    void UpdateAnimation(float timeStep);
    void OnFingerDown(ivec2 location);
private:
    std::vector<Vertex> vertices;
};

以下是渲染引擎方法:

void RenderingEngine::Initialize(int width, int height) {
    ...

    glViewport(0, 0, 320, 480);

    glMatrixMode(GL_PROJECTION);

    const float maxX = 2;
    const float maxY = 3;
    glOrthof(-maxX, +maxX, -maxY, +maxY, -1, 1);

    glMatrixMode(GL_MODELVIEW);
}

//here you can see the single draw call
void RenderingEngine::Render() {
    glClearColor(1, 1, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glBindTexture(GL_TEXTURE_2D, m_texture);
    glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].Position.x);
    glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].TexCoord.x);
    glDrawArrays(GL_TRIANGLES, 0, vertices.size());

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

/*since all the vertices are only for bullets at this
point, all we need to do is move each vertex up by the
same amount*/
void RenderingEngine::UpdateAnimation(float timeStep) {
    for (int i = 0; i < vertices.size(); ++i) {
        vertices[i].Position.y += timeStep*4.4f;
    }
}

/*I'm using GL_TRIANGLES, so when the user fingers
down I am pushing two triangles(six vertices) onto
the vertices vector. This only happens when the user
taps on the bottom 60 pixels of the screen.*/
void RenderingEngine::OnFingerDown(ivec2 location) {
    if (location.y >= 420) {
        vertices.push_back(Vertex(vec2(location.x*0.0125f - 2.0f - 32*0.0125f,
                                       -2.4f + 32*0.0125f), vec2(0, 1)));

        vertices.push_back(Vertex(vec2(location.x*0.0125f - 2.0f - 32*0.0125f,
                                       -2.4f - 32*0.0125f), vec2(0, 0)));

        vertices.push_back(Vertex(vec2(location.x*0.0125f - 2.0f + 32*0.0125f,
                                       -2.4f + 32*0.0125f), vec2(1, 1)));

        vertices.push_back(Vertex(vec2(location.x*0.0125f - 2.0f - 32*0.0125f,
                                       -2.4f - 32*0.0125f), vec2(0, 0)));

        vertices.push_back(Vertex(vec2(location.x*0.0125f - 2.0f + 32*0.0125f,
                                       -2.4f + 32*0.0125f), vec2(1, 1)));

        vertices.push_back(Vertex(vec2(location.x*0.0125f - 2.0f + 32*0.0125f,
                                       -2.4f - 32*0.0125f), vec2(1, 0)));
}
}

顶点看起来像这样:

struct Vertex {
    Vertex() {}
    Vertex(vec2 position, vec2 texCoord) : Position(position), TexCoord(texCoord) {}
    vec2 Position;
    vec2 TexCoord;
};

这是简化的ivec2和vec2:

template <typename T>
struct Vector2 {
    Vector2() {}
    Vector2(T x, T y) : x(x), y(y) {}
}

typedef Vector2<int> ivec2;
typedef Vector2<float> vec2;

因此,以上所有内容对我来说都是有效的,并且当用户点击屏幕底部时,我会从屏幕底部向顶部发射子弹。 我想做的就是将其添加到渲染引擎中:

std::vector<HeroBullet> heroBullets;

然后我想将更新动画方法更改为:

void RenderingEngine1::UpdateAnimation(float timeStep) {
    for (int i = 0; i < heroBullets.size(); ++i) {
       heroBullets[i].UpdateAnimation(timeStep);
    }
}

我认为这是个好主意,因为我不希望在此单个UpdateAnimation()方法中更新和管理所有原始顶点数据。 但是我更希望这个UpdateAnimation()方法在我的每个对象上调用UpdateAnimation()(现在它只是英雄的子弹,但将来会是敌人,敌人的子弹等。)

因此,我的主要问题是我应该如何实例化HeroBullet对象,以便那些实例能够访问和修改其在渲染引擎顶点向量中的顶点数据?

而且,我正在这样做吗? 这似乎是游戏需要的常见设置。

看起来不错,我个人会为所有那些硬编码的浮点数添加一些变量,这样可以使代码更整洁,并且以后更容易调整(如果要针对不同的显示尺寸等,就可以缩放)。

至于更新,据我所知,您将通过与原始数据分开进行所有操作来增加额外的开销,但是这可能是无法避免的,在这种情况下,您最好将项目符号子类化为实现子类的各种子类。各种差异,所有这些差异都继承一个通用的draw()函数或重写getVertices()函数(以及可能的getTexVertices())以允许在那里进行不同的实现。 (这意味着在其各自的子类中而不是在渲染引擎中具有各个顶点,如果IMHO以任何方式对其进行独立更新,则IMHO将会更清洁)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM