2D_Game_Engine
Loading...
Searching...
No Matches
vertexbuffer.cpp
Go to the documentation of this file.
1#include <pch.hpp>
2#include <vertexbuffer.hpp>
3
4VertexBuffer::VertexBuffer(unsigned int num_vertices,unsigned int vertex_size,GLenum usage){
5 glGenBuffers(1,&m_ID);
6 glBindBuffer(GL_ARRAY_BUFFER,m_ID);
7 glBufferData(GL_ARRAY_BUFFER,num_vertices*vertex_size,nullptr,usage); //allocate 4 vertices for every quad
8}
9
11 glDeleteBuffers(1,&m_ID);
12}
13
14void VertexBuffer::Bind() const{
15 glBindBuffer(GL_ARRAY_BUFFER,m_ID);
16}
17
19 glBindBuffer(GL_ARRAY_BUFFER,0);
20}
21
22std::array<Vertex,4> VertexBuffer::CreateQuad(float x,float y,float w,float h,int layer,float texID){
23 Vertex v1;
24 v1.position={x,y};
25 v1.texcoords={0.0f,0.0f};
26 v1.layer=layer;
27 v1.texID=texID;
28
29 Vertex v2;
30 v2.position={x,y+h};
31 v2.texcoords={0.0f,1.0f};
32 v2.layer=layer;
33 v2.texID=texID;
34
35 Vertex v3;
36 v3.position={x+w,y+h};
37 v3.texcoords={1.0f,1.0f};
38 v3.layer=layer;
39 v3.texID=texID;
40
41 Vertex v4;
42 v4.position={x+w,y};
43 v4.texcoords={1.0f,0.0f};
44 v4.layer=layer;
45 v4.texID=texID;
46
47 return {v1,v2,v3,v4};
48}
49
50std::array<Vertex,4> VertexBuffer::CreateQuad(float x,float y,float w,float h,float tx,float ty,float tw,float th,float ttw,float tth,int layer,float texID){
51 Vertex v1;
52 v1.position={x,y};
53 v1.texcoords={tx/ttw,ty/tth};
54 v1.layer=layer;
55 v1.texID=texID;
56
57 Vertex v2;
58 v2.position={x,y+h};
59 v2.texcoords={tx/ttw,(ty+th)/tth};
60 v2.layer=layer;
61 v2.texID=texID;
62
63 Vertex v3;
64 v3.position={x+w,y+h};
65 v3.texcoords={(tx+tw)/ttw,(ty+th)/tth};
66 v3.layer=layer;
67 v3.texID=texID;
68
69 Vertex v4;
70 v4.position={x+w,y};
71 v4.texcoords={(tx+tw)/ttw,ty/tth};
72 v4.layer=layer;
73 v4.texID=texID;
74
75 return {v1,v2,v3,v4};
76}
77
78
79void VertexBuffer::SetData(unsigned int vertex_index,float *data,unsigned int num_vertices,unsigned int VertexSize){
80 glBufferSubData(GL_ARRAY_BUFFER,vertex_index*sizeof(Vertex),VertexSize*num_vertices,(const void *)data);
81}
void SetData(unsigned int vertex_index, float *data, unsigned int num_vertices, unsigned int VertexSize)
void Bind() const
static std::array< Vertex, 4 > CreateQuad(float x, float y, float w, float h, int layer, float texID)
void Unbind() const
VertexBuffer()=default
YAML::Node data
float texID
Definition structs.hpp:32
Vec2 position
Definition structs.hpp:29
Vec2 texcoords
Definition structs.hpp:30
int layer
Definition structs.hpp:31