为什么这些GLSL着色器不起作用?

时间:2016-04-18 06:45:56

标签: c++ opengl glsl

我正在尝试使用着色器渲染螺旋作为全红色以覆盖颜色。由于某种原因,他们编译和链接但什么都不做。

请参阅以下代码

fragmentshader.glsl

#version 430

out vec4 outColor; 
in vec4 color; 

void main(){
    outColor = color;
}

vertexshader.glsl

#version 430

in layout(location=0) vec2 position;
out vec4 color;

void main(){
    gl_Position = vec4(position, 0.0, 1.0); 
    color = vec4(1.0, 0.0f, 0.0f, 1.0f);
}

Window.cpp

#include <GL\glew.h>
#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include "MeGLWindow.h"

#include <iostream>
#include <fstream>

#define Pi 3.14159265358979
#define E 2.718281828F
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define MAX_SPIRAL 25000

std::string readFile(const char* relPath);
void installShaders();
bool checkShaderStatus(GLuint shaderID);
bool checkProgramStatus(GLuint programID);

GLuint programID;

void MeGLWindow::initializeGL() {
    glewInit();
    glEnable(GL_DEPTH_TEST);

    installShaders();

    const int verts_num = MAX_SPIRAL * 2;
    GLfloat verts[50000];
    GLuint myBufferID;
    float a = 0.06f;
    float b = 0.06f;    

    float cx = 0.0;
    float cy = 0.0;
    int z = 0;
    for (int i = 0; i < MAX_SPIRAL; i++) {
        float ang = (Pi / 720) * i;
        float factor = pow(E, b * ang);
        float x = cx + (a * (cos(ang)) * factor);
        float y = cy - (a * (sin(ang)) * factor);
        verts[2 * i] = x;
        verts[(2 * i) + 1] = y;
    }
    glGenBuffers(1, &myBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, myBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); 
}

void MeGLWindow::paintGL() {
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glViewport(0, 0, width(), height());

    glDrawArrays(GL_POINTS, 0, 2*25000);
}

void installShaders() {
    GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    const char* adapter[1];

    std::string file = readFile("vertexshader.glsl"); 
    const char* vertexFile = file.c_str(); 
    adapter[0] = vertexFile;
    glShaderSource(vertexShaderID, 1, adapter, 0);

    file = readFile("fragmentshader.glsl");
    const char* fragmentFile = file.c_str();
    adapter[0] = fragmentFile;
    glShaderSource(fragmentShaderID, 1, adapter, 0);

    glCompileShader(vertexShaderID);
    glCompileShader(fragmentShaderID);

    if (!checkShaderStatus(vertexShaderID) ||                 !checkShaderStatus(fragmentShaderID)) {
        return; 
    }
    programID = glCreateProgram();
    glAttachShader(programID, vertexShaderID);
    glAttachShader(programID, fragmentShaderID);

    glLinkProgram(programID);
    if (!checkProgramStatus(programID)) {    
        return; 
    }


    glUseProgram(programID);
}

的main.cpp

#include <Qt\qapplication.h>
#include "MeGLWindow.h"

int main(int argc, char* argv[]) {
    QApplication app(argc, argv);
    MeGLWindow meWindow; 
    meWindow.show(); 
    return app.exec();
}

根据链接着色器,螺旋中的顶点应为红色!我究竟做错了什么?请帮忙!

1 个答案:

答案 0 :(得分:1)

  • 没有对着色器程序进行错误检查。或者更确切地说,您检查错误,但不要以任何方式通知应用程序创建程序失败。

  • 您没有提供checkProgramStatus的来源,但是如果您检查GL_LINK_STATUS,那么

  • 目前尚不清楚如何从提供的代码初始化OpenGL,但是您没有设置核心配置文件所需的VAO。