@IliaNeverov

Как исправить ошибки QOpenGLShader: Unable to open file «shaders/vertexshader1.vsh» QOpenGLShader: Unable to open file «shaders/fragmentshader1.fsh»?

Всем доброго времени суток, скажите пожалуйста почему при запуске программы qt выдает что шейдеры(вершинный и фрагментный) не открываются и как это исправить?
вот мои исходники:
структура проекта-
5fa4e3d2a533b384988463.png
mainwindow.h -
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMatrix4x4>
#include <QMainWindow>
#include <QGLWidget>
#include <QOpenGLWidget>
#include <QOpenGLShaderProgram>
#include "QOpenGLTexture"
#include "QOpenGLBuffer"
#include <QVector3D>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

struct Vertexofcube {
    Vertexofcube(){

    }
    Vertexofcube(QVector3D p, QVector2D t, QVector3D n):
    position(p), textureposition(t), normal(n)
    {

    }

    QVector3D position;
    QVector2D textureposition;
    QVector3D normal;
};
class MainWindow : public QOpenGLWidget
{
    Q_OBJECT

public slots:
     void FixedPaint();

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    int nWidth ,nHeight; // Размеры окна нашей программы
    void initializeGL(); // Метод для инициализирования opengl
    void resizeGL(int nWidth, int nHeight); // Метод вызываемый после каждого изменения размера окна
    void paintGL(); // Метод для вывода изображения на экран
    void initCube();
    void initShaders();
    float width;
    float x,y,z;
protected:

  QOpenGLBuffer ArrayBuffer1, IndexBuffer1;
  QOpenGLTexture *texture1;
  QOpenGLShaderProgram shaderprogram;
  QMatrix4x4 projectionmatrix;

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp -
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMatrix4x4>

void MainWindow::FixedPaint()
{

}

MainWindow::MainWindow(QWidget *parent)
    : QOpenGLWidget(parent)
    ,IndexBuffer1(QOpenGLBuffer::IndexBuffer), texture1(0)
{


}

void MainWindow::initializeGL()
{
    glClearColor(0.0,0.0,0.0,1);
    glViewport( 0, 0, nWidth, nHeight );
            glLoadIdentity();
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    initShaders();
    initCube();
    shaderprogram.link();
}

void MainWindow::resizeGL(int nWidth, int nHeight)
{
    float aspect = nWidth / (float)nHeight;
    projectionmatrix.setToIdentity();
    projectionmatrix.perspective(45, aspect, 0.1, 10);
}

void MainWindow::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    QMatrix4x4 modelViewMatrix;
    modelViewMatrix.setToIdentity();
    modelViewMatrix.translate(0.0f, 0.0f, -5.0f);

    texture1->bind(0);

    //m_shaderProgramm.bind();
    shaderprogram.setUniformValue("MVPMatrix", projectionmatrix * modelViewMatrix);
    shaderprogram.setUniformValue("qt_Texture0", 0);


    // Bind buffers
    ArrayBuffer1.bind();
    IndexBuffer1.bind();

    int offset = 0;

    int location = shaderprogram.attributeLocation("a_position");
    shaderprogram.enableAttributeArray(location);
    shaderprogram.setAttributeBuffer(location, GL_FLOAT, offset, 3, sizeof(Vertexofcube));

    offset += sizeof(QVector3D);

    location = shaderprogram.attributeLocation("a_textcoord0");
    shaderprogram.enableAttributeArray(location);
    shaderprogram.setAttributeBuffer(location, GL_FLOAT, offset, 2, sizeof(Vertexofcube));



    glDrawElements(GL_TRIANGLES, IndexBuffer1.size(), GL_UNSIGNED_INT, 0);
    shaderprogram.link();

}

void MainWindow::initCube()
{
    float width_div_2 =  1.0f;
    QVector<Vertexofcube> vertexes;
    // Top side
    vertexes.append(Vertexofcube(QVector3D(width_div_2, width_div_2, width_div_2), QVector2D(0.0f, 1.0f), QVector3D(0.0f, 1.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(width_div_2, width_div_2, -width_div_2), QVector2D(0.0f, 0.0f), QVector3D(0.0f, 1.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, width_div_2, width_div_2), QVector2D(1.0f, 1.0f), QVector3D(0.0f, 1.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, width_div_2, -width_div_2), QVector2D(1.0f, 0.0f), QVector3D(0.0f, 1.0f, 0.0f)));


    // Down side
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, -width_div_2, width_div_2), QVector2D(0.0f, 1.0f), QVector3D(0.0f, -1.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, -width_div_2, -width_div_2), QVector2D(0.0f, 0.0f), QVector3D(0.0f, -1.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(width_div_2, -width_div_2, width_div_2), QVector2D(1.0f, 1.0f), QVector3D(0.0f, -1.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(width_div_2, -width_div_2, -width_div_2), QVector2D(1.0f, 0.0f), QVector3D(0.0f, -1.0f, 0.0f)));

    // Left side
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, width_div_2, width_div_2), QVector2D(0.0f, 1.0f), QVector3D(-1.0f, 0.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, width_div_2, -width_div_2), QVector2D(0.0f, 0.0f), QVector3D(-1.0f, 0.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, -width_div_2, width_div_2), QVector2D(1.0f, 1.0f), QVector3D(-1.0f, 0.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, -width_div_2, -width_div_2), QVector2D(1.0f, 0.0f), QVector3D(-1.0f, 0.0f, 0.0f)));

    // Right side
    vertexes.append(Vertexofcube(QVector3D(width_div_2, width_div_2, width_div_2), QVector2D(0.0f, 1.0f), QVector3D(1.0f, 0.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(width_div_2, -width_div_2, width_div_2), QVector2D(0.0f, 0.0f), QVector3D(1.0f, 0.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(width_div_2, width_div_2, -width_div_2), QVector2D(1.0f, 1.0f), QVector3D(1.0f, 0.0f, 0.0f)));
    vertexes.append(Vertexofcube(QVector3D(width_div_2, -width_div_2, -width_div_2), QVector2D(1.0f, 0.0f), QVector3D(1.0f, 0.0f, 0.0f)));

    // Front side
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, width_div_2, width_div_2), QVector2D(0.0f, 1.0f), QVector3D(0.0f, 0.0f, 1.0f)));
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, -width_div_2, width_div_2), QVector2D(0.0f, 0.0f), QVector3D(0.0f, 0.0f, 1.0f)));
    vertexes.append(Vertexofcube(QVector3D(width_div_2, width_div_2, width_div_2), QVector2D(1.0f, 1.0f), QVector3D(0.0f, 0.0f, 1.0f)));
    vertexes.append(Vertexofcube(QVector3D(width_div_2, -width_div_2, width_div_2), QVector2D(1.0f, 0.0f), QVector3D(0.0f, 0.0f, 1.0f)));

    // Back side
    vertexes.append(Vertexofcube(QVector3D(width_div_2, width_div_2, -width_div_2), QVector2D(0.0f, 1.0f), QVector3D(0.0f, 0.0f, -1.0f)));
    vertexes.append(Vertexofcube(QVector3D(width_div_2, -width_div_2, -width_div_2), QVector2D(0.0f, 0.0f), QVector3D(0.0f, 0.0f, -1.0f)));
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, width_div_2, -width_div_2), QVector2D(1.0f, 1.0f), QVector3D(0.0f, 0.0f, -1.0f)));
    vertexes.append(Vertexofcube(QVector3D(-width_div_2, -width_div_2, -width_div_2), QVector2D(1.0f, 0.0f), QVector3D(0.0f, 0.0f, -1.0f)));

    QVector <GLuint> indexes;
    for(int i=0; i<24; i+=4){
      indexes.append(0 + i);
      indexes.append(1 + i);
      indexes.append(2 + i);
      indexes.append(2 + i);
      indexes.append(1 + i);
      indexes.append(3 + i);

    }
    ArrayBuffer1.create();
    ArrayBuffer1.bind();
    ArrayBuffer1.allocate(vertexes.constData(), vertexes.size() * sizeof(Vertexofcube));
    ArrayBuffer1.release();

    IndexBuffer1.create();
    IndexBuffer1.bind();
    IndexBuffer1.allocate(vertexes.constData(), vertexes.size() * sizeof(GLuint));
    IndexBuffer1.release();



    texture1 = new QOpenGLTexture(QImage("textures/travka.png").mirrored());

    texture1->setMinificationFilter(QOpenGLTexture::Nearest);
    texture1->setMagnificationFilter(QOpenGLTexture::Linear);
    texture1->setWrapMode(QOpenGLTexture::Repeat);
}

void MainWindow::initShaders()
{
  if (!shaderprogram.addShaderFromSourceFile(QOpenGLShader::Vertex,"shaders/vertexshader1.vsh")){
      close();
  }
  if (!shaderprogram.addShaderFromSourceFile(QOpenGLShader::Fragment,"shaders/fragmentshader1.fsh")){
      close();
  }
  if (!shaderprogram.link()){
      close();
  }
  if (!shaderprogram.bind())
      close();

}


MainWindow::~MainWindow()
{
    delete ui;
}
  • Вопрос задан
  • 155 просмотров
Решения вопроса 1
Скорее всего проблемы с путями. Укажите полный путь к файлам vertexshader1.vsh, shaders/fragmentshader1.fsh
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы