我正在学习交错的VBO,当我使用GL_BYTES作为颜色类型时,我不确定为什么我只得到黑线?当我使用GL_FLOAT时,我会得到颜色。完成以下项目。绘制四条正方形的线条。取消注释USE_BYTES_FOR_COLOR以显示问题。
QT += core gui widgets opengl
TARGET = test
TEMPLATE = app
SOURCES = main.cpp
HEADERS = 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;
};
#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();
}
答案 0 :(得分:4)
...
{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},
...
GLbyte
的范围是-128到127,而不是0到255。
编辑:正如Vallentin指出切换到GL_UNSIGNED_BYTE
有效:
(没有为Qt设置,抱歉)
#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;
}