openGL不呈现颜色

时间:2014-03-11 08:43:32

标签: c++ opengl

我在绘制几个三角形的颜色时遇到了麻烦。着色器加载很好,我有一种轻微的感觉,这就是我布置数据的方式,但总的来说,我不知道为什么它不会用彩色绘制它。

main.cpp:

//g++ main.cpp -lGL -lGLEW -lGLU -lglut LoadShaders.cpp -o run
#include<GL/glew.h>
#include<GL/freeglut.h>
#include<GL/gl.h>
#include<iostream>

#include"LoadShaders.h"
#include"vgl.h"
using namespace std;

enum VAO_IDs {Triangles, NumVAOs };
enum Attrib_IDs {vPosition = 0, cPosition = 1};

GLuint VAOs;
GLuint Buffers;

const GLuint NumVertices = 6;

void init(void)
{
    glGenVertexArrays(1, &VAOs);
    glBindVertexArray(VAOs);

    GLfloat vertices[NumVertices] [2] = {
        {-0.90, -0.90},
        {0.85, -0.90 },
        {-0.90, 0.85 },
        {0.90, -0.85 },
        {0.90, 0.90  },
        {-0.85, 0.90 }
    };

    glGenBuffers(1, &Buffers);
    glBindBuffer(GL_ARRAY_BUFFER, Buffers);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    GLfloat colours[NumVertices] [3] = {
        {0.583f, 0.568f, 1.000f},
        {0.100f, 0.435f, 0.235f},
        {0.456f, 0.345f, 0.654f},
        {0.345f, 0.222f, 0.564f},
        {0.109f, 0.538f, 1.000f},
        {0.057f, 0.453f, 0.777f},
    };

    GLuint colourBuffer;

    glGenBuffers(1, &colourBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, colourBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colours), colours, GL_STATIC_DRAW);

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(0);
    ShaderInfo shaders[]={
        {GL_VERTEX_SHADER, "triangles.vert"},
        {GL_FRAGMENT_SHADER, "triangles.frag"},
        {GL_NONE}
    };

    GLuint program = LoadShaders(shaders);
    glUseProgram(program);

}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBindVertexArray(VAOs);
    glDrawArrays(GL_TRIANGLES, 0, NumVertices);

    glFlush();
}
int main(int argc, char** argv)
{

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(512,512);
    glutInitContextVersion( 4, 2 );
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutCreateWindow(argv[0]);

    if (glewInit()) {
        cerr << "Unable to initialize GLEW"<<endl;
        exit(EXIT_FAILURE);
    }

    init();

    glutDisplayFunc(display);

    glutMainLoop();
}

triangles.vert:

#version 420 core

layout(location = 0) in vec2 vPosition;
layout(location = 1) in vec3 cPosition;
void main()
{
    gl_Position = vPosition;
    fragmentColor = cPosition;
}

triangles.frag:

#version 420 core
in vec3 fragmentColor;
out vec3 fColor;

void main()
{
    fColor = fragmentColor;
}

1 个答案:

答案 0 :(得分:2)

顶点着色器中存在两个问题:

  • 缺少fragmentColor的声明。
  • 隐式转化无效:gl_Positionvec4,而不是vec2。您必须使用vPosition构造函数手动展开vec4vec4( vPosition, 0.0, 1.0 )

所有在一起:

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <vector>
#include <iostream>

struct Program
{
    static GLuint Load( const char* vert, const char* geom, const char* frag )
    {
        GLuint prog = glCreateProgram();
        if( vert ) AttachShader( prog, GL_VERTEX_SHADER, vert );
        if( geom ) AttachShader( prog, GL_GEOMETRY_SHADER, geom );
        if( frag ) AttachShader( prog, GL_FRAGMENT_SHADER, frag );
        glLinkProgram( prog );
        CheckStatus( prog );
        return prog;
    }

private:
    static void CheckStatus( GLuint obj )
    {
        GLint status = GL_FALSE;
        if( glIsShader(obj) ) glGetShaderiv( obj, GL_COMPILE_STATUS, &status );
        if( glIsProgram(obj) ) glGetProgramiv( obj, GL_LINK_STATUS, &status );
        if( status == GL_TRUE ) return;
        GLchar log[ 1 << 15 ] = { 0 };
        if( glIsShader(obj) ) glGetShaderInfoLog( obj, sizeof(log), NULL, log );
        if( glIsProgram(obj) ) glGetProgramInfoLog( obj, sizeof(log), NULL, log );
        std::cerr << log << std::endl;
        exit( -1 );
    }

    static void AttachShader( GLuint program, GLenum type, const char* src )
    {
        GLuint shader = glCreateShader( type );
        glShaderSource( shader, 1, &src, NULL );
        glCompileShader( shader );
        CheckStatus( shader );
        glAttachShader( program, shader );
        glDeleteShader( shader );
    }
};

#define GLSL(version, shader) "#version " #version "\n" #shader


const char* vert = GLSL
( 
    420 core,
    layout(location = 0) in vec2 vPosition;
    layout(location = 1) in vec3 cPosition;
    out vec3 fragmentColor;
    void main()
    {
        gl_Position = vec4( vPosition, 0.0, 1.0 );
        fragmentColor = cPosition;
    }
);

const char* frag = GLSL
( 
    420 core,
    in vec3 fragmentColor;
    out vec3 fColor;
    void main()
    {
        fColor = fragmentColor;
    }
);


GLuint VAO;
const GLuint NumVertices = 6;
void init()
{
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    GLfloat vertices[NumVertices] [2] = 
    {
        {-0.90, -0.90},
        {0.85, -0.90 },
        {-0.90, 0.85 },
        {0.90, -0.85 },
        {0.90, 0.90  },
        {-0.85, 0.90 },
    };

    GLuint Buffer;
    glGenBuffers(1, &Buffer);
    glBindBuffer(GL_ARRAY_BUFFER, Buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    GLfloat colours[NumVertices] [3] = 
    {
        {0.583f, 0.568f, 1.000f},
        {0.100f, 0.435f, 0.235f},
        {0.456f, 0.345f, 0.654f},
        {0.345f, 0.222f, 0.564f},
        {0.109f, 0.538f, 1.000f},
        {0.057f, 0.453f, 0.777f},
    };

    GLuint colourBuffer;
    glGenBuffers(1, &colourBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, colourBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colours), colours, GL_STATIC_DRAW);

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);

    glEnableVertexAttribArray(1);
    glEnableVertexAttribArray(0);

    GLuint program = Program::Load( vert, NULL, frag );
    glUseProgram(program);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, NumVertices);

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
     glutInit(&argc, argv);
     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
     glutInitWindowSize(512, 512);
     glutInitContextVersion(4, 2);
     glutInitContextProfile(GLUT_CORE_PROFILE);
     glutCreateWindow(argv[0]);

     glewExperimental = GL_TRUE;
     if( GLEW_OK != glewInit() )
     {
         return 1;
     }

     init();

     glutDisplayFunc(display);
     glutMainLoop();
     return 0;
}