my problem is not properly drawing simple plane;
I dont know why i get once error at begining : Invalid Operation 1282.
Can someone check that?
My variables :
GLfloat *tab for verticies plane
int *tab2 for indecies
this is my init funcion:
// kolor tła - zawartość bufora koloru
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
// wczytanie shaderów i przygotowanie obsługi programu
program=LoadShaders("shader.vert","shader.frag" );
//Vertexy
//VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
//Verticies;
glGenBuffers(1, &Verticies);
glBindBuffer(GL_ARRAY_BUFFER, Verticies);
glBufferData( GL_ARRAY_BUFFER,foo*sizeof(GLfloat), tab, GL_STATIC_DRAW );
unsigned int m_ciezarkow[]={0,1,0};
//Indicies
glGenBuffers(1,&Indeksy);
glBindBuffer(GL_ARRAY_BUFFER,Indeksy);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*size, tab2, GL_STATIC_DRAW);
glUseProgram( program );
pos_cam=glGetUniformLocation( program, "camera" );
pos_color=glGetUniformLocation( program, "_color" );
glUseProgram(0);
This is my draw function:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
//my camera
glUniformMatrix4fv(pos_cam,1,GL_FALSE,&cam.matrix()[0][0]);
//Verticies
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, Verticies);
glVertexAttribPointer( 0, 3 , GL_FLOAT, GL_FALSE, 0, (void*)0 );
//Masa
//glEnableVertexAttribArray(1);
//glBindBuffer(GL_ARRAY_BUFFER, Masa);
//glVertexAttribPointer( 0, 3 , GL_FLOAT, GL_FALSE, 0, (void*)0 );
//Indicies
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,Indeksy);
glDrawElements(GL_TRIANGLES,size,GL_UNSIGNED_INT,(void*)0);
glUseProgram( 0 );
glDisableVertexAttribArray(0);`
There is a mistake here:
//Indicies
glGenBuffers(1,&Indeksy);
glBindBuffer(GL_ARRAY_BUFFER,Indeksy);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*size, tab2, GL_STATIC_DRAW);
You bind Indeksy as GL_ARRAY_BUFFER, but it should be GL_ELEMENT_ARRAY_BUFFER. With your code, there is probably 0 bound as index buffer, and the glBufferData for that target will fail with GL_INVALID_OPERATION.
Related
I am trying to display 2 simple triangles in OpenGL, but they don't appear, despite checking numerous sites. Did I forget anything in the code ?
#define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + (bytes))
// Prepare:
GLfloat vertices[]={0,0,1,
0.5,0,-0.5,
0,0,0,
0,0,1,
0,0,0,
-0.5,0,-0.5};
GLushort indices[]={0,1,2,3,4,5};
GLfloat *generatedVertices=new GLfloat[18];
GLushort *generatedIndices=new GLushort[6];
GLfloat *colors=new GLfloat[18];
glGenBuffers( 3, triangleBuffers ); // triangleBuffers = Global GLuint
for (int p=0;p<6;p++)
{
MVector V(vertices[p*3],vertices[p*3+1],vertices[p*3+2]); // Maya type
generatedVertices[p*3]=V.x;
generatedVertices[p*3+1]=V.y;
generatedVertices[p*3+2]=V.z;
generatedIndices[p]=p;
colors[p*3]=0.8f;
colors[p*3+1]=0.6f;
colors[p*3+2]=0.0f;
}
// vertices
glBindBuffer(GL_ARRAY_BUFFER, data->triangleBuffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*18, generatedVertices, GL_STATIC_DRAW);
// indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data->triangleBuffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*6, generatedIndices, GL_STATIC_DRAW);
// couloirs
glBindBuffer(GL_ARRAY_BUFFER, data->triangleBuffers[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*18*data->totalSize, colors, GL_STATIC_DRAW);
// Render:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, data->triangleBuffers[0]);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data->triangleBuffers[1]);
glBindBuffer(GL_ARRAY_BUFFER, data->triangleBuffers[2]);
glColorPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glDrawElements(GL_TRIANGLES, totalSize*6, GL_UNSIGNED_INT, 0);
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
Issue is coming from maya. In OpenGL-Legacy mode, the mesh is properly displayed, in OpenGL-Strict mode, it isn't. I will investigate in this direction.
I'm a newbie to OpenGL and I'm trying draw two triangles using two VAOs and only one VBO. Even if after some research, I came to have a better understanding of VAO, VBO and how the needed glew functions work, I have no clue why my program displays only one triangle instead of two. Can somebody help?
...
GLfloat points[] = {
0.5f, 0.5f, 0.0f, //First Triangle
-0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.0f, 0.0f, //Second Triangle
-1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f
};
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLuint vao1;
glGenVertexArrays(1, &vao1);
glBindVertexArray(vao1);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
GLuint vao2;
glGenVertexArrays(1, &vao2);
glBindVertexArray(vao2);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL + 9);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
...
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader_program);
glBindVertexArray(vao1);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glBindVertexArray(vao2);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glUseProgram(0);
glfwPollEvents();
glfwSwapBuffers(window);
}
...
The last parameter for this function call is incorrect:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL + 9);
You're telling it to add 9 bytes, but your points are floats
Try this:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL + 9 * sizeof(float));
I know in 2.0- openGL we can draw a line simply like this.
glBegin(GL_LINES);
glVertex3f(20.0f,150.0f,0.0f);
glVertex3f(220.0f,150.0f,0.0f);
glVertex3f(200.0f,160.0f,0.0f);
glVertex3f(200.0f,160.0f,0.0f);
glEnd();
but how to do similar thing in modern openGL(3.0+)
I have read Drawing round points using modern OpenGL but the answer is not about certain point,since I want to draw polygon with points have certain coordinates,it's not quite helpful.
I use this code,but it shows nothing except a blue background.what do I missed?
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
static const GLfloat g_vertex_buffer_data[] = {
20.0f, 150.0f, 0.0f, 1.0f,
220.0f, 150.0f, 0.0f, 1.0f,
200.0f, 160.0f, 0.0f, 1.0f
};
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
do{
// Clear the screen
glClear( GL_COLOR_BUFFER_BIT );
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
4, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_LINES, 0, 2); // 3 indices starting at 0 -> 1 triangle
glDisableVertexAttribArray(0);
// Swap buffers
glfwSwapBuffers(window);
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
1) You have to define an array of vertices, that contain the points of your polygon lines. Like in your example:
GLfloat vertices[] =
{
20.0f, 150.0f, 0.0f, 1.0f,
220.0f, 150.0f, 0.0f, 1.0f,
200.0f, 160.0f, 0.0f, 1.0f
};
2) You have to define and bind a Vertex Buffer Object (VBO) to be able to pass your vertices to the vertex shader. Like this:
// This is the identifier for your vertex buffer
GLuint vbo;
// This creates our identifier and puts it in vbo
glGenBuffers(1, &vbo);
// This binds our vbo
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// This hands the vertices into the vbo and to the rendering pipeline
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
3) Now we are ready to draw. Doing this:
// "Enable a port" to the shader pipeline
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// pass information about how vertex array is composed
glVertexAttribPointer(0, // same as in glEnableVertexAttribArray(0)
4, // # of coordinates that build a vertex
GL_FLOAT, // data type
GL_FALSE, // normalized?
0, // stride
(void*)0);// vbo offset
glDrawArrays(GL_LINES, 0, 2);
glDisableVertexAttribArray(0);
Step 1) and 2) can be done before rendering as initialization. Step 3) is done in your rendering loop. Also you'll need a vertex shader and a fragment shader to visualize the line with color.
If you don't know anything about these things and like to start with OpenGL 3, I'd suggest to start over with a tutorial like this:
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-1-opening-a-window/
I got the problem that the first vertex is always drawn at (0,0,0) no matter where I want it to be (all other positions are correct). I think it is a mistake in my 'init' function.
Here are the positions I use:
positions.push_back( glm::vec3(-0.5f , -0.5f , 0.0f ) );
positions.push_back( glm::vec3( 0.0f , 0.5f , 0.0f ) );
positions.push_back( glm::vec3( 0.5f , -0.5f , 0.0f ) );
My 'init' function:
// generate buffers
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glGenBuffers(1, &this->EBO);
glBindVertexArray(this->VAO);
// fill buffers with data
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * this->positions.size(), &this->positions[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * this->indices.size(), &this->indices[0], GL_STATIC_DRAW);
// linking vertex attributes
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
// unbind (only) VAO
glBindVertexArray(0);
And my 'render' function (just to render the VAO, no shaders):
glBindVertexArray(this->VAO);
glDrawElements(GL_TRIANGLES, this->numElements, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
I also tried to adjust the 'stride' and 'pointer' values in the 'glVertexAttribPointer' function but it just didn't work.
Ok thanks to Anton Angelov I found the mistake... The indices of the mesh need to start from 0 (not from 1)! Thanks again for your comments
I am drawing a frame with pendulums hanging off of it, the pendulums have a texture applied but the frame has no textures. When I display both i get
But when i render only the pendulums they draw correctly and i get
I'm unsure of why this is. I blew the pendulums up and the textures seem to be mapping to the vertices from the frame. Here is the vao decleration
// Create a vertex array object
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
// Create and initialize two buffer objects
glGenBuffers( 2, buffers);
//one buffer for the vertices and colours
glBindBuffer( GL_ARRAY_BUFFER, buffers[0]);
glBufferData( GL_ARRAY_BUFFER, numFPointBytes + numVertexColourBytes,NULL, GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER, 0, numFPointBytes, fPoints );
glBufferSubData( GL_ARRAY_BUFFER, numVertexPositionBytes, numVertexColourBytes, frameVertexColours);
//one buffer for the indices
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(frameIndices),frameIndices, GL_STATIC_DRAW );
// set up vertex arrays
GLuint fVPosition = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( fVPosition );
glVertexAttribPointer( fVPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
GLuint fVColor = glGetAttribLocation( program, "vColor" );
glEnableVertexAttribArray( fVColor );
glVertexAttribPointer( fVColor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVertexPositionBytes) );
glBindVertexArray(0);
glGenVertexArrays( 2, &pVao );
glBindVertexArray( pVao );
// Create and initialize two buffer objects
glGenBuffers( 1, pBuffers);
//glBufferSubData( GL_ARRAY_BUFFER, numPVertexPositionBytes, numPVertexColourBytes, pCols);
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, pBuffers[0]);
glEnableVertexAttribArray(0);
glBufferData( GL_ELEMENT_ARRAY_BUFFER,numPVertexPositionBytes+ numPVertexColourBytes+numTexCoordBytes, NULL, GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER, 0, numPVertexPositionBytes, pendulumVertexPos );
glBufferSubData( GL_ARRAY_BUFFER, numPVertexPositionBytes, numPVertexColourBytes, pCols);
glBufferSubData( GL_ARRAY_BUFFER, numPVertexPositionBytes+numPVertexColourBytes, numTexCoordBytes, texCoords);
// set up vertex arrays
GLuint pVPosition = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( pVPosition );
glVertexAttribPointer( pVPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
GLuint pVColor = glGetAttribLocation( program, "vColor" );
glEnableVertexAttribArray( pVColor );
glVertexAttribPointer( pVColor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numPVertexPositionBytes) );
GLuint vTexCoord = glGetAttribLocation( program, "vTexCoord" );
glEnableVertexAttribArray(vTexCoord);
glVertexAttribPointer( vTexCoord, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numPVertexPositionBytes+numPVertexColourBytes) );
and the display
void
display( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
modelViewStack.loadIdentity();
modelViewStack.pushMatrix();
glDisable(GL_TEXTURE_2D);
modelViewStack.lookAt(radius*sin(theta)*cos(phi),
radius*sin(theta)*sin(phi),
radius*cos(theta),
0.0,0.0,0.0,
0.0,1.0,0.0);
modelViewStack.rotatef(rotate,0.0,1.0,0.0);
glUniformMatrix4fv(modelView, 1, GL_FALSE, modelViewStack.getMatrixf());
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, numVertexPositionBytes, frameVertexPositions );
glBufferSubData( GL_ARRAY_BUFFER, numVertexPositionBytes, numVertexColourBytes, frameVertexColours);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
//Indexing into vertices we need to use glDrawElements
glDrawElements(GL_TRIANGLES, NumFIndices, GL_UNSIGNED_BYTE, 0);
modelViewStack.popMatrix();
lineScale = 1.0/8.0;
pendulumScale = 1.0/8.0;
for(int i = 0; i<NumPendulums; i++){
if(!active[i]){
currentTheta[i] = 0.0;
}
modelViewStack.loadIdentity();
modelViewStack.pushMatrix();
glEnable(GL_TEXTURE_2D);
modelViewStack.lookAt(radius*sin(theta)*cos(phi),
radius*sin(theta)*sin(phi),
radius*cos(theta),
0.0,0.0,0.0,
0.0,1.0,0.0);
modelViewStack.scalef(pendulumScale,pendulumScale,pendulumScale);
modelViewStack.rotatef(currentTheta[i],0.0,0.0,1.0);
modelViewStack.rotatef(rotate,0.0,1.0,0.0);
modelViewStack.translatef(dhVals[i][1],dhVals[i][4],0.0);
glUniformMatrix4fv(modelView, 1, GL_FALSE, modelViewStack.getMatrixf());
glBindVertexArray(pVao);
glBindBuffer(GL_ARRAY_BUFFER, pBuffers[0]);
glDrawArrays(GL_TRIANGLES,0,NumPVertices);
glBindVertexArray(0);
glDisable(GL_TEXTURE_2D);
modelViewStack.popMatrix();
modelViewStack.loadIdentity();
modelViewStack.pushMatrix();
modelViewStack.lookAt(radius*sin(theta)*cos(phi),
radius*sin(theta)*sin(phi),
radius*cos(theta),
0.0,0.0,0.0,
0.0,1.0,0.0);
modelViewStack.rotatef(currentTheta[i],0.0,0.0,1.0);
modelViewStack.rotatef(rotate,0.0,1.0,0.0);
modelViewStack.scalef(pendulumScale,lineScale,pendulumScale);
modelViewStack.translatef(dhVals[i][1],0.0,0.0);
glUniformMatrix4fv(modelView, 1, GL_FALSE, modelViewStack.getMatrixf());
glEnableVertexAttribArray(0);
glBindVertexArray(lineVao);
glDrawArrays(GL_LINES,0,NumLVertices);
glBindVertexArray(0);
modelViewStack.popMatrix();
float temp = changeAngle(currentTheta[i], dhVals[i][5], dhVals[i][6], dhVals[i][7], i);
currentTheta[i] = temp;
lineScale+=0.09;
}
projectionStack.loadIdentity();
projectionStack.ortho(-20.0,20.0,-20.0,10.0,-20.0,20.0);
calculateLighting(fPoints,frameVertexColours,NumFSides,6);
calculateLighting(pPoints,pCols,NumPSides,6);
glUniformMatrix4fv(projection, 1, GL_FALSE, projectionStack.getMatrixf());
glutSwapBuffers();
}
Is this a problem with maintaining verteces or is it something else?
I don't see glDisableVertexAttribArray calls in your code. Which would mean, that there's still texture coordinates read from the texcoord buffer. Also you'll have to either unbind the texture or at least use a shader that doesn't read from the texture.