0

创建顶点数组和索引数组时出现问题。我不知道代码的真正问题是什么,但我想是数组类型的问题,有人可以告诉我吗?

#define GL_GLEXT_PROTOTYPES
#include<GL/glut.h>
#include<iostream>
using namespace std;

#define BUFFER_OFFSET(offset) ((GLfloat*) NULL + offset)

const GLuint numDiv = 2;
const GLuint numVerts = 9;
GLuint VAO;

void display(void) {  

  enum vertex {VERTICES, INDICES, NUM_BUFFERS};

  GLuint * buffers = new GLuint[NUM_BUFFERS];

  GLfloat (*squareVerts)[2] = new GLfloat[numVerts][2];
  GLubyte * indices = new GLubyte[numDiv*numDiv*4];

  GLuint delta = 80/numDiv;
  for(GLuint i = 0; i < numVerts; i++)
  {
  squareVerts[i][1] = (i/(numDiv+1))*delta;
  squareVerts[i][0] = (i%(numDiv+1))*delta;
  }

  for(GLuint i=0; i < numDiv; i++){
  for(GLuint j=0; j < numDiv; j++){
      //cada iteracao gera 4 pontos
      #define NUM_VERT(ii,jj) ((ii)*(numDiv+1)+(jj))
      #define INDICE(ii,jj) (4*((ii)*numDiv+(jj)))
      indices[INDICE(i,j)]   = NUM_VERT(i,j);
      indices[INDICE(i,j)+1] = NUM_VERT(i,j+1);
      indices[INDICE(i,j)+2] = NUM_VERT(i+1,j+1);
      indices[INDICE(i,j)+3] = NUM_VERT(i+1,j);
  }
  }

  glGenVertexArrays(1, &VAO);
  glBindVertexArray(VAO);

  glGenBuffers(NUM_BUFFERS, buffers);

  glBindBuffer(GL_ARRAY_BUFFER, buffers[VERTICES]);
  glBufferData(GL_ARRAY_BUFFER, sizeof(squareVerts),
  squareVerts, GL_STATIC_DRAW);
  glVertexPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));
  glEnableClientState(GL_VERTEX_ARRAY);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDICES]);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),
      indices, GL_STATIC_DRAW);

  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f(1.0,1.0,1.0);
  glDrawElements(GL_POINTS, 16, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
  glutSwapBuffers();
}

void init() 
{
  glClearColor(0.0, 0.0, 0.0, 0.0);
  gluOrtho2D((GLdouble) -1.0, (GLdouble) 90.0, (GLdouble) -1.0, (GLdouble) 90.0);  
}

int main(int argv, char** argc) {

  glutInit(&argv, argc);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  glutInitWindowSize(500,500);
  glutInitWindowPosition(100,100); 
  glutCreateWindow("myCode.cpp");
  init();
  glutDisplayFunc(display);
  glutMainLoop();

  return 0;  
}

编辑:

这里的问题是绘图根本不起作用。但我没有收到任何错误,这只是不显示我想要显示的内容。即使我将制作顶点的代码放入缓冲区中的不同函数中,这也不起作用。我只是尝试这样做:

void display(void) {  

      glBindVertexArray(VAO);  
      glClear(GL_COLOR_BUFFER_BIT);
      glColor3f(1.0,1.0,1.0);
      glDrawElements(GL_POINTS, 16, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
      glutSwapBuffers();
}

我将其余的代码显示在程序启动时调用的另一个函数中。但问题依旧

编辑2:

在 glBufferData(GL_ARRAY_BUFFER,sizeof(squareVerts),squareVerts,GL_STATIC_DRAW);

sizeof(squareVerts) 应该是 sizeof(GLfloat)*x,与索引相同。

4

0 回答 0