我正在尝试学习|| Qt中的代码openGL。我做了一个显示两个数字的应用程序。一个是三角形“A”,另一个三角形“B”与“A”相同,只是它围绕z轴旋转了-90度(z轴垂直于计算机屏幕)。现在,问题是轮换使维度发生变化。我发布了下面的“main.cpp”,
#include <QApplication>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QtOpenGL/QGLWidget>
#include <QWidget>
class MyOpenGL:public QGLWidget
{
public:
MyOpenGL(QWidget * parent);
~MyOpenGL();
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
};
MyOpenGL::MyOpenGL(QWidget *parent):QGLWidget(QGLFormat(QGL::SampleBuffers),parent)
{
setAutoFillBackground(false);
}
MyOpenGL::~MyOpenGL()
{
}
void MyOpenGL::initializeGL()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
}
void MyOpenGL::resizeGL(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,width()/height(),10.0f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void MyOpenGL::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//glTranslatef(0.0,0.0,-10.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,0.0f,-10.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,0.0f,-10.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,4.0f,-10.0f);
glEnd();
glRotatef(90.0,0.0f,0.0f,-1.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,0.0f,-10.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,0.0f,-10.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,4.0f,-10.0f);
glEnd();
glLoadIdentity();
}
int main(int argc,char * argv[])
{
QApplication app(argc,argv);
MyOpenGL * f = new MyOpenGL(NULL);
f->show();
return app.exec();
}
这是专业档案
SOURCES += \
main.cpp
QT += opengl
这是应用程序的屏幕截图
据我所知,轮换不会对尺寸做任何改变。但这里的维度正在发生变化。如果有人在这个问题上表示怀疑,我将非常感谢他/她。
答案 0 :(得分:4)
我不能确定发生了什么,但我确实看到可能存在一些问题。
gluPerspective(45.0f,width()/height(),10.0f,100.0f);
如果width
和height
都返回整数(他们可能会这样做),那么将一个除以另一个将导致整数。这不会给你一个适当的宽高比。将其中一个投射到浮子上以获得真实的纵横比。
接下来,尝试将glLoadIdentity()调用放在paintGL函数的顶部。
答案 1 :(得分:0)
如果您需要2D渲染,请使用gluOrtho2D而不是gluPerspective(3D渲染)。
实际上,这不是问题,这只是将3D物体正常投影到2D平面上。
答案 2 :(得分:0)
您需要设置视图:
void MyOpenGL::resizeGL(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
gluPerspective(45.0f,(double)w/(double)h,10.0f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
updateGL();
}
此外,渲染图像时无需重置投影矩阵:
void MyOpenGL::paintGL()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//glTranslatef(0.0,0.0,-10.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,0.0f,-10.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,0.0f,-10.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,4.0f,-10.0f);
glEnd();
glRotatef(90.0,0.0f,0.0f,-1.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f,0.0f,-10.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(1.0f,0.0f,-10.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,4.0f,-10.0f);
glEnd();
}
但有人说,尝试使用glOrtho而不是gluPerspective
答案 3 :(得分:-1)
要进行旋转,您必须先将事物带到原点,然后旋转,然后将其放到所需的位置。