我对openGL的体验并不是很好,所以其中一些可能看起来很草率,但我有一个项目,我使用opengl渲染一个对象使用我自己的着色器。然后,我应该能够使用鼠标和WASD键在相机周围自由漫游。我看不到对象,我的键和鼠标也没有影响对象。我一直盯着这几个小时在这里和那里改变小东西,但似乎没有什么工作我真的需要一双新眼睛看这个。我知道vmath已经老了,过剩了,但这是我们教授希望我们使用的。 这些是我认为问题所在的代码。 这是我的主要内容:
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH) - 750) / 2, (glutGet(GLUT_SCREEN_HEIGHT) - 750) / 2);
glutInitWindowSize(width, height);
glutCreateWindow("Assignment Two");
glewExperimental = GL_TRUE;
glewInit();
glViewport(0, 0, width, height);
glutPassiveMotionFunc(mouseMove);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutIdleFunc(display);
glEnable(GL_DEPTH_TEST);
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindVertexArray(0);
glutMainLoop();
return 1;
}
这是我的显示功能,用于过剩和显示功能:
void display()
{
Shader ourShader("Shader.vs", "Shader.frag");
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
ourShader.Use();
vmath::mat4 view;
view = vmath::lookat(cPosition, cFront + cPosition, cUp);
vmath::mat4 projection;
projection = vmath::perspective(45.0f, (GLfloat)width / (GLfloat)height, nearC, farC);
GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, view);
glUniformMatrix4fv(projLoc, 1, GL_FALSE, projection);
glBindVertexArray(VAO);
for (GLuint i = 0; i < 10; i++)
{
vmath::mat4 model;
model *= vmath::translate(cubePositions[i]);
GLfloat angle = 20.0f * i;
model *= vmath::rotate(angle, cubePositions[i]);
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model);
glDrawArrays(renderType, 0, 36);
}
glBindVertexArray(0);
glFlush();
glutSwapBuffers();
}
这是在我的glutkeyboardfunc中调用的键盘:
void keyboard(unsigned char key, int xx, int yy)
{
GLfloat cameraSpeed = 0.01f;
int i, j;
std::string userInput;
if (key == 'q' || key == 'Q')
{
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
exit(0);
}
else if (key == 'w' || key == 'W')
{
cPosition += cameraSpeed * cFront;
}
else if (key == 's' || key == 'S')
{
cPosition -= cameraSpeed * cFront;
}
else if (key == 'a' || key == 'A')
{
cPosition -= vmath::normalize(vmath::cross(cFront, cUp)) * cameraSpeed;
}
else if (key == 'd' || key == 'D')
{
cPosition += vmath::normalize(vmath::cross(cFront, cUp)) * cameraSpeed;
}
}
然后这是我的mouseMove函数,它在我的glutPassiveMotionFunc中调用:
void mouseMove(int posX, int posY)
{
GLfloat xOffset;
GLfloat yOffset;
GLfloat sensitivity = 0.2f;
if (firstTime)
{
lastX = (float)posX;
lastY = (float)posY;
firstTime = false;
}
xOffset = posX - lastX;
yOffset = lastY - posY;
lastX = (GLfloat)posX;
lastY = (GLfloat)posY;
xOffset *= sensitivity;
yOffset *= sensitivity;
yaw += xOffset;
pitch += yOffset;
if (pitch > 89.0f)
{
pitch = 89.0f;
}
if (pitch < -89.0f)
{
pitch = -89.0f;
}
vmath::vec3 front;
front[0] = (GLfloat)(cos((M_PI / 180)*yaw) * cos((M_PI / 180)*pitch));
front[1] = (GLfloat)(sin((M_PI / 180)*pitch));
front[2] = (GLfloat)(sin((M_PI / 180)*yaw) * cos((M_PI / 180)*pitch));
cFront = vmath::normalize(front);
}
这是我的Shader.vs:
layout (location = 0) in vec3 position;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(position, 1.0f);
}
我认为某些事情没有被正确更改,或者没有正确更新这一事实。如果您希望我发布任何内容或通过我的代码让您了解其他任何内容,请与我们联系。
答案 0 :(得分:0)
您忘记在模型周期中初始化vmath::mat4 model
。
你可以这样做:
for (GLuint i = 0; i < 10; i++)
{
vmath::mat4 model( vmath::translate(cubePositions[i]) );
GLfloat angle = 20.0f * i;
model *= vmath::rotate(angle, cubePositions[i]);
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model);
glDrawArrays(renderType, 0, 36);
}