Reputation: 631
I am working with OpenGL and I am pretty close to where I want to be. I am using VBO's however for some reason my picture is only drawing about half of its vertices (GL_LINE_STRIP). If I change the line:
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0 );
to
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex)*2, (GLvoid*)0 );
I get the full picture. The parameter that I am changing is the 'stride'. Does anyone know why it is having this effect? If I load I file with more vertices I must increase my stride again to display all vertices. If I change the parameter to anything that is not a multiple of 32 (sizeof(Vertex)), it makes a nonsense picture. Also, if I increase it too much the drawing becomes jagged and it skips vertices.
I am sure I am passing something incorrectly, I just don't know where. (I am not drawing cubes btw, I am just working off of an example). Here is my code:
CreateCube Function:
void CreateCube()
{
string line;
ifstream myfile("C:/Users/Andrew/Documents/Visual Studio 2013/Projects/Control/Control/bin/Debug/TempGeo.test");
if (myfile.is_open())
{
Vertex temp;
int count = 0;
while (getline(myfile, line))
{
if (count == 0)
{
temp.Position[0] = (float)atof(line.c_str());
count++;
}
else if (count == 1)
{
temp.Position[1] = (float)atof(line.c_str());
count++;
}
else if (count == 2)
{
temp.Position[2] = (float)atof(line.c_str());
temp.Position[3] = 1;
temp.Color[0] = 1.0;
temp.Color[1] = 0.0;
temp.Color[2] = 0.0;
temp.Color[3] = 1.0;
verts.push_back(temp);
count = 0;
}
}
cout << verts.size() << endl;
myfile.close();
}
//getMinMax(vertices);
//getDiameter();
ind.push_back(0);
for (int i = 1; i < verts.size()-1; i++)
{
if (i % 2 == 0)
ind.push_back( (GLuint)i / 2);
else
ind.push_back( (GLuint)(i / 2) + 1);
}
ShaderIds[0] = glCreateProgram();
ExitOnGLError("ERROR: Could not create the shader program");
{
//ShaderIds[1] = LoadShader("./OpenGL 3.3/SimpleShader.fragment.3.3.glsl", GL_FRAGMENT_SHADER);
//ShaderIds[2] = LoadShader("./OpenGL 3.3/SimpleShader.vertex.3.3.glsl", GL_VERTEX_SHADER);
ShaderIds[1] = LoadShader("C:/Users/Andrew/Documents/SimpleShader.fragment.3.3.glsl", GL_FRAGMENT_SHADER);
ShaderIds[2] = LoadShader("C:/Users/Andrew/Documents/SimpleShader.vertex.3.3.glsl", GL_VERTEX_SHADER);
glAttachShader(ShaderIds[0], ShaderIds[1]);
glAttachShader(ShaderIds[0], ShaderIds[2]);
}
glLinkProgram(ShaderIds[0]);
ExitOnGLError("ERROR: Could not link the shader program");
ModelMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ModelMatrix");
ViewMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ViewMatrix");
ProjectionMatrixUniformLocation = glGetUniformLocation(ShaderIds[0], "ProjectionMatrix");
ExitOnGLError("ERROR: Could not get shader uniform locations");
glGenVertexArrays(1, &BufferIds[0]);
ExitOnGLError("ERROR: Could not generate the VAO");
glBindVertexArray(BufferIds[0]);
ExitOnGLError("ERROR: Could not bind the VAO");
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
ExitOnGLError("ERROR: Could not enable vertex attributes");
glGenBuffers(2, &BufferIds[1]);
ExitOnGLError("ERROR: Could not generate the buffer objects");
glBindBuffer(GL_ARRAY_BUFFER, BufferIds[1]);
//glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*verts.size(), &verts[0], GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind the VBO to the VAO");
cout << sizeof(verts[0].Position) << endl;
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)sizeof(verts[0].Position));
ExitOnGLError("ERROR: Could not set VAO attributes");
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferIds[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*ind.size(), &ind[0], GL_STATIC_DRAW);
ExitOnGLError("ERROR: Could not bind the IBO to the VAO");
glBindVertexArray(0);
}
DrawCube function:
void DrawCube(void)
{
float CubeAngle;
clock_t Now = clock();
if (LastTime == 0)
LastTime = Now;
CubeRotation += 45.0f * ((float)(Now - LastTime) / CLOCKS_PER_SEC);
CubeAngle = DegreesToRadians(CubeRotation);
LastTime = Now;
ModelMatrix = IDENTITY_MATRIX;
RotateAboutY(&ModelMatrix, CubeAngle);
RotateAboutX(&ModelMatrix, CubeAngle);
glUseProgram(ShaderIds[0]);
ExitOnGLError("ERROR: Could not use the shader program");
glUniformMatrix4fv(ModelMatrixUniformLocation, 1, GL_FALSE, ModelMatrix.m);
glUniformMatrix4fv(ViewMatrixUniformLocation, 1, GL_FALSE, ViewMatrix.m);
ExitOnGLError("ERROR: Could not set the shader uniforms");
glBindVertexArray(BufferIds[0]);
ExitOnGLError("ERROR: Could not bind the VAO for drawing purposes");
glDrawElements(GL_LINE_STRIP, 29000, GL_UNSIGNED_INT, (GLvoid*)0);
//glDrawElements(GL_LINE_STRIP, 29000, GL_UNSIGNED_INT, &verts[0]);
ExitOnGLError("ERROR: Could not draw the cube");
glBindVertexArray(0);
glUseProgram(0);
}
Upvotes: 0
Views: 860
Reputation: 631
I figured it out, I was inputting my indices wrong. I was making it so the elements were 1, 2, 2, 3, 3, 4, 4... (indices vector). but actually it really should just be consecutive counts, 1, 2, 3, 4, 5, 6, ... vector.size() - 1, vector.size(). I did not know how indices worked I thought you had to connnect 1 to 2, 2 to 3, 3 to 4... and so that's why I was putting in two of each number. However, it seems that it just goes from 1 to 2 to 3 to 4.
So change:
ind.push_back(0);
for (int i = 1; i < verts.size()-1; i++)
{
if (i % 2 == 0)
ind.push_back( (GLuint)i / 2);
else
ind.push_back( (GLuint)(i / 2) + 1);
}
to
ind.push_back(0);
for (int i = 1; i < verts.size(); i++)
{
ind.push_back(i);
}
Upvotes: 2