Answer the question
In order to leave comments, you need to log in
How to fix errors QOpenGLShader: Unable to open file "shaders/vertexshader1.vsh" QOpenGLShader: Unable to open file "shaders/fragmentshader1.fsh"?
Good day to all, please tell me why, when starting the program, qt gives out that the shaders (vertex and fragment) do not open and how to fix it?
here are my sources:
project structure
- 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
#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;
}
Answer the question
In order to leave comments, you need to log in
Most likely a problem with the paths. Specify the full path to the vertexshader1.vsh, shaders/fragmentshader1.fsh files
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question