我正在尝试加载我在混合器中绘制的东西的.obj文件,并在我的GUI上显示它,但我对如何继续有点迷失。一些文档提供了有关某些功能的非常简要的信息。
这是我在编写时所遵循的tutorial。
我现在根本不知道如何在我的openGLWidget上绘图。
这是我的代码 - 我认为 - 应该显示我的gui,但它没有:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
load_obj("/home/yalishanda/Desktop/car.obj", suzanne_vertices, suzanne_normals, suzanne_elements);
//enable all vertices attributes
for(int i=0;i<suzanne_vertices;i++)
{
glEnableVertexAttribArray(i);
}
GLuint buffer;
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glVertexAttribPointer(
0,
4,
GL_FLOAT,
GL_FALSE,
0,
0
);
// glEnableVertexAttribArray(0);
GLuint buffer2;
glBindBuffer(GL_ARRAY_BUFFER, buffer2);
glVertexAttribPointer(
0,
3,
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // offset of first element
);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
glDrawElements(GL_TRIANGLES, size/sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
paintGL();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::load_obj(const char* filename, std::vector<QVector4D> &vertices, std::vector<QVector3D> &normals, std::vector<GLushort> &elements)
{
std::ifstream in(filename, std::ios::in);
if (!in)
{
std::cerr << "Cannot open " << filename << std::endl; exit(1);
}
std::string line;
while (std::getline(in, line))
{
if (line.substr(0,2) == "v ")
{
std::istringstream s(line.substr(2));
QVector4D v;
//s >> v.x;
//s >> v.y;
//s >> v.z;
//v.w = 1.0f;
float helpValue;
s>>helpValue;
v.setX(helpValue);
v.setY(helpValue);
v.setZ(helpValue);
v.setW(1.0f);
vertices.push_back(v);
}
else if (line.substr(0,2) == "f ")
{
std::istringstream s(line.substr(2));
GLushort a,b,c;
s >> a; s >> b; s >> c;
a--; b--; c--;
elements.push_back(a); elements.push_back(b); elements.push_back(c);
}
else if (line[0] == '#')
{
/* ignoring this line */
}
else
{
/* ignoring this line */
}
}
normals.resize(vertices.size(), QVector3D(0.0, 0.0, 0.0));
for (int i = 0; i < elements.size(); i+=3)
{
GLushort ia = elements[i];
GLushort ib = elements[i+1];
GLushort ic = elements[i+2];
QVector3D difference = QVector3D::crossProduct(
QVector3D(vertices[ib]) - QVector3D(vertices[ia]),
QVector3D(vertices[ic]) - QVector3D(vertices[ia]) );
QVector3D normal = difference.normalized();
normals[ia] = normals[ib] = normals[ic] = normal;
}
}
void MainWindow::initializeGL()
{
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
void MainWindow::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//draw all the vertices
// I guess that here I should select my openGL widget somehow and draw the vertices *on* it
glBegin(GL_VERTEX4_BIT_PGI);
for(int i=0; i<suzanne_vertices.size();i++)
{
glVertex3f(suzanne_vertices.at(i).x, suzanne_vertices.at(i).y, suzanne_vertices.at(i).z);
}
glEnd();
}
答案 0 :(得分:0)
我认为首先你没有指定相机?
我从教程中获取了这个片段,
glm::mat4 view = glm::lookAt(
glm::vec3(0.0, 2.0, 4.0), // eye
glm::vec3(0.0, 0.0, 0.0), // direction
glm::vec3(0.0, 1.0, 0.0)); // up
glm::mat4 projection = glm::perspective(45.0f, 1.0f*screen_width/screen_height, 0.1f, 100.0f);
OpenGL在开始时是一个非常痛苦的库,它要求管道完全正确以呈现正确的东西,其他它不会告诉你哪里出错。
此外,您可能想要禁用材质和灯光,并将渲染颜色设置为(让我们说是绿色),以便在开头看到某些内容,如果您的材料未正确设置,光也可能使一切都变黑。