简体   繁体   English

无法将颜色传递给 FragmentShader

[英]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我试图通过我的主要 function 传递我的三角形的颜色,但是当我尝试这样做时,我的三角形只会变成白色,就像它没有片段着色器一样

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:链接属性 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.正如我在 C 中所写的那样,我创建的函数必须将自己的结构作为参数 si 我有函数开始归因于每个 function 试图模拟 ZA8CFDE6331BD59EB2AC96F8911C4B6 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 function 开始着色器获取带有着色器的文件并编译它们并生成程序着色器,开始 VAO 生成顶点数组 object 这是我的画在此处输入图像描述

The vertex shader doesn't compile for 2 reasons:顶点着色器无法编译有两个原因:

  1. There is missing a ;缺少一个; after gl_Position = positiongl_Position = position之后
  2. gl_Position and position have different types gl_Positionposition有不同的类型

gl_Position = position

gl_Position = vec4(position, 1.0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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