C++ OpenGL局部绘图

C++ OpenGL局部绘图,c++,opengl,drawing,vertex-buffer,C++,Opengl,Drawing,Vertex Buffer,我正在使用OpenGL,我离我想去的地方非常近。我使用VBO的,但由于某些原因,我的图片只绘制了大约一半的顶点(GL_LINE_STRIP)。如果我更改行: glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0 ); 到 我了解全部情况。我正在更改的参数是“步幅”。有人知道为什么会有这种效果吗?如果我加载了更多顶点的文件,我必须再次增加步幅以显示所有顶点。如果我将参数更改为不是32的倍数(sizeo

我正在使用OpenGL,我离我想去的地方非常近。我使用VBO的,但由于某些原因,我的图片只绘制了大约一半的顶点(GL_LINE_STRIP)。如果我更改行:

glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (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);
}

我明白了,我输入的索引是错误的。我制作的元素是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);
对于(int i=1;i

ind.push_-back(0);
对于(int i=1;i
为什么要使用29000作为GLD元素的计数参数?如何定义
ind
ind?ind只是一个GLuint向量@这是我拥有的顶点数,但后来我将其改为verts.size()。@TheBlindSpring:你不应该使用那里的顶点数,而应该使用
ind
数组中的索引数。如果你想重用顶点,通常会使用索引。绘制多维数据集是何时使用索引的一个好例子。一个立方体有8个顶点,但是当你为一个立方体绘制三角形时,你会得到12个三角形,因此索引会告诉你每个三角形由哪些顶点组成。在这种情况下,顶点数为8,索引数为36。
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);
}
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);
}