简体   繁体   English

OPENGL:使用VBO的方形类

[英]OPENGL: Square Class Using VBO

So, I am trying to make a basic "Drawable" class that handles a lot of the drawing for me in the background and I want to use modern OpenGL (no begin and end statements). 因此,我正在尝试制作一个基本的“ Drawable”类,该类在后台为我处理大量图形,并且我想使用现代OpenGL(无begin和end语句)。 I keep just getting a blank screen when I run draw(). 当我运行draw()时,我一直保持空白。

I have run the debugger and checked, my array is initialized properly with 3xFLOAT for position and 4xFLOAT for color. 我已经运行了调试器并进行了检查,我的数组已正确初始化,位置为3xFLOAT,颜色为4xFLOAT。 Any idea what is going wrong? 知道出了什么问题吗? I am very new to this library. 我是这个图书馆的新手。 My example tries to draw a red cube at (+-0.5, +-0.5, 0.0), so the indexData array is just { 0, 1, 2, 3 }. 我的示例尝试在(+ -0.5,+-0.5、0.0)处绘制一个红色立方体,因此indexData数组仅为{0、1、2、3}。

#define DRAWABLE_VERTEX_DEPTH 3
#define SIZE_OF_VERTEX_ELEMENT sizeof(GLfloat)
#define VERTEX_SIZE (DRAWABLE_VERTEX_DEPTH * SIZE_OF_VERTEX_ELEMENT)
#define DRAWABLE_COLOR_DEPTH 4
#define SIZE_OF_COLOR_ELEMENT sizeof(GLfloat)
#define COLOR_SIZE (DRAWABLE_COLOR_DEPTH * SIZE_OF_COLOR_ELEMENT)
#define INDEX_SIZE sizeof(GLushort)
#define DRAWABLE_STRIDE (VERTEX_SIZE + COLOR_SIZE)

inline Drawable(/*Arguments omitted for brevity...*/)
    {
        //Standard initialization omitted....

        glGenBuffers(1, &vboID);
        glGenBuffers(1, &vioID);
        glGenVertexArrays(1, &vaoID);

        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vioID);

        glBufferData(GL_ARRAY_BUFFER, (VERTEX_SIZE + COLOR_SIZE) * vertexCount, vertexData, drawType);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, INDEX_SIZE * indexCount, indexData, drawType);

        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

        //Generate Vertex Array

        glBindVertexArray(vaoID);
        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(0, DRAWABLE_VERTEX_DEPTH, GL_FLOAT, GL_FALSE, DRAWABLE_STRIDE, 0);
        glVertexAttribPointer(1, DRAWABLE_COLOR_DEPTH, GL_FLOAT, GL_FALSE, DRAWABLE_STRIDE, (GLbyte*)VERTEX_SIZE);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vioID);
        glBindVertexArray(0);
    }

    inline void draw()
    {
        glBindVertexArray(vaoID);
        glDrawElements(drawMode, indexCount, GL_UNSIGNED_SHORT, NULL);
        glBindVertexArray(0);
    }

GLSL Vertex Shader: GLSL顶点着色器:

#version 430\r\n

in layout(location=0) vec3 inPosition;
in layout(location=1) vec4 inColor;
out vec4 outVertexColor;

void main()
{
    gl_Position = vec4(inPosition, 1.0);
    outVertexColor = inColor;
}

GLSL Fragment Shader: GLSL片段着色器:

#version 430\r\n

in vec4 outVertexColor;
out vec4 outFragmentcolor;

void main()
{
    outFragmentcolor = outVertexColor;
}

Apart from the issues mentioned in the comments, your index array is GLushort (unsigned 16 bit), while your draw call specifies GL_UNSIGNED_INT (unsigned 32 bit). 除了注释中提到的问题外,索引数组是GLushort(无符号16位),而绘图调用指定了GL_UNSIGNED_INT(无符号32位)。 Replace with GL_UNSIGNED_SHORT. 替换为GL_UNSIGNED_SHORT。

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

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