这个问题仅在Windows下存在,在Linux和OSX下面是可以正常编译和运行的。
问题出在如下函数:
void GLWidget::drawBackground(QPainter *, const QRectF &)
{
glClearColor(0, 0, 102 / 255, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(0, 0, 0);
glLineWidth(2);
float margin = 0.05;
float l = margin, r = 1 - margin, b = margin, t = 1 - margin;
int splitNum = 9;
float dx =(1 - margin * 2) / (splitNum + 1);
glBegin(GL_LINE_LOOP);
glVertex2f(l, b);
glVertex2f(l, t);
glVertex2f(r, t);
glVertex2f(r, b);
glEnd();
glBegin(GL_LINES);
for(int i = 1; i <= splitNum; i++)
{
glVertex2f(l, b + dx * i);
glVertex2f(r, b + dx * i);
glVertex2f(l + dx * i, b);
glVertex2f(l + dx * i, t);
}
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
以及(这个是加载obj3D模型用的)
void Model::render(bool wireframe, bool normals) const
{
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_VERTEX_ARRAY);
if (wireframe) {
glVertexPointer(3, GL_FLOAT, 0, (float *)m_points.data());
glDrawElements(GL_LINES, m_edgeIndices.size(), GL_UNSIGNED_INT, m_edgeIndices.data());
} else {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, (float *)m_points.data());
glNormalPointer(GL_FLOAT, 0, (float *)m_normals.data());
glDrawElements(GL_TRIANGLES, m_pointIndices.size(), GL_UNSIGNED_INT, m_pointIndices.data());
glDisableClientState(GL_NORMAL_ARRAY);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);
}
if (normals) {
QVector<Point3d> normals;
for (int i = 0; i < m_normals.size(); ++i)
normals << m_points.at(i) << (m_points.at(i) + m_normals.at(i) * 0.02f);
glVertexPointer(3, GL_FLOAT, 0, (float *)normals.data());
glDrawArrays(GL_LINES, 0, normals.size());
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_DEPTH_TEST);
}
在Windows下面会出现一堆undefined reference的错误,但在Linux和OSX下面却能正常编译和运行,是Windows需要装哪个依赖包吗?
使用的是Qt5自带OpenGL库,Qt5.6 MinGW 32Bit。
已解決,參考Qt5 Docs Windows系統需求
在專案的.pro檔案中新增:
雷雷編譯成功。
如果引用路徑都沒錯的話,可以考慮下圖形驅動的問題。好像Windows下Qt的gl庫裡面某些API是基於DX11(9)實現的,裝下DX的SDK試試
貼出錯的圖片吧