Frage

I am learning interleaved VBOs and I'm not sure why I'm getting only black lines when I use GL_BYTES as the color type? When I use GL_FLOAT, I get colors. Complete project below. Draws four lines in the shape of a square. uncomment USE_BYTES_FOR_COLOR to show the problem.

main.pro

QT += core gui widgets opengl
TARGET = test
TEMPLATE = app

SOURCES = main.cpp
HEADERS = main.h

main.h

#include <QGLWidget>
#include <QGLFunctions>

class glview : public QGLWidget, protected QGLFunctions
{
    Q_OBJECT

public:
    explicit glview(QWidget *parent = 0);
    ~glview();
    QSize sizeHint() const;

protected:
    void initializeGL();
    void resizeGL(int w, int h);
    void paintGL();

private:
    quint32 vbo_id;
};

main.cpp

#include <QApplication>
#include "main.h"

//#define USE_BYTES_FOR_COLOR

struct vrtx {
    GLint   x;
    GLint   y;
    #ifdef USE_BYTES_FOR_COLOR
    GLbyte  r;
    GLbyte  g;
    GLbyte  b;
    #else
    GLfloat r;
    GLfloat g;
    GLfloat b;
    #endif
}__attribute__((packed)) geo[] = {
    #ifdef USE_BYTES_FOR_COLOR
    //x, y, r, g, b
    {0, 0, 255, 0, 0},
    {0, 1, 0, 255, 0},
    {0, 1, 0, 255, 0},
    {1, 1, 255, 0, 0},
    {1, 1, 255, 0, 0},
    {1, 0, 0, 255, 0},
    {1, 0, 0, 255, 0},
    {0, 0, 255, 0, 0},
    #else
    //x, y, r, g, b
    {0, 0, 1, 0, 0},
    {0, 1, 0, 1, 0},
    {0, 1, 0, 1, 0},
    {1, 1, 1, 0, 0},
    {1, 1, 1, 0, 0},
    {1, 0, 0, 1, 0},
    {1, 0, 0, 1, 0},
    {0, 0, 1, 0, 0},
    #endif
};

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

glview::glview(QWidget *parent) : QGLWidget(parent)
{

}

glview::~glview()
{

}

QSize glview::sizeHint() const
{
    return QSize(400, 400);
}

void glview::initializeGL()
{
    initializeGLFunctions();
    qglClearColor(Qt::white);

    glGenBuffers(1, &vbo_id);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
    glBufferData(GL_ARRAY_BUFFER, sizeof(geo), geo, GL_STATIC_DRAW);
}

void glview::resizeGL(int w, int h)
{
    glViewport(0, 0, w, h);
}

void glview::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glLoadIdentity();
    glOrtho(-1, 2, -1, 2, -1, 1);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
    glVertexPointer(2, GL_INT, sizeof(struct vrtx), 0);
    #ifdef USE_BYTES_FOR_COLOR
    glColorPointer(3, GL_BYTE, sizeof(struct vrtx), ((char*)NULL + 8));
    #else
    glColorPointer(3, GL_FLOAT, sizeof(struct vrtx), ((char*)NULL + 8));
    #endif

    glDrawArrays(GL_LINES, 0, sizeof(geo) / sizeof(struct vrtx));
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    glFlush();
}
War es hilfreich?

Lösung

...
{0, 0, 255, 0, 0},
{0, 1, 0, 255, 0},
{0, 1, 0, 255, 0},
{1, 1, 255, 0, 0},
{1, 1, 255, 0, 0},
{1, 0, 0, 255, 0},
{1, 0, 0, 255, 0},
{0, 0, 255, 0, 0},
...

The range of GLbyte is -128 to 127, not 0 to 255.

EDIT: As Vallentin pointed out switching to GL_UNSIGNED_BYTE works:

color-box

(not set up for Qt, sorry)

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

#define USE_BYTES_FOR_COLOR
struct vrtx 
{
    GLint   x;
    GLint   y;
#ifdef USE_BYTES_FOR_COLOR
    GLubyte  r;
    GLubyte  g;
    GLubyte  b;
#else
    GLfloat r;
    GLfloat g;
    GLfloat b;
#endif
} 
geo[] = 
{
#ifdef USE_BYTES_FOR_COLOR
    //x, y, r, g, b
    {0, 0, 255, 0, 0},
    {0, 1, 0, 255, 0},
    {0, 1, 0, 255, 0},
    {1, 1, 255, 0, 0},
    {1, 1, 255, 0, 0},
    {1, 0, 0, 255, 0},
    {1, 0, 0, 255, 0},
    {0, 0, 255, 0, 0},
#else
    //x, y, r, g, b
    {0, 0, 1, 0, 0},
    {0, 1, 0, 1, 0},
    {0, 1, 0, 1, 0},
    {1, 1, 1, 0, 0},
    {1, 1, 1, 0, 0},
    {1, 0, 0, 1, 0},
    {1, 0, 0, 1, 0},
    {0, 0, 1, 0, 0},
#endif
};

GLuint vbo_id;
void init()
{
    glGenBuffers( 1, &vbo_id );
    glBindBuffer( GL_ARRAY_BUFFER, vbo_id );
    glBufferData( GL_ARRAY_BUFFER, sizeof(geo), geo, GL_STATIC_DRAW );
}

void display()
{
    glClearColor( 1, 1, 1, 1 );
    glClear( GL_COLOR_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( -1, 2, -1, 2, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
    glVertexPointer(2, GL_INT, sizeof(struct vrtx), 0);
#ifdef USE_BYTES_FOR_COLOR
    glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(struct vrtx), ((char*)NULL + 8));
#else
    glColorPointer(3, GL_FLOAT, sizeof(struct vrtx), ((char*)NULL + 8));
#endif

    glDrawArrays(GL_LINES, 0, sizeof(geo) / sizeof(struct vrtx));
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    glutSwapBuffers();
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "GLUT" );
    glewInit();
    init();
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top