简体   繁体   中英

Can't pass color to FragmentShader

I'm trying to pass the color of my triangle through my main function, but when I try to do it my triangle only gets white like it has no fragment shader

Vertex Shader:

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

Fragment Shader:

#version 330 core

in vec3 color;
out vec4 FragColor;

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

main code:

 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 function:

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

As I'm written in C the functions I create have to pass the own struct as param si I have the functions begin to attribute to each function trying to simulate an object. The function begin shader gets the file with the shaders and compile them and generate the Program Shader, the begin VAO generate the vertex array object This the draw I have 在此处输入图像描述

The vertex shader doesn't compile for 2 reasons:

  1. There is missing a ; after gl_Position = position
  2. gl_Position and position have different types

gl_Position = position

gl_Position = vec4(position, 1.0);

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