OpenGL局部绘图

OpenGL Partial Drawing

本文关键字:绘图 局部 OpenGL      更新时间:2023-10-16

我正在使用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 );

我了解全部情况。我要改变的参数是"stride"。有人知道为什么会有这种效果吗?如果我加载有更多顶点的文件,我必须再次增加步幅来显示所有顶点。如果我将参数更改为任何不是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);
}

DrawCube功能:

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……(指数向量)。但实际上它应该是连续计数,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);
}