🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

[Fragment shader problem]

Started by
9 comments, last by 8Observer8 1 year, 11 months ago

Hello guys,

I hope you're all doing well.

So I'm trying to render a 3D model ( OBJ file) and after creating vertex and fragment shaders, I got this error:

Fragment shader contains a user varying, but is linked without a vertex shader.

Out of resource error

I don't really get what's happening.

here is the vertex shader:

#version 330 core
layout(locaiton=0) in vec4 position;

layout(location=1) in vec4 color;
uniform mat4 u_mvp;

out vec4 v_color;
void main (void)

{

gl_Position=u_mvp * position;

v_color=color;

}

and here is my fragment shader:

#version 330 core
in vec4 v_color;
void main(void)

{

gl_FragColor=v_color;

}

Also the application crashes and I get :

ASSERT: "QOpenGLFunctions::isInitialized(d_ptr)"

I searched on the internet and found that I should intialize OpenGL functions in my intializeGL() function. However I've already done that and I still get that error.

Can someone please tell what's wrong with my app.

Thank you all and have a good day.

Advertisement

The fragment shader error is solved: it was a spelling mistake, instead of writing location, I wrote locaition.

Sorry.

However I'm still struggling with the second error: OpenGL functions were initialized in my initializeGL() function but the app is still crashing because of this line:

glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

When I comment this line, the app doesn't crash.

( I couldn't modify the title of the post since now the problem is related to OpenGL functions intialization).

Is it possible that the buffer you're trying to clear wasn't set up properly?

Yeah you need to give us some more context than just the clear call because we have no idea which buffer you are trying to clear here.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Probably you are calling this

glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

before initializing the opengl context

Check every call for returning errors, and print those if they happen (and fix them of course!).

If you ignore such things and blindly move on then of course at some point the system gives up.

They are referring to glGetError(). Call this before and see what might be setup wrong. Try putting in something like this:

static bool test = false;

if(test){ glClear…..}.
Pause your program and manually change test to true in the debugger. If that works, then as others said one of your other GL calls may have needed to be called before this one.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

if you are using GLU you can translate the error codes with gluGetErrorString(glGetErro()) into something you can print out and read without constantly having to look up the error code.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

A good trick is to, instead of simply saying that `glClear` is the culprit - try several OpenGL functions in the same spot to see, whether an OpenGL context has actually been set up for you to use. It could be literally anything. This also depends on what library you are using to create the window - most libraries will create an OpenGL context for you while creating the window, so this shouldn't be an issue.

You should probably use GLFW - it has very good documentation and widespread usage and support (plus an active community fixing bugs). You may also try SDL.

If NONE of the OpenGL functions work for you, then it is because you forgot to fetch the OpenGL implementation functions from your graphics driver. With OpenGL this is done by using a library such as GLEW or GLAD. If you omit to do this (which must be done AFTER window creation), then naturally OpenGL will not work for you! ?

QOpenGLFunctions

You use Qt. My example draws a cube. Maybe it will help you. It is Qt 5.15.2

// Add this line to .pro:
// win32: LIBS += -lopengl32

#ifdef _WIN32
#include <windows.h>
extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x00000001;
#endif

#include <QtWidgets/QApplication>
#include <QtWidgets/QOpenGLWidget>
#include <QtGui/QOpenGLShaderProgram>
#include <QtGui/QOpenGLBuffer>
#include <QtGui/QMatrix4x4>

class OpenGLWidget : public QOpenGLWidget {
    Q_OBJECT
public:
    OpenGLWidget(QWidget *parent = nullptr)
        : QOpenGLWidget(parent)
        , m_indexBuffer(QOpenGLBuffer::IndexBuffer)
    {
        setWindowTitle("Qt C++, OpenGL");
        resize(268, 268);
    }
private:
    QOpenGLShaderProgram m_program;
    QOpenGLBuffer m_vertPosBuffer;
    QOpenGLBuffer m_normalBuffer;
    QOpenGLBuffer m_indexBuffer;
    QMatrix4x4 m_projMatrix;
    QMatrix4x4 m_viewMatrix;
    QMatrix4x4 m_modelMatrix;
    int m_amountOfVertices;

    void initializeGL() override {
        glClearColor(0.5f, 0.8f, 0.7f, 1.f);
        glEnable(GL_DEPTH_TEST);
        const char *vertShaderSrc =
                "#version 330 core\n"
                "in vec3 aPosition;"
                "in vec4 aNormal;"
                "uniform mat4 uMvpMatrix;"
                "uniform mat4 uNormalMatrix;"
                "out vec4 vColor;"
                "void main()"
                "{"
                "    gl_Position = uMvpMatrix * vec4(aPosition, 1.0);"
                "    vec3 lightDirection = normalize(vec3(0.0, 0.5, 0.7));"
                "    vec4 color = vec4(1.0, 0.4, 0.0, 1.0);"
                "    vec3 normal = normalize((uNormalMatrix * aNormal).xyz);"
                "    float nDotL = max(dot(normal, lightDirection), 0.0);"
                "    vColor = vec4(color.rgb * nDotL + vec3(0.1), color.a);"
                "}";
        const char *fragShaderSrc =
                "#version 330 core\n"
                "in vec4 vColor;"
                "out vec4 fragColor;"
                "void main()"
                "{"
                "    fragColor = vColor;"
                "}";
        m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertShaderSrc);
        m_program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragShaderSrc);
        m_program.link();
        m_program.bind();
        m_amountOfVertices = initVertexBuffers();
        m_viewMatrix.lookAt(
                    QVector3D(20.f, 15.f, 30.f), // eye position
                    QVector3D(0.f, 0.f, 0.f),    // center
                    QVector3D(0.f, 1.f, 0.f));   // up
        m_modelMatrix.translate(QVector3D(0.f, 0.f, 0.f));
        m_modelMatrix.rotate(0.f, QVector3D(0.f, 1.f, 0.f));
        m_modelMatrix.scale(7.f, 7.f, 7.f);
    }
    void paintGL() override {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        drawBox();
    }
    void drawBox() {
        QMatrix4x4 mvpMatrix = m_projMatrix * m_viewMatrix * m_modelMatrix;
        m_program.bind();
        m_program.setUniformValue("uMvpMatrix", mvpMatrix);
        QMatrix4x4 normalMatrix;
        normalMatrix = m_modelMatrix.inverted();
        normalMatrix = normalMatrix.transposed();
        m_program.setUniformValue("uNormalMatrix", normalMatrix);
        glDrawElements(GL_TRIANGLES, m_amountOfVertices, GL_UNSIGNED_INT, nullptr);
    }
    void resizeGL(int w, int h) override {
        glViewport(0, 0, w, h);
        m_projMatrix.setToIdentity();
        m_projMatrix.perspective(50.f, (float)width()/height(), 0.1f, 100.f);
    }
    int initVertexBuffers() {
        // Create a cube
        //    v6----- v5
        //   /|      /|
        //  v1------v0|
        //  | |     | |
        //  | |v7---|-|v4
        //  |/      |/
        //  v2------v3
        float vertPositions[] = {
            // v0-v1-v2-v3 front
            1.f, 1.f, 1.f, -1.f, 1.f, 1.f, -1.f, -1.f, 1.f, 1.f, -1.f, 1.f,
            // v0-v3-v4-v5 right
            1.f, 1.f, 1.f, 1.f, -1.f, 1.f, 1.f, -1.f, -1.f, 1.f, 1.f, -1.f,
            // v0-v5-v6-v1 up
            1.f, 1.f, 1.f, 1.f, 1.f, -1.f, -1.f, 1.f, -1.f, -1.f, 1.f, 1.f,
            // v1-v6-v7-v2 left
           -1.f, 1.f, 1.f, -1.f, 1.f, -1.f, -1.f, -1.f, -1.f, -1.f, -1.f, 1.f,
            // v7-v4-v3-v2 down
           -1.f, -1.f, -1.f, 1.f, -1.f, -1.f, 1.f, -1.f, 1.f, -1.f, -1.f, 1.f,
            // v4-v7-v6-v5 back
            1.f, -1.f, -1.f, -1.f, -1.f, -1.f, -1.f, 1.f, -1.f, 1.f, 1.f, -1.f
        };
        m_vertPosBuffer.create();
        m_vertPosBuffer.bind();
        m_vertPosBuffer.allocate(vertPositions, sizeof(vertPositions));
        m_program.bindAttributeLocation("aPosition", 0);
        m_program.setAttributeBuffer(0, GL_FLOAT, 0, 3);
        m_program.enableAttributeArray(0);
        float normals[] = {
            // v0-v1-v2-v3 front
            0.f, 0.f, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f,
            // v0-v3-v4-v5 right
            1.f, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f,
            // v0-v5-v6-v1 up
            0.f, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f, 0.f,
            // v1-v6-v7-v2 left
            -1.f, 0.f, 0.f, -1.f, 0.f, 0.f, -1.f, 0.f, 0.f, -1.f, 0.f, 0.f,
            // v7-v4-v3-v2 down
            0.f, -1.f, 0.f, 0.f, -1.f, 0.f, 0.f, -1.f, 0.f, 0.f, -1.f, 0.f,
            // v4-v7-v6-v5 back
            0.f, 0.f, -1.f, 0.f, 0.f, -1.f, 0.f, 0.f, -1.f, 0.f, 0.f, -1.f
        };
        m_normalBuffer.create();
        m_normalBuffer.bind();
        m_normalBuffer.allocate(normals, sizeof(normals));
        m_program.bindAttributeLocation("aNormal", 1);
        m_program.setAttributeBuffer(1, GL_FLOAT, 0, 3);
        m_program.enableAttributeArray(1);
        int indices[] = {
            0, 1, 2, 0, 2, 3,           // front
            4, 5, 6, 4, 6, 7,           // right
            8, 9, 10, 8, 10, 11,        // up
            12, 13, 14, 12, 14, 15,     // left
            16, 17, 18, 16, 18, 19,     // down
            20, 21, 22, 20, 22, 23      // back
        };
        m_indexBuffer.create();
        m_indexBuffer.bind();
        m_indexBuffer.allocate(indices, sizeof(indices));

        int amountOfVertices = sizeof(indices) / sizeof(indices[0]);
        return amountOfVertices;
    }
};

#include "main.moc"

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    OpenGLWidget w;
    w.show();
    return a.exec();
}

This topic is closed to new replies.

Advertisement