简体   繁体   中英

OpenGL - shader test

I'd like to create a simple tool to test my shaders. It should try to compile them, and output any messages from the driver. However, I'm having trouble: it segfaults. This is the code:

#include <stdlib.h>

#include <GL/glew.h>
#include <GL/gl.h>


/* Test shader */
static const GLchar * vertexSource =
    "#version 150 core\n"
    "in vec2 position;"
    "in vec3 color;"
    "out vec3 Color;"
    "void main() {"
    "   Color = color;"
    "   gl_Position = vec4(position, 0.0, 1.0);"
    "}";


int main(void)
{
    GLuint vertexShader;

    glewExperimental = GL_TRUE;
    glewInit();

    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexSource, NULL);
    glCompileShader(vertexShader);
    glDeleteShader(vertexShader);

    return EXIT_SUCCESS;
}

No other messages other than Segmentation fault are emitted. Can anyone explain what is happening?

PS: FWIW, I'm using the R600g Mesa (10.0) driver on a Radeon HD 3200, GCC version 4.8.2.

Create a GL context and make it current before attempting to call glewInit() or any other GL-related functions.

You can use FreeGLUT to create a window and associated GL context:

#include <GL/glew.h>
#include <GL/freeglut.h>

int main(int argc, char **argv)
{
    glutInit( &argc, argv );
    glutInitWindowSize( 600, 600 );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutInitContextVersion( 3, 2 );
    glutInitContextProfile( GLUT_CORE_PROFILE );
    glutCreateWindow( "GLUT" );

    glewExperimental = GL_TRUE;
    glewInit();

    // attempt shader compile here...

    glutMainLoop();
    return 0;
}

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