0

このコードが機能しないのはなぜですか?

 // 1 square (made by 4 quads) to be rendered
GLfloat vertices_position[] = {
    x, y,
    x+w, y,
    x+w, y+h,
    x, y+h,
};

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices_position);

glDrawArrays(GL_QUADS, 0, 4);

glDisableClientState(GL_VERTEX_ARRAY);

これで glew を使用していますが、コンパイル エラーは発生しません。画面に何も描画されないだけです。

代わりにこれを使用すると、正常に動作します:

glPushMatrix();

    glTranslatef(0, 0, 0);

    glScalef(scale,scale,1);
    //set color
    glColor4f(R, G, B, A);

    glBegin( GL_QUADS );

        glTexCoord2f(  texLeft,    texTop );
        glVertex2f( x, y );

        glTexCoord2f( texRight,    texTop );
        glVertex2f( x+w, y );

        glTexCoord2f( texRight, texBottom );
        glVertex2f( x+w, y+h );

        glTexCoord2f(  texLeft, texBottom );
        glVertex2f( x, y+h );

    glEnd();

glPopMatrix();
4

2 に答える 2

0

ここでうまくいきます:

#include <GL/glew.h>
#include <GL/glut.h>

bool vertexArray = false;
void keyboard( unsigned char key, int x, int y )
{
    if( key == 't' ) vertexArray = !vertexArray;
    glutPostRedisplay();
}

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

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    double winWidth = glutGet( GLUT_WINDOW_WIDTH );
    double winHeight = glutGet( GLUT_WINDOW_HEIGHT );
    double ar = winWidth / winHeight;
    glOrtho( -1 * ar, 1 * ar, -1, 1, -1, 1);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    float x = -0.5f;
    float y = -0.5f;
    float w = 1;
    float h = 1;
    float scale = 1;

    glPushMatrix();
    glTranslatef(0, 0, 0);
    glScalef(scale,scale,1);
    //set color
    glColor4f( 1, 0, 0, 1 );

    if( vertexArray )
    {
        GLfloat vertices_position[] = {
            x, y,
            x+w, y,
            x+w, y+h,
            x, y+h,
        };

        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(2, GL_FLOAT, 0, vertices_position);
        glDrawArrays(GL_QUADS, 0, 4);
        glDisableClientState(GL_VERTEX_ARRAY);
    }
    else
    {
        glBegin( GL_QUADS );
        glVertex2f( x, y );
        glVertex2f( x+w, y );
        glVertex2f( x+w, y+h );
        glVertex2f( x, y+h );
        glEnd();
    }

    glPopMatrix();

    glutSwapBuffers();
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "GLUT" );
    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );
    glewInit();
    glutMainLoop();
    return 0;
}
于 2013-05-23T17:46:59.563 に答える