简体   繁体   中英

Qt - GLUT - vertex shader program link

I have coded a N-Body/GPU program with OpenCL and GLUT functions, and it works fine. I try now to convert it in order to get the main GLUT window into a QGLWidget subclass ("here GLWidget") with a Qt graphical interface.

My problem is that the "program vertex shader" link fails. Here is this part of the source file "GLWidget.cpp" where I get in " _compileProgram " the error " Failed to link program " :

extern const char *vertexShader;

void GLWidget::GLInit()
{
    LoadGLTextures();  // load the textures.
    glClearColor(0.0 ,0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);    
    glLoadIdentity();

    m_program = _compileProgram(vertexShader);

    glClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
    glClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);

    // memsize of GPU data 
    unsigned int memSize = sizeof(cl_double4) * 4 * Galaxy->getNumParticles();

    createVBO(memSize);
}

void GLWidget::createVBO(uint size)
{
    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, size, Galaxy->pos, GL_DYNAMIC_DRAW);
}

GLuint GLWidget::_compileProgram(const char *vsource)
{
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vsource, 0);
    glCompileShader(vertexShader);
    GLuint program = glCreateProgram();
    glAttachShader(program, vertexShader);
    glLinkProgram(program);

    // check if program linked
    GLint success = 0;
    glGetProgramiv(program, GL_LINK_STATUS, &success);

    if (!success) {
        char temp[256];
        glGetProgramInfoLog(program, 256, 0, temp);
        printf("Failed to link program:\n%s\n", temp);
        glDeleteProgram(program);
        program = 0;
}
    return program;
}

I use the following vertex shader program in shader.cpp :

#define STRINGIFY(A) #A

// vertex shader
const char *vertexShader = STRINGIFY(
uniform float pointRadius;  // point size in world space
uniform float pointScale;   // scale to calculate size in pixels
void main()
{
    // calculate window-space point size
    vec3 posEye = vec3(gl_ModelViewMatrix * vec4(gl_Vertex.xyz, 1.0));
    float dist = length(posEye);
    gl_PointSize = pointRadius * (pointScale / dist);

    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position = gl_ModelViewProjectionMatrix * vec4(gl_Vertex.xyz, 1.0);

    gl_FrontColor = gl_Color;
}
);

I make you notice that the "GLuint m_program" is a data member of the subclass GLWidget.

Anyone could see what's wrong?

More generally, can I use directly the same GLUT functions " glAttachShader ", " glLinkProgram " on a GLWidget object like I did it with the first version of my code (ie without Qt user interface)?

Depending on which OpenGL / GLSL version you're targeting, don't you need a trivial fragment shader in order to link successfully? You mention OpenGL 4.1 but your vertex shader code is using GLSL variable names which are deprecated in GLSL 3.3 (gl_TexCoord, for example)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM