我正在使用 OpenGL,而且我非常接近我想去的地方。我正在使用 VBO,但由于某种原因,我的图片只绘制了大约一半的顶点 (GL_LINE_STRIP)。如果我更改行:
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0 );
到
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex)*2, (GLvoid*)0 );
我得到了全貌。我要更改的参数是“步幅”。有谁知道为什么会产生这种效果?如果我用更多顶点加载我的文件,我必须再次增加我的步幅以显示所有顶点。如果我将参数更改为不是 32 的倍数 (sizeof(Vertex)) 的任何值,它就会生成一张无意义的图片。此外,如果我将它增加太多,绘图会变得参差不齐并且会跳过顶点。
我确定我传递的东西不正确,我只是不知道在哪里。 (顺便说一句,我不是在画立方体,我只是在做一个例子)。这是我的代码:
CreateCube 函数:
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);
}
绘制立方体函数:
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);
}
最佳答案
我想通了,我输入的索引有误。我正在这样做,所以元素是 1、2、2、3、3、4、4...(索引 vector )。但实际上它应该只是连续计数,1、2、3、4、5、6,... vector.size() - 1,vector.size()。我不知道索引是如何工作的,我认为你必须将 1 连接到 2、2 连接到 3、3 连接到 4……所以这就是为什么我在每个数字中输入两个。然而,它似乎只是从 1 到 2 到 3 到 4。
所以改变:
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);
}
到
ind.push_back(0);
for (int i = 1; i < verts.size(); i++)
{
ind.push_back(i);
}
关于c++ - OpenGL 局部绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22489213/