使用着色器渲染“顶点彩色”三角形时的OpenGL“黑屏”

Posted

技术标签:

【中文标题】使用着色器渲染“顶点彩色”三角形时的OpenGL“黑屏”【英文标题】:OpenGL "black screen" when rendering "vertex colored" triangle using shader 【发布时间】:2016-02-28 01:57:59 【问题描述】:

我是 OpenGL 的新手,一直在学习这个非常简单的教程,我应该在其中创建一个顶点颜色的三角形 (the tutorial in question)。

虽然,我的结果只是黑屏。 我已经尝试完全复制他的代码,但它仍然不起作用,这让我相信教程已经过时了。

我尝试删除着色器中的“#version 130”,但没有任何效果。我还调试了渲染函数,它确实被调用了。我不知道可能出了什么问题。

这是我的代码:

ma​​in.cpp

#ifdef _WIN32
#define _CRT_SECURE_NO_DEPRECATE
#endif

#include <GL\glew.h>
#include <GL\freeglut.h>
#include <stdio.h>

#define BUFFER_OFFSET(i) ((char *)NULL + (i))
GLuint shaderProgramID;
GLuint vao = 0;
GLuint vbo;
GLuint positionID, colorID;

#pragma region SHADER_FUNCTIONS
static char* readFile(const char* filename) 
    //Open the file
    FILE* fp = fopen(filename, "r");
    //Move to the end of file
    fseek(fp, 0, SEEK_END);
    long file_length = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    char* contents = new char[file_length + 1];
    //zero out memory
    for (int i = 0; i < file_length + 1; i++) 
        contents[i] = 0;
    
    //Read file
    fread(contents, 1, file_length, fp);
    //Denote end
    contents[file_length + 1] = '\0';
    fclose(fp);
    return contents;


GLuint makeVertexShader(const char* shaderSource) 
    GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShaderID, 1, (const GLchar**)&shaderSource, NULL);
    glCompileShader(vertexShaderID);
    return vertexShaderID;


GLuint makeFragmentShader(const char* shaderSource) 
    GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShaderID, 1, (const GLchar**)&shaderSource, NULL);
    glCompileShader(fragmentShaderID);
    return fragmentShaderID;


GLuint makeShaderProgram(GLuint vertexShaderID, GLuint fragmentShaderID) 
    GLuint shaderID = glCreateProgram();
    glAttachShader(shaderID, vertexShaderID);
    glAttachShader(shaderID, fragmentShaderID);
    glLinkProgram(shaderID);
    return shaderID;

#pragma endregion

void changeViewport(int w, int h) 
    glViewport(0, 0, w, h);


void render() 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glutSwapBuffers();
    printf("Debug");


int main(int argc, char** argv) 
    //Init GLUT
    glutInit(&argc, argv);

    //Set up some memory buffers for our display
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    //Set up window size
    glutInitWindowSize(800, 600);
    //Create window
    glutCreateWindow("Project 1");
    //Bind the two functions to respond when necessary
    glutReshapeFunc(changeViewport);
    glutDisplayFunc(render);

    //Initialize OpenGL driver
    GLenum err = glewInit();
    if (GLEW_OK != err) 
        fprintf(stderr, "GLEW error");
        return 1;
    

    //Vertices and colors of trangle
    GLfloat vertices[] = 
        -0.5f, -0.5f, 0.0f,
        0.5f, -0.5f, 0.0f,
        0.0f,-0.5f, 0.0f
    ;
    GLfloat colors[] = 
        1.0f, 0.0f, 0.0f, 1.0f,
        0.0f, 1.0f, 0.0f, 1.0f,
        0.0f, 0.0f, 1.0f, 1.0f
    ;

    #pragma region MAKE_SHADER
    char* vertexShaderSourceCode = readFile("vertexShader.vs");
    char* fragmentShaderSourceCode = readFile("fragmentShader.fs");
    GLuint vertShaderID = makeVertexShader(vertexShaderSourceCode);
    GLuint fragShaderID = makeFragmentShader(fragmentShaderSourceCode);
    GLuint shaderProgramID = makeShaderProgram(vertShaderID, fragShaderID);

    printf("vertShaderID is %d\n", vertShaderID);
    printf("fragmentShaderID is %d\n", fragShaderID);
    printf("shaderProgramID is %d\n", shaderProgramID);
    #pragma endregion

    //Create the "remember-all"
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    //Create buffer
    glBufferData(GL_ARRAY_BUFFER, 7 * 3 * sizeof(GLfloat), NULL, GL_STATIC_DRAW);
    //Load the vertex points
    glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * 3 * sizeof(GLfloat), vertices);
    //Load the colors
    glBufferSubData(GL_ARRAY_BUFFER, 3 * 3 * sizeof(GLfloat), 3 * 4 * sizeof(GLfloat), colors);

    //Find variables in shader
    positionID = glGetAttribLocation(shaderProgramID, "s_vPosition");
    colorID = glGetAttribLocation(shaderProgramID, "s_vColor");

    glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glVertexAttribPointer(colorID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(3 * 3 * sizeof(GLfloat)));
    glUseProgram(shaderProgramID);
    glEnableVertexAttribArray(positionID);
    glEnableVertexAttribArray(colorID);

    //Start up a loop that runs in the background
    glutMainLoop();
    return 0;

fragmentShader.fs

#version 130

in vec4 color;
out vec4 fColor;

void main () 
    fColor = color;

vertexShader.vs

#version 130

in vec4 s_vPosition;
in vec4 s_vColor;
out vec4 color;

void main () 
    //gl_Position = s_mP*s_mV*s_mM*s_vPosition;
    color = s_vColor;
    gl_Position = s_vPosition;

【问题讨论】:

【参考方案1】:

呃,我很抱歉创建了这个。在睡觉并再次查看我的代码后,我意识到我所有的顶点都具有相同的“y值”。

GLfloat vertices[] = 
   -0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    0.0f, -0.5f, 0.0f
;

不管怎样,已经解决了。天哪,我觉得自己很愚蠢。

【讨论】:

以上是关于使用着色器渲染“顶点彩色”三角形时的OpenGL“黑屏”的主要内容,如果未能解决你的问题,请参考以下文章

Android OpenGL学习:最小系统绘制

顶点着色器后的OpenGL三角形退化?

OpenGL

OpenGL

片段着色器不会在 OpenGL GLSL 中创建像光一样的渐变

OpenGL4.3新特性: 计算着色器 Compute Shader