1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-23 02:42:39 +01:00

Improved Collision data loading and debugging.

- Removed interleaved data from CollisionInstance as Bullet didn't
understand.
- Improved performance of the DebugDraw to around 2-3 fps
This commit is contained in:
Daniel Evans 2013-07-22 02:38:43 +01:00
parent 46c400e078
commit 04a8a7d9c5
6 changed files with 91 additions and 49 deletions

View File

@ -13,7 +13,8 @@ DebugDraw::DebugDraw()
{
glGenBuffers(1, &vbo);
glGenVertexArrays(1, &vao);
glGenTextures(1, &texture);
glGenTextures(1, &texture);
maxlines = 0;
}
DebugDraw::~DebugDraw()
@ -25,41 +26,14 @@ DebugDraw::~DebugDraw()
void DebugDraw::drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
float img[] = {color.getX(), color.getY(), color.getZ()};
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGB, 1, 1,
0, GL_RGB, GL_FLOAT, img
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
float verts[] = {
from.getX(), from.getY(), from.getZ(),
to.getX(), to.getY(), to.getZ(),
};
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
GLint uniModel = glGetUniformLocation(shaderProgram, "model");
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW);
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
glm::mat4 model;
glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_LINES, 0, 2);
lines.push_back(glm::vec3(from.getX(), from.getY(), from.getZ()));
lines.push_back(glm::vec3(to.getX(), to.getY(), to.getZ()));
this->color = color;
}
void DebugDraw::drawTriangle(const btVector3 &a, const btVector3 &b, const btVector3 &c, const btVector3 &color, btScalar alpha)
{
glActiveTexture(GL_TEXTURE0);
/*glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
float img[] = {color.getX(), color.getY(), color.getZ()};
glTexImage2D(
@ -90,7 +64,7 @@ void DebugDraw::drawTriangle(const btVector3 &a, const btVector3 &b, const btVec
glm::mat4 model;
glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawArrays(GL_TRIANGLES, 0, 3);*/
}
void DebugDraw::drawContactPoint(const btVector3 &pointOnB, const btVector3 &normalOnB, btScalar distance, int lifeTime, const btVector3 &color)
@ -98,6 +72,50 @@ void DebugDraw::drawContactPoint(const btVector3 &pointOnB, const btVector3 &nor
}
void DebugDraw::drawAllLines()
{
if(lines.size() == 0) {
return;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
float img[] = {color.getX(), color.getY(), color.getZ()};
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGB, 1, 1,
0, GL_RGB, GL_FLOAT, img
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
GLint uniModel = glGetUniformLocation(shaderProgram, "model");
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
if( lines.size() > maxlines ) {
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * lines.size(), &lines[0].x, GL_STREAM_DRAW);
}
else {
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(glm::vec3) * lines.size(), &lines[0].x);
}
maxlines = std::max(maxlines, lines.size());
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
glm::mat4 model;
glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_LINES, 0, lines.size());
lines.clear();
}
void DebugDraw::reportErrorWarning(const char *warningString)
{
std::cerr << warningString << std::endl;
@ -116,4 +134,4 @@ void DebugDraw::setDebugMode(int debugMode)
int DebugDraw::getDebugMode() const
{
return debugMode;
}
}

View File

@ -160,8 +160,8 @@ bool GTAEngine::placeItems(const std::string& name)
if( physInst.triangles.size() > 0 ) {
btTriangleIndexVertexArray* vertarray = new btTriangleIndexVertexArray(
physInst.triangles.size(),
(int*) &(physInst.triangles.data()->a),
sizeof(CollTFace),
(int*) physInst.triangles.data(),
sizeof(CollTFaceTriangle),
physInst.vertices.size(),
&(physInst.vertices[0].x),
sizeof(glm::vec3)

View File

@ -33,7 +33,7 @@ bool LoaderCOL::load(char* data, const size_t size)
std::vector<CollTSphere> spheres;
std::vector<CollTBox> boxes;
std::vector<CollTVertex> meshvertices;
std::vector<CollTFaceV1> meshfaces;
std::vector<CollTFaceTriangle> meshfaces;
if(version >= 2)
{
@ -97,12 +97,12 @@ bool LoaderCOL::load(char* data, const size_t size)
CollTFaceV1 face = readType<CollTFaceV1>(data, &dataI);
size_t maxv = std::max(face.a, std::max(face.b, face.c));
maxvert = std::max( maxvert, maxv );
meshfaces.push_back(face);
meshfaces.push_back({face.a, face.b, face.c});
}
// Load up to maxvert vertices.
meshvertices.reserve(maxvert);
for( size_t v = 0, vertI = head2.offsetverts; v < maxvert; ++v ) {
meshvertices.reserve(maxvert+1);
for( size_t v = 0, vertI = head2.offsetverts; v < maxvert+1; ++v ) {
CollTVertex vert = readType<CollTVertex>(data, &vertI);
meshvertices.push_back(vert);
}

View File

@ -58,9 +58,9 @@ typedef glm::vec3 CollTVertex;
struct CollTFace
{
uint16_t a, b, c;
uint8_t material;
uint8_t light;
uint16_t a, b, c;
uint8_t material;
uint8_t light;
};
struct CollTFaceV1
@ -69,6 +69,17 @@ struct CollTFaceV1
CollTSurface surface;
};
struct CollTFaceTriangle
{
uint32_t a, b, c;
};
struct CollTFaceData
{
uint8_t material;
uint8_t light;
};
struct CollTHeader
{
char magic[4];
@ -113,7 +124,7 @@ public:
std::vector<CollTSphere> spheres;
std::vector<CollTBox> boxes;
std::vector<CollTVertex> vertices;
std::vector<CollTFaceV1> triangles;
std::vector<CollTFaceTriangle> triangles;
};
/**

View File

@ -4,6 +4,8 @@
#define GLEW_STATIC
#include <GL/glew.h>
#include <vector>
#include <glm/glm.hpp>
class DebugDraw : public btIDebugDraw
{
@ -19,13 +21,21 @@ public:
void setDebugMode(int debugMode);
int getDebugMode() const;
void drawAllLines();
void setShaderProgram(GLuint shaderProgram) {
this->shaderProgram = shaderProgram;
}
protected:
int debugMode;
std::vector<glm::vec3> lines;
size_t maxlines;
btVector3 color;
GLuint shaderProgram;
GLuint vbo, vao, texture;
};
};

View File

@ -24,6 +24,8 @@ sf::RenderWindow window;
GTAEngine* gta = nullptr;
DebugDraw* debugDrawer = nullptr;
glm::vec3 plyPos;
glm::vec2 plyLook;
float moveSpeed = 20.0f;
@ -98,10 +100,10 @@ void init(std::string gtapath)
if((k++ % 4) == 0) { spawnPos += glm::vec3(-20, -15, 0); }
}
DebugDraw* debg = new DebugDraw;
debg->setShaderProgram(gta->renderer.worldProgram);
debg->setDebugMode(btIDebugDraw::DBG_DrawWireframe);
gta->dynamicsWorld->setDebugDrawer(debg);
debugDrawer = new DebugDraw;
debugDrawer->setShaderProgram(gta->renderer.worldProgram);
debugDrawer->setDebugMode(btIDebugDraw::DBG_DrawWireframe);
gta->dynamicsWorld->setDebugDrawer(debugDrawer);
}
void update(float dt)
@ -170,6 +172,7 @@ void render()
glUniformMatrix4fv(gta->renderer.uniView, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(gta->renderer.uniProj, 1, GL_FALSE, glm::value_ptr(proj));
gta->dynamicsWorld->debugDrawWorld();
debugDrawer->drawAllLines();
}
else {
gta->renderer.renderWorld(gta);