2013-01-22 144 views
2

我試圖通過頂點顏色低谷的頂點,幾何和片段着色器:如何通過着色器管道傳遞頂點顏色?

glBegin(GL_POINTS); 
    glVertex3f(-2.0f, 0.0f, 0.0); glColor3f(0.0,1.0,0.0); 
    glVertex3f(+2.0f, 0.0f, 0.0); glColor3f(0.0,0.0,1.0); 
glEnd(); 

頂點着色器:

# version 130 
varying vec4 colorv; 
void main() { 
    // pass trough: 
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex; 
    colorv = gl_Color; 
} 

幾何着色器:

#version 150 

layout(points) in; // origo of cell 
layout(points, max_vertices = 1) out; 

varying vec4 colorv; 
varying vec4 color; 
void main (void) 
{ 
    for(int i = 0; i < gl_in.length(); ++i) 
    { 
     color = colorv; 
     gl_Position = gl_in[i].gl_Position; 
     EmitVertex(); 
     EndPrimitive();  
    } 
} 

片段着色器:

# version 130 
varying vec4 color; 
void main (void) 
{ 
    // pass-trough:  
    gl_FragColor = color; 
} 

然而,它不工作:-(。兩點都會變成灰色。 我該怎麼做?

+2

調用'glColor()' *之前* glVertex()'你想要它應用。 – genpfault

+2

爲什麼不在所有着色器中使用'#version 150'? – genpfault

+1

@genpfault:1. colorv由頂點着色器寫入2.謝謝!但問題依然存在。 3.當涉及到片段着色器時,我發現使用gl_FragColor更容易(尚未理解如何在版本150中執行此操作)。 – Andy

回答

2

嘗試從此改變你的幾何着色器的輸入/輸出中聲明:

varying vec4 colorv; 
varying vec4 color; 

這樣:

in vec4 colorv[]; 
out vec4 color; 

像這樣:

#include <GL/glew.h> 
#include <GL/glut.h> 
#include <iostream> 
#include <vector> 

using namespace std; 

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

const GLchar* vert = GLSL 
( 
    130, 
    varying vec4 colorv; 
    void main() { 
     // pass trough: 
     gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex; 
     colorv = gl_Color; 
    } 
); 

const GLchar* geom = GLSL 
( 
    150, 
    layout(points) in; // origo of cell 
    layout(points, max_vertices = 1) out; 

    in vec4 colorv[]; 
    out vec4 color; 
    void main (void) 
    { 
     for(int i = 0; i < gl_in.length(); ++i) 
     { 
      color = colorv[i]; 
      gl_Position = gl_in[i].gl_Position; 
      EmitVertex(); 
     } 
     EndPrimitive();  
    } 
); 

const GLchar* frag = GLSL 
(
    130, 
    varying vec4 color; 
    void main (void) 
    { 
     // pass-trough:  
     gl_FragColor = color; 
    } 
); 

GLuint LoadShader(GLenum type, const string& aShaderSrc) 
{ 
    GLuint shader = glCreateShader(type); 
    const char* srcPtr = aShaderSrc.c_str(); 
    glShaderSource(shader, 1, &srcPtr, NULL); 
    glCompileShader(shader); 
    return shader; 
} 

GLuint LoadProgram(const string& aVertSrc, const string& aGeomSrc, const string& aFragSrc) 
{ 
    GLuint vert = LoadShader(GL_VERTEX_SHADER, aVertSrc); 
    GLuint geom = LoadShader(GL_GEOMETRY_SHADER, aGeomSrc); 
    GLuint frag = LoadShader(GL_FRAGMENT_SHADER, aFragSrc); 
    GLuint program = glCreateProgram(); 
    glAttachShader(program, vert); 
    glAttachShader(program, geom); 
    glAttachShader(program, frag); 
    glLinkProgram(program); 
    return program; 
} 

GLuint prog = 0; 
void display() 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    double w = glutGet(GLUT_WINDOW_WIDTH); 
    double h = glutGet(GLUT_WINDOW_HEIGHT); 
    double ar = w/h; 
    glOrtho(-2 * ar, 2 * ar, -2, 2, -1, 1); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glUseProgram(prog); 

    glBegin(GL_POINTS); 
    glColor3ub(0, 255, 0); 
    glVertex3f(-1.0f, 0.0f, 0.0); 
    glColor3ub(0, 0, 255); 
    glVertex3f(+1.0f, 0.0f, 0.0); 
    glEnd(); 

    glutSwapBuffers(); 
} 

int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); 
    glutInitWindowSize(640, 480); 
    glutCreateWindow("Geometry Shaders"); 
    glewInit(); 

    prog = LoadProgram(vert, geom, frag); 

    glutDisplayFunc(display); 
    glutMainLoop(); 
    return 0; 
} 
+1

工作正常!感謝genpfault!這樣一個小變化,但有很大的影響:-)。 – Andy