[Add] Ground plane.

This commit is contained in:
Ritchie Cunningham 2025-09-14 21:26:54 +01:00
parent e208f49674
commit a6646820c5
3 changed files with 49 additions and 7 deletions

View File

@ -2,6 +2,8 @@
out vec4 FragColor;
uniform vec3 overrideColor;
void main() {
FragColor = vec4(0.2f, 0.5f, 0.8f, 1.0f);
FragColor = vec4(overrideColor, 1.0f);
}

View File

@ -20,7 +20,7 @@
} \
} while(0)
Renderer::Renderer(void) : _vao(0), _vbo(0) {}
Renderer::Renderer(void) : _vao(0), _vbo(0), _ground_vao(0), _ground_vbo(0) {}
Renderer::~Renderer(void) {
if(_vao != 0) {
@ -29,6 +29,12 @@ Renderer::~Renderer(void) {
if(_vbo != 0) {
glDeleteBuffers(1, &_vbo);
}
if(_ground_vao != 0) {
glDeleteVertexArrays(1, &_ground_vao);
}
if(_ground_vbo != 0) {
glDeleteBuffers(1, &_ground_vbo);
}
}
bool Renderer::init(int screen_width, int screen_height) {
@ -98,6 +104,28 @@ bool Renderer::init(int screen_width, int screen_height) {
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
/* Setup ground plane. */
float ground_vertices[] = {
/* Positions. */
500.0f, -0.5f, 500.0f,
-500.0f, -0.5f, 500.0f,
-500.0f, -0.5f, -500.0f,
500.0f, -0.5f, 500.0f,
-500.0f, -0.5f, -500.0f,
500.0f, -0.5f, -500.0f,
};
glGenVertexArrays(1, &_ground_vao);
glGenBuffers(1, &_ground_vbo);
glBindVertexArray(_ground_vao);
glBindBuffer(GL_ARRAY_BUFFER, _ground_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(ground_vertices), ground_vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glViewport(0,0,screen_width, screen_height);
GL_CHECK_ERROR();
glEnable(GL_DEPTH_TEST); /* Depth testing for 3D! */
@ -124,32 +152,42 @@ void Renderer::render(const Player& player, const std::vector<RemotePlayer>& rem
GLint view_loc = glGetUniformLocation(_shader.get_id(), "view");
GLint proj_loc = glGetUniformLocation(_shader.get_id(), "projection");
GLint model_loc = glGetUniformLocation(_shader.get_id(), "model");
GLint color_loc = glGetUniformLocation(_shader.get_id(), "overrideColor");
glUniformMatrix4fv(view_loc, 1, GL_FALSE, view.get_ptr());
GL_CHECK_ERROR();
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection.get_ptr());
GL_CHECK_ERROR();
/* Draw the ground. */
BettolaMath::Mat4 ground_model = BettolaMath::Mat4::translation(0.0f, 0.0f, 0.0f);
glUniformMatrix4fv(model_loc, 1, GL_FALSE, ground_model.get_ptr());
glUniform3f(color_loc, 0.2f, 0.4f, 0.2f);
glBindVertexArray(_ground_vao);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0); /* WHOOAAA! Got to make sure we unbind the VAO! */
/* Set players colour back. */
glUniform3f(color_loc, 0.2f, 0.5f, 0.8f);
/* Draw the local player's cube. */
BettolaMath::Mat4 model = BettolaMath::Mat4::translation(player.get_x(), 0.0f, player.get_y());
glUniformMatrix4fv(model_loc, 1, GL_FALSE, model.get_ptr());
GL_CHECK_ERROR();
glBindVertexArray(_vao);
GL_CHECK_ERROR();
glDrawArrays(GL_TRIANGLES, 0, 36);
GL_CHECK_ERROR();
glBindVertexArray(0); /* F.ck me! Forgot to unbind here too?!?!? */
/* Draw remote players' cube. */
for(const auto& remote_player : remote_players) {
glBindVertexArray(_vao); /* bind cube VAO for each remote player. */
BettolaMath::Mat4 remote_model = BettolaMath::Mat4::translation(remote_player.get_x(),
0.0f, remote_player.get_y());
glUniformMatrix4fv(model_loc, 1, GL_FALSE, remote_model.get_ptr());
glDrawArrays(GL_TRIANGLES, 0, 36);
GL_CHECK_ERROR();
glBindVertexArray(0); /* Unbind it! */
}
glBindVertexArray(0);
}
bool Renderer::_init_shaders(void) {

View File

@ -20,6 +20,8 @@ private:
Shader _shader;
unsigned int _vao;
unsigned int _vbo;
unsigned int _ground_vao;
unsigned int _ground_vbo;
BettolaMath::Mat4 _projection;
};