简体   繁体   English

使用VBO进行OpenGL绘图

[英]OpenGL Drawing with VBO

Iam trying to write a small Opengl program to draw a single triangle using only Vertex Buffer Objects (without using VAO)s but whenever i want to compile it, it only shows a blue screen 我试图编写一个小的Opengl程序,仅使用顶点缓冲对象(不使用VAO)绘制一个三角形,但每当我想编译它时,它只显示一个蓝屏

Here is my code 这是我的代码

#include <iostream>
#include <GLUT/glut.h>
#include <OpenGL/gl3.h>

GLuint VBO;
GLuint VAO;

void display();

float vertex[] = {-1.0, 0.0 , 0.0,
                   0.0 , 1.0 , 0.0 ,
                   1.0 , 0.0 , 0.0 };

int main (int argc, char *argv[])

{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(1000, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("My First GLUT/OpenGL Window");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

 void display()

{
glClearColor(0, 0, 1,1);
glClear(GL_COLOR_BUFFER_BIT);

glGenBuffers(1,&VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER,9 *sizeof(vertex),vertex, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);

glVertexAttribPointer(0, 3,GL_FLOAT, GL_TRUE, 0, 0);


glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(0);

glutSwapBuffers();
};

Three problems: 三个问题:

  • Your code misses setting the viewport (if the window happens to be created with a size of 0×0 and gets resized only later the initial viewport size will be 0×0). 您的代码错过了设置视口(如果窗口碰巧创建的大小为0×0,并且仅在稍后的初始视口大小为0×0时调整大小)。

  • Your use of the sizeof operator is wrong. 您使用sizeof运算符是错误的。 vertex is a statically allocated array, and so the sizeof operator will return the total size of the vertex array, nout just the size of a single element. vertex是一个静态分配的数组,因此sizeof运算符将返回顶点数组的总大小,nout只是单个元素的大小。 So in that particular case just sizeof(vertex) without multiplying it with 9 would suffice. 因此,在这种特殊情况下,只需将sizeof(vertex)乘以9就足够了。

And last but not least, and the true cause of your problem: 最后但并非最不重要的,以及问题的真正原因:

  • Where are your shaders? 你的着色器在哪里? Using generic vertex attributes, and of course mandatory by OpenGL-3 you must supply a valid combination of a vertex and fragment shader. 使用通用顶点属性,当然必须由OpenGL-3强制使用,您必须提供顶点和片段着色器的有效组合。 Without those, nothing will render. 没有这些,没有任何东西会呈现

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

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