1
0
Fork 0

Oprava vykreslovania AABB stromu. Vylepsenie parsovania OBJ

GLSL zakazuje pouzit deklarovat premenne zacinajuce na gl_
master
Dušan Poizl 9 years ago
parent 861ce277c1
commit 8026db57cd
  1. 6
      aabb.geom
  2. 10
      aabb.vert
  3. 22
      bvh/tree.cpp
  4. 13
      bvh/tree.h
  5. 47
      glwidget.cpp
  6. 8
      glwidget.h
  7. 2
      intersect_kernel.cl
  8. 8
      main.cpp
  9. 24
      objmodel.cpp
  10. 10
      objmodel.h
  11. 4
      tracer.cpp

@ -9,13 +9,13 @@ in vert_geom
{
vec4 min;
vec4 max;
}gl_in[];
}_in[];
void main()
{
vec4 v[8];
vec4 min = gl_in[0].min;
vec4 max = gl_in[0].max;
vec4 min = _in[0].min;
vec4 max = _in[0].max;
for(int i=0;i<8;i++)v[i] = min;
v[1].x = max.x;

@ -1,16 +1,16 @@
#version 400
in vec4 min;
in vec4 max;
in vec3 min;
in vec3 max;
out vert_geom
{
vec4 min;
vec4 max;
}gl_out;
}_out;
void main()
{
gl_out.min = min;
gl_out.max = max;
_out.min = vec4(min, 1.0);
_out.max = vec4(max, 1.0);
}

@ -2,37 +2,45 @@
#include <algorithm>
#include <queue>
using namespace std;
namespace BVH
{
Comparator::Comparator(vector<Triangle> *s)
{
tris_center.reserve(s->size());
tris_center = new vector<cl_float3>;
tris_center->reserve(s->size());
for(vector<Triangle>::iterator i=s->begin();i!=s->end();i++)
{
cl_float3 center;
center.x = (i->a.x+i->b.x+i->c.x)/3;
center.y = (i->a.y+i->b.y+i->c.y)/3;
center.z = (i->a.z+i->b.z+i->c.z)/3;
tris_center.push_back(center);
tris_center->push_back(center);
}
}
void Comparator::clear()
{
delete tris_center;
}
bool Comparator::operator()(int a, int b)
{
switch(axis)
{
case 0:
return tris_center[a].x < tris_center[b].x;
return (*tris_center)[a].x < (*tris_center)[b].x;
break;
case 1:
return tris_center[a].y < tris_center[b].y;
return (*tris_center)[a].y < (*tris_center)[b].y;
break;
case 2:
return tris_center[a].z < tris_center[b].z;
return (*tris_center)[a].z < (*tris_center)[b].z;
break;
}
return tris_center[a].x < tris_center[b].x;
return (*tris_center)[a].x < (*tris_center)[b].x;
}
void Tree::buildTree()
@ -76,6 +84,8 @@ void Tree::buildTree()
else tree[n.node].setRightTriangle(tris_index[n.end]);
}
for(int i=tree.size()-1;i>=0;i--)tree[i].calcAABB(triangles, tree);
comp.clear();
}
Tree::Tree()

@ -6,16 +6,15 @@
#include "triangle.h"
#include "node.h"
using namespace std;
namespace BVH
{
class Comparator
{
vector<cl_float3> tris_center;
std::vector<cl_float3> *tris_center;
public:
Comparator(vector<Triangle> *s);
Comparator(std::vector<Triangle> *s);
void clear();
bool operator()(int a, int b);
uint axis;
};
@ -28,12 +27,12 @@ struct NodeItem
class Tree
{
public:
vector<Triangle> triangles;
vector<Node> tree;
std::vector<Triangle> triangles;
std::vector<Node> tree;
void buildTree();
public:
Tree();
Tree(vector<int> &indicies, vector<cl_float3> &vertexs);
Tree(std::vector<int> &indicies, std::vector<cl_float3> &vertexs);
};
}

@ -7,7 +7,9 @@
#include <QTimer>
#include "objmodel.h"
GLWidget::GLWidget()
GLWidget::GLWidget() :
start(0),
count(1)
{
resize(512, 512);
}
@ -18,19 +20,21 @@ GLWidget::~GLWidget()
void GLWidget::initializeGL()
{
program = new QGLShaderProgram(this);
program->addShaderFromSourceFile(QGLShader::Vertex, "shader.vert");
program->addShaderFromSourceFile(QGLShader::Fragment, "shader.frag");
program = new QOpenGLShaderProgram(this);
program->addShaderFromSourceFile(QOpenGLShader::Vertex, "shader.vert");
program->addShaderFromSourceFile(QOpenGLShader::Fragment, "shader.frag");
program->bindAttributeLocation("vertex", 0);
program->link();
//cout << program->log().toStdString() << endl;
ObjModel model("mesh.obj");
ObjModel model("sponza.obj");
vt.create();
vt.bind();
vt.allocate(&model.getVertexs()[0], sizeof(cl_float3)*model.getVertexs().size());
indices = QGLBuffer(QGLBuffer::IndexBuffer);
qDebug() << "model loaded";
indices = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
indices.create();
indices.bind();
vector<int> indexs;
@ -46,8 +50,10 @@ void GLWidget::initializeGL()
}
indices.allocate(&indexs[0], sizeof(int)*o);
qDebug() << "indices loaded";
tree = BVH::Tree(indexs, model.getVertexs());
vertex = QGLBuffer(QGLBuffer::VertexBuffer);
vertex = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
vertex.create();
vertex.bind();
vertex.allocate(&tree.tree[0], sizeof(BVH::Node)*tree.tree.size());
@ -56,8 +62,8 @@ void GLWidget::initializeGL()
QMatrix4x4 modelview;
QMatrix4x4 projection;
projection.perspective(50, 1, 0.1, 1000);
modelview.lookAt(QVector3D(0, 4, 4), QVector3D(0, 0, 0), QVector3D(0, 1, 0));
projection.perspective(50, 1, 0.1, 10000);
modelview.lookAt(QVector3D(0, tree.tree[0].getMax().y*2, tree.tree[0].getMax().z*2), QVector3D(0, 0, 0), QVector3D(0, 1, 0));
program->bind();
mat = projection*modelview;
program->setUniformValue("ModelViewProjectionMatrix", mat);
@ -68,15 +74,18 @@ void GLWidget::initializeGL()
timer->start();
glEnable(GL_DEPTH_TEST);
aabb = new QGLShaderProgram(this);
aabb->addShaderFromSourceFile(QGLShader::Vertex, "aabb.vert");
aabb->addShaderFromSourceFile(QGLShader::Geometry, "aabb.geom");
aabb->addShaderFromSourceFile(QGLShader::Fragment, "aabb.frag");
aabb = new QOpenGLShaderProgram(this);
aabb->addShaderFromSourceFile(QOpenGLShader::Vertex, "aabb.vert");
aabb->addShaderFromSourceFile(QOpenGLShader::Geometry, "aabb.geom");
aabb->addShaderFromSourceFile(QOpenGLShader::Fragment, "aabb.frag");
aabb->bindAttributeLocation("min", 0);
aabb->bindAttributeLocation("max", 1);
aabb->link();
aabb->bind();
aabb->setUniformValue("ModelViewProjectionMatrix", mat);
qDebug() << tree.tree[0].getMin().x << tree.tree[0].getMin().y << tree.tree[0].getMin().z;
qDebug() << tree.tree[0].getMax().x << tree.tree[0].getMax().y << tree.tree[0].getMax().z;
}
void GLWidget::resizeGL(int w, int h)
@ -102,14 +111,15 @@ void GLWidget::paintGL()
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(BVH::Node), 0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, sizeof(BVH::Node), (void*)(sizeof(cl_float3)));
glVertexAttribPointer(1, 3, GL_FLOAT, false, sizeof(BVH::Node), (void*)sizeof(cl_float3));
aabb->bind();
aabb->setUniformValue("ModelViewProjectionMatrix", mat);
aabb->setUniformValue("color", QVector4D(0, 1, 0, 1));
glDrawArrays(GL_POINTS, start, count);
glDrawArrays(GL_POINTS, start, count>3000 ? 3000 : count);
// glDrawArrays(GL_POINTS, 0, 1);
aabb->setUniformValue("color", QVector4D(1, 1, 0, 1));
glDrawArrays(GL_POINTS, start*2+1, count*2);
//glDrawArrays(GL_POINTS, start*2+1, count*2);
}
void GLWidget::keyPressEvent(QKeyEvent *event)
@ -124,8 +134,9 @@ void GLWidget::keyPressEvent(QKeyEvent *event)
start = (start-1)/2;
count /= 2;
}
if(count<=0)count = 3;
if(count>3*1024)count = 3*1024;
if(count<=0)count = 1;
//if(count>3*1024)count = 3*1024;
if(event->key()==Qt::Key_Right)
{
mat.rotate(0.5, 0, 1, 0);

@ -5,8 +5,8 @@
#define GL_GLEXT_PROTOTYPES 1
#include <GL/glext.h>
#include <QKeyEvent>
#include <QGLShaderProgram>
#include <QGLBuffer>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QGLWidget>
#include "bvh/tree.h"
@ -16,8 +16,8 @@ class GLWidget : public QGLWidget
{
Q_OBJECT
int start, count;
QGLShaderProgram *program, *aabb;
QGLBuffer vertex, vt, indices;
QOpenGLShaderProgram *program, *aabb;
QOpenGLBuffer vertex, vt, indices;
int obj_count;
QMatrix4x4 mat;
void initializeGL();

@ -91,7 +91,7 @@ __kernel void aabb_traverse(int num_triangles, __global float3 *vertexs, __globa
view.xy /= convert_float2(gsize);
view.xy -= (float2)(0.5f);
float3 origin = (float3)(0.000001f, 0, -4);
float3 origin = (float3)(0.0f, 0, -4);
float min_t = INFINITY;
uint current = 0;

@ -8,11 +8,11 @@ int main(int argc, char **argv)
{
QApplication app(argc, argv);
//GLWidget glwidget;
//glwidget.show();
GLWidget glwidget;
glwidget.show();
ImgView imgview;
imgview.show();
//ImgView imgview;
//imgview.show();
return app.exec();
}

@ -1,36 +1,36 @@
#include "objmodel.h"
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
ObjModel::ObjModel(const char* filename)
{
ifstream fr(filename);
string s,s1,s2,s3;
cl_float3 v;
cl_uint3 f;
char line[1000];
string line;
if(fr.good())
{
while(1)
{
fr >> s;
if(fr.eof())break;
getline(fr, line);
if(fr.eof() || !fr.good())break;
if(s=="v")//vertex
if(line.substr(0, 2) =="v ")//vertex
{
fr >> v.x >> v.y >> v.z;
istringstream s(line.substr(2));
s >> v.x >> v.y >> v.z;
vertexs.push_back(v);
}
else if(s=="f")//face
else if(line.substr(0, 2)=="f ")//face
{
fr >> f.x >> f.y >> f.z;
istringstream s(line.substr(2));
s >> f.x >> f.y >> f.z;
f.x--; f.y--; f.z--;
faces.push_back(f);
}
else
{
fr.getline(line, 1000);
}
}
}
}

@ -4,17 +4,15 @@
#include <CL/cl.h>
#include <vector>
using namespace std;
class ObjModel
{
vector<cl_float3> vertexs;
vector<cl_uint3> faces;
std::vector<cl_float3> vertexs;
std::vector<cl_uint3> faces;
public:
ObjModel(const char* filename);
~ObjModel();
vector<cl_float3>& getVertexs(){ return vertexs; }
vector<cl_uint3>& getFaces(){ return faces; }
std::vector<cl_float3>& getVertexs(){ return vertexs; }
std::vector<cl_uint3>& getFaces(){ return faces; }
};
#endif // _OBJMODEL_H_NOU_

@ -29,7 +29,7 @@ Tracer::Tracer(cl::Device &device)
stringstream buffer;
buffer << fr.rdbuf();
program = unique_ptr<cl::Program>(new cl::Program(*context, buffer.str(), false, &err));
program->build("-g");
program->build();
cout << "program " << err << endl;
if(1||err!=CL_SUCCESS)
{
@ -156,7 +156,7 @@ QImage Tracer::getImg2()
for(int i=0;i<512*512*4;i++)sum += data[i];
cout << "suma" << sum << endl;
QImage img(data, 512, 512, QImage::Format_ARGB32);
delete [] data;
//delete [] data;
return img;
}

Loading…
Cancel
Save