顶点着色器到片段着色器插值

时间:2016-01-20 23:19:31

标签: c++ opengl glsl glfw

你好我正在编写一个使用OpenGL的程序,在窗口上渲染一个三角形。该程序有效,但我的三角形没有着色。它看起来是一个白色的三角形。我已多次查看代码,但我仍然无法找到错误的位置:

这是两个浮点数组,一个用于位置,另一个用于颜色:

GLfloat points[] = {
    0.0f,  0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
   -0.5f, -0.5f, 0.0f
};

GLfloat colours[] = {
    1.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 1.0f
};

接下来,我为每个数组创建顶点缓冲区对象:

GLuint points_vbo = 0;
glGenBuffers(1, &points_vbo);
glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

GLuint colour_vbo = 0;
glGenBuffers(1, &colour_vbo);
glBindBuffer(GL_ARRAY_BUFFER, colour_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(colours), colours, GL_STATIC_DRAW);

现在我使用顶点属性对象

设置这两个布局
GLuint vao = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, colour_vbo);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);

//enabling the first attribute; 0
glEnableVertexAttribArray(0);
//enabling the second attributes; 1
glEnableVertexAttribArray(1);

然后我创建了顶点着色器:

#version 330

layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 vertex_colour;

out vec3 colours;

void main(){
       colours = vertex_colour;
       gl_position = vec4(vertex_position, 1.0);
}

片段着色器:

#version 330

in vec3 colours;
out vec4 frag_colour;

void main(){
      frag_colour = vec4(colours, 1.0);
}

这里我链接了着色器:

//loading the strings into a GL shader
const char* vertexShader = "colorShading.vert";
const char* fragmentShader = "colorShading.frag";

//creating the input stream object
std::ifstream vertexFile(vertexShader);
//make sure the file stream is good
if (!vertexFile){
    std::cout << "The vertex shader failed to load" << std::endl;
    return -1;
}

//creating the input stream object
std::ifstream fragmentFile(fragmentShader);
//make sure the file stream is good
if (!fragmentFile){
    std::cout << "The fragment shader failed to load" << std::endl;
    return -1;
}

//vertexShader
//File contents stores all the text in the file
std::string vertexFileContents = "";
//line is used to grab each line of the file
std::string vertexLine;

//Get all the lines in the file and add it to the contents
while (std::getline(vertexFile, vertexLine)){
    vertexFileContents += vertexLine + "\n";
}

vertexFile.close();

//get a pointer to our file contents c string
const char* vertexContentsPtr = vertexFileContents.c_str();


//fragmentShader
//File contents stores all the text in the file
std::string fragmentFileContents = "";
//line is used to grab each line of the file
std::string fragmentLine;

//Get all the lines in the file and add it to the contents
while (std::getline(fragmentFile, fragmentLine)){
    fragmentFileContents += fragmentLine + "\n";
}

fragmentFile.close();

//get a pointer to our file contents c string
const char* fragmentContentsPtr = fragmentFileContents.c_str();

//loading the vertexShader string into a GL_SHADER
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertexContentsPtr, NULL);
glCompileShader(vs);

//loading the fragmentShader string into a GL_SHADER
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragmentContentsPtr, NULL);
glCompileShader(fs);

//combining the compiled shaders into a single executable GPU shader program
GLuint shader_program = glCreateProgram(); //creating an empty program
//attaching the shaders
glAttachShader(shader_program, fs);
glAttachShader(shader_program, vs);
//location binding code
glBindAttribLocation(shader_program, 0, "vertex_position");
glBindAttribLocation(shader_program, 1, "vertex_colour");
//linking the shaders together
glLinkProgram(shader_program);
glClearColor(0.6f, 0.6f, 0.8f, 1.0f);

//the rendering loop
while (!glfwWindowShouldClose(window)){
    //wipe the drawing surface clear
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glUseProgram(shader_program);
    glBindVertexArray(vao);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    //update other events like input handling
    glfwPollEvents();
    //put the stuff we've been drawing onto the display
    glfwSwapBuffers(window);

    //GLFW's keyboard handling to allow an escape key to close the window
    int state = glfwGetKey(window, GLFW_KEY_ESCAPE);
    if (state == GLFW_PRESS)
        glfwSetWindowShouldClose(window, 1);
}


//close GL context and any other GLFW resources
glfwTerminate();
return 0;
}

所以这里是所有相关的代码。正如我所说,我设法在屏幕上渲染三角形,但三角形是完全白色的。建议将不胜感激。

0 个答案:

没有答案