1
votes

I checked this question.

gldrawarrays does not draw anything => not applicable because I am using shaders.

My code goes as follows:

GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

This initialises a vao first.

GLuint vertexBuffer;
glGenBuffers(1,&vertexBuffer);


float vertices[]={
  -0.5f,-0.5f,
  0.0f,0.0f,
  0.5f,0.5f
};

glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);    

Binds the buffer and adds the data

const char* vertexSource = reader("./shaders/chapter-one.vert").c_str();
GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vShader, 1, &vertexSource, NULL);
glCompileShader(vShader);
GLint vStatus;
glGetShaderiv(vShader, GL_COMPILE_STATUS, &vStatus);
if(vStatus!=GL_TRUE){
  printf("Vertex Shader not Compiled\n");
}

char vBuffer[512];
glGetShaderInfoLog(vShader, 512, NULL, vBuffer);
cout<<vBuffer;


const char* fragmentSource = reader("./shaders/chapter-one.frag").c_str();
GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fShader, 1, &fragmentSource, NULL);
glCompileShader(fShader);
GLint fStatus;
glGetShaderiv(fShader, GL_COMPILE_STATUS, &fStatus);
if(fStatus!=GL_TRUE){
  printf("Fragment Shader not compiled");
}

char fBuffer[512];
glGetShaderInfoLog(fShader, 512, NULL, fBuffer);
cout << fBuffer;

GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vShader);
glAttachShader(shaderProgram, fShader);
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);

Adds the shader and creates a program. I also retrieve errors and logs for each step which are empty currently. So no errors compiling or creating shaders and program.

GLuint posLoc = glGetAttribLocation(shaderProgram, "p");
printf("%i\n", posLoc);
glVertexAttribPointer(posLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(posLoc);

Gets the attribute from shader to feed the data to.

glDrawArrays(GL_LINES, 0, 3); // => This part does not work

Upon calling glGetError() after glDrawArrays I get 1282 error. I know glDrawArrays is causing this error as just before glDrawArrays I tried calling glGetErrors() and there wer no errors. I think the problem is in setup somewhere

Any Suggestions??

Edit: I tried changing my code according to Rabbid76's suggestion and I am still getting the same error. I checked the documentation and I think this is the applicable case.

GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to an enabled array and the buffer object's data store is currently mapped.

What does it mean in this situation and what changes should I make?

Full code here

Yes, I got took both my code and example code side by side and changed anything that seemed inconsistent. I changed both glDrawElements and had to adjust my glVertexAttribPointer. Stupid mistake. Thank you.

1

1 Answers

2
votes

There is no glDrawArrays in your code. There is just a glDrawElements and that will cause an error, because you have not specified any Index buffer.
Furthermore, the type argument to glDrawElements has to be the type of the indices. The allowed types are GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT.

Remove glDrawElements and use glDrawArrays to solve the issue:

glDrawElements(GL_TRIANGLES, 3, GL_FLOAT, 0);

glDrawArrays(GL_TRIANGLES, 0, 3);

Furthermore the Vertex Buffer Object and the Vertex Array Object have to be bound, before invoking glVertexAttribPointer. glVertexAttribPointer associates the buffer which is currently bound to the target GL_ARRAY_BUFFER to the specified attribute (index) and states the specification in the state vector of the Vertex Array Object.

glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); 
glVertexAttribPointer(posLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);