The most ugly way to render a grid known to mankind
This commit is contained in:
parent
546648cbdd
commit
7a95e4515d
1 changed files with 35 additions and 21 deletions
56
main.c
56
main.c
|
|
@ -101,16 +101,26 @@ int main()
|
|||
|
||||
|
||||
hexgrid_t grid;
|
||||
grid.rows = 1;
|
||||
grid.columns = 1;
|
||||
grid.rows = 3;
|
||||
grid.columns = 3;
|
||||
grid.hexes = calloc(1, sizeof(hex_t) * grid.rows * grid.columns);
|
||||
|
||||
for (size_t i = 0; i < grid.rows * grid.columns; ++i)
|
||||
float scale = 1.0f / 5.0f;
|
||||
|
||||
for (size_t i = 0; i < grid.rows; ++i)
|
||||
{
|
||||
grid.hexes[i].position.x = 0.0f;
|
||||
grid.hexes[i].position.y = 0.0f;
|
||||
grid.hexes[i].position.z = 0.0f;
|
||||
grid.hexes[i].position.w = 1.0f;
|
||||
for (size_t j = 0; j < grid.columns; ++j)
|
||||
{
|
||||
float offset = 0.0f;
|
||||
if (j % 2)
|
||||
offset = sint(1.0f/6);
|
||||
|
||||
grid.hexes[i*grid.columns+j].position.x = (0.0f + j*(1 + cost(1.0f/6)));
|
||||
grid.hexes[i*grid.columns+j].position.y = (0.0f + i*(sint(1.0f/6))*2.0f) + offset;
|
||||
grid.hexes[i*grid.columns+j].position.z = 0.0f;
|
||||
grid.hexes[i*grid.columns+j].position.w = 1.0f;
|
||||
fprintf(stdout, "Position of hex %ld %ld is %f %f\n", i, j, grid.hexes[i*grid.columns+j].position.x, grid.hexes[i*grid.columns+j].position.y);
|
||||
}
|
||||
}
|
||||
|
||||
// Get OpenGL objects
|
||||
|
|
@ -119,27 +129,31 @@ int main()
|
|||
|
||||
for (uint64_t i = 0; i < grid.columns * grid.rows; ++i)
|
||||
{
|
||||
vertices[i*21] = grid.hexes[i].position.x;
|
||||
vertices[i*21+1] = grid.hexes[i].position.y;
|
||||
vertices[i*21] = grid.hexes[i].position.x * scale;
|
||||
vertices[i*21+1] = grid.hexes[i].position.y * scale;
|
||||
vertices[i*21+2] = grid.hexes[i].position.z;
|
||||
|
||||
for (int j = 1; j < 7; ++j)
|
||||
{
|
||||
vertices[i*21+j*3] = grid.hexes[i].position.x + cost((j-1)/6.0f);
|
||||
vertices[i*21+j*3+1] = grid.hexes[i].position.y + sint((j-1)/6.0f);
|
||||
vertices[i*21+j*3] = (grid.hexes[i].position.x + cost((j-1)/6.0f)) * scale;
|
||||
vertices[i*21+j*3+1] = (grid.hexes[i].position.y + sint((j-1)/6.0f)) * scale;
|
||||
vertices[i*21+j*3+2] = grid.hexes[i].position.z;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int indices[] =
|
||||
size_t nindices = grid.columns * grid.rows * 3 * 6;
|
||||
unsigned int *indices = calloc(nindices, sizeof(unsigned int));
|
||||
|
||||
|
||||
for (size_t i = 0; i < grid.columns*grid.rows; ++i)
|
||||
{
|
||||
0,1,2,
|
||||
0,2,3,
|
||||
0,3,4,
|
||||
0,4,5,
|
||||
0,5,6,
|
||||
0,6,1
|
||||
};
|
||||
indices[i*6*3] = 0 + i*7; indices[i*6*3+1] = 1 + i*7; indices[i*6*3+2] = 2 + i*7;
|
||||
indices[i*6*3+3] = 0 + i*7; indices[i*6*3+4] = 2 + i*7; indices[i*6*3+5] = 3 + i*7;
|
||||
indices[i*6*3+6] = 0 + i*7; indices[i*6*3+7] = 3 + i*7; indices[i*6*3+8] = 4 + i*7;
|
||||
indices[i*6*3+9] = 0 + i*7; indices[i*6*3+10] = 4 + i*7; indices[i*6*3+11] = 5 + i*7;
|
||||
indices[i*6*3+12] = 0 + i*7; indices[i*6*3+13] = 5 + i*7; indices[i*6*3+14] = 6 + i*7;
|
||||
indices[i*6*3+15] = 0 + i*7; indices[i*6*3+16] = 6 + i*7; indices[i*6*3+17] = 1 + i*7;
|
||||
}
|
||||
|
||||
glGenVertexArrays(1, &vao);
|
||||
glGenBuffers(1,&vbo);
|
||||
|
|
@ -150,7 +164,7 @@ int main()
|
|||
glBufferData(GL_ARRAY_BUFFER, 7*3*grid.rows*grid.columns*sizeof(float), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, nindices * sizeof(unsigned int), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
|
@ -170,7 +184,7 @@ int main()
|
|||
glUseProgram(shader_program);
|
||||
glBindVertexArray(vao);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
glDrawElements(GL_TRIANGLES, 3*6, GL_UNSIGNED_INT, 0);
|
||||
glDrawElements(GL_TRIANGLES, 3*6*grid.rows*grid.columns, GL_UNSIGNED_INT, 0);
|
||||
|
||||
glfwPollEvents();
|
||||
glfwSwapBuffers(window);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue