goat3d

diff goatview/src/goatview.cc @ 75:76dea247f75c

in progress
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 08 May 2014 00:50:16 +0300
parents ab66cdabf6f2
children 9785847d52d4
line diff
     1.1 --- a/goatview/src/goatview.cc	Tue May 06 13:26:52 2014 +0300
     1.2 +++ b/goatview/src/goatview.cc	Thu May 08 00:50:16 2014 +0300
     1.3 @@ -1,7 +1,13 @@
     1.4 +#include <GL/glu.h>
     1.5  #include "goatview.h"
     1.6  #include "goat3d.h"
     1.7  
     1.8  goat3d *scene;
     1.9 +QSettings *settings;
    1.10 +
    1.11 +static long anim_time;
    1.12 +static float cam_theta, cam_phi, cam_dist = 8;
    1.13 +
    1.14  
    1.15  GoatView::GoatView()
    1.16  {
    1.17 @@ -12,12 +18,20 @@
    1.18  	statusBar();
    1.19  
    1.20  	setWindowTitle("GoatView");
    1.21 +	resize(settings->value("main/size", QSize(1024, 768)).toSize());
    1.22 +	move(settings->value("main/pos", QPoint(100, 100)).toPoint());
    1.23  }
    1.24  
    1.25  GoatView::~GoatView()
    1.26  {
    1.27  }
    1.28  
    1.29 +void GoatView::closeEvent(QCloseEvent *ev)
    1.30 +{
    1.31 +	settings->setValue("main/size", size());
    1.32 +	settings->setValue("main/pos", pos());
    1.33 +}
    1.34 +
    1.35  bool GoatView::make_menu()
    1.36  {
    1.37  	QMenu *menu_file = menuBar()->addMenu("&File");
    1.38 @@ -120,15 +134,79 @@
    1.39  
    1.40  void GoatViewport::initializeGL()
    1.41  {
    1.42 +	glClearColor(0.1, 0.1, 0.1, 1);
    1.43  }
    1.44  
    1.45  void GoatViewport::resizeGL(int xsz, int ysz)
    1.46  {
    1.47  	glViewport(0, 0, xsz, ysz);
    1.48 +	glMatrixMode(GL_PROJECTION);
    1.49 +	glLoadIdentity();
    1.50 +	gluPerspective(60.0, (float)xsz / (float)ysz, 0.5, 5000.0);
    1.51  }
    1.52  
    1.53 +static void draw_node(goat3d_node *node);
    1.54 +
    1.55  void GoatViewport::paintGL()
    1.56  {
    1.57 -	glClearColor(1, 0, 0, 1);
    1.58 -	glClear(GL_COLOR_BUFFER_BIT);
    1.59 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.60 +
    1.61 +	glMatrixMode(GL_MODELVIEW);
    1.62 +	glLoadIdentity();
    1.63 +	glTranslatef(0, 0, -cam_dist);
    1.64 +	glRotatef(cam_phi, 1, 0, 0);
    1.65 +	glRotatef(cam_theta, 0, 1, 0);
    1.66 +
    1.67 +	if(scene) {
    1.68 +		int node_count = goat3d_get_node_count(scene);
    1.69 +		for(int i=0; i<node_count; i++) {
    1.70 +			goat3d_node *node = goat3d_get_node(scene, i);
    1.71 +			draw_node(node);
    1.72 +		}
    1.73 +	}
    1.74  }
    1.75 +
    1.76 +static void draw_node(goat3d_node *node)
    1.77 +{
    1.78 +	float xform[16];
    1.79 +	goat3d_get_node_matrix(node, xform, anim_time);
    1.80 +	glMultMatrixf(xform);
    1.81 +
    1.82 +	if(goat3d_get_node_type(node) == GOAT3D_NODE_MESH) {
    1.83 +		goat3d_mesh *mesh = (goat3d_mesh*)goat3d_get_node_object(node);
    1.84 +
    1.85 +		int num_faces = goat3d_get_mesh_face_count(mesh);
    1.86 +		int num_verts = goat3d_get_mesh_attrib_count(mesh, GOAT3D_MESH_ATTR_VERTEX);
    1.87 +
    1.88 +		glEnableClientState(GL_VERTEX_ARRAY);
    1.89 +		glVertexPointer(3, GL_FLOAT, 0, goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_VERTEX));
    1.90 +
    1.91 +		float *data;
    1.92 +		if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_NORMAL))) {
    1.93 +			glEnableClientState(GL_NORMAL_ARRAY);
    1.94 +			glNormalPointer(GL_FLOAT, 0, data);
    1.95 +		}
    1.96 +		if((data = (float*)goat3d_get_mesh_attribs(mesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
    1.97 +			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    1.98 +			glTexCoordPointer(2, GL_FLOAT, 0, data);
    1.99 +		}
   1.100 +
   1.101 +		int *indices;
   1.102 +		if((indices = goat3d_get_mesh_faces(mesh))) {
   1.103 +			glDrawElements(GL_TRIANGLES, num_faces * 3, GL_UNSIGNED_INT, indices);
   1.104 +		} else {
   1.105 +			glDrawArrays(GL_TRIANGLES, 0, num_verts * 3);
   1.106 +		}
   1.107 +
   1.108 +		glDisableClientState(GL_VERTEX_ARRAY);
   1.109 +		glDisableClientState(GL_NORMAL_ARRAY);
   1.110 +		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   1.111 +	}
   1.112 +
   1.113 +	int num_child = goat3d_get_node_child_count(node);
   1.114 +	for(int i=0; i<num_child; i++) {
   1.115 +		draw_node(goat3d_get_node_child(node, i));
   1.116 +	}
   1.117 +}
   1.118 +
   1.119 +