When I make 2 or more instance of TestLine class and add it in the render class, only the last one is rendered. The previous objects doesn't render or disappear
TestLine class:
void update(){
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_VERTEX_ARRAY, xbo);
glVertexPointer(2, GL_FLOAT, 0, 0);
glDrawArrays(GL_LINES, 0, verts.size());
glDisableClientState(GL_VERTEX_ARRAY);
}
void set(float x, float y, float x2, float y2){
verts.push_back(x);
verts.push_back(y);
verts.push_back(x2);
verts.push_back(y2);
glGenBuffers(1, &xbo);
glBindBuffer(GL_ARRAY_BUFFER, xbo);
glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(GLfloat), &verts[0], GL_STATIC_DRAW);
}
functions in render class
void draw(){
int size = lines.size();
for(int i = 0; i < size; i++)
lines[i]->update();
}
void add(TestLine* d){
lines.push_back(d);
}
glBindBuffer(GL_VERTEX_ARRAY, xbo);
GL_VERTEX_ARRAY is not a valid argument for glBindBuffer():
target: Specifies the target to which the buffer object is bound. The symbolic constant must be GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER, GL_PIXEL_PACK_BUFFER, or GL_PIXEL_UNPACK_BUFFER.
Use GL_ARRAY_BUFFER like in your other invocation.
glDrawArrays(GL_LINES, 0, verts.size());
set() only specifies two vertices, not four. If verts contained some sort of multi-float struct (e.g.: struct Vertex { float x, y; };) instead of raw floats this would have worked.
Try this:
glDrawArrays(GL_LINES, 0, verts.size() / 2);
Related
I have a sprite class to handle initializing a sprite and drawing it but i keep getting this error Exception thrown at 0x68ECC760 (nvoglv32.dll) this seems to be mainly because of _vboID and _vertNum I'm not exactly sure what is wrong with them though, here's the source code for it
void Sprite::init(int vertNum, float r, Shape shape) {
_vertNum = vertNum;
if (_vboID = 0) {
glGenBuffers(1, &_vboID);
}
std::vector<Vertex> vertexData(vertNum);
if (shape == Shape::CIRCLE) {
for (int i = 0; i < vertNum; i++) {
vertexData[i].setPosition(r*cos(i), r*sin(i));
}
}
for (int i = 0; i < _vertNum; i++) {
vertexData[i].setColor(1, 1, 1, 1);
}
glBindBuffer(GL_VERTEX_ARRAY, _vboID);
glBufferData(GL_VERTEX_ARRAY, sizeof(vertexData), vertexData.data(), GL_DYNAMIC_DRAW);
glBindBuffer(GL_VERTEX_ARRAY, 0);
}
void Sprite::draw() {
//bind buffer array
glBindBuffer(GL_VERTEX_ARRAY, _vboID);
//use first vertex attrib array
glEnableVertexAttribArray(0);
//pointer to vertices location
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
//pointer to vertices color
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color));
//Draw the 6 vertices to the screen
glDrawArrays(GL_POLYGON, 0, _vertNum);
//Disable the vertex attrib array. This is not optional.
glDisableVertexAttribArray(0);
//Unbind the VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
GL_VERTEX_ARRAY is not a valid buffer binding target. What you are looking for is GL_ARRAY_BUFFER.
When you use GL_VERTEX_ARRAY in contexts where a buffer binding target is expected, you should only get a GL_INVALID_ENUM error, and the calls will have no further effect. As a result, you have no GL_ARRAY_BUFFER bound during your glVertexAttribPointer call, which in a legacy or core profile will result the GL in interpreting your pointers as pointers to client memory.
You should really add some error checks, or use a debug callback, so that such simple mistakes can be spotted early.
I've got a problem with Vertex Buffer Object, it seems it doesn't work properly.
It doesn't show anything on screen.
Here is my Code:
void gl::glRecti(int x,int y,int w,int h,glColor *color)
{
GLuint vbo = 0;
GLfloat verts[] =
{
x,y,
x,y + h,
x + w,y + h,
x + w,y,
};
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glVertexPointer( 4 , GL_FLOAT , sizeof(float) * 8, NULL );
glDrawArrays(GL_QUADS,0,4);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
}
PS: I'm very new in OpenGL programming. Any help would be appreciated.
Your vertex pointer does not make sense:
glVertexPointer( 4 , GL_FLOAT , sizeof(float) * 8, NULL );
You are telling the GL that each vertex position is specified as a 4-dimensional vector, and that the offset between two consecutive vertices is 8 floats.
What you supply is a tighlty packed array of 2-dimensional positions, so you should use 2 as the size parameter, and 2*sizeof(float) for the stride (or 0, which is a shortcut for thigly packed arrays).
My problem is the glDrawArray command stops working. I've written a few programs which I used that command. However now, I don't have any idea why it doesn't work.
But I still can draw lines to the screen with GL_LINES instead of GL_TRIANGLES.
Here is my code:
#include "Mesh.h"
#include <iostream>
Mesh::Mesh()
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
size = 0;
}
void Mesh::AddVertices(const Vertex vertices[], int length)
{
size = length;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}
void Mesh::Draw()
{
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, Vertex::SIZE * 4, 0);
glDrawArrays(GL_TRIANGLES, 0, size);
glDisableVertexAttribArray(0);
}
Draw is always called by Game class. And my Vertex class:
#include "Vertex.h"
Vertex::Vertex(glm::vec3 pos)
{
this->pos = pos;
}
Vertex::Vertex(float x, float y, float z)
{
pos = glm::vec3(x, y, z);
}
glm::vec3 Vertex::GetPos() const
{
return pos;
}
void Vertex::SetPos(glm::vec3 pos)
{
this->pos = pos;
}
sizeof(vertices) is sizeof(void*), because in C or C++ arrays in function arguments decays to pointers. You should use length provided in second argument, if it is size in bytes.
I've found the solution. When I initialize my OpenGL functions, I have accidentally written glFrontFace(GL_CW) instead of glFrontFace(GL_CCW).
I am trying to draw part of my tile image but I am getting GL_INVALID_VALUE error when I call glDrawElements function. There is no problem when I change this function with glDrawArrays. The problem is that the indices count parameter is not negative number.
There is a code:
#define BUFFER_OFFSET(i) ((char *)nullptr + (i))
#define VERTEX_ATTR_PTR(loc, count, member, type) \
glEnableVertexAttribArray(loc); \
glVertexAttribPointer(loc, count, GL_FLOAT, GL_FALSE, sizeof(type), BUFFER_OFFSET(offsetof(struct type, member)))
// ---------- TextRenderer
void TextRenderer::setText(const string& text) {
vector<Vertex2f> vertex_buffer;
vector<GLuint> index_buffer;
GLfloat cursor = 0.f;
FPoint2D cell_size = font->getCellSize();
for (char c : text) {
TILE_ITER iter = font->getCharacter(c);
{
// UV
for (GLuint i = 0; i < 4; ++i) {
TILE_ITER _v = iter + i;
vertex_buffer.push_back( {
{
_v->pos[0] + cursor,
_v->pos[1],
_v->pos[2]
},
{ _v->uv[0], _v->uv[1] }
});
}
// Index
for (GLuint i = 0; i < 6; ++i)
index_buffer.push_back(
Tile::indices[i] + vertex_buffer.size() - 4);
}
cursor += cell_size.X;
}
vertices_count = vertex_buffer.size();
indices_count = index_buffer.size();
glBindVertexArray(vao);
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER,
0,
indices_count * sizeof(GLuint),
&index_buffer[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferSubData(GL_ARRAY_BUFFER,
0,
vertices_count * sizeof(Vertex2f),
&vertex_buffer[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
glBindVertexArray(0);
}
void TextRenderer::create() {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
{
indices = genGLBuffer( {
nullptr,
BUFFER_SIZE / 2 * sizeof(GLuint),
GL_ELEMENT_ARRAY_BUFFER
}, true, GL_DYNAMIC_DRAW);
vbo = genGLBuffer( {
nullptr,
BUFFER_SIZE * sizeof(Vertex2f),
GL_ARRAY_BUFFER
}, true, GL_DYNAMIC_DRAW);
VERTEX_ATTR_PTR(0, 3, pos, Vertex2f); // Vertex
VERTEX_ATTR_PTR(1, 2, uv, Vertex2f); // UV
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
setText("DUPA");
}
void TextRenderer::draw(MatrixStack& matrix, GLint) {
static Shader shader(
getFileContents("shaders/text_frag.glsl"),
getFileContents("shaders/text_vert.glsl"),
"");
shader.begin();
shader.setUniform(GL_TEXTURE_2D, "texture", 0,
font->getHandle());
shader.setUniform("matrix.mvp", matrix.vp_matrix * matrix.model);
shader.setUniform("col", col);
{
glBindVertexArray(vao);
//glDrawArrays(GL_LINE_STRIP, 0, vertices_count);
glDrawElements(GL_LINES, indices_count, GL_UNSIGNED_INT,
nullptr);
glBindVertexArray(0);
showGLErrors();
}
shader.end();
}
The problem is with the following (shortened) call sequence in your setText() method:
glBindVertexArray(vao);
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, ...);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
...
}
glBindVertexArray(0);
The binding of the GL_ELEMENT_ARRAY_BUFFER is part of the VAO state. So by making this call while the VAO is bound:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
you're setting the VAO state to have an element array buffer binding of 0. So when you later bind the VAO in your draw() method, you won't have a binding for GL_ELEMENT_ARRAY_BUFFER.
To avoid this, the simplest solution is to just remove that call. If you want to explicitly unbind it because you're worried that having it bound might have undesired side effects on other code, you need to move it after unbinding the VAO:
glBindVertexArray(vao);
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, ...);
...
}
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Without seeing the whole code and knowing exact GL version, I will attempt to give you a correct answer.
First, if you're using ES2 then using index type as GL_UNSIGNED_INT is not supported by default, however I don't think that's your problem.
Your actual issue is that element arrays are actually not stored in your VAO object, only vertex data configuration. Therefore glDrawElements will give you this error as it will think no element array is bound and you passed NULL as indices argument to the function.
To solve this, bind the appropriate element array before you call glDrawElements
in my project I want to display many objects (spheres) using vbo.
I manage to display 1 object without a problem ,but when it comes to 2 or more, all objects(vbos) are replaced by the last defined object(vbo).
CosmicBody(int x)
{
this->verticesSize=0;
//this->VaoId=x;
//this->VaoId=1;
this->VboId=x;
};
void CosmicBody::InitShape(unsigned int uiStacks, unsigned int uiSlices, float fA, float fB, float fC)
{
float tStep = (Pi) / (float)uiSlices;
float sStep = (Pi) / (float)uiStacks;
float SlicesCount=(Pi+0.0001)/tStep;
float StackCount=(2*Pi+0.0001)/sStep;
this->verticesSize=((int) (SlicesCount+1) * (int) (StackCount+1))*2;
glm::vec4 *vertices=NULL;
vertices=new glm::vec4[verticesSize];
int count=0;
for(float t = -Pi/2; t <= (Pi/2)+.0001; t += tStep)
{
for(float s = -Pi; s <= Pi+.0001; s += sStep)
{
vertices[count++]=glm::vec4(fA * cos(t) * cos(s),fB * cos(t) * sin(s),fC * sin(t),1.0f);
vertices[count++]=glm::vec4(fA * cos(t+tStep) * cos(s),fB * cos(t+tStep) * sin(s),fC * sin(t+tStep),1.0f);
}
}
glGenBuffers(1, &VboId);
glBindBuffer(GL_ARRAY_BUFFER, VboId);
glBufferData(GL_ARRAY_BUFFER, 16*verticesSize, vertices, GL_STATIC_DRAW);
glGenVertexArrays(1, &VaoId);
glBindVertexArray(VaoId);
glBindBuffer(GL_ARRAY_BUFFER, VboId);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
delete[] vertices;
}
void CosmicBody::Draw()
{
glBindBuffer(GL_ARRAY_BUFFER, this->VboId);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLE_STRIP, 0,this->verticesSize); //when I replace this->verticesSize with number of vertices of last object ,instead of getting x different objects I get same instances of the last one.
glDisableVertexAttribArray(0);
}
When you use VAO's you should bind VAO for drawing, not the VBO buffer:
void CosmicBody::Draw()
{
glBindVertexArray( this->VaoId ); // <-- this is the difference
glDrawArrays(GL_TRIANGLE_STRIP, 0,this->verticesSize);
glBindVertexArray(0); // unbind the VAO
}
And move the glEnableVertexAttribArray(0); in the CosmicBody::InitShape after you bind VAO, no need to enable/disable it every time you draw:
...
glGenVertexArrays(1, &VaoId);
glBindVertexArray(VaoId);
glEnableVertexAttribArray(0); // <-- here
glBindBuffer(GL_ARRAY_BUFFER, VboId);
...