我正在使用一个顶点,其中三个浮点数作为位置(XYZ),另外三个浮点数作为颜色(RGB):
XYZ RGB XYZ RGB ...
我目前正在尝试绘制一个红色三角形。不幸的是,我最终得到了一个白色的窗口。我认为步幅有问题,但我无法弄清楚。我已经尝试了许多关于步幅和大小的值,但它似乎仍然没有显示任何内容。
//main.cpp
#include "data.h"
GLuint ID;
int size,el_size;
void init(){
vector<float>data_vector(18);
data_vector[0]=0; //x
data_vector[1]=0; //y
data_vector[2]=0; //z
data_vector[3]=1;
data_vector[4]=0;
data_vector[5]=0;
data_vector[6]=1; //x
data_vector[7]=0; //y
data_vector[8]=0; //z
data_vector[9]=1;
data_vector[10]=0;
data_vector[11]=0;
data_vector[12]=0; //x
data_vector[13]=1; //y
data_vector[14]=0; //z
data_vector[15]=1;
data_vector[16]=0;
data_vector[17]=0;
size=data_vector.size();
// Init GLEW
if ( glewInit() != GLEW_OK ){
cerr << "Failed to initialize GLEW." << endl;
exit(-1);
}
if ( !glewIsSupported("GL_VERSION_1_5") && !glewIsSupported( "GL_ARB_vertex_buffer_object" ) ){
cerr << "ARB_vertex_buffer_object not supported!" << endl;
exit(-2);
}
glOrtho(-1, 1,1,-1, -5.0f, 5.0f);
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glShadeModel(GL_SMOOTH);
glEnableClientState(GL_VERTEX_ARRAY);
glGenBuffers(1,&ID);
glBindBuffer(GL_ARRAY_BUFFER, ID);
glBufferData(GL_ARRAY_BUFFER,size*sizeof(float), &data_vector[0], GL_STATIC_DRAW);
el_size=3*sizeof(data_vector[0]);
}
void reshape(int w, int h){
cout<<"reshape"<<endl;
glViewport(0,0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, (GLdouble) w, 0.0f, (GLdouble) h);
}
void display(){
cout<<"display"<<endl;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, ID);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, el_size, 0);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3,GL_FLOAT, el_size,(void*)(el_size));
glDrawArrays(GL_TRIANGLES,0,size/6);
glFlush();
}
int main(int argc, char **argv){
cout<<"main"<<endl;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(300,300);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
// glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}