From e07e3cbe61e9c5c5e73d62f4c8360cf1fce64395 Mon Sep 17 00:00:00 2001 From: blobt Date: Fri, 11 Sep 2020 09:40:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0VertexBuffer=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- renderFramework.vcxproj | 2 ++ renderFramework.vcxproj.filters | 6 ++++ vertexbuffer.cpp | 55 +++++++++++++++++++++++++++++++++ vertexbuffer.h | 25 +++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 vertexbuffer.cpp create mode 100644 vertexbuffer.h diff --git a/renderFramework.vcxproj b/renderFramework.vcxproj index 38a6a15..99c9717 100644 --- a/renderFramework.vcxproj +++ b/renderFramework.vcxproj @@ -129,11 +129,13 @@ + + diff --git a/renderFramework.vcxproj.filters b/renderFramework.vcxproj.filters index 82c116f..1158e0a 100644 --- a/renderFramework.vcxproj.filters +++ b/renderFramework.vcxproj.filters @@ -24,6 +24,9 @@ 源文件 + + 源文件 + @@ -35,5 +38,8 @@ 源文件 + + 源文件 + \ No newline at end of file diff --git a/vertexbuffer.cpp b/vertexbuffer.cpp new file mode 100644 index 0000000..82410d7 --- /dev/null +++ b/vertexbuffer.cpp @@ -0,0 +1,55 @@ +#include "vertexbuffer.h" + +GLuint VertexBuffer::CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void * data) +{ + GLuint object; + glGenBuffers(1, &object); + glBindBuffer(bufferType, object); + glBufferData(bufferType, size, data, usage); + glBindBuffer(bufferType, 0); + return object; +} + +void VertexBuffer::SetSize(int vertexCount) +{ + mVertexCount = vertexCount; + mVertexes = new Vertex[mVertexCount]; + memset(mVertexes, 0, sizeof(Vertex)*mVertexCount); + mVBO = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(Vertex)*mVertexCount, GL_STATIC_DRAW, nullptr); +} + +void VertexBuffer::SetPosition(int index, float x, float y, float z, float w) { + mVertexes[index].Position[0] = x; + mVertexes[index].Position[1] = y; + mVertexes[index].Position[2] = z; + mVertexes[index].Position[3] = w; +} +void VertexBuffer::SetColor(int index, float r, float g, float b, float a) { + mVertexes[index].Color[0] = r; + mVertexes[index].Color[1] = g; + mVertexes[index].Color[2] = b; + mVertexes[index].Color[3] = a; +} +void VertexBuffer::SetTexcoord(int index, float x, float y) { + mVertexes[index].Texcoord[0] = x; + mVertexes[index].Texcoord[1] = y; +} +void VertexBuffer::SetNormal(int index, float x, float y, float z) { + mVertexes[index].Normal[0] = x; + mVertexes[index].Normal[1] = y; + mVertexes[index].Normal[2] = z; + mVertexes[index].Normal[3] = 1.0; +} + +void VertexBuffer::Bind() { + glBindBuffer(GL_ARRAY_BUFFER, mVBO); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Vertex)*mVertexCount, mVertexes); +} + +void VertexBuffer::Unbind() { + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +Vertex& VertexBuffer::Get(int index) { + return mVertexes[index]; +} diff --git a/vertexbuffer.h b/vertexbuffer.h new file mode 100644 index 0000000..8be26c3 --- /dev/null +++ b/vertexbuffer.h @@ -0,0 +1,25 @@ +#pragma once +#include "ggl.h" +struct Vertex { + float Position[4]; + float Color[4]; + float Texcoord[4]; + float Normal[4]; +}; + +class VertexBuffer { +public : + static GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void* data = nullptr); +public: + Vertex *mVertexes; + int mVertexCount; + GLuint mVBO; + void SetSize(int vertexCount); + void SetPosition(int index, float x, float y, float z, float w = 1.0f); + void SetColor(int index, float r, float g, float b, float a = 1.0); + void SetTexcoord(int index, float x, float y); + void SetNormal(int index, float x, float y, float z); + void Bind(); + void Unbind(); + Vertex& Get(int index); +}; \ No newline at end of file