0

我试图了解(通俗地说)VAO 和 VBO 的工作方式。

我编写了一个小的 OpenGL 程序,它以图形方式创建了一个看起来很酷的对象。

它使用两个三角形和 VAO/VBO 绘制一个正方形。它还扩大了顶点。

下面是我的代码(为了清楚起见,我已经包含了除 win32 之外的所有内容):

我的问题是,我需要多个 VAO 和 VBO 来绘制更多对象/形状吗?

基本上我从一个三角形的教程改编了这个程序。我想知道像我一样绘制第二个三角形是否正确,或者我是否应该以不同的方式使用 VAO 和 VBO。

例如,如果我想画第二个正方形,我需要更多的 VAO 吗?抱歉,如果这个问题看起来很愚蠢或毫无意义,我是 OpenGL3.2 的初学者。

欢迎任何建议或解释。

#include <windows.h>
#include <iostream>
using namespace std;
#include <math.h>
#include "console.h"
#include "shaders/Shader.h"
#include "gl/glew.h"
#include "gl/wglew.h"
#pragma comment(lib, "glew32.lib")

unsigned int m_vaoID;       // vertex array object
unsigned int m_vboID[2];    // two VBOs - used for colours and vertex data

const int numOfVerts = 6;
const int numOfTris = 2;
float verts[18];
float cols[18];

Shader myShader;
ConsoleWindow console;

int screenWidth=600, screenHeight=600; //changed from 480x480px

void display();             //called in winmain to draw everything to the screen
void init();                //called in winmain when the program starts.
void createGeometry();      //used to create Vertex Array Objects
void reshape(int width, int height);

/*************    START OF OPENGL FUNCTIONS   ****************/

void reshape(int width, int height)         // Resize the OpenGL window
{
    int b = 50; //border of 50px

    screenWidth = width; 
    screenHeight = height;

    glViewport(0+b,0+b,width-(b*2),height-(b*2));   // set Viewport dimensions
}

void display()                                  
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(myShader.handle());  // use the shader

    //draw objects
    glBindVertexArray(m_vaoID);     // select VAO

    glDrawArrays(GL_TRIANGLES, 0, numOfVerts); //draw some geometry

        //Enlarge ALL the Points
        glEnable(GL_POINT_SMOOTH);
        glPointSize(100);
        glDrawArrays(GL_POINTS,0, numOfVerts);

    glBindBuffer(GL_ARRAY_BUFFER, 0);  //unbind the buffer

    glBindVertexArray(0); //unbind the vertex array object

    glUseProgram(0); //turn off the current shader
}

void createGeometry()
{
    // First simple object
    float dim = 0.5;
    //Triangle 1
    //points    X                   Y                   Z
    verts[0] = -dim;  verts[ 1] = -dim;  verts[ 2] = -dim; //Point 1
    verts[3] = -dim;  verts[ 4] =  dim;  verts[ 5] = -dim; //Point 2
    verts[6] =  dim;  verts[ 7] =  dim;  verts[ 8] = -dim; //Point 3

    //Triangle 2
    //points    X                   Y                   Z
    verts[ 9] =  dim;  verts[10] = -dim;  verts[11] = -dim; //Point 4
    verts[12] = -dim;  verts[13] = -dim;  verts[14] = -dim; //Point 1
    verts[15] =  dim;  verts[16] =  dim;  verts[17] = -dim; //Point 3

    //colour    R               G               B
    cols[ 0] = 1.0;  cols[ 1] = 0.0;  cols[ 2] = 1.0; //Point1
    cols[ 3] = 0.0;  cols[ 4] = 1.0;  cols[ 5] = 1.0; //Point2
    cols[ 6] = 1.0;  cols[ 7] = 1.0;  cols[ 8] = 0.0; //Point3

    //colour2   R               G               B
    cols[ 9] = 0.0;  cols[10] = 1.0;  cols[11] = 1.0; //Point4
    cols[12] = 1.0;  cols[13] = 0.0;  cols[14] = 1.0; //Point5
    cols[15] = 1.0;  cols[16] = 1.0;  cols[17] = 0.0; //Point6

    // VAO allocation
    glGenVertexArrays(1, &m_vaoID);

    // First VAO setup
    glBindVertexArray(m_vaoID);

    glGenBuffers(2, m_vboID);

    glBindBuffer(GL_ARRAY_BUFFER, m_vboID[0]);
    //initialises data storage of vertex buffer object
    glBufferData(GL_ARRAY_BUFFER, numOfVerts*3*sizeof(GLfloat), verts, GL_STATIC_DRAW);

    GLint vertexLocation= glGetAttribLocation(myShader.handle(), "in_Position");
    glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); 
    glEnableVertexAttribArray(vertexLocation);

    glBindBuffer(GL_ARRAY_BUFFER, m_vboID[1]);
    glBufferData(GL_ARRAY_BUFFER, numOfVerts*3*sizeof(GLfloat), cols, GL_STATIC_DRAW);
    GLint colorLocation= glGetAttribLocation(myShader.handle(), "in_Color");
    glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); 
    glEnableVertexAttribArray(colorLocation);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glEnableVertexAttribArray(0);

    glBindVertexArray(0);
}

void init()
{
    glClearColor(0.0,0.0,0.0,0.0);
    // Shaders
    if(!myShader.load("BasicView", "glslfiles/basic.vert", "glslfiles/basic.frag"))
    {
        cout << "failed to load shader" << endl;
    }                           
    createGeometry();
    glEnable(GL_DEPTH_TEST);
}
4

0 回答 0