Camera follows object in 3rd person
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
@@ -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;
|
||||||
|
|||||||
10
src/game.c
10
src/game.c
@@ -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);
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
71
src/render.c
71
src/render.c
@@ -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,35 +318,38 @@ 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++)
|
||||||
glBindVertexArray(shape->VAO);
|
|
||||||
|
|
||||||
// apply transformations to each instance
|
|
||||||
for (int j = 0; j < shape->numInstances; j++)
|
|
||||||
{
|
{
|
||||||
ShapeInstance* instance = shape->instances + j;
|
Shape *shape = gs->shapes[i];
|
||||||
mat4* matrix = (void*)(shape->instanceData + (j * 17) + 1);
|
glBindVertexArray(shape->VAO);
|
||||||
mat4 tempMat;
|
|
||||||
glm_mat4_identity(tempMat);
|
|
||||||
|
|
||||||
glm_translate(tempMat, instance->position);
|
// apply transformations to each instance
|
||||||
glm_rotate_x(tempMat, instance->rotation[0], tempMat);
|
for (int j = 0; j < shape->numInstances; j++)
|
||||||
glm_rotate_y(tempMat, instance->rotation[1], tempMat);
|
{
|
||||||
glm_rotate_z(tempMat, instance->rotation[2], tempMat);
|
ShapeInstance* instance = shape->instances + j;
|
||||||
|
mat4* matrix = (void*)(shape->instanceData + (j * 17) + 1);
|
||||||
|
mat4 tempMat;
|
||||||
|
glm_mat4_identity(tempMat);
|
||||||
|
|
||||||
scale[0] = instance->scale;
|
glm_translate(tempMat, instance->position);
|
||||||
scale[1] = instance->scale;
|
glm_rotate_x(tempMat, instance->rotation[0], tempMat);
|
||||||
scale[2] = instance->scale;
|
glm_rotate_y(tempMat, instance->rotation[1], tempMat);
|
||||||
glm_scale(tempMat, scale);
|
glm_rotate_z(tempMat, instance->rotation[2], tempMat);
|
||||||
|
|
||||||
memcpy(matrix, tempMat, sizeof(mat4));
|
scale[0] = instance->scale;
|
||||||
|
scale[1] = instance->scale;
|
||||||
|
scale[2] = instance->scale;
|
||||||
|
glm_scale(tempMat, scale);
|
||||||
|
|
||||||
|
memcpy(matrix, tempMat, sizeof(mat4));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
SDL_GL_SwapWindow(gs->window);
|
||||||
}
|
}
|
||||||
|
|||||||
52
src/shape.c
52
src/shape.c
@@ -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);
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user