redesigned model a bit.
This commit is contained in:
parent
26e1d4fd19
commit
677e749ac2
4 changed files with 125 additions and 51 deletions
|
@ -1,12 +1,83 @@
|
||||||
#include "polygon_model.hpp"
|
#include "polygon_model.hpp"
|
||||||
|
|
||||||
|
void discardLastGlError()
|
||||||
|
{
|
||||||
|
glGetError();
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkAndPrintGlError()
|
||||||
|
{
|
||||||
|
GLenum err = glGetError();
|
||||||
|
const char *errString;
|
||||||
|
switch(err) {
|
||||||
|
case GL_INVALID_ENUM: errString = "GL_INVALID_ENUM"; break;
|
||||||
|
case GL_INVALID_VALUE: errString = "GL_INVALID_VALUE"; break;
|
||||||
|
case GL_INVALID_OPERATION: errString = "GL_INVALID_OPERATION"; break;
|
||||||
|
case GL_OUT_OF_MEMORY: errString = "GL_OUT_OF_MEMORY"; break;
|
||||||
|
case GL_INVALID_FRAMEBUFFER_OPERATION: errString = "GL_INVALID_FRAMEBUFFER_OPERATION"; break;
|
||||||
|
default: errString = "<unknown>"; break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (err != GL_NO_ERROR) {
|
||||||
|
std::cout<<"glGetAttribLocation() returned error: " << errString << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
namespace endofthejedi {
|
namespace endofthejedi {
|
||||||
PolygonModel::PolygonModel(const std::string &filename) : m_filename(filename)
|
PolygonModel::PolygonModel(const std::string &filename)
|
||||||
|
: m_filename(filename)
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PolygonModel::bind(Shader &shader)
|
// TODO: add change/set shader function because it stays for all calls of an
|
||||||
|
// instance the same.
|
||||||
|
void PolygonModel::setup(Shader *shader)
|
||||||
|
{
|
||||||
|
if (shader == nullptr) {
|
||||||
|
std::cout<<"shader is NULL" << std::endl;
|
||||||
|
exit(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shader->program() == 0) {
|
||||||
|
std::cout<<"program is invalid" << std::endl;
|
||||||
|
exit(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout<<"program: " << shader->program() << std::endl;
|
||||||
|
|
||||||
|
m_shader = shader;
|
||||||
|
|
||||||
|
// TODO error checks
|
||||||
|
const char *names[] = {
|
||||||
|
"in_vertex",
|
||||||
|
"in_normal"
|
||||||
|
};
|
||||||
|
|
||||||
|
discardLastGlError();
|
||||||
|
|
||||||
|
for (int i=0; i<2; i++) {
|
||||||
|
const char *name = names[i];
|
||||||
|
GLint loc = glGetAttribLocation(m_shader->program(), name);
|
||||||
|
m_attr_locations[i] = loc;
|
||||||
|
|
||||||
|
checkAndPrintGlError();
|
||||||
|
|
||||||
|
if (m_attr_locations[i] == -1) {
|
||||||
|
std::cerr<<"[polygonmodel] warning: attribute location #"
|
||||||
|
<< i << " (for '" << name
|
||||||
|
<< "') is invalid!" << std::endl;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
std::cout<<"[polygonmodel] attribute location #"
|
||||||
|
<< i << " (for '" << name
|
||||||
|
<< "') is " << m_attr_locations[i] << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PolygonModel::bind()
|
||||||
{
|
{
|
||||||
if (!m_loaded_to_opengl) {
|
if (!m_loaded_to_opengl) {
|
||||||
std::cout<<"[polygonmodel] warning: try to bind model vbo "
|
std::cout<<"[polygonmodel] warning: try to bind model vbo "
|
||||||
|
@ -17,39 +88,25 @@ namespace endofthejedi {
|
||||||
}
|
}
|
||||||
|
|
||||||
// bind position vbo
|
// bind position vbo
|
||||||
glEnableVertexAttribArray(0);
|
for (int i=0; i<2; i++) {
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position);
|
glEnableVertexAttribArray(m_attr_locations[0]);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id[i]);
|
||||||
|
glVertexAttribPointer(m_attr_locations[i], 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
||||||
// bind normal vbo
|
}
|
||||||
GLint loc = glGetAttribLocation(shader.program(), "in_normal");
|
|
||||||
glEnableVertexAttribArray(loc);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_normal);
|
|
||||||
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
|
|
||||||
|
|
||||||
m_binding_active = true;
|
m_binding_active = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PolygonModel::render(Shader& shader)
|
bool PolygonModel::render()
|
||||||
{
|
{
|
||||||
(void) shader;
|
|
||||||
|
|
||||||
if (!m_binding_active || !m_loaded_to_opengl) {
|
if (!m_binding_active || !m_loaded_to_opengl) {
|
||||||
std::cout<<"[polygonmodel] warning: try to render model without bind()" << std::endl;
|
std::cout<<"[polygonmodel] warning: try to render model without bind()" << std::endl;
|
||||||
exit(-1);
|
exit(-1);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//GLint l0 = glGetAttribLocation(shader.program(), "in_normal");
|
|
||||||
//GLint l1 = glGetAttribLocation(shader.program(), "in_vertex");
|
|
||||||
//GLint l2 = glGetAttribLocation(shader.program(), "in_fakkkke");
|
|
||||||
//std::cout<<"locations: " << l0 << " " << l1 << " " << l2 << std::endl;
|
|
||||||
|
|
||||||
//glBindAttribLocation(shader.program(), 0, "in_vertex");
|
|
||||||
//glBindAttribLocation(shader.program(), 1, "in_normal");
|
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, m_numVertices);
|
glDrawArrays(GL_TRIANGLES, 0, m_numVertices);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -116,13 +173,14 @@ namespace endofthejedi {
|
||||||
/* Position data */
|
/* Position data */
|
||||||
/************************************************************/
|
/************************************************************/
|
||||||
|
|
||||||
glEnableVertexAttribArray(0);
|
glGenBuffers(2, m_vbo_id); // Generate buffer
|
||||||
|
|
||||||
glGenBuffers(1, &m_vbo_id_position); // Generate buffer
|
glEnableVertexAttribArray(m_attr_locations[0]);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position); // Bind buffer
|
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id[0]); // Bind buffer
|
||||||
|
|
||||||
glVertexAttribPointer(0,
|
glVertexAttribPointer(
|
||||||
3, // three floats per vertex
|
m_attr_locations[0],
|
||||||
|
3, // three floats per vertex
|
||||||
GL_FLOAT, // Data is floating point type
|
GL_FLOAT, // Data is floating point type
|
||||||
GL_FALSE, // No fixed point scaling
|
GL_FALSE, // No fixed point scaling
|
||||||
0, // stride: no
|
0, // stride: no
|
||||||
|
@ -139,12 +197,11 @@ namespace endofthejedi {
|
||||||
/* Normal data */
|
/* Normal data */
|
||||||
/************************************************************/
|
/************************************************************/
|
||||||
|
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(m_attr_locations[1]);
|
||||||
glGenBuffers(1, &m_vbo_id_normal); // Generate buffer
|
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id[1]); // Bind buffer
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_normal); // Bind buffer
|
|
||||||
|
|
||||||
glVertexAttribPointer(
|
glVertexAttribPointer(
|
||||||
1,
|
m_attr_locations[1],
|
||||||
3, // three floats per normal
|
3, // three floats per normal
|
||||||
GL_FLOAT, // Data is floating point type
|
GL_FLOAT, // Data is floating point type
|
||||||
GL_FALSE, // No fixed point scaling
|
GL_FALSE, // No fixed point scaling
|
||||||
|
@ -206,8 +263,11 @@ namespace endofthejedi {
|
||||||
m_data_position.clear();
|
m_data_position.clear();
|
||||||
m_data_normal.clear();
|
m_data_normal.clear();
|
||||||
|
|
||||||
m_vbo_id_position = 0;
|
for (int i=0; i<2; i++) {
|
||||||
m_vbo_id_normal = 0;
|
m_vbo_id[i] = -1;
|
||||||
|
m_attr_locations[i] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
m_numVertices = 0;
|
m_numVertices = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,13 +23,25 @@ namespace endofthejedi {
|
||||||
public:
|
public:
|
||||||
PolygonModel(const std::string &filename);
|
PolygonModel(const std::string &filename);
|
||||||
|
|
||||||
void clear();
|
// (re) load data from file
|
||||||
|
|
||||||
bool import();
|
bool import();
|
||||||
|
|
||||||
|
// connect stuff with the shader for rendering
|
||||||
|
// supply the current shader that should be used for this model.
|
||||||
|
// when it recompiles, call setup() again.
|
||||||
|
void setup(Shader *shader);
|
||||||
|
|
||||||
|
// upload model data to GPU
|
||||||
bool uploadToOpenGl();
|
bool uploadToOpenGl();
|
||||||
bool bind(Shader &shader);
|
|
||||||
bool render(Shader& shader);
|
// call bind before rendering
|
||||||
|
bool bind();
|
||||||
|
|
||||||
|
// call to render
|
||||||
|
bool render();
|
||||||
|
|
||||||
|
// clear everything and delete data
|
||||||
|
void clear();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool copyVertices(const aiScene *scene);
|
bool copyVertices(const aiScene *scene);
|
||||||
|
@ -48,7 +60,9 @@ namespace endofthejedi {
|
||||||
|
|
||||||
size_t m_numVertices;
|
size_t m_numVertices;
|
||||||
|
|
||||||
GLuint m_vbo_id_position;
|
// TODO: watch for shader recompile
|
||||||
GLuint m_vbo_id_normal;
|
GLint m_attr_locations[2];
|
||||||
|
GLuint m_vbo_id[2];
|
||||||
|
Shader *m_shader;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,12 +13,6 @@ namespace endofthejedi {
|
||||||
|
|
||||||
std::cout<<"setup polygon 3d" << std::endl;
|
std::cout<<"setup polygon 3d" << std::endl;
|
||||||
|
|
||||||
//m_missileModel = new PolygonModel("../data/mesh/quad_screen_coords.stl");
|
|
||||||
|
|
||||||
addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel);
|
|
||||||
addModel("../data/mesh/planet_64.stl", &m_planetModel);
|
|
||||||
addModel("../data/mesh/ship_ufo.stl", &m_shipModel);
|
|
||||||
|
|
||||||
std::string vss_simple =
|
std::string vss_simple =
|
||||||
#include "simple.vert"
|
#include "simple.vert"
|
||||||
;
|
;
|
||||||
|
@ -48,6 +42,10 @@ namespace endofthejedi {
|
||||||
m_shader.load(vss_game_objects.c_str(), GL_VERTEX_SHADER);
|
m_shader.load(vss_game_objects.c_str(), GL_VERTEX_SHADER);
|
||||||
m_shader.load(fss_game_objects.c_str(), GL_FRAGMENT_SHADER);
|
m_shader.load(fss_game_objects.c_str(), GL_FRAGMENT_SHADER);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
addModel("../data/mesh/small_atomic_bomb.stl", &m_missileModel);
|
||||||
|
addModel("../data/mesh/planet_64.stl", &m_planetModel);
|
||||||
|
addModel("../data/mesh/ship_ufo.stl", &m_shipModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RendererPolygon3d::render(const game::State *state)
|
void RendererPolygon3d::render(const game::State *state)
|
||||||
|
@ -154,7 +152,7 @@ namespace endofthejedi {
|
||||||
|
|
||||||
void RendererPolygon3d::renderPlanets()
|
void RendererPolygon3d::renderPlanets()
|
||||||
{
|
{
|
||||||
m_planetModel->bind(m_shader);
|
m_planetModel->bind();
|
||||||
|
|
||||||
for (const game::Planet *planet : m_state->planets) {
|
for (const game::Planet *planet : m_state->planets) {
|
||||||
glm::mat4 model = computeModelMatrix(planet);
|
glm::mat4 model = computeModelMatrix(planet);
|
||||||
|
@ -164,13 +162,13 @@ namespace endofthejedi {
|
||||||
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
|
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
|
||||||
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
|
glUniform3f(m_shader.location("color"), c.x, c.y, c.z);
|
||||||
|
|
||||||
m_planetModel->render(m_shader);
|
m_planetModel->render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RendererPolygon3d::renderMissiles()
|
void RendererPolygon3d::renderMissiles()
|
||||||
{
|
{
|
||||||
m_missileModel->bind(m_shader);
|
m_missileModel->bind();
|
||||||
|
|
||||||
for (const game::Player *player : m_state->players) {
|
for (const game::Player *player : m_state->players) {
|
||||||
for (const game::Missile *missile : player->missiles) {
|
for (const game::Missile *missile : player->missiles) {
|
||||||
|
@ -180,14 +178,14 @@ namespace endofthejedi {
|
||||||
glm::mat4 model = computeModelMatrix(missile);
|
glm::mat4 model = computeModelMatrix(missile);
|
||||||
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
|
glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model));
|
||||||
|
|
||||||
m_missileModel->render(m_shader);
|
m_missileModel->render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RendererPolygon3d::renderShips()
|
void RendererPolygon3d::renderShips()
|
||||||
{
|
{
|
||||||
m_shipModel->bind(m_shader);
|
m_shipModel->bind();
|
||||||
|
|
||||||
for (const game::Ship *ship : m_state->ships) {
|
for (const game::Ship *ship : m_state->ships) {
|
||||||
glm::mat4 model = computeModelMatrix(ship);
|
glm::mat4 model = computeModelMatrix(ship);
|
||||||
|
@ -196,7 +194,7 @@ namespace endofthejedi {
|
||||||
glm::vec3 c = glm::vec3(0.1, 1.0, 0.2);
|
glm::vec3 c = glm::vec3(0.1, 1.0, 0.2);
|
||||||
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
|
glUniform3f(m_shader.location("materialColor"), c.x, c.y, c.z);
|
||||||
|
|
||||||
m_shipModel->render(m_shader);
|
m_shipModel->render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,6 +208,7 @@ namespace endofthejedi {
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(*dest)->setup(&m_shader);
|
||||||
(*dest)->uploadToOpenGl();
|
(*dest)->uploadToOpenGl();
|
||||||
|
|
||||||
m_models.push_back(*dest);
|
m_models.push_back(*dest);
|
||||||
|
|
|
@ -30,6 +30,7 @@ namespace game {
|
||||||
|
|
||||||
void Trace::addPointFromMissile(bool forceAdd)
|
void Trace::addPointFromMissile(bool forceAdd)
|
||||||
{
|
{
|
||||||
|
// TODO: make configurable
|
||||||
fidelityCounter++;
|
fidelityCounter++;
|
||||||
if (forceAdd || fidelityCounter >= 10) {
|
if (forceAdd || fidelityCounter >= 10) {
|
||||||
fidelityCounter = 0;
|
fidelityCounter = 0;
|
||||||
|
|
Loading…
Reference in a new issue