Camera follows object in 3rd person
This commit is contained in:
71
src/render.c
71
src/render.c
@@ -227,6 +227,7 @@ bool Render_Init(GameState *gs)
|
||||
if (gs->shaderProgramId < 0)
|
||||
{
|
||||
printf("Failed to load shaders.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
@@ -236,17 +237,32 @@ bool Render_Init(GameState *gs)
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
printf("Initialized OpenGL.\n");
|
||||
|
||||
gs->numShapes = 2;
|
||||
|
||||
Shape *shape = Shape_MakePyramid(5);
|
||||
gs->testShape = shape;
|
||||
gs->shapes[0] = shape;
|
||||
glGenVertexArrays(1, &shape->VAO);
|
||||
glGenBuffers(1, &shape->VBO);
|
||||
glGenBuffers(1, &shape->IBO);
|
||||
glGenBuffers(1, &shape->EBO);
|
||||
printf("Created shape 0.\n");
|
||||
|
||||
shape = Shape_MakePlane();
|
||||
gs->shapes[1] = shape;
|
||||
glGenVertexArrays(1, &shape->VAO);
|
||||
glGenBuffers(1, &shape->VBO);
|
||||
glGenBuffers(1, &shape->IBO);
|
||||
glGenBuffers(1, &shape->EBO);
|
||||
printf("Created shape 1.\n");
|
||||
|
||||
LoadTexture(gs);
|
||||
printf("Loaded textures.\n");
|
||||
Camera_Init(&gs->camera);
|
||||
|
||||
InitShapeBuffers(gs->testShape);
|
||||
InitShapeBuffers(gs->shapes[0]);
|
||||
InitShapeBuffers(gs->shapes[1]);
|
||||
printf("Initialized buffers.\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
void Render_Destroy(GameState *gs)
|
||||
@@ -302,35 +318,38 @@ void Render_Draw(GameState *gs)
|
||||
glBindTexture(GL_TEXTURE_2D, gs->textureId);
|
||||
vec3 scale = { 0, 0, 0 };
|
||||
|
||||
Shape *shape = gs->testShape;
|
||||
glBindVertexArray(shape->VAO);
|
||||
|
||||
// apply transformations to each instance
|
||||
for (int j = 0; j < shape->numInstances; j++)
|
||||
for (int i = 0; i < gs->numShapes; i++)
|
||||
{
|
||||
ShapeInstance* instance = shape->instances + j;
|
||||
mat4* matrix = (void*)(shape->instanceData + (j * 17) + 1);
|
||||
mat4 tempMat;
|
||||
glm_mat4_identity(tempMat);
|
||||
Shape *shape = gs->shapes[i];
|
||||
glBindVertexArray(shape->VAO);
|
||||
|
||||
glm_translate(tempMat, instance->position);
|
||||
glm_rotate_x(tempMat, instance->rotation[0], tempMat);
|
||||
glm_rotate_y(tempMat, instance->rotation[1], tempMat);
|
||||
glm_rotate_z(tempMat, instance->rotation[2], tempMat);
|
||||
// apply transformations to each instance
|
||||
for (int j = 0; j < shape->numInstances; j++)
|
||||
{
|
||||
ShapeInstance* instance = shape->instances + j;
|
||||
mat4* matrix = (void*)(shape->instanceData + (j * 17) + 1);
|
||||
mat4 tempMat;
|
||||
glm_mat4_identity(tempMat);
|
||||
|
||||
glm_translate(tempMat, instance->position);
|
||||
glm_rotate_x(tempMat, instance->rotation[0], tempMat);
|
||||
glm_rotate_y(tempMat, instance->rotation[1], tempMat);
|
||||
glm_rotate_z(tempMat, instance->rotation[2], tempMat);
|
||||
|
||||
scale[0] = instance->scale;
|
||||
scale[1] = instance->scale;
|
||||
scale[2] = instance->scale;
|
||||
glm_scale(tempMat, scale);
|
||||
|
||||
memcpy(matrix, tempMat, sizeof(mat4));
|
||||
}
|
||||
|
||||
scale[0] = instance->scale;
|
||||
scale[1] = instance->scale;
|
||||
scale[2] = instance->scale;
|
||||
glm_scale(tempMat, scale);
|
||||
// re-buffer the instance data because transformations may have changed
|
||||
glBindBuffer(GL_ARRAY_BUFFER, shape->IBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, shape->numInstances * SHAPE_INSTANCE_SIZE, shape->instanceData, GL_DYNAMIC_DRAW);
|
||||
|
||||
memcpy(matrix, tempMat, sizeof(mat4));
|
||||
glDrawElementsInstanced(GL_TRIANGLES, shape->numIndices, GL_UNSIGNED_SHORT, 0, shape->numInstances);
|
||||
}
|
||||
|
||||
// re-buffer the instance data because transformations may have changed
|
||||
glBindBuffer(GL_ARRAY_BUFFER, shape->IBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, shape->numInstances * SHAPE_INSTANCE_SIZE, shape->instanceData, GL_DYNAMIC_DRAW);
|
||||
|
||||
glDrawElementsInstanced(GL_TRIANGLES, shape->numIndices, GL_UNSIGNED_SHORT, 0, shape->numInstances);
|
||||
|
||||
SDL_GL_SwapWindow(gs->window);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user