glDrawElements()访问冲突?

时间:2014-09-05 21:43:32

标签: c++ opengl

我一直在尝试使用assimp加载一个obj文件,每次都会崩溃glDrawElements :(。

代码非常简单,我也编写了基本的着色器,我只是加载了一个基本的cube.obj文件,其中包含正确的顶点和索引数据,但是当我尝试绘制它时,我得到了访问冲突......

我的基本初始化函数:

GLuint vertexBuffer;
std::vector<unsigned int> indices;
    std::vector<float> vertices;
    std::vector<float> uvs;
    std::vector<float> normals;
void init(){
    GLchar *VertexShader, *FragmentShader;
    int loadstatus = 0;
    loadObj("cube.obj", indices, vertices, uvs, normals);
    readShader("vertex.v", &VertexShader);
    readShader("frag.f",&FragmentShader);
    loadstatus = InstallShader(VertexShader, FragmentShader);
    if(!loadstatus)
        printf("Erro ao compilar shader");

    GLuint elementBuffer;
    glGenBuffers(1, &elementBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);


    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(aiVector3D), &vertices[0], GL_STATIC_DRAW);
    GLint loc = glGetAttribLocation(programObj, "position");
    glVertexAttribPointer(loc,3,GL_FLOAT,GL_FALSE,0,nullptr);
    glEnableVertexAttribArray(loc);
}

和我的绘图功能:

void draw(){
    glClearColor(0.,0.,0.,0.);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibo);
    glDrawElements(GL_TRIANGLES,indices.size(),GL_UNSIGNED_INT,0);
    glutSwapBuffers();
    glutPostRedisplay();
}

绘制函数曾经有一些相机,但为了简单起见我删除了,如果你想知道这里是着色器: vertex.v:

#version 330

in vec3 position;
//uniform mat4 mvp;
out vec3 pos;

void main(){
    pos = position;
    vec4 p = vec4(position,1.);
    //p = mvp * p;
    gl_Position = p;
}

和frag.f:

#version 330

in vec3 pos;
out vec4 fragColor;

void main(){
    fragColor = vec4(1.);
}

没有把loadObj,readShader和InstallShader放在一起,因为我很确定它们都是正确的......

包括loadObj函数,这是我在另一个主题中找到的函数:OpenGL Assimp imported model not rendering在尝试解决问题时

void loadObj(const char *path,
std::vector<unsigned int> &outIndices,
std::vector<float> &outVertices,
std::vector<float> &outUVs,
std::vector<float> &outNormals)
{
    Assimp::Importer importer;
    const aiScene *scene = importer.ReadFile(path, aiProcessPreset_TargetRealtime_Fast);
    aiMesh *mesh = scene->mMeshes[0];
    int numOfFaces = mesh->mNumFaces;
    int numOfIndices = numOfFaces*3;
    outIndices.resize(numOfIndices);

    for(unsigned int i = 0 ; i< mesh->mNumFaces; i++)
    {
        const aiFace &face = mesh->mFaces[i];
        assert(face.mNumIndices == 3);
        outIndices[i * 3 + 0] = face.mIndices[0];
        outIndices[i * 3 + 1] = face.mIndices[1];
        outIndices[i * 3 + 2] = face.mIndices[2];
    }
    int numOfVertice = mesh->mNumVertices;
    outVertices.resize(numOfVertice*3);
    outNormals.resize(numOfVertice * 3);
    outUVs.resize(numOfVertice * 2);
    for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
        if (mesh->HasPositions()) {
            outVertices[i * 3 + 0] = mesh->mVertices[i].x;
            outVertices[i * 3 + 1] = mesh->mVertices[i].y;
            outVertices[i * 3 + 2] = mesh->mVertices[i].z;
        }

        if (mesh->HasNormals()) {
            outNormals[i * 3 + 0] = mesh->mNormals[i].x;
            outNormals[i * 3 + 1] = mesh->mNormals[i].x;
            outNormals[i * 3 + 2] = mesh->mNormals[i].x;
        }

        if (mesh->HasTextureCoords(0)) {
            outUVs[i * 2 + 0] = mesh->mTextureCoords[0][i].x;
            outUVs[i * 2 + 1] = mesh->mTextureCoords[0][i].y;
        }
    }

}

0 个答案:

没有答案