无法将颜色传递给 FragmentShader

Can't pass color to FragmentShader

我试图通过我的主要函数传递我的三角形的颜色,但是当我尝试这样做时我的三角形只变成白色,就像它没有片段着色器一样

顶点着色器:

#version 330 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 aColor;

out vec3 color;

void main()
{
    gl_Position = position
    color = aColor;
}

片段着色器:

#version 330 core

in vec3 color;
out vec4 FragColor;

void main()
{
 FragColor = vec4(color, 1.0);
};

主要代码:

 float positions[] = {
      -0.5f, -0.5f, 0.f, 0.8f, 0.3f, 0.02f,                       //inferior esquerdo
      0.5, -0.5f, 0.f, 0.8f, 0.3f, 0.2f,                          //inferior direito
      0.0f, (sqrt(3.f) - 1.f) / 2.f, 0.f, 1.f, 0.6f, 0.32f,       //Topo
      -0.5f / 2.f, (0.37f - 0.5f) / 2.f, 0.f, 0.9f, 0.45f, 0.17f, //Meio esquerdo
      0.5f / 2, (0.37f - 0.5f) / 2.f, 0.f, 0.9f, 0.45f, 0.17f,    //Meio direito
      0.f, -0.5f, 0.f, 0.8f, 0.3f, 0.02f};                        //Meio
 unsigned int indices[9] =
      {
          0, 5, 3,
          5, 1, 4,
          3, 4, 2};

  unsigned int buffer, attribute, EBO;
  //Gera o Shader
  struct Shader shaderProgram = beginShader("res/shaders/sv1.shader", "res/shaders/sf1.shader");
  //Gerao Vertex Array Object e bainda ele
  struct VAO vao1 = beginVAO();
  vao1.Bind(vao1);

  //Gera o Vertex Buffer linkando-o ao vértice
  struct VBO vbo1 = beginVBO(positions, sizeof(positions));
  //Gera o Element Buffer e linka ele com os índices do vetor posições
  struct EBO ebo1 = beginEBO(indices, sizeof(indices));

  //Conecta o VBO ao VAO
  vao1.LinkAttrib(vbo1, 0, 3, GL_FLOAT, 6 * sizeof(float), (void *)0);
  vao1.LinkAttrib(vbo1, 1, 3, GL_FLOAT, 6 * sizeof(float), (void *)(3 * sizeof(float)));
  //Unbind em todos os objetos que foram bindados
  vao1.Unbind(vao1);
  vbo1.Unbind(vbo1);
  ebo1.Unbind(ebo1);

  while (true)
  {
    if (SDL_PollEvent(&windowEvent))
    {
      if (SDL_QUIT == windowEvent.type)
      {
        break;
      }
    }
    glClearColor(0.05f, 0.57f, 0.38f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    shaderProgram.Activate(shaderProgram);
    vao1.Bind(vao1);
    glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_INT, 0);
    glEnd();
    SDL_GL_SwapWindow(window); //update the window
  }
  vao1.Delete(vao1);
  vbo1.Delete(vbo1);
  ebo1.Delete(ebo1);
  shaderProgram.Delete(shaderProgram);
  SDL_DestroyWindow(window);
  SDL_Quit();

LinkAttrib 函数:

void LinkAttrib(struct VBO VBO, GLuint layout, GLuint numComponents, GLenum type, 
GLsizeiptr stride, void *offset)
{
 VBO.Bind(VBO);
 glVertexAttribPointer(layout, numComponents, type, GL_FALSE, stride, offset);
 glEnableVertexAttribArray(layout);
 VBO.Unbind(VBO);
}

因为我是用 C 编写的,所以我创建的函数必须将自己的结构作为参数 si 传递,我让函数开始归因于每个试图模拟对象的函数。函数 begin shader 获取带有着色器的文件并编译它们并生成 Program Shader,begin VAO 生成顶点数组对象 这是我的平局

顶点着色器无法编译有两个原因:

  1. gl_Position = position
  2. 之后少了一个;
  3. gl_Positionposition有不同的类型

gl_Position = position

gl_Position = vec4(position, 1.0);