0

I have a problem when creating the vertex array and the indices array. I don't know what really is the problem with the code, but I guess is something with the type of the arrays, can someone please give me a light on this?

#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;  
}

Edit:

The problem here is that drawing don't work at all. But I don't get any error, this just don't display what I want to display. Even if I put the code that make the vertices and put them in the buffers in a diferent function, this don't work. I just tried to do this:

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();
}

and I placed the rest of the code in display in another function that is called on the start of the program. But the problem still

edit2:

Found the problem in glBufferData(GL_ARRAY_BUFFER,sizeof(squareVerts),squareVerts,GL_STATIC_DRAW);

sizeof(squareVerts) should be sizeof(GLfloat)*x, same to indices.

user1905910
  • 162
  • 1
  • 13
  • Are you getting a specific error ? Can you describe more precisely what the problem is ? – Étienne Dec 15 '12 at 10:49
  • Display() function might not be the best place to manipulate vertex data in case you want frame rates to work. the data is supposed to be constant in display() function. – tp1 Dec 15 '12 at 13:34
  • Still trying to find a solution for this. This only works when I don't generate the data in the array. If I just put the data manually, works. But I need a function that generate the arrays in execution, anyone could help me???? – user1905910 Dec 15 '12 at 18:34

0 Answers0