因此,我正在尝试研究 vbo 并仅做一些示例,但 opengl 正在杀死我。我已经尝试了四个小时来完成这个代码工作,但我不知道为什么这不起作用。我试图用不同的颜色绘制 4 个正方形。但是如果我尝试使用GL_POINTS
我在屏幕上只看到 4 个点进行绘制,并且应该是 9 个点。如果我尝试使用GL_QUADS
它,就不要画任何东西。有趣的是,当我更改此行时:
glDrawElements(GL_POINTS, 16, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
对此:
glDrawElements(GL_POINTS, 64, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
Opengl 绘制了 9 个点,但仅此而已,GL_QUADS 不起作用。拜托,有人给我一个关于这个问题的光,它正在杀死我!完整的代码在这里:
#define GL_GLEXT_PROTOTYPES
#include<GL/glut.h>
#include<iostream>
using namespace std;
#define BUFFER_OFFSET(offset) ((GLfloat*) NULL + offset)
#define VERTICES 0
#define INDICES 1
#define NUM_BUFFERS 2
void init_2(){
GLuint buffers[NUM_BUFFERS];
GLfloat vertices[][3]= {
{0.0, 0.0, 0.0},
{0.0, 40.0, 0.0},
{0.0, 80.0, 0.0},
{40.0, 0.0, 0.0},
{40.0, 40.0, 0.0},
{40.0, 80.0, 0.0},
{80.0, 0.0, 0.0},
{80.0, 40.0, 0.0},
{80.0, 80.0, 0.0}
};
GLuint indices[][4] = {
{0, 1, 4, 3},
{1, 2, 5, 4},
{3, 4, 7, 6},
{4, 5, 8, 7}
};
glGenBuffers(NUM_BUFFERS, buffers);
glBindBuffer(GL_ARRAY_BUFFER, buffers[VERTICES]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDICES]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
void display(void){
glPushMatrix();
glDrawElements(GL_POINTS, 16, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
glPopMatrix();
glutSwapBuffers();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
gluOrtho2D((GLdouble) -1.0, (GLdouble) 90.0, (GLdouble) -1.0, (GLdouble) 90.0);
init2();
}
int main(int argv, char** argc) {
glutInit(&argv, argc);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("myCode.cpp");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}