OpenGL绘图三角形

时间:2015-08-30 23:02:26

标签: c++ opengl

我是OpenGL的新手,我已经学过一些教程,现在我正试着自己玩。所以我有这个结构:

struct Vertex
{
    vec3 position;
    vec3 color;
};

如果我完成了所有这些:

glGenVertexArrays(1, &vaoID);
glGenBuffers(1, &vboVerticesID);

glBindVertexArray(vaoID);

glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * mesh.vertCount, mesh.verts, GL_STATIC_DRAW);

glEnableVertexAttribArray(shader["vVertex"]);
glVertexAttribPointer(shader["vVertex"], 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)offsetof(Vertex, position));

glEnableVertexAttribArray(shader["vColor"]);
glVertexAttribPointer(shader["vColor"], 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)offsetof(Vertex, color));

mesh.verticesVertex*类型。它们被放入数组中,后面的每三个都形成一个三角形。

问题是如何调用渲染三角形?我尝试了glDrawArrays(GL_TRIANGLES, 0, mesh.vertCount);但没有发生任何事情,没有抛出任何错误,也没有渲染三角形。在为顶点位置,颜色和索引设置单独的数组之前,glDrawElements工作得很好。

网格构造函数:

Mesh::Mesh(const string sourcePath)
{
    ifstream modelfile;
    modelfile.open(sourcePath.c_str(), ios_base::in);

    if (!modelfile.good())
    {
        cout << "Cannot load model file (" << sourcePath << ")!\n";
    }

    string line;

    vector<vec3> tempVertices;
    vector<vec3> tempNormals;
    vector<Vertex> verticesVector;

    while (getline(modelfile, line))
    {
        if (line[0] == 'v')
        {
            if (line[1] == ' ')
            {
                vec3 v;
                sscanf_s(line.c_str(), "%*s %f %f %f", &v.x, &v.y, &v.z);
                tempVertices.push_back(v);
            }
            else if (line[1] == 'n')
            {
                vec3 n;
                sscanf_s(line.c_str(), "%*s %f %f %f", &n.x, &n.y, &n.z);
                tempNormals.push_back(n);
            }
        }
        else if (line[0] == 'f')
        {
            vector<string> elems;
            stringstream s(line);
            string item;
            while (getline(s, item, ' '))
            {
                elems.push_back(item);
            }

            if (elems.size() != 4)
            {
                cout << sourcePath << " parsing error!\n";
            }

            for (int i = 1; i < 4; ++i)
            {
                stringstream s(elems[i]);
                string item;
                vector<unsigned int> vs;
                while (getline(s, item, '/'))
                {
                    vs.push_back((unsigned int)atoi(item.c_str())-1);
                }

                if (vs.size() != 3)
                    cout << sourcePath << " parsing error!\n";

                Vertex vert; 
                vert.position = tempVertices[vs[0]];
                vert.color = vec3(.5f);
//              vert.normal = vs[2] != -1 ? tempNormals[vs[2]] : vec3(0.f, 1.f, 0.f);

                verticesVector.push_back(vert);
            }
        }
    }

    vertCount = verticesVector.size();
    verts = (Vertex*)malloc(vertCount * sizeof(Vertex));
    for (int i = 0; i < vertCount; ++i)
    {
        verts[i] = verticesVector[i];
        cout << verticesVector[i].position.x << " " << verticesVector[i].position.y << " " << verticesVector[i].position.z << "\n";
    }

    cout << "Model loaded(" << sourcePath << ")!\n";
}

初始化功能:

void OnInit()
{
    GL_CHECK_ERRORS;

    shader.LoadFromFile(GL_VERTEX_SHADER, "shaders/shader.vert");
    shader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/shader.frag");

    shader.CreateAndLingProgram();

    shader.Use();
    shader.AddAttribute("vVertex");
    shader.AddAttribute("vColor");
//  shader.AddAttribute("vNormal");
    shader.AddUniform("MVP");
//  shader.AddUniform("lightDir");
    shader.UnUse();

    GL_CHECK_ERRORS;

    glGenVertexArrays(1, &vaoID);
    glGenBuffers(1, &vboVerticesID);

    glBindVertexArray(vaoID);

    glBindBuffer(GL_ARRAY_BUFFER, vboVerticesID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * mesh.vertCount, mesh.verts, GL_STATIC_DRAW);
    GL_CHECK_ERRORS;

    glEnableVertexAttribArray(shader["vVertex"]);
    glVertexAttribPointer(shader["vVertex"], 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLubyte*)NULL + offsetof(Vertex, position));
    GL_CHECK_ERRORS;

    glEnableVertexAttribArray(shader["vColor"]);
    glVertexAttribPointer(shader["vColor"], 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLubyte*)NULL + offsetof(Vertex, color));
    GL_CHECK_ERRORS;

//  glEnableVertexAttribArray(shader["vNormal"]);
//  glVertexAttribPointer(shader["vNormal"], 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)offsetof(Vertex, normal));
//  GL_CHECK_ERRORS;

    glClearColor(.3, .3, .3, 1);
    glutPassiveMotionFunc(MouseFunc);
    glutWarpPointer(WIDTH / 2, HEIGHT / 2);
    cout << "Initialization completed.\n" << endl;
}

渲染功能:

void OnRender()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    shader.Use();
    glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(p * v * m));
    glDrawArrays(GL_POINTS, 0, mesh.vertCount);
    GL_CHECK_ERRORS;
    shader.UnUse();

    glutSwapBuffers();

    glutPostRedisplay();
}

1 个答案:

答案 0 :(得分:0)

我最终已经恢复了,这里的所有代码都没问题,错误地使用了网格构造函数(Mesh mesh = Mesh(filePath)而不是Mesh mesh(filePath)) 谢谢你的帮助。