我使用QOpenglWidget在ubuntu 18.04上的qt5中渲染obj模型(从互联网上下载的nanosuit.obj),我希望模型绕Y轴旋转90度,因此我在其中调用了glRotatef(90.0f, 0.0f, 1.0f, 0.0f)
initializeGL()
功能,但它完全没有旋转模型。然后我在glGetError()
函数之前和之后调用glRotatef
,在glRotatef
之后我收到一个错误INVALID_OPERATION,有人可以帮我一个忙,我是opengl的新手,非常感谢!
更新:
initializeGL()中的代码:
initializeOpenGLFunctions();
CheckGLerror();
glEnable(GL_DEPTH_TEST);
CheckGLerror();
glEnable(GL_CULL_FACE);
CheckGLerror();
glRotatef(90, 0,1,0);
CheckGLerror();
glewExperimental = GL_TRUE;
GLenum status = glewInit();
if (status != GLEW_OK)
{
glfwTerminate();
std::system("pause");
return;
}
//shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// Check for compile time errors
GLint success;
GLchar infoLog[512];
glCompileShader(vertexShader);
GLint compileStatus = 0;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compileStatus);
if (compileStatus == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength);
std::vector<GLchar> errLog(maxLength);
glGetShaderInfoLog(vertexShader, maxLength, &maxLength, &errLog[0]);
std::cout << "Error::Shader file [" << " ] compiled failed,"
<< &errLog[0] << std::endl;
success = false;
}
// Fragment shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// Check for compile time errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" <<
infoLog << std::endl;
}
CheckGLerror();
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// Check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDetachShader(shaderProgram, vertexShader);
glDetachShader(shaderProgram, fragmentShader);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
CheckGLerror();
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(glm::vec3), &this->vertices[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);
CheckGLerror();
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);`
paintGL():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(this->VAO);
glDrawArrays(GL_TRIANGLES, 0, this->vertices.size()); //3 this->vertices.size()/
glBindVertexArray(0);
update2: 顶点着色器:
"#version 330 core\n"
"in vec3 position;\n"
"uniform mat4 projection;"
"uniform mat4 view;"
"uniform mat4 model;"
"void main()\n"
"{\n"
"gl_Position = projection * view * model * vec4(position, 1.0);\n"
"}\n";
片段着色器:
"#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 1.0f,1.0f,1.0f);\n"
"}\n";
在paintGL()中添加一些代码:
...
glm::mat4 model;
model = glm::rotate(model, 90.0f, glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 view;
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
glm::mat4 projection;
projection = glm::perspective(45.0f, static_cast<GLfloat>(w_width/w_height), 0.1f, 1000.0f);
GLint model_loc = glGetUniformLocation(shaderProgram, "model");
glUniformMatrix4fv(model_loc, 1, GL_FALSE, glm::value_ptr(model));
GLint view_loc = glGetUniformLocation(shaderProgram, "view");
glUniformMatrix4fv(view_loc, 1, GL_FALSE, glm::value_ptr(view));
GLint projection_loc = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(projection_loc, 1, GL_FALSE, glm::value_ptr(projection));
...