gpt4 book ai didi

c - OpenGL-GLSL 绘制 3D GL_LINES 顶点着色器与顶点

转载 作者:行者123 更新时间:2023-11-30 18:49:42 25 4
gpt4 key购买 nike

我正在尝试使用着色器在 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);
}

我必须提到着色器编译得很好,那么我在这里缺少什么?

最佳答案

您绑定(bind)了属性“position”,但在着色器中您将其称为“pos”(in vec3 pos)

关于c - OpenGL-GLSL 绘制 3D GL_LINES 顶点着色器与顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42090891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com