0

我刚开始使用视觉工作室和 C++,我一直在关注 Ed Angel 的交互式计算机图形书中的示例。我似乎遇到了旋转 RGB 立方体示例的障碍。我完全按照,我让它运行并显示一个立方体,但立方体全是黑色而不是彩色。据我了解,一切都是正确的,我无法从谷歌搜索中找到任何关于出现问题的建议或提示。我想知道这里是否有比我更有经验的人可以帮助我弄清楚发生了什么或为我指明方向。

// Display a rotating color cube

#include "Angel.h"

typedef Angel::vec4 color4;
typedef Angel::vec4 point4;

const int NumVertices = 36;


point4 points[NumVertices];
color4 colors[NumVertices];

//Vertices of a unit cube centered at origin, sides aligned with axes
point4 vertices[8] = {
    point4( -0.5, -0.5, 0.5, 1.0),
    point4( -0.5, 0.5, 0.5, 1.0),
    point4( 0.5, 0.5, 0.5, 1.0),
    point4( 0.5, -0.5, 0.5, 1.0),
    point4( -0.5, -0.5, -0.5, 1.0),
    point4( -0.5, 0.5, -0.5, 1.0),
    point4( 0.5, 0.5, -0.5, 1.0),
    point4( 0.5, -0.5, -0.5, 1.0)
};

//RGBA colors
color4 vertex_colors[8] = {
    color4( 0.0, 0.0, 0.0, 1.0),  //black
    color4( 1.0, 0.0, 0.0, 1.0),  //red
    color4( 1.0, 1.0, 0.0, 1.0),  //yellow
    color4( 0.0, 1.0, 0.0, 1.0),  //blue
    color4( 0.0, 0.0, 1.0, 1.0),  //green
    color4( 1.0, 0.0, 1.0, 1.0),  //magenta
    color4( 1.0, 1.0, 1.0, 1.0),  //white
    color4( 0.0, 1.0, 1.0, 1.0)  //cyan
};

//Array of rotation angles (in degrees) for each coordinate axis
enum { Xaxis = 0, Yaxis = 1, Zaxis = 2, NumAxes = 3 };
int Axis = Xaxis;
GLfloat Theta[NumAxes] = { 0.0, 0.0, 0.0};

GLuint theta; //The location of the "theta" shader uniform variable
//----------------------------------------------------------------------------

//quad generates two triangles for each face and assigns colors to the vertices

int Index = 0;
void
quad( int a, int b, int c, int d)
{
    colors[Index] = vertex_colors[a]; points[Index] = vertices[a]; Index++;
    colors[Index] = vertex_colors[b]; points[Index] = vertices[b]; Index++;
    colors[Index] = vertex_colors[c]; points[Index] = vertices[c]; Index++;
    colors[Index] = vertex_colors[a]; points[Index] = vertices[a]; Index++;
    colors[Index] = vertex_colors[c]; points[Index] = vertices[c]; Index++;
    colors[Index] = vertex_colors[d]; points[Index] = vertices[d]; Index++;
}

//----------------------------------------------------------------------------

//generate 12 triangles: 36 vertices and 36 colors
void
colorcube( void )
{
    quad( 1, 0, 3, 2);
    quad( 2, 3, 7, 6);
    quad( 3, 0, 4, 7);
    quad( 6, 5, 1, 2);
    quad( 4, 5, 6, 7);
    quad( 5, 4, 0, 1);
}

//----------------------------------------------------------------------------

//OpenGL initialization
void
init( void )
{
    colorcube();

    //Create a vertex array object
    GLuint vao;
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );

    //Create and initialize a buffer object
    GLuint buffer;
    glGenBuffers( 1, &buffer );
    glBindBuffer( GL_ARRAY_BUFFER, buffer );
    glBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors), NULL, GL_STATIC_DRAW );
    glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), points );
    glBufferSubData( GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors );

    //Load shaders and use the resulting shader program
    GLuint program = InitShader( "vshader36.glsl", "fshader36.glsl" );
    std::cout << "Program ID:" <<program;
    glUseProgram ( program );

    //set up vertex arrays
    GLuint vPosition = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( vPosition );
    glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)) );

    theta = glGetUniformLocation( program, "theta" );

    glEnable( GL_DEPTH_TEST );
    glClearColor( 1.0, 1.0, 1.0, 1.0 );
}

//----------------------------------------------------------------------------

void
display( void )
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glUniform3fv( theta, 1, Theta );
    glDrawArrays( GL_TRIANGLES, 0, NumVertices );

    glutSwapBuffers();
}

//----------------------------------------------------------------------------

void
keyboard( unsigned char key, int x, int y )
{
    switch ( key ) {
    case 033:
    case 'q': case 'Q':
        exit( EXIT_SUCCESS );
        break;
    }
}

//----------------------------------------------------------------------------

void
mouse( int button, int state, int x, int y )
{
    if ( state == GLUT_DOWN ) {
        switch( button ) {
            case GLUT_LEFT_BUTTON: Axis = Xaxis; break;
            case GLUT_MIDDLE_BUTTON: Axis = Yaxis; break;
            case GLUT_RIGHT_BUTTON: Axis = Zaxis; break;
        }
    }
}

//----------------------------------------------------------------------------

void
idle( void )
{
    Theta[Axis] += 0.01;

    if ( Theta[Axis] > 360.0 ) {
        Theta[Axis] -= 360.0;
    }

    glutPostRedisplay();
}

//----------------------------------------------------------------------------
int
main( int argc, char **argv )
{

    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
    glutInitWindowSize( 512, 512 );

    glutCreateWindow( "Color Cube" );

    glewInit();

    init();

    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );
    glutMouseFunc( mouse );
    glutIdleFunc( idle );

    glutMainLoop();
    return 0;
} 

fshader36.glsl

#version 150

in  vec4 color;
out vec4 fColor;

void main() 
{ 
    fColor = color;
}

vshader36.glsl

#version 150

in  vec4 vPosition;
in  vec4 vColor;
out vec4 color;

uniform vec3 theta;

void main() 
{

    vec3 angles = radians( theta );
    vec3 c = cos( angles );
    vec3 s = sin( angles );

    mat4 rx = mat4( 1.0,  0.0,  0.0, 0.0,
            0.0,  c.x,  -s.x, 0.0,
            0.0, s.x,  c.x, 0.0,
            0.0,  0.0,  0.0, 1.0 );

    mat4 ry = mat4( c.y, 0.0, s.y, 0.0,
            0.0, 1.0,  0.0, 0.0,
            -s.y, 0.0,  c.y, 0.0,
            0.0, 0.0,  0.0, 1.0 );

    mat4 rz = mat4( c.z, -s.z, 0.0, 0.0,
            s.z,  c.z, 0.0, 0.0,
            0.0,  0.0, 1.0, 0.0,
            0.0,  0.0, 0.0, 1.0 );

    color = vColor;
    gl_Position = rz * ry * rx * vPosition;
} 
4

2 回答 2

0

您实际上并没有将任何颜色信息传递给着色器。

于 2013-09-14T16:15:38.880 回答
0

您必须实际设置 vColor 以使其具有有效值。执行与设置 vPosition 相同的操作:

GLuint vColor= glGetAttribLocation( program, "vColor" );
glEnableVertexAttribArray( vColor);
glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(colors)) );
于 2013-09-14T18:22:21.147 回答