1

我开始学习OpenGL;我试图显示几个顶点以渲染多边形。如果我使用以下代码,我的显示功能会成功:

glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.5, 0.0, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glEnd();

但我无法使用glDrawArrays. 以下是完整的程序(在 OS X 上开发)。

#include <OpenGL/gl.h>
#include <OpenGl/glu.h>

#ifdef __APPLE__
  #include <GLUT/glut.h>
#else
  #include <GL/glut.h>
#endif

#ifdef __APPLE__
  #define glGenVertexArrays glGenVertexArraysAPPLE
  #define glBindVertexArrays glBindVertexArraysAPPLE
  #define glBindVertexArray glBindVertexArrayAPPLE
  #define glDeleteVertexArrays glDeleteVertexArraysAPPLE
#endif

/** Triangle vertices. */
GLuint handleVertex;
const float vertex[] = 
{
  0.75f, 0.75f, 0.0f, 1.0f,
  0.75f, -0.75f, 0.0f, 1.0f,
  -0.75f, -0.75f, 0.0f, 1.0f,
};

static void init_vertices()
{
  glGenBuffers(1, &handleVertex);
  glBindBuffer(GL_ARRAY_BUFFER, handleVertex);
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
  glBindBuffer(GL_ARRAY_BUFFER, 0);
}

static void display()
{
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClear(GL_COLOR_BUFFER_BIT);

  /*glBegin(GL_POLYGON);
  glVertex3f(0.0, 0.0, 0.0);
  glVertex3f(0.5, 0.0, 0.0);
  glVertex3f(0.5, 0.5, 0.0);
  glVertex3f(0.0, 0.5, 0.0);
  glEnd();*/

  glBindBuffer(GL_ARRAY_BUFFER, handleVertex);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
  glDrawArrays(GL_TRIANGLES, 0, 3);
  glDisableVertexAttribArray(0);

  glutSwapBuffers();
  glFlush();
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);

  unsigned displayMode = GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH | GLUT_STENCIL;
  glutInitDisplayMode(displayMode);
  glutInitWindowSize(500, 500); 
  glutInitWindowPosition(300, 200);
  glutCreateWindow(argv[0]);

  init_vertices();

  glutDisplayFunc(display); 
  glutMainLoop();

  return 0;
}

我想知道我做错了什么。

4

0 回答 0