我正在尝试使用着色器在OpenGL中绘制3D线...到目前为止无法做到这一点。
我的工作正常:
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 :(得分:2)
您绑定属性“position”,但在着色器中,您将其称为“pos”(in vec3 pos
)
答案 1 :(得分:0)
好的发现错误,这是错误的:
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
应该是:
glBufferData(GL_ARRAY_BUFFER, nbonds*6*sizeof(float), vertices, GL_STATIC_DRAW);
它可以解决问题。