however when I change the size paramter to 8 * sizeof(Vertex) instead of 4 * sizeof(Vertex), which in my head should be 2 rectangles i don't get the result i anticipated.
i've looked in the memory window in VS, and i can see that the vertex data doesn't seem to lie tightly packed ( there are a couple of bytes between the vertices ), but even if i add much more than just 8 * sizeof(Vertex) to the glbuffersubdata size parameter, only the one rectangle shows up.
Does this have to do with the fact that the entity struct has more than just the Vertex vtx[4] data in it?
And should it therefor be better to just have the Vertex vtx[4] be standalone and not part of some bigger "Entity struct"?
All the Entities are allocated as a single big array ( Entity entity[MAX_ENTITIES] = {}; ),
so I guess they should lie behind eachother in memory and therefor I should be able to set the last glbuffersubdata parameter to the address of the vertices of the first entity, or am I wrong about that??
gamecode.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | struct Entity { V3f pos; V3f dimension; V4f_COL col; V2f texCoord; V2f texDimension; Vertex vtx[4]; void Init(float _x, float _y, float _w, float _h) { pos.x = _x; pos.y = _y; pos.z = 0.0f; dimension.x = _w; dimension.y = _h; dimension.z = 0.0f; texCoord.x = 0.0f; texCoord.y = 0.0f; texDimension.x = 1.0f; texDimension.y = 1.0f; } void SetSolidColor(float _r, float _g, float _b, float _a) { col.r = _r; col.g = _g; col.b = _b; col.a = _a; } void SetTextureCoordsToFullMap() { texCoord.x = 0.0f; texCoord.y = 0.0f; texDimension.x = 1.0f; texDimension.y = 1.0f; } void UpdatePos() { vtx[0].pos.x = pos.x; vtx[0].pos.y = pos.y; vtx[1].pos.x = pos.x; vtx[1].pos.y = pos.y + dimension.y; vtx[2].pos.x = pos.x + dimension.x; vtx[2].pos.y = pos.y; vtx[3].pos.x = pos.x + dimension.x; vtx[3].pos.y = pos.y + dimension.y; } void UpdateCol() { vtx[0].col.r = col.r; vtx[0].col.g = col.g; vtx[0].col.b = col.b; vtx[0].col.a = col.a; vtx[1].col.r = col.r; vtx[1].col.g = col.g; vtx[1].col.b = col.b; vtx[1].col.a = col.a; vtx[2].col.r = col.r; vtx[2].col.g = col.g; vtx[2].col.b = col.b; vtx[2].col.a = col.a; vtx[3].col.r = col.r; vtx[3].col.g = col.g; vtx[3].col.b = col.b; vtx[3].col.a = col.a; } void UpdateTexCoord() { vtx[0].texCoord.x = texCoord.x; vtx[0].texCoord.y = texCoord.y; vtx[1].texCoord.x = texCoord.x; vtx[1].texCoord.y = texCoord.y + texDimension.y; vtx[2].texCoord.x = texCoord.x + texDimension.x; vtx[2].texCoord.y = texCoord.y; vtx[3].texCoord.x = texCoord.x + texDimension.x; vtx[3].texCoord.y = texCoord.y + texDimension.y; } }; #define UPDATEGAMECODE(name) extern "C" __declspec(dllexport) void name(GameState *gameState, Input *input, Entity *entity) UPDATEGAMECODE(UpdateGameCode); typedef void (*PFNUPDATEGAMECODE)(GameState*, Input*, Entity*); void InitEntities(Entity *entity); |
gamcode.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | UPDATEGAMECODE(UpdateGameCode) { if(!gameState->initializedEntities) { InitEntities(entity); gameState->initializedEntities = true; } // Resest Initialization if(input->keyPressed['R']) { gameState->initializedEntities = false; } // Close program & change gamestate if(input->keyPressed[Esc]) { gameState->running = false; } if(input->keyPressed['W']) { entity[0].pos.y -= 4.0f; } if(input->keyPressed['S']) { entity[0].pos.y += 4.0f; } if(input->keyPressed['D']) { entity[0].pos.x += 4.0f; } if(input->keyPressed['A']) { entity[0].pos.x -= 4.0f; } for(int i = 0; i < 10; i ++) { entity[i].UpdatePos(); entity[i].UpdateCol(); entity[i].UpdateTexCoord(); } } void InitEntities(Entity *entity) { entity[0].Init(100.0f, 200.0f, 500.0f, 200.0f); entity[0].SetSolidColor(1.0f, 1.0f, 1.0f, 1.0f); entity[0].SetTextureCoordsToFullMap(); entity[1].Init(0.0f, 0.0f, 50.0f, 20.0f); entity[1].SetSolidColor(1.0f, 1.0f, 1.0f, 1.0f); entity[1].SetTextureCoordsToFullMap(); } |
main.cpp (excerpts)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | int main() { GameState gameState = {}; Entity entity[MAX_ENTITIES] = {}; Input input = {}; GameCodeDLL gameCode = {}; WindowDimension winDim = {}; gameState.running = true; gameState.initializedEntities = false; u32 VAO; u32 VBO; u32 IBO; u32 colAttribOffset = sizeof(V3f); u32 texCoordAttribOffset = colAttribOffset + sizeof(V4f_COL); u32 MaxMemory = sizeof(float) * TOTAL_AMOUNT_ATTRIBS; u32 vtxIndices[MAX_ENTITIES * 6] = {}; for(int i = 0; i < MAX_ENTITIES; i++) { vtxIndices[(i * 6) + 0] = (i * 4) + 0; vtxIndices[(i * 6) + 1] = (i * 4) + 1; vtxIndices[(i * 6) + 2] = (i * 4) + 2; vtxIndices[(i * 6) + 3] = (i * 4) + 1; vtxIndices[(i * 6) + 4] = (i * 4) + 2; vtxIndices[(i * 6) + 5] = (i * 4) + 3; } glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &IBO); glGenTextures(10, imageTexture); glGenTextures(10, fontTexture); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, MaxMemory, 0, GL_DYNAMIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex), (void*)0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 4, GL_FLOAT, false, sizeof(Vertex), (void*)colAttribOffset); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(Vertex), (void*)texCoordAttribOffset); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vtxIndices), vtxIndices, GL_STATIC_DRAW); while(running) { // LOADING GAMECODE UnloadGameCode(&gameCode); gameCode = LoadGameCode(); // UNLOADING GAMECODE glClearColor(0.2f, 0.2f, 0.2f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); gameCode.UpdateGameCode(&gameState, &input, entity); glBufferSubData(GL_ARRAY_BUFFER, 0 * sizeof(entity[0].vtx), 4 * sizeof(Vertex), entity[0].vtx); BindTexture2D(imageTexture[myPng]); glDrawElements(GL_TRIANGLES, MAX_ENTITIES * 6, GL_UNSIGNED_INT, 0); BindTexture2D(imageTexture[devastator]); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (unsigned int*)(6 * 4)); glfwSwapBuffers(hWindow); glfwPollEvents(); } } |