OpenGL-GLSL 绘图 3D GL_LINES 顶点着色器与顶点
OpenGL-GLSL drawing 3D GL_LINES vertex shaders vs. vertex
我正在尝试使用着色器在 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);
}
我不得不提到着色器编译得很好,所以我在这里缺少什么?
您绑定了属性 "position",但在您的着色器中您称它为 "pos"(in vec3 pos
)
ok 发现错误了,这里是错误的:
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
应该是:
glBufferData(GL_ARRAY_BUFFER, nbonds*6*sizeof(float), vertices, GL_STATIC_DRAW);
它确实有效。
我正在尝试使用着色器在 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);
}
我不得不提到着色器编译得很好,所以我在这里缺少什么?
您绑定了属性 "position",但在您的着色器中您称它为 "pos"(in vec3 pos
)
ok 发现错误了,这里是错误的:
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
应该是:
glBufferData(GL_ARRAY_BUFFER, nbonds*6*sizeof(float), vertices, GL_STATIC_DRAW);
它确实有效。