Add drawText.

Add bmesh test data.
master
Jeremy Hu 2016-12-20 19:23:13 +09:30
parent edeec37d01
commit b6af60f08c
7 changed files with 72 additions and 35 deletions

View File

@ -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.
<img src="screenshot/dust3d_node_edge_with_glu.png">
*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`
<img src="screenshot/dust3d_bmesh_nodes.png">
- [ ] Export Wavefront .obj
- [ ] Render B-Mesh result
- [ ] Design UI for monster parts configuration

BIN
data/bmesh_test_1.blend Normal file

Binary file not shown.

12
data/bmesh_test_1.h Normal file
View File

@ -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}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 KiB

View File

@ -3,11 +3,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef __APPLE__
#include <OpenGL/glu.h>
#else
#include <GL/glu.h>
#endif
#include <stdarg.h>
#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);
}

View File

@ -1,6 +1,11 @@
#ifndef DRAW_SPHERE_H
#define DRAW_SPHERE_H
#include "vector3d.h"
#ifdef __APPLE__
#include <OpenGL/glu.h>
#else
#include <GL/glu.h>
#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
}

View File

@ -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();