1

我创建了一个类,可以在屏幕上呈现多个对象,我称之为“实体”,这是 3 个构造函数的代码:

Entity::Entity()
{
    x = y = z = 0;
    std::cout<<"INIT MODEL"<<std::endl;
    model = Model();
    model.loadModel2("huis_0.txt");
} 
Entity::Entity(float xVal,float yVal,float zVal,std::string source)
{
    std::cout<<"INIT MODEL2"<<std::endl;
    x = xVal;
    y = yVal;
    z = zVal;
    model = Model();
    model.loadModel2(source);
}
Entity::Entity(std::string source)
{
    x = y = z = 0;
    std::cout<<"INIT MODEL3"<<std::endl;
    model = Model();
    model.loadModel2(source);
}

解构器简单打印出“删除实体”。然后我使用第二种方法在我的主文件中加载一个实体。Model 是另一个简单地从文件加载顶点和索引的类。在运行期间,这是我得到的输出:

INIT2
++loading model+++
++done loading model++
deleting entity
//other prints
deleting entity

然后在我关闭窗口之后。我收到以下错误:“调试失败:表达式:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)”我猜我收到此错误是因为我删除了两次对象。我该如何防止这种情况?那也是我唯一拥有的实体,所以不可能。

编辑:实体对象是在我的主文件顶部的任何方法之外创建的,因为我想使用实体的“render()”方法。

EDIT2:这是使用剑的所有功能,请原谅它很乱,我正在尝试我学到的openGL代码:

Entity sword = Entity(0,0,0,"pirate_sword.txt");

void Initialize(int argc, char* argv[])
{
    InitWindow(argc, argv);

    fprintf(stdout,"INFO: OpenGL Version: %s\n",glGetString(GL_VERSION));
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    //model.loadModel2("huis_0.txt");
    model.loadModel2("test_zwaard_0.txt");

    glGenVertexArrays(1,&vao);
    glBindVertexArray(vao);

    ShaderLoader loader;
    std::string test = loader.loadStringFromFile("vertex_shader.txt");
    std::string frag = loader.loadStringFromFile("fragment_shader.txt");
    program = loader.loadProgram(GL_VERTEX_SHADER,test,GL_FRAGMENT_SHADER,frag);
    glUseProgram(program);

    /*LIGHTING*/
    ambient_loc = glGetUniformLocation(program,"ambient");
    lightColor_loc = glGetUniformLocation(program,"lightColor");
    lightDirection_loc = glGetUniformLocation(program,"lightDirection");
    halfVector_loc = glGetUniformLocation(program,"halfVector");
    shiny_loc = glGetUniformLocation(program,"shiny");
    strength_loc = glGetUniformLocation(program,"strength");
    GLfloat ambient[3] = {
        0.4,0.4,0.4
    };
    glUniform3f(ambient_loc,ambient[0],ambient[1],ambient[2]);
    GLfloat lightColor[3] = {
        0.4,0.4,0.4
    };
    glUniform3f(lightColor_loc,lightColor[0],lightColor[1],lightColor[2]);
    GLfloat lightDirection[3] = {
        1.0,0.2,0.4
    };
    glUniform3f(lightDirection_loc,lightDirection[0],lightDirection[1],lightDirection[2]);
    GLfloat halfVector[3] = {
        1.0,0.2,0.4
    };
    glUniform3f(halfVector_loc,halfVector[0],halfVector[1],halfVector[2]);
    float shiny = 0.2f;
    glUniform1f(shiny_loc,shiny);
    float strength = 0.7f;
    glUniform1f(strength_loc,strength);

    /*END OF LIGHTING*/
    /*TRANSFORM*/
    persp_loc = glGetUniformLocation(program,"persp");
    model_loc = glGetUniformLocation(program,"model");
    camera_loc = glGetUniformLocation(program,"camera");

    glm::mat4 transform_model = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f));
        glm::rotate(transform_model,20.0f,glm::vec3(x,y,z));
        glUniformMatrix4fv(model_loc,16,false,&transform_model[0][0]);
    glm::mat4 transform_camera = (glm::lookAt(glm::vec3(3,5,-5),glm::vec3(0,0,0),glm::vec3(0,1,0)));
        glUniformMatrix4fv(camera_loc,16,false,&transform_camera[0][0]);
    glm::mat4 transform_persp = glm::perspective(45.0f,float(4/3),(float)0.5,(float)100);

    //Matrix4 m;
    //m.translateMatrix(0.2f,0.0f,0.0f);
    float frustumScale = 1.0f,zNear = 0.1f,zFar = 100.0f;
    GLfloat persp[16];
    memset(persp,0.0f,sizeof(persp));
    persp[0] = frustumScale;
    persp[5] = frustumScale;
    persp[10] = (zFar + zNear) / (zNear - zFar);
    persp[14] = (2 * zFar * zNear) / (zNear - zFar);
    persp[11] = -1.0f;
    glUniformMatrix4fv(persp_loc,16,false,&transform_persp[0][0]);
    glUseProgram(0);

    glGenBuffers(1,&vbo);
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    GLfloat model_test[834]; //834
    memset(model_test,0.0f,sizeof(model_test));
    for(unsigned int i=0;i<834;i++) model_test[i] = model.getIndex(i);
    glBufferData(GL_ARRAY_BUFFER,sizeof(model_test),&model_test,GL_STATIC_DRAW);

    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(0));
    glEnableVertexAttribArray(0);

    GLuint ibo;
    glGenBuffers(1,&ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ibo);
    GLshort indices[1656];//1656
    memset(indices,0,sizeof(indices));
    for(int i=0;i<1656;i++) indices[i] = model.getVertexIndex(i);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),&indices,GL_STATIC_DRAW);

    getGLerrors("init");

    glEnable(GL_DEPTH_TEST);

    getGLerrors("depth");

    sword.init();
    sword.move(0.3f,0.0f,0.0f);

    getGLerrors("sword init");
}
void RenderFunction(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(program);
        glm::mat4 transform_model = glm::mat4(0.5f);
        glm::rotate(transform_model,20.0f,glm::vec3(x,y,z));
        glUniformMatrix4fv(model_loc,16,false,&transform_model[0][0]);
    //glDrawArrays(GL_TRIANGLES,0,model.getSize()/3);
    glBindVertexArray(vao);
    if(rasterize)glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
    glDrawElements(GL_TRIANGLES,model.getIndexSize(),GL_UNSIGNED_SHORT,0);

    glUniformMatrix4fv(model_loc,16,false,&sword.getTransform()[0][0]);
    sword.render();

    getGLerrors("draw");

    glutSwapBuffers();
    glutPostRedisplay();
}
4

1 回答 1

2

我确定你是如何创建Entity的,但这是创建一个临时的Model,复制到model,然后销毁临时的:

model = Model();

我敢打赌你正在做类似的事情Entity

于 2013-08-07T19:02:01.720 回答