【发布时间】:2017-09-13 18:18:12
【问题描述】:
我写了一个简单的openGL代码来渲染一个三角形,它编译成功但渲染三角形失败。仅创建一个窗口,因此glGetError() api 调用不会返回错误。相同的代码在 AMD R9 GPU 上运行良好。驱动程序安装也是正确的,因为我能够运行 glxdemos (如 glxgears 或 glxhead)而不会出现任何错误。
请帮我找出这个问题的根本原因。
这是我的系统配置。 CPU - 英特尔 i5 7400(Kaby Lake 630 高清 GPU) 操作系统 - Ubuntu 16.04 64 位 台面 - 3.0 v17.03
这是我渲染三角形的代码。
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(int agrc, char **argv)
{
//do windowing related stuff here
if ( !glfwInit())
{
printf("Error: Failed to initialize GLFW\n");
return -1;
}
GLFWwindow* window = glfwCreateWindow(800, 600, "Triangle", NULL, NULL);
if (window == NULL)
{
printf("Failed to create GLFW window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
printf("Error: Failed to initialize GLEW\n");
return -1;
}
//declare vertices
GLfloat verts[] =
{
+0.0f, +0.5f, +0.0f,
-0.5f, -0.5f, +0.0f,
+0.5f, -0.5f, +0.0f,
};
//VBO related activity
//declare VAO, VBO
GLuint VAO, VBO, EBO;
//get unique name/ID
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
//glGenBuffers(1, &EBO);
// Bind VAO first, then bind and set VBOs and then configure vertex attributes
//bind VAO
glBindVertexArray(VAO);
//bind VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//copy data to GPU
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
do{
glfwPollEvents();
}while(glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);
return 0;
}
【问题讨论】:
-
如果你用鼠标改变窗口的大小,它会画什么,甚至只是背景颜色?
-
@Rabbid76 在 Ubuntu 中可能会发生许多奇怪的事情 :( 就像 OGL 仅在某些操作后才会启动。我以前在 Intel HD4000 上见过它。
-
一些驱动默认启用depth_testing(违反opengl标准)。尝试明确禁用它。编辑:哦,我看到你正在运行 Mesa。你真勇敢,真是个赌徒;-)
-
@Ripi2 - 背景颜色变化是反射的,但没有三角形,如果尝试使用鼠标更改窗口大小,则会创建损坏的图像。
-
@Andreas - 在“glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);”中添加了 GL_DEPTH_BUFFER_BIT但对输出没有影响。
标签: opengl rendering ubuntu-16.04 intel mesa