[英]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.