简体   繁体   English

简单的VBO不显示

[英]Simple VBO doesn't display

I'm using a vertex with three floats for the position (XYZ) and three other for the color (RGB): 我正在使用一个顶点,其中三个浮点用于位置(XYZ),另外三个用于颜色(RGB):

XYZ RGB XYZ RGB ... XYZ RGB XYZ RGB ...

I'm currently trying to plot a red triangle. 我目前正在尝试绘制一个红色三角形。 Unfortunately I end up with a white window. 不幸的是,我最终遇到了一个白色的窗户。 I think there's a problem with the stride but I can't figure it out. 我认为跨步有问题,但我无法弄清楚。 I've tried many values for the stride and the size, still it doesn't seem to display anything. 我已经尝试了许多关于步幅和大小的值,但似乎什么也没显示。

//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;
}

You do appear to be using the incorrect stride. 您确实使用了错误的步幅。 The stride should be the distance from the start of one vertex to the start of the next vertex, or in your case 6 floats. 跨度应该是从一个顶点的起点到下一个顶点的起点的距离,或者在您的情况下为6个浮点。

You've set the stride as el_size , which is only 3 floats. 您已将步幅设置为el_size ,只有3个浮点数。

Also take care that your resize function is using an ortho matrix from 0 to screen width, and your init function is setting it from -1 to 1. If resize ever gets called your scene will become radically different. 还要注意,调整大小函数使用的是从0到屏幕宽度的正交矩阵,而初始化函数则将其设置为-1到1。如果调用调整大小,则场景将大为不同。

One problem I see is that call to glOrtho in the init function. 我看到的一个问题是在init函数中对glOrtho的调用。 Whatever you intend to do, this surely doesn't do what you want. 无论您打算做什么,这肯定不会满足您的要求。 As a general rule, put all drawing state related commands only into the display function, nowhere else. 通常,将所有与图形状态相关的命令仅放入显示功能,而无其他地方。 Setting transformation matrices (and the viewport) should happen there. 在那里设置转换矩阵(和视口)。 Doing so saves a lot of headaches later. 这样做以后可以省去很多麻烦。

The other problem is, that the stride you define is to short. 另一个问题是,您定义的步幅很短。 The stride is the distance from vertex to vertex in an interlaced array, not the length of a single attribute. 跨度是交错阵列中顶点到顶点的距离,而不是单个属性的长度。 el_size is calculated wrong. el_size计算错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM