我能够使用顶点数组绘制单个对象。但是,在绘制多个对象时,它给我带来了困难。
我有一个这样的数据数组,三个顶点渲染一个三角形:
-0.0570917f,0.490469f,-0.142433f
-0.0581126f,0.5f,-0.160615f
-0.0651903f,0.490827f,-0.147409f
-0.0501295f,0.499886f,-0.158146f
-0.0491703f,0.49039f,-0.140236f
-0.0422027f,0.499864f,-0.157942f
-0.041255f,0.49031f,-0.138206f
-0.0333885f,0.490314f,-0.137995f
-0.0967512f,0.48302f,-0.150394f
...
我正在尝试使用以下代码渲染多个三角形:
// this array contains arrays with one vertex (three elements)
vector<vector<GLfloat>> pointVertex;
// Render OpenGL here
for (int i = 0; i < pointVertex.size(); i++) {
vector<GLfloat> temp = pointVertex[i];
GLfloat *pointVertexArrayPoint = temp.data();
glEnable(GL_POINT_SMOOTH);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, pointVertexArrayPoint);
glDrawArrays(GL_LINES, 0, temp.size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_POINT_SMOOTH);
}
现在,我得到了一个结果,但一切都从同一点开始。我希望它呈现不同的多个独立三角形。我怎样才能改变它?
答案 0 :(得分:3)
有几种方法可以做到这一点,而不会偏离你正在使用的技术(传统管道)..
下面的代码将绘制两个三角形..就像这样:
每个三角形呈现每个三角形1次绘制调用:
void onDisplayRefreshSO(void* ctx)
{
//Setup OpenGL once..
static std::once_flag flag;
std::call_once(flag, []{
GLfloat view_port[4] = {0};
glGetFloatv(GL_VIEWPORT, &view_port[0]);
GLfloat screen_width = view_port[2] - view_port[0];
GLfloat screen_height = view_port[3] - view_port[1];
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0, screen_width / screen_height, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
});
/** Setup Drawing Triangles **/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
GLfloat normals[] = {
0, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 1, 0, 0, 1, 0, 0, 1
};
GLfloat colors[] = {
1, 1, 1, 1, 1, 0, 1, 0, 0,
0, 1, 1, 0, 1, 0, 0, 0, 1
};
GLfloat vertices[] = {
1, 1, 1, -1, 1, 1, -1,-1, 1,
1, 1, 1, 1, -1, 1, -1,-1, 1
};
//9 floats per triangle.. 3 * (X, Y, Z)..
for (int i = 0; i < sizeof(vertices) / sizeof(vertices[0]) / 9; ++i)
{
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT, 0, normals + (i * 9));
glColorPointer(3, GL_FLOAT, 0, colors + (i * 9));
glVertexPointer(3, GL_FLOAT, 0, vertices + (i * 9));
glPushMatrix();
glTranslatef(-1.0f, 0.0f, -6.0f);
glDrawArrays(GL_TRIANGLES, 0, 3); //3 vertices (X, Y, Z) per triangle..
glPopMatrix();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
}
在单个绘图调用中呈现的所有三角形:
void onDisplayRefreshSO2(void* ctx)
{
//Setup OpenGL once..
static std::once_flag flag;
std::call_once(flag, []{
GLfloat view_port[4] = {0};
glGetFloatv(GL_VIEWPORT, &view_port[0]);
GLfloat screen_width = view_port[2] - view_port[0];
GLfloat screen_height = view_port[3] - view_port[1];
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0, screen_width / screen_height, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
});
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
/** Setup Drawing Triangles **/
GLfloat normals[] = {
0, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 1, 0, 0, 1, 0, 0, 1
};
GLfloat colors[] = {
1, 1, 1, 1, 1, 0, 1, 0, 0,
0, 1, 1, 0, 1, 0, 0, 0, 1
};
GLfloat vertices[] = {
1, 1, 1, -1, 1, 1, -1,-1, 1,
1, 1, 1, 1, -1, 1, -1,-1, 1
};
//Draw both triangles in a single call..
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT, 0, normals);
glColorPointer(3, GL_FLOAT, 0, colors);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glPushMatrix();
glTranslatef(-1.0f, 0.0f, -6.0f);
glDrawArrays(GL_TRIANGLES, 0, (sizeof(vertices) / sizeof(vertices[0])) / 3); //6 vertices (X, Y, Z) for 2 triangles..
glPopMatrix();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
答案 1 :(得分:-1)
好像你需要添加索引缓冲区。请参阅示例:http://www.opengl-tutorial.org/ru/intermediate-tutorials/tutorial-9-vbo-indexing/
管道: