简体   繁体   中英

OpenGL-GLSL drawing 3D GL_LINES vertex shaders vs. vertex

I am trying to draw 3D lines in OpenGL using shaders ... so far no way to do it.

I got this working just fine:

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);
}

But then this does not:

#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);
}

I have to mention that the shader compiles just fine, so what am I missing here ?

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

Ok found the error, this is wrong:

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

instead it should be:

glBufferData(GL_ARRAY_BUFFER, nbonds*6*sizeof(float), vertices, GL_STATIC_DRAW);

And it does the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM