GLSL着色器变黑/透明

时间:2017-09-19 15:47:54

标签: opengl glsl shader

我正在尝试为我的过剩场景对象添加一些着色器。

此时我正在尝试实施“hello world”着色器 但是当我使用默认的顶点着色器时,我的对象会消失。

着色器:

#define GLSL(version, shader) "#version " #version " core\n" #shader
const char* vert = GLSL
(
330,

layout (std140) uniform Matrices {
        mat4 pvm;
} ;

in vec4 position;

out vec4 color;

void main()
{
    color = position;
    gl_Position = pvm * position ;
} 
);

const char* frag = GLSL
(
330,    

in vec4 color;

out vec4 outputF;

void main()
{
    outputF = vec4(1.0, 0.5, 0.25, 1.0);
} 

);

编译显示没有错误:

Compiling shader : vertex shader
VERTEX STATUS:1
Compiling shader : fragment shader
FRAGMENT STATUS:1
Linking program
PROGRAM STATUS:1
PROGRAM ID : 3

在致电glUseProgram之前:

enter image description here

致电glUseProgram后:

enter image description here

调用glUseProgram后没有附加顶点着色器: enter image description here

用于渲染的代码:

int opengl_draw_path_gl(rendered_path_t *p) {
    unsigned int num_vertices,j;
    unsigned int face_size;
    unsigned long i,num_elems;
    vect_t *a,*b;
    num_elems=p->num_prisms;
    num_vertices=p->prism_faces;
    face_size=num_vertices*2;
    a=p->data+2; // saltem punt centre primera cara
    b=a+face_size;

    glColor4fv(p->color);

    // dibuixem tapa inici
    _opengl_draw_path_terminator(num_vertices,p->data,a);

    // Dibuixem tots els prismes
    glBegin(GL_TRIANGLE_STRIP);
    for(i=0;i<num_elems;i++) {
        for(j=0;j<num_vertices;j++) {
            glNormal3fv((GLfloat *)(a+j*2));
            glVertex3fv((GLfloat *)(a+j*2+1));
            glNormal3fv((GLfloat *)(b+j*2));
            glVertex3fv((GLfloat *)(b+j*2+1));
        }
        glNormal3fv((GLfloat *)(a));
        glVertex3fv((GLfloat *)(a+1));
        glNormal3fv((GLfloat *)(b));
        glVertex3fv((GLfloat *)(b+1));
        a+=face_size;
        b+=face_size;
    }
    glEnd();

    // dibuixem tapa final
    _opengl_draw_path_terminator(num_vertices,b,a);
   return 0;
}

1 个答案:

答案 0 :(得分:2)

首先,我建议您阅读有关vertex array objects的教程。

但是,由于您正在使用glBeginglEnd deprecated绘图,因此您必须使用compatibility mode shaders。根据OpenGL命令gl_Vertexgl_Normal,您必须使用已弃用的内置制服glVertex3fvglNormal3fv

调整你的代码somhow:

#define GLSL(version, shader) "#version " #version "\n" #shader

顶点着色器:

const char* vert = GLSL
(
    110,

    varying vec4 position;
    varying vec3 normal;

    void main()
    {
        position    = gl_ModelViewMatrix * gl_Vertex;
        normal      = normalize( gl_NormalMatrix * gl_Normal.xyz );
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    } 
);

片段着色器:

const char* frag  = GLSL
(
    110,

    varying vec4 position;
    varying vec3 normal;

    void main()
    {
        gl_FragColor = vec4(1.0, 0.5, 0.25, 1.0);
    } 
);