goat3d

diff goatview/src/goatview.cc @ 78:53ea5b25426e

progress
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 08 May 2014 16:25:04 +0300
parents 9785847d52d4
children a42f6cd4e2fa
line diff
     1.1 --- a/goatview/src/goatview.cc	Thu May 08 13:55:13 2014 +0300
     1.2 +++ b/goatview/src/goatview.cc	Thu May 08 16:25:04 2014 +0300
     1.3 @@ -1,9 +1,12 @@
     1.4 +#include <stdio.h>
     1.5  #include <QtOpenGL/QtOpenGL>
     1.6  #include <GL/glu.h>
     1.7  #include <vmath/vmath.h>
     1.8  #include "goatview.h"
     1.9  #include "goat3d.h"
    1.10  
    1.11 +static void draw_node(goat3d_node *node);
    1.12 +
    1.13  goat3d *scene;
    1.14  QSettings *settings;
    1.15  
    1.16 @@ -151,6 +154,9 @@
    1.17  void GoatViewport::initializeGL()
    1.18  {
    1.19  	glClearColor(0.1, 0.1, 0.1, 1);
    1.20 +
    1.21 +	glEnable(GL_DEPTH_TEST);
    1.22 +	glEnable(GL_CULL_FACE);
    1.23  }
    1.24  
    1.25  void GoatViewport::resizeGL(int xsz, int ysz)
    1.26 @@ -161,8 +167,6 @@
    1.27  	gluPerspective(60.0, (float)xsz / (float)ysz, 0.5, 5000.0);
    1.28  }
    1.29  
    1.30 -static void draw_node(goat3d_node *node);
    1.31 -
    1.32  void GoatViewport::paintGL()
    1.33  {
    1.34  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.35 @@ -177,7 +181,9 @@
    1.36  		int node_count = goat3d_get_node_count(scene);
    1.37  		for(int i=0; i<node_count; i++) {
    1.38  			goat3d_node *node = goat3d_get_node(scene, i);
    1.39 -			draw_node(node);
    1.40 +			if(!goat3d_get_node_parent(node)) {
    1.41 +				draw_node(node);	// only draw root nodes, the rest will be drawn recursively
    1.42 +			}
    1.43  		}
    1.44  	}
    1.45  }
    1.46 @@ -186,7 +192,14 @@
    1.47  {
    1.48  	float xform[16];
    1.49  	goat3d_get_node_matrix(node, xform, anim_time);
    1.50 -	glMultMatrixf(xform);
    1.51 +	for(int i=0; i<4; i++) {
    1.52 +		float *row = xform + i * 4;
    1.53 +		printf("[%3.3f %3.3f %3.3f %3.3f]\n", row[0], row[1], row[2], row[3]);
    1.54 +	}
    1.55 +	putchar('\n');
    1.56 +
    1.57 +	glPushMatrix();
    1.58 +	glMultTransposeMatrixf(xform);
    1.59  
    1.60  	if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
    1.61  		goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
    1.62 @@ -223,4 +236,36 @@
    1.63  	for(int i=0; i<num_child; i++) {
    1.64  		draw_node(goat3d_get_node_child(node, i));
    1.65  	}
    1.66 -}
    1.67 \ No newline at end of file
    1.68 +
    1.69 +	glPopMatrix();
    1.70 +}
    1.71 +
    1.72 +
    1.73 +static float prev_x, prev_y;
    1.74 +void GoatViewport::mousePressEvent(QMouseEvent *ev)
    1.75 +{
    1.76 +	prev_x = ev->x();
    1.77 +	prev_y = ev->y();
    1.78 +}
    1.79 +
    1.80 +void GoatViewport::mouseMoveEvent(QMouseEvent *ev)
    1.81 +{
    1.82 +	int dx = ev->x() - prev_x;
    1.83 +	int dy = ev->y() - prev_y;
    1.84 +
    1.85 +	if(!dx && !dy) return;
    1.86 +
    1.87 +	if(ev->buttons() & Qt::LeftButton) {
    1.88 +		cam_theta += dx  * 0.5;
    1.89 +		cam_phi += dy * 0.5;
    1.90 +
    1.91 +		if(cam_phi < -90) cam_phi = -90;
    1.92 +		if(cam_phi > 90) cam_phi = 90;
    1.93 +	}
    1.94 +	if(ev->buttons() & Qt::RightButton) {
    1.95 +		cam_dist += dy * 0.1;
    1.96 +
    1.97 +		if(cam_dist < 0.0) cam_dist = 0.0;
    1.98 +	}
    1.99 +	updateGL();
   1.100 +}