Camera follows object in 3rd person

This commit is contained in:
var
2026-04-01 22:07:55 -05:00
parent 299cb740d4
commit 06535ccf01
7 changed files with 107 additions and 33 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -4,7 +4,7 @@
void Camera_Init(Camera* c) void Camera_Init(Camera* c)
{ {
c->position[0] = 5.0f; c->position[0] = 5.0f;
c->position[1] = 0.0f; c->position[1] = 8.0f;
c->position[2] = 0.0f; c->position[2] = 0.0f;
c->width[0] = 0.6f; c->width[0] = 0.6f;
c->width[1] = 5.8f; c->width[1] = 5.8f;

View File

@@ -7,7 +7,8 @@
GameState *Game_New() GameState *Game_New()
{ {
GameState *gs = calloc(1, sizeof(GameState)); GameState *gs = calloc(1, sizeof(GameState) + (2 * sizeof(Shape**)));
gs->shapes = (void*)(gs + 1);
if (!Render_Init(gs)) return NULL; if (!Render_Init(gs)) return NULL;
@@ -26,7 +27,12 @@ void Game_Update(GameState *gs)
else if (gs->input.d) move[1] = -speed; else if (gs->input.d) move[1] = -speed;
if (gs->input.q) move[2] = speed; if (gs->input.q) move[2] = speed;
else if (gs->input.e) move[2] = -speed; else if (gs->input.e) move[2] = -speed;
glm_vec3_add(gs->camera.position, move, gs->camera.position);
ShapeInstance *target = &(gs->shapes[0]->instances[2]);
vec3 displacement;
glm_vec3_scale(gs->camera.front, -10.0, displacement);
glm_vec3_add(target->position, move, target->position);
glm_vec3_add(target->position, displacement, gs->camera.position);
Camera_UpdateVectors(&gs->camera); Camera_UpdateVectors(&gs->camera);
SDL_Delay(5); SDL_Delay(5);

View File

@@ -25,7 +25,8 @@ typedef struct
GLuint textureId; GLuint textureId;
int shaderProgramId; int shaderProgramId;
Camera camera; Camera camera;
Shape *testShape; Shape **shapes;
int numShapes;
mat4 projMatrix; mat4 projMatrix;
mat4 viewMatrix; mat4 viewMatrix;
} GameState; } GameState;

View File

@@ -227,6 +227,7 @@ bool Render_Init(GameState *gs)
if (gs->shaderProgramId < 0) if (gs->shaderProgramId < 0)
{ {
printf("Failed to load shaders.\n"); printf("Failed to load shaders.\n");
return false;
} }
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
@@ -236,17 +237,32 @@ bool Render_Init(GameState *gs)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
printf("Initialized OpenGL.\n"); printf("Initialized OpenGL.\n");
gs->numShapes = 2;
Shape *shape = Shape_MakePyramid(5); Shape *shape = Shape_MakePyramid(5);
gs->testShape = shape; gs->shapes[0] = shape;
glGenVertexArrays(1, &shape->VAO); glGenVertexArrays(1, &shape->VAO);
glGenBuffers(1, &shape->VBO); glGenBuffers(1, &shape->VBO);
glGenBuffers(1, &shape->IBO); glGenBuffers(1, &shape->IBO);
glGenBuffers(1, &shape->EBO); 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); LoadTexture(gs);
printf("Loaded textures.\n");
Camera_Init(&gs->camera); 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) void Render_Destroy(GameState *gs)
@@ -302,7 +318,9 @@ void Render_Draw(GameState *gs)
glBindTexture(GL_TEXTURE_2D, gs->textureId); glBindTexture(GL_TEXTURE_2D, gs->textureId);
vec3 scale = { 0, 0, 0 }; vec3 scale = { 0, 0, 0 };
Shape *shape = gs->testShape; for (int i = 0; i < gs->numShapes; i++)
{
Shape *shape = gs->shapes[i];
glBindVertexArray(shape->VAO); glBindVertexArray(shape->VAO);
// apply transformations to each instance // apply transformations to each instance
@@ -331,6 +349,7 @@ void Render_Draw(GameState *gs)
glBufferData(GL_ARRAY_BUFFER, shape->numInstances * SHAPE_INSTANCE_SIZE, shape->instanceData, GL_DYNAMIC_DRAW); 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); glDrawElementsInstanced(GL_TRIANGLES, shape->numIndices, GL_UNSIGNED_SHORT, 0, shape->numInstances);
}
SDL_GL_SwapWindow(gs->window); SDL_GL_SwapWindow(gs->window);
} }

View File

@@ -63,8 +63,8 @@ Shape *Shape_MakePyramid(int numInstances)
{ {
ShapeInstance *instance = shape->instances + i; ShapeInstance *instance = shape->instances + i;
instance->position[0] = 0.0f; instance->position[0] = 0.0f;
instance->position[0] = 0.0f; instance->position[1] = 3.0f;
instance->position[0] = (5.0f * i) - 10.0; instance->position[2] = (5.0f * i) - 10.0f;
instance->scale = 1.0f; instance->scale = 1.0f;
instance->collisionRadius = 1.0f; instance->collisionRadius = 1.0f;
glm_vec3_zero(instance->velocity); glm_vec3_zero(instance->velocity);
@@ -77,6 +77,54 @@ Shape *Shape_MakePyramid(int numInstances)
return shape; return shape;
} }
Shape *Shape_MakePlane()
{
GLfloat vertices[] =
{
-30.0f, 0.0f, -30.0f, 0.0f, 0.0f,
-30.0f, 0.0f, 30.0f, 0.0f, 1.0f,
30.0f, 0.0f, 30.0f, 1.0f, 1.0f,
30.0f, 0.0f, -30.0f, 1.0f, 0.0f,
};
GLushort indices[] =
{
1, 2, 0,
2, 3, 0
};
size_t numBytesVertices = sizeof(vertices);
size_t numBytesIndices = sizeof(indices);
Shape *shape = malloc(sizeof(Shape) + numBytesVertices + numBytesIndices);
void *tempPtr = (void*)(shape + 1);
shape->vertices = tempPtr;
shape->numVertices = numBytesVertices / sizeof(vertices[0]);
shape->indices = tempPtr + numBytesVertices;
shape->numIndices = numBytesIndices / sizeof(indices[0]);
memcpy(shape->vertices, vertices, numBytesVertices);
memcpy(shape->indices, indices, numBytesIndices);
shape->instances = calloc(1, sizeof(ShapeInstance) + SHAPE_INSTANCE_SIZE);
shape->instanceData = (void*)(shape->instances + 1);
shape->numInstances = 1;
ShapeInstance *instance = shape->instances;
instance->position[0] = 0.0f;
instance->position[1] = 0.0f;
instance->position[2] = 0.0f;
instance->scale = 1.0f;
instance->collisionRadius = 1.0f;
glm_vec3_zero(instance->velocity);
glm_vec3_zero(instance->rotation);
float *textureId = shape->instanceData + (0 * 17);
*textureId = 5;
mat4 *matrix = (void*)(textureId + 1);
return shape;
}
void *Shape_Destroy(Shape *shape) void *Shape_Destroy(Shape *shape)
{ {
free(shape); free(shape);

View File

@@ -15,7 +15,6 @@ typedef struct
vec3 velocity; vec3 velocity;
float scale; float scale;
float collisionRadius; float collisionRadius;
} ShapeInstance; } ShapeInstance;
typedef struct typedef struct
@@ -35,3 +34,4 @@ typedef struct
} Shape; } Shape;
Shape *Shape_MakePyramid(int numInstances); Shape *Shape_MakePyramid(int numInstances);
Shape *Shape_MakePlane();