使用OpenGL绘制点阵列

时间:2012-02-09 15:21:58

标签: c++ opengl

为什么在这个程序中glVertex3f绘制的四个点在尖头位置渲染,而不是四个阵列点只在窗口中心渲染一个?

#include <GL/glew.h>
#include <GL/glut.h>

#include <iostream>
#include <vector>

using namespace std;

struct point3 {
    GLfloat x, y, z;
};

vector<point3> Vertices(4);

void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT);
    GLuint abuffer;
    glGenVertexArrays(1, &abuffer);
    glBindVertexArray(abuffer);
    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    Vertices = {{0.3f, 0.3f, 0.5f}, {0.6f, 0.6f, 0.5f}, {0.6f, 0.3f, 0.5f}, {0.3f, 0.6f, 0.5f}};
    glBufferData(GL_ARRAY_BUFFER, Vertices.size() * sizeof(Vertices[0]), &Vertices[0], GL_STATIC_DRAW);
    glVertexPointer(3, GL_FLOAT, 0, &Vertices[0]);
    glDrawArrays(GL_POINTS, 0, Vertices.size());
    glFlush();
    glBegin(GL_POINTS);
    glVertex3f(0.25f, 0.25f, 0.5f);
    glVertex3f(0.25f, 0.75f, 0.5f);
    glVertex3f(0.75f, 0.75f, 0.5f);
    glVertex3f(0.75f, 0.25f, 0.5f);
    glEnd();    
    glFlush();
} 

void init (void)
{
    glClearColor (0.0, 0.0, 0.0, 0.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

} 

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("simple OpenGL example");
    glutDisplayFunc(display);
    GLenum err = glewInit();
    if (GLEW_OK != err) {
        cerr << "Error: " << glewGetErrorString(err) << endl;
    }
    cout << "Status: Using GLEW" << glewGetString(GLEW_VERSION) << endl;    
    init();
    glutMainLoop();
}

1 个答案:

答案 0 :(得分:2)

您尚未启用该属性以实际使用该数组。你这样做:

  • (OpenGL 2)glEnableClientState(GL_VERTEX_ARRAY)(或GL_COLOR_ARRAY等)
  • (OpenGL 3)glEnableVertexArray(attributeId)

如果不这样做,您实际上不会使用glVertexPointer(GL2)或glVertexAttribPointer(GL3)指向的内容。