From 59b81aebe088e60cda2c2215ff676c9d621c763a Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 6 Oct 2012 22:03:37 -0400 Subject: [PATCH] add sphere object, attempting to draw shot but position is way wrong --- src/client/Client-gl.cc | 81 +++++++++++++++++++++++++++++++++++++++++ src/client/Client.h | 3 ++ 2 files changed, 84 insertions(+) diff --git a/src/client/Client-gl.cc b/src/client/Client-gl.cc index 498297f..7b6d3e3 100644 --- a/src/client/Client-gl.cc +++ b/src/client/Client-gl.cc @@ -18,6 +18,7 @@ using namespace std; #define NUM_SKY_STEPS 9 #define LAVA_SIZE 100 #define NUM_SHOT_RING_STEPS 24 +#define NUM_SPHERE_SLICES 12 /* points of a horizontal hexagon 1.0 units high */ static const float overlay_hex_attributes[][3] = { @@ -219,6 +220,48 @@ bool Client::initgl() cerr << "Error creating shot ring attributes buffer" << endl; return false; } + vector sphere_attributes( + (NUM_SPHERE_SLICES + 1) * (NUM_SPHERE_SLICES / 2 + 1) * (3 + 3)); + for (int i = 0, idx = 0; i <= NUM_SPHERE_SLICES; i++) + { + for (int j = 0; j <= NUM_SPHERE_SLICES / 2; j++) + { + double z_angle = i * M_PI * 2.0 / NUM_SPHERE_SLICES; + GLfloat xo = cos(z_angle); + GLfloat yo = sin(z_angle); + double x_angle = j * M_PI * 2.0 / NUM_SPHERE_SLICES; + GLfloat r = sin(x_angle); + GLfloat x = r * xo; + GLfloat y = r * yo; + GLfloat z = -cos(x_angle); + sphere_attributes[idx++] = x; + sphere_attributes[idx++] = y; + sphere_attributes[idx++] = z; + sphere_attributes[idx++] = x; + sphere_attributes[idx++] = y; + sphere_attributes[idx++] = z; + } + } + if (!m_sphere_attributes.create(GL_ARRAY_BUFFER, GL_STATIC_DRAW, + &sphere_attributes[0], + sizeof(sphere_attributes[0]) * sphere_attributes.size())) + { + cerr << "Error creating sphere attributes buffer" << endl; + return false; + } + vector sphere_indices; + for (int i = 0; i <= NUM_SPHERE_SLICES / 2; i++) + { + sphere_indices.push_back(i); + sphere_indices.push_back(i + (NUM_SPHERE_SLICES / 2) + 1); + } + if (!m_sphere_indices.create(GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW, + &sphere_indices[0], + sizeof(sphere_indices[0]) * sphere_indices.size())) + { + cerr << "Error creating sphere indices buffer" << endl; + return false; + } unsigned int lava_texture_length; const uint8_t *lava_texture = CFS.get_file("textures/lava.jpg", &lava_texture_length); @@ -273,6 +316,11 @@ void Client::redraw() draw_lava(); draw_shot_ring(); + for (m_shots_iterator_t it = m_shots.begin(); it != m_shots.end(); it++) + { + draw_shot(*it); + } + draw_overlay(); } @@ -392,6 +440,39 @@ void Client::draw_map() } } +void Client::draw_shot(refptr shot) +{ + m_obj_program.use(); + m_sphere_attributes.bind(); + m_sphere_indices.bind(); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, + 6 * sizeof(GLfloat), NULL); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, + 6 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat))); + m_modelview.push(); + sf::Vector3f pos = shot->get_position(); + m_modelview.translate(pos.x, pos.y, pos.z); + m_projection.to_uniform(m_obj_program.uniform("projection")); + m_modelview.to_uniform(m_obj_program.uniform("modelview")); + glUniform4f(m_obj_program.uniform("ambient"), 0, 0, 1, 1); + glUniform4f(m_obj_program.uniform("diffuse"), 0, 0, 1, 1); + glUniform4f(m_obj_program.uniform("specular"), 1, 1, 1, 1); + glUniform1f(m_obj_program.uniform("shininess"), 1); + for (int i = 0; i < NUM_SPHERE_SLICES; i++) + { + glDrawElementsBaseVertex(GL_TRIANGLE_STRIP, + 2 * ((NUM_SPHERE_SLICES / 2) + 1), + GL_UNSIGNED_SHORT, + NULL, + i * ((NUM_SPHERE_SLICES / 2) + 1)); + } + m_modelview.pop(); + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); +} + void Client::draw_overlay() { /* draw overlay map */ diff --git a/src/client/Client.h b/src/client/Client.h index a4cd811..76f8b6e 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -33,6 +33,7 @@ class Client void recenter_cursor(); void draw_player(refptr player); void draw_map(); + void draw_shot(refptr shot); void draw_overlay(); void draw_sky(); void draw_lava(); @@ -67,6 +68,8 @@ class Client GLBuffer m_tex_quad_attributes; GLBuffer m_quad_attributes; GLBuffer m_shot_ring_attributes; + GLBuffer m_sphere_attributes; + GLBuffer m_sphere_indices; refptr m_net_client; bool m_client_has_focus; sf::Texture m_lava_texture;