用QtOpenGL写了个东西,但出来一直是白色的,谁能帮忙看看啊?
下面是源代码:
.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
CONFIG += -lopengl
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
win32: LIBS += -L$$PWD/../../Qt_Freeglut/freeglut/lib/x64/ -lfreeglut
INCLUDEPATH += $$PWD/../../Qt_Freeglut/freeglut/include
DEPENDPATH += $$PWD/../../Qt_Freeglut/freeglut/include
RESOURCES += \
Resource.qrc
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QOpenGLWidget>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions>
class Widget : public QOpenGLWidget,protected QOpenGLFunctions
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected:
void initializeGL();
void resizeGL(int width,int height);
void paintGL();
private:
QOpenGLShaderProgram *program;
QOpenGLBuffer VBO;
};
#endif // WIDGET_H
widget.cpp:
#include "widget.h"
#include <GL/glut.h>
#include <QtOpenGL/QtOpenGL>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QOpenGLWidget(parent)
{
setMinimumSize(700,700);
}
Widget::~Widget() {}
void Widget::initializeGL()
{
initializeOpenGLFunctions();
QOpenGLShader vsrc(QOpenGLShader::Vertex,this);
vsrc.compileSourceFile("qrc:/shaders/vertex.vert");
program = new QOpenGLShaderProgram();
program->addShader(&vsrc);
QOpenGLShader fsrc(QOpenGLShader::Fragment,this);
fsrc.compileSourceFile("qrc:/shaders/frag.frag");
program->addShader(&fsrc);
program->link();
program->bind();
}
void Widget::resizeGL(int w, int h)
{
glViewport(0,0,w,h);
}
void Widget::paintGL()
{
static const GLfloat verc[] = {
1.0,1.0,
1.0,-1.0,
-1.0,-0.8
};
static const GLfloat colors[] = {
1.0,0.0,0.0,
1.0,0.0,0.0,
1.0,0.0,0.0
};
VBO.create();
VBO.bind();
VBO.allocate(0,15*sizeof(GL_FLOAT));
program->setAttributeBuffer(0,GL_FLOAT,0,2,0);
program->setAttributeBuffer(1,GL_FLOAT,6*sizeof(GLfloat),3,0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
VBO.write(0,verc,6*sizeof(GLfloat));
VBO.write(6*sizeof(GLfloat),colors,9*sizeof(GLfloat));
glDrawArrays(GL_TRIANGLES,0,3);
}
Resource.qrc:
<RCC>
<qresource prefix = 'shaders'>
<file>vertext.vert</file>
<file>frag.frag</file>
</qresource>
</RCC>
main.cpp:
#include "widget.h"
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSurfaceFormat f;
f.setVersion(3,3);
Widget *w = new Widget;
w->show();
return a.exec();
}
vertext.vert:
#version 330 core
layout(location=0) in vec4 vPosition;
layout(location=1) in vec3 vColor;
out vec4 ourColor;
void main(void)
{
gl_Position = vPosition;
ourColor = vec4(vColor,1.0);
}
frag.frag:
#version 330 core
in vec4 ourColor;
void main(void)
{
gl_FragColor = ourColor;
}
运行图片:
我需要一个彩色的,但调了半天也一直是黑白的,望好心人帮忙解决一下,有办法请@我哦