hjh175 2016-07-26 01:49 采纳率: 0%
浏览 4368

使用glClear和glClearColor,为什么背景颜色变不了呢,一直是黑的



#include "glwidget.h"
#include "qwidget.h"
#include "ui_widget.h"
#include "QTOPENGL\glu.h"
#include <QtGui>
#include <QtOpenGL>


/*
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}
*/

NeHeWidget::NeHeWidget(QGLWidget *parent,bool fs):
    QGLWidget(parent)
//  ui(new Ui::NeHeWidget)
{
//    ui->setupUi(this);
    fullscreen=fs;
    setGeometry(100,200,640,480);
    setWindowTitle("NeHe'OpenGL Framework");
    if(fullscreen)
        showFullScreen();
}
NeHeWidget::~NeHeWidget()
{
 //   delete ui;
}
void NeHeWidget::initializeGL()
{
    glClearColor(0.5,0.5,0.0,0.0);
    glShadeModel(GL_SMOOTH);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
}
void NeHeWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BITS);
    glLoadIdentity();
    glBegin(GL_TRIANGLES);
    glColor3f(1.0,0.0,0.0);
    glVertex3f(0.0,1.0,0.0);
    glVertex3f(-1.0,-1.0,0.0);
    glVertex3f(1.0,-1.0,0.0);
    glEnd();
}
void NeHeWidget::resizeGL(int width, int height)
{
    if(height==0)
    {
        height=1;
    }
    glViewport(0,0,(GLint)width,(GLint)height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0,(GLfloat)width/(GLfloat)height,0.1,100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void NeHeWidget::keyPressEvent(QKeyEvent*e)
{
    switch(e->key())
    {
    case Qt::Key_F2:
        fullscreen=!fullscreen;
        if(fullscreen)
        {
            showFullScreen();
        }
        else
        {
            showNormal();
            setGeometry(100,200,640,480);
        }
        updateGL();
        break;
    case Qt::Key_Escape:
        close();
    }
}

  • 写回答

2条回答 默认 最新

  • 无名程序员就是我 2022-02-27 16:16
    关注

    可以

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?