diff --git a/README.md b/README.md index 67fbd5f9..6de8f926 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,9 @@ Camera rotate/zoom implemented, [here](http://www.songho.ca/opengl/gl_transform. Added x,z axis, looks better than last screenshot. I have to use the GLU library, the previous implementation of drawSphere and drawCylinder looks not good, and take too much time to debug. +*B-Mesh data struct* +I created the test nodes's geometry information from Blender. Here is the render result of `data/bmesh_test_1.h` + - [ ] Export Wavefront .obj - [ ] Render B-Mesh result - [ ] Design UI for monster parts configuration diff --git a/data/bmesh_test_1.blend b/data/bmesh_test_1.blend new file mode 100644 index 00000000..ccf66f28 Binary files /dev/null and b/data/bmesh_test_1.blend differ diff --git a/data/bmesh_test_1.h b/data/bmesh_test_1.h new file mode 100644 index 00000000..32df44ff --- /dev/null +++ b/data/bmesh_test_1.h @@ -0,0 +1,12 @@ +const float bmeshTest1Nodes[][4] = { + {0, -2.07575, 1.53902, 0.04122}, {1, 2.40837, 2.34882, 0.48585}, + {2, -0.91403, 0.77069, 0.62299}, {3, 2.25224, 0.74973, 0.85115}, + {4, 0, 0, 0}, + {5, 0.00920, -0.66115, -2.04601}, + {6, 0.01726, -0.88224, -2.87471} +}; + +const int bmeshTest1Edges[][2] = { + {0, 2}, {2, 4}, {4, 3}, {3, 1}, + {4, 5}, {5, 6} +}; diff --git a/screenshot/dust3d_bmesh_nodes.png b/screenshot/dust3d_bmesh_nodes.png new file mode 100644 index 00000000..1e0b2499 Binary files /dev/null and b/screenshot/dust3d_bmesh_nodes.png differ diff --git a/src/draw.cpp b/src/draw.cpp index 953c5bb2..ff7b8ce3 100644 --- a/src/draw.cpp +++ b/src/draw.cpp @@ -3,11 +3,7 @@ #include #include #include -#ifdef __APPLE__ -#include -#else -#include -#endif +#include #include "draw.h" static GLUquadricObj *quadricId = 0; @@ -92,3 +88,11 @@ int drawGrid(float size, float step) { glEnable(GL_LIGHTING); return 0; } + +int drawPrintf(int x, int y, const char *fmt, ...) { + va_list args; + char text[1024]; + va_start(args, fmt); + vsnprintf(text, sizeof(text), fmt, args); + return drawText(x, y, text); +} diff --git a/src/draw.h b/src/draw.h index 257d6020..a3acb605 100644 --- a/src/draw.h +++ b/src/draw.h @@ -1,6 +1,11 @@ #ifndef DRAW_SPHERE_H #define DRAW_SPHERE_H #include "vector3d.h" +#ifdef __APPLE__ +#include +#else +#include +#endif #ifdef __cplusplus extern "C" { @@ -21,6 +26,8 @@ void drawTriangle(triangle *poly); int drawCylinder(vec3 *topOrigin, vec3 *bottomOrigin, float radius, int slices, int stacks); int drawGrid(float size, float step); +int drawText(int x, int y, char *text); +int drawPrintf(int x, int y, const char *fmt, ...); #ifdef __cplusplus } diff --git a/src/render.cpp b/src/render.cpp index 24b1c6a4..9fd51405 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -7,6 +7,8 @@ #include "bmesh.h" #include "matrix.h" +static QGLWidget *_this = 0; + static int drawBmeshNode(bmesh *bm, bmeshNode *node) { float color1[3] = {1, 0, 0}; glColor3fv(color1); @@ -15,7 +17,7 @@ static int drawBmeshNode(bmesh *bm, bmeshNode *node) { } static int drawBmeshEdge(bmesh *bm, bmeshEdge *edge) { - float color2[3] = {0, 0, 1}; + float color2[3] = {1, 1, 0}; glColor3fv(color2); bmeshNode *firstNode = bmeshGetNode(bm, edge->firstNode); bmeshNode *secondNode = bmeshGetNode(bm, edge->secondNode); @@ -23,6 +25,11 @@ static int drawBmeshEdge(bmesh *bm, bmeshEdge *edge) { return 0; } +int drawText(int x, int y, char *text) { + _this->renderText(x, y, QString(text)); + return 0; +} + Render::Render(QWidget *parent) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) { QTimer *timer = new QTimer(this); @@ -31,8 +38,8 @@ Render::Render(QWidget *parent) mouseX = 0; mouseY = 0; - cameraAngleX = 45; - cameraAngleY = -45; + cameraAngleX = 20; + cameraAngleY = -225; cameraDistance = 3; } @@ -43,8 +50,14 @@ void Render::initializeGL() { glShadeModel(GL_SMOOTH); glEnable(GL_CULL_FACE); glEnable(GL_BLEND); + glDepthFunc(GL_LEQUAL); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glEnable(GL_DEPTH_TEST); + glEnable(GL_LIGHTING); + glEnable(GL_CULL_FACE); qglClearColor(QWidget::palette().color(QWidget::backgroundRole())); + glClearStencil(0); glClearDepth(1.0f); GLfloat ambientLight[] = {0.0f, 0.0f, 0.0f, 1.0f}; @@ -61,26 +74,22 @@ void Render::initializeGL() { float shininess = 64.0f; float specularColor[] = {1.0, 1.0f, 1.0f, 1.0f}; - glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess); // range 0 ~ 128 + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specularColor); - glClearStencil(0); - glClearDepth(1.0f); - glDepthFunc(GL_LEQUAL); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - glEnable(GL_DEPTH_TEST); - glEnable(GL_LIGHTING); - glEnable(GL_CULL_FACE); - glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); glEnable(GL_COLOR_MATERIAL); drawInit(); } +#include "../data/bmesh_test_1.h" + void Render::paintGL() { static bmesh *bm = 0; + _this = this; + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); @@ -89,6 +98,10 @@ void Render::paintGL() { glRotatef(cameraAngleX, 1, 0, 0); glRotatef(cameraAngleY, 0, 1, 0); + glColor3f(0, 0, 0); + drawPrintf(0, 10, "cameraAngleX:%f cameraAngleY:%f", cameraAngleX, + cameraAngleY); + drawGrid(10, 1); glEnable(GL_LIGHTING); @@ -96,26 +109,24 @@ void Render::paintGL() { if (0 == bm) { bmeshNode node; bmeshEdge edge; + int i; bm = bmeshCreate(); - memset(&node, 0, sizeof(node)); - node.position.x = 0; - node.position.y = 0; - node.position.z = 3; - node.radius = 0.5; - bmeshAddNode(bm, &node); + for (i = 0; i < sizeof(bmeshTest1Nodes) / sizeof(bmeshTest1Nodes[0]); ++i) { + memset(&node, 0, sizeof(node)); + node.position.x = bmeshTest1Nodes[i][1]; + node.position.y = bmeshTest1Nodes[i][2]; + node.position.z = bmeshTest1Nodes[i][3]; + node.radius = 0.35; + bmeshAddNode(bm, &node); + } - memset(&node, 0, sizeof(node)); - node.position.x = 1; - node.position.y = 0; - node.position.z = 2; - node.radius = 0.5; - bmeshAddNode(bm, &node); - - memset(&edge, 0, sizeof(edge)); - edge.firstNode = 1; - edge.secondNode = 0; - bmeshAddEdge(bm, &edge); + for (i = 0; i < sizeof(bmeshTest1Edges) / sizeof(bmeshTest1Edges[0]); ++i) { + memset(&edge, 0, sizeof(edge)); + edge.firstNode = bmeshTest1Edges[i][0]; + edge.secondNode = bmeshTest1Edges[i][1]; + bmeshAddEdge(bm, &edge); + } } { @@ -138,7 +149,7 @@ void Render::resizeGL(int w, int h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glFrustum(-2, 2, -1.5, 1.5, 1, 100); + gluPerspective(60.0f, w/(h/2.0f), 1, 100); glMatrixMode(GL_MODELVIEW); glLoadIdentity();