Hey guys, I’m learning Opengl 3.3. So basically I would like to display three objects on the screen using a separate vbo for each object. I’m using the models of mobile, chair and table as my objects whose data is stored in files I’m reading. I’m not having any issues trying to display them on the screen one object at a time. But when I try to display them together, I’m facing issues.
void loadObject()
{
if(mode=='I')
{
initBuffersGL();
// changeObjects();
char file_name[100];
std::cout<<"Enter file to be loaded: ";
std::cin>>file_name;
FILE *fp;
int i;
float x,y,z;
float r,g,b;
/* open the file */
fp = fopen(file_name, "r");
if (fp == NULL)
{
printf("Couldn't open file for reading.
");
exit(0);
}
i=0;
tri_idx=0;
// reinitializepos();
//6 float points constituting to x,y,z,r,g,b of a vertex.
while (fscanf(fp, "%f,%f,%f,%f,%f,%f
", &x, &y, &z, &r, &g, &b) == 6)
{
v_positions[vbo_count][i]=glm::vec4(x,y,z,1.0f);
v_colors[vbo_count][i]=glm::vec4(r,g,b,1.0f);
i++;
}
fclose(fp);
tri_idx=i;
//reset the rotation and translation matrices
// recenter();
glBindBuffer (GL_ARRAY_BUFFER, vbo);
// glEnableVertexAttribArray(0);
glBufferData (GL_ARRAY_BUFFER, sizeof (v_positions[vbo_count]) + sizeof(v_colors[vbo_count]) , NULL, GL_DYNAMIC_DRAW);
glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof (v_positions[vbo_count]), v_positions[vbo_count] );
glBufferSubData( GL_ARRAY_BUFFER, sizeof (v_positions[vbo_count]), sizeof(v_colors[vbo_count]), v_colors[vbo_count] );
// glBindVertexArray (vao);
glDrawArrays(GL_TRIANGLES, 0, num_vertices);
vbo_count++;
/* open the file */
// glGenVertexArrays(1, &vao1);
// glBindVertexArray(vao1);
glGenBuffers(1,&vbo1);
glBindBuffer(GL_ARRAY_BUFFER, vbo1);
// glEnableVertexAttribArray(0);
fp = fopen("chair.raw", "r");
if (fp == NULL)
{
printf("Couldn't open file for reading.
");
exit(0);
}
i=0;
tri_idx=0;
// reinitializepos();
//6 float points constituting to x,y,z,r,g,b of a vertex.
while (fscanf(fp, "%f,%f,%f,%f,%f,%f
", &x, &y, &z, &r, &g, &b) == 6)
{
v_positions[vbo_count][i]=glm::vec4(x,y,z,1.0f);
v_colors[vbo_count][i]=glm::vec4(r,g,b,1.0f);
i++;
}
fclose(fp);
glBindBuffer (GL_ARRAY_BUFFER, vbo1);
// glEnableVertexAttribArray(0);
glBufferData (GL_ARRAY_BUFFER, sizeof (v_positions[vbo_count]) + sizeof(v_colors[vbo_count]) , NULL, GL_DYNAMIC_DRAW);
glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof (v_positions[vbo_count]), v_positions[vbo_count] );
glBufferSubData( GL_ARRAY_BUFFER, sizeof (v_positions[vbo_count]), sizeof(v_colors[vbo_count]), v_colors[vbo_count] );
glBindVertexArray (vao);
glDrawArrays(GL_TRIANGLES, 0, num_vertices);
vbo_count++;
}
Here I’m trying to load data of the objects into the separate vbos and get them displayed, but only the first object gets displayed.
Here’s my initBuffersGL:
void initBuffersGL(void)
{
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
//Copy the points into the current buffer
glBufferData (GL_ARRAY_BUFFER, sizeof (v_positions[vbo_count]) + sizeof(v_colors[vbo_count]), NULL, GL_DYNAMIC_DRAW);
glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(v_positions[vbo_count]), v_positions[vbo_count] );
glBufferSubData( GL_ARRAY_BUFFER, sizeof(v_positions[vbo_count]), sizeof(v_colors[vbo_count]), v_colors[vbo_count] );
// Load shaders and use the resulting shader program
std::string vertex_shader_file("03_vshader.glsl");
std::string fragment_shader_file("03_fshader.glsl");
std::vector<GLuint> shaderList;
shaderList.push_back(csX75::LoadShaderGL(GL_VERTEX_SHADER, vertex_shader_file));
shaderList.push_back(csX75::LoadShaderGL(GL_FRAGMENT_SHADER, fragment_shader_file));
shaderProgram = csX75::CreateProgramGL(shaderList);
glUseProgram( shaderProgram );
// set up vertex arrays
GLuint vPosition = glGetAttribLocation( shaderProgram, "vPosition" );
glEnableVertexAttribArray( vPosition );
glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
GLuint vColor = glGetAttribLocation( shaderProgram, "vColor" );
glEnableVertexAttribArray( vColor );
glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(v_positions[vbo_count])) );
uModelViewMatrix = glGetUniformLocation( shaderProgram, "uModelViewMatrix");
}
Since my first object was getting displayed just fine, I also tried an alternative approach, here:
void loadObject()
{
// Same code as previous
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, sizeof (v_positions[vbo_count])*3 + sizeof(v_colors[vbo_count])*3 , NULL, GL_DYNAMIC_DRAW);
vbo_count++;
// Same code
glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof (v_positions[vbo_count-1]), v_positions[vbo_count-1] );
glBufferSubData( GL_ARRAY_BUFFER, sizeof (v_positions[vbo_count-1]), sizeof(v_colors[vbo_count-1]), v_colors[vbo_count-1] );
glBufferSubData( GL_ARRAY_BUFFER, sizeof (v_positions[vbo_count-1]) + sizeof(v_colors[vbo_count-1]),sizeof (v_positions[vbo_count]) , v_positions[vbo_count] );
glBufferSubData( GL_ARRAY_BUFFER, sizeof (v_positions[vbo_count-1]) + sizeof (v_positions[vbo_count]) + sizeof (v_colors[vbo_count-1]), sizeof(v_colors[vbo_count]), v_colors[vbo_count] );
glDrawArrays(GL_TRIANGLES, 0, num_vertices);
vbo_count++;
}
Here I’m trying to load data of both objects into the same vbo and get them displayed, but again only the first object gets displayed.
So how can I get my objects to display on the screen simultaneously?
Added note: Just thought I’d mention that I’m using glfw, contrary to glut which i found being used in tutorials all over.