无法弄清楚为什么我会得到GL_INVALID_OPERATION

时间:2017-01-24 01:15:10

标签: c++ opengl

我正在创建一个小游戏,我想从类内部的主循环中取出东西。

我想绘制与玩家绑定的数字。

    //identity matrix
    Matrix44 model; 

    //set up the model to world transform
    model.SetTranslation(player.position);

    //pass a uniform which specifies the color of the player to the fragment shader
    glUniform3f(colorLocation, player.color._x, player.color._y, player.color._z);

    //pass the model as well 
    glUniformMatrix4fv(transformLocation, 1, GL_FALSE, &model.m[0][0]);

    //draw
    glDrawArrays(GL_TRIANGLES, 0, 36);

这是主循环中的实现。它就像一个魅力。 当我尝试从Player类调用相同的函数时出现问题。

void Player::drawPlayer(GLint transformLocation, GLint colorLocation)
{   

Matrix44 model;     
model.SetTranslation(position); 

glUniform3f(colorLocation, color._x, color._y, color._z);   
std::cout << glGetError() << std::endl;  <- I get error 1028 here

glUniformMatrix4fv(transformLocation, 1, GL_FALSE, &model.m[0][0]);
std::cout << glGetError() << std::endl;   <- I get error 1028 here

glDrawArrays(GL_TRIANGLES, 0, 36);
std::cout << glGetError() << std::endl;

}

在我成功编译了着色器程序之后,我正在进行这些调用,该程序在没有任何问题的情况下将其余对象渲染到场景中,并且在我设置了VBO,VAO等之后。

我已经实现了一个类似的类来绘制一些立方体以创建关卡。 (两者都使用相同的VAO和VBO)

 void Level::drawLevelTiles(GLint colorLocation, GLint transformLocation)

{     std :: vector :: iterator it;

for (it = levelTiles.begin(); it != levelTiles.end(); ++it)
{
    Matrix44 model;
    GameObject temp = *it;

    model.SetTranslation(temp.position);
    glUniform3f(colorLocation, temp.color._x, temp.color._y, temp.color._z);
    glUniformMatrix4fv(transformLocation, 1, GL_FALSE, &model.m[0][0]);
    glDrawArrays(GL_TRIANGLES, 0, 36);
}

for (it = fallingBlocks.begin(); it != fallingBlocks.end(); ++it)
{
    Matrix44 model;
    GameObject temp = *it;
    model.SetTranslation(temp.position);

    glUniform3f(colorLocation, temp.color._x, temp.color._y, temp.color._z);        
    glUniformMatrix4fv(transformLocation, 1, GL_FALSE, &model.m[0][0]);
    glDrawArrays(GL_TRIANGLES, 0, 36);
}

上面的代码实际上是相同的,但它的工作原理。并且这两个类都继承自相同的基类 - GameObject。我可以离开它并使用解决方法,但它感觉不对。

这是整个主循环:

  ourShader.Use();

    Matrix44 view;
    Matrix44 projection;        

    projection = projection.CreatePerspective(45.f, width / height, 0.1f, 100.f);       

    Vector3 eye(0.f, 0.f, -3.f);
    Vector3 center(0.f, 0.f, 0.f);
    Vector3 up(0.f, 1.f, 0.f);
    view = view.CreateLookAt(eye, center, up);


    GLint transformLocation = glGetUniformLocation(ourShader.Program, "model");
    GLint viewLocation = glGetUniformLocation(ourShader.Program, "view");
    GLint projectionLocation = glGetUniformLocation(ourShader.Program, "projection");
    GLint colorLocation = glGetUniformLocation(ourShader.Program, "cubeColor");

    glUniformMatrix4fv(viewLocation, 1, GL_FALSE, &view.m[0][0]);       
    glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, &projection.m[0][0]);       
    //=====================================================================================

    //
    //draw the player
    //player.drawPlayer(colorLocation, transformLocation);

    // draw the level tiles
    glBindVertexArray(level._VAO);
    level.drawLevelTiles(colorLocation, transformLocation);
    player.drawPlayer(colorLocation, transformLocation);

    glBindVertexArray(0);

    // Swap the screen buffers
    glfwSwapBuffers(window);

任何建议都非常受欢迎!

1 个答案:

答案 0 :(得分:0)

比较'drawPlayer`的定义和用法

void Player::drawPlayer(GLint transformLocation, GLint colorLocation)
player.drawPlayer(colorLocation, transformLocation);

您是否看到参数不匹配?