2017-02-07 22 views
-1

シェーダを使用してOpenGLで3D線を描画しようとしています...これまでの方法はありません。OpenGL-GLSL描画3D GL_LINES頂点シェーダ対頂点

私はうまく働いてこれを得た:

void draw_bonds (int nbonds) 
{ 
    glDisable (GL_LIGHTING); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    // in 3D each bonds is defined using 6 floats 
    // a.x, a.y, a.z, b.x, b.y and b.z 
    vertices = calloc(nbonds*6, sizeof*vertices); 

    // I will skip the following, basically I store coordinates 
    // in the table "vertices"  
    prepare_bonds (nbonds) 

    glVertexPointer (3, GL_FLOAT, 0, vertices); 
    glDrawArrays (GL_LINES, 0, nbonds); 

    glDisableClientState (GL_VERTEX_ARRAY); 
    glEnable (GL_LIGHTING); 
} 

しかし、その後、これはない:

#define GLSL(src) "#version 130\n" #src 

const GLchar * vertex = GLSL(
    uniform mat4 P; // projection matrix 
    uniform mat4 M; // modelview matrix 
    in vec3 position; 
    void main() 
    { 
    gl_Position = P * M * vec4 (position, 1.0); 
    } 
); 

/* Create and compile a shader */ 
static GLuint create_shader (int type, const GLchar * src) 
{ 
    GLuint shader; 
    int status; 

    shader = glCreateShader (type); 
    glShaderSource (shader, 1, & src, NULL); 
    glCompileShader (shader); 

    glGetShaderiv (shader, GL_COMPILE_STATUS, & status); 
    if (status == GL_FALSE) 
    { 
    int log_len; 
    char *buffer; 
    glGetShaderiv (shader, GL_INFO_LOG_LENGTH, & log_len); 
    buffer = g_malloc (log_len + 1); 
    glGetShaderInfoLog (shader, log_len, NULL, buffer); 
    g_warning ("Compile failure in %s shader:\n%s", 
       type == GL_VERTEX_SHADER ? "vertex" : "fragment", 
       buffer); 
    g_free (buffer); 
    glDeleteShader (shader); 
    return 0; 
    } 
    return shader; 
} 

GLuint program; 
GLuint posAttrib; 

static void init_shaders() 
{ 
    GLuint vertexShader = create_shader (GL_VERTEX_SHADER, vertex); 

    program = glCreateProgram(); 
    glAttachShader (program, vertexShader); 
    glBindAttribLocation (program, vertexShader, "position"); 

    glLinkProgram (program); 
    glUseProgram (program); 

    GLint lp = glGetUniformLocation (program, "P"); 
    GLint lm = glGetUniformLocation (program, "M"); 

    posAttrib = glGetAttribLocation (program, "position"); 

    // projection_matrix and model_view_matrix are the same 
    // as the ones used in the first version of the code. 
    glUniformMatrix4fv (lp, 1, GL_FALSE, projection_matrix); 
    glUniformMatrix4fv (lm, 1, GL_FALSE, model_view_matrix); 
} 

void draw_bonds (int nbonds) 
{ 
    glDisable (GL_LIGHTING); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    // in 3D each bonds is defined using 6 floats 
    // a.x, a.y, a.z, b.x, b.y and b.z 
    vertices = calloc(nbonds*6, sizeof*vertices); 

    // I will skip the following, basically I store coordinates 
    // in the table "vertices"  
    prepare_bonds (nbonds) 

    init_shaders(); 

    glGenVertexArrays (1, & vao); 
    glBindVertexArray (vao); 
    glGenBuffers (1, & vbo); 
    glBindBuffer(GL_ARRAY_BUFFER, vbo); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 

    glEnableVertexAttribArray (posAttrib); 
    glVertexAttribPointer (posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0) 

    glDrawArrays (GL_LINES, 0, nbonds); 

    glDisableClientState (GL_VERTEX_ARRAY); 
    glEnable (GL_LIGHTING); 
} 

を私はシェーダがうまくコンパイルされることを言及する必要があり、私はここで何をしないのですか?

+0

私はお勧めします。 1) 'glEnableClientState'は使用しないでください。シェーダーのない古いOGLのものでした。 2)エラー原因を検索するには、gl-callを実行するたびに 'glGetError()'を使います。 3) 'glBindVertexArray'の後にユニフォーム(glUniformxxx呼び出し)を渡します。 – Ripi2

+0

あなたの答えをありがとう、私はあなたの指示に従いました、今までのところ行はありません... –

+0

色を設定するフラグメントバッファーが表示されません。黒い背景の上に黒い線を描いているようです。 – Ripi2

答えて

0

[OK]を、エラーを発見し、それを呼び出し、これは間違っている:代わりに

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 

それがあるべき:

glBufferData(GL_ARRAY_BUFFER, nbonds*6*sizeof(float), vertices, GL_STATIC_DRAW); 

そしてそれはトリックです。

2

あなたは「位置」属性を結合するが、あなたのシェーダであなたは「POS」(in vec3 pos

+0

ありがとう、実際には私の実際のコードではなく、ポストの中でそのミスをしました。申し訳ありませんが、それを指摘してくれてありがとう、私はそれを訂正しました...したがってもちろんこれは問題ではありません... –

関連する問題