Example #1
0
CGLWidget::CGLWidget(QWidget *parent, const QGLWidget* shareWidget, Qt::WindowFlags f) :
    QGLWidget(parent, shareWidget, f)
{
    menu = new QMenu(this);
    QMenu* rotate(new QMenu(menu));
    rotate->setTitle("Выровнять относительносительно");
    sidesMenu << new QAction("Плоскости XY", rotate)
              << new QAction("Плоскости XZ", rotate)
              << new QAction("Плоскости YZ", rotate);
    connect(rotate, SIGNAL(hovered(QAction*)), SLOT(menuHandling(QAction *)));
    rotate->addActions(sidesMenu);
    menu->addMenu(rotate);

    transformIdentity();
}
void draw(struct hypnomix *hyp)
{
	float angle = hyp->var.smooth[0] * 2.0 * M_PI; // * hyp->pr.var[0];
	float translate = hyp->var.smooth[1] * hyp->pr.var[1];
	float scale = hyp->var.smooth[2] * hyp->pr.var[2];

	float facevtx[] = {
		-1.0, -1.0, 0.0,
		1.0, -1.0, 0.0,
		-1.0, 1.0, 0.0,
		1.0, 1.0, 0.0,
	};
	float facenrm[] = {
		0.0, 0.0, 1.0,
		0.0, 0.0, 1.0,
		0.0, 0.0, 1.0,
		0.0, 0.0, 1.0
	};
	float faceclr[] = {
		0.0, 0.0, 1.0, 1.0,
		0.0, 0.0, 1.0, 1.0,
		0.0, 0.0, 1.0, 1.0,
		0.0, 0.0, 1.0, 1.0 
	}; 
	unsigned int faceidx[] = {
		0, 1, 2,
		2, 1, 3
	};
	skyboxDraw(hyp, 0.0, angle, 0.0);

	transformIdentity(&hyp->tr);
//	transformModelScale(&hyp->tr, scale, scale, scale);
	transformModelRotate(&hyp->tr, 0.0, angle, 0.0); 
//	transformViewRotate(&hyp->tr, 0.0, angle, 0.0);
	transformViewTranslate(&hyp->tr, 0.0, 0.0, -4.0);
	transformMVP(&hyp->tr);



	glUseProgram(hyp->pg.id);
	glEnableVertexAttribArray(hyp->pg.pos);
	glEnableVertexAttribArray(hyp->pg.clr);
	glEnableVertexAttribArray(hyp->pg.nrm);

	glUniformMatrix4fv(hyp->pg.mvp, 1, GL_FALSE, hyp->tr.mvp);
	glUniformMatrix3fv(hyp->pg.mnormal, 1, GL_FALSE, hyp->tr.mnormal);

//	transformMVCoordinates(&hyp->tr, &(hyp->lights.dir[0]), lightdir[0]);
//lightdir[0][0] = 0.0;
//lightdir[0][1] = 0.0;
//lightdir[0][2] = 1.0;
//	glUniform3fv(hyp->pg.lightdir, hyp->lights.nb, lightdir[0]);
//	transformMVCoordinates(&hyp->tr, &(hyp->lights.pos[0]), lightpos[0]);
//	glUniform3fv(hyp->pg.lightpos, hyp->lights.nb, lightpos[0]);

vec4 lightdir; 
vec4 tmp;
tmp[0] = hyp->lights.dir[0][0];
tmp[1] = hyp->lights.dir[0][1];
tmp[2] = hyp->lights.dir[0][2];
tmp[3] = 0.0;
	mat4Vec4Mul(hyp->tr.view, tmp, lightdir);
	vec4Normalize(lightdir, lightdir);

/*fprintf(stderr, "LIGHTDIR=%f,%f,%f,%f\n", lightdir[0], lightdir[1], lightdir[2], lightdir[3]); */
glUniform3fv(hyp->pg.lightdir, hyp->lights.nb, lightdir);
//glUniform3fv(hyp->pg.lightpos, hyp->lights.nb, hyp->lights.pos);
/*
	glVertexAttribPointer(hyp->pg.pos, 3, GL_FLOAT, GL_FALSE, 0, vtx);
	glVertexAttribPointer(hyp->pg.nrm, 3, GL_FLOAT, GL_FALSE, 0, nrm);
	glVertexAttribPointer(hyp->pg.clr, 4, GL_FLOAT, GL_FALSE, 0, clr);
*/
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);

glVertexAttribPointer(hyp->pg.pos, 3, GL_FLOAT, GL_FALSE, 0, facevtx);
glVertexAttribPointer(hyp->pg.nrm, 3, GL_FLOAT, GL_FALSE, 0, facenrm);
glVertexAttribPointer(hyp->pg.clr, 4, GL_FLOAT, GL_FALSE, 0, faceclr);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, faceidx);


//	glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, idx);
//glDisable(GL_CULL_FACE);


/* show normals */

float nrmlines[4*6];
float x, y, z;
int i;

for(i = 0; i < 4; i++) {
	x = facevtx[i*3];
	y = facevtx[i*3+1];
	z = facevtx[i*3+2];

	nrmlines[i*6] = x;
	nrmlines[i*6+1] = y;
	nrmlines[i*6+2] = z;

	nrmlines[i*6+3] = x + facenrm[i*3] * 0.4;
	nrmlines[i*6+4] = y + facenrm[i*3+1] * 0.4;
	nrmlines[i*6+5] = z + facenrm[i*3+2] * 0.4;
}
glVertexAttribPointer(hyp->pg.pos, 3, GL_FLOAT, GL_FALSE, 0, nrmlines);
glDrawArrays(GL_LINES, 0, 8); 
/* end show normals */

	glDisableVertexAttribArray(hyp->pg.nrm);
	glDisableVertexAttribArray(hyp->pg.clr);
	glDisableVertexAttribArray(hyp->pg.pos);
}
/* FIXME: enhancement: draw several objects at the same time with some little variation */
void draw(struct hypnomix *hyp)
{
	float ax, ay, az;
	int id;
	vec3 light; 

	ax = hyp->var.smooth[0] * M_PI * hyp->pr.var[0];
	ay = hyp->var.smooth[1] * M_PI * hyp->pr.var[1];
	az = hyp->var.smooth[2] * M_PI * hyp->pr.var[2];

	transformIdentity(&hyp->tr);
	transformModelTranslate(&hyp->tr, 0.0, -obj.centery, -obj.scalingz);
static float angle = 0.0;
	transformModelRotate(&hyp->tr, 0.0, angle, 0.0);
angle += 0.01;

	transformViewTranslate(&hyp->tr, 0.0, 0.0, -2.0);
	// transformViewRotate(&hyp->tr, 0.0, angle, 0.0);

	transformMVP(&hyp->tr);

//glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);

	glUseProgram(hyp->pg.id);
	glUniformMatrix4fv(hyp->pg.mvp, 1, GL_FALSE, hyp->tr.mvp);
	glUniformMatrix3fv(hyp->pg.mnormal, 1, GL_FALSE, hyp->tr.mnormal);
//	glUniformMatrix4fv(hyp->pg.mmodel, 1, GL_FALSE, mmodel);

	glEnableVertexAttribArray(hyp->pg.pos);
	glEnableVertexAttribArray(hyp->pg.clr);
	glEnableVertexAttribArray(hyp->pg.nrm);

	transformToView(&hyp->tr, hyp->lights.dir[0], light);
	glUniform3fv(hyp->pg.lightdir, hyp->lights.nb, light);

	glVertexAttribPointer(hyp->pg.pos, 3, GL_FLOAT, GL_FALSE, 0, obj.vtx);
	glVertexAttribPointer(hyp->pg.nrm, 3, GL_FLOAT, GL_FALSE, 0, obj.nrm);
	glVertexAttribPointer(hyp->pg.clr, 4, GL_FLOAT, GL_FALSE, 0, obj.clr);

	// glDrawArrays(GL_TRIANGLES, 0, obj.nbvtx);

	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
 	glDrawElements(GL_TRIANGLES, obj.nbidx*3, GL_UNSIGNED_INT, obj.idx);

	id = obj.nbidx * hyp->var.band[0];
	if(id > obj.nbidx) {
		id = obj.nbidx;
	}
	
	glVertexAttribPointer(hyp->pg.clr, 4, GL_FLOAT, GL_FALSE, 0, clr);
// 	glDrawElements(GL_LINES, id*3, GL_UNSIGNED_INT, obj.idx);

//	glVertexAttribPointer(hyp->pg.pos, 3, GL_FLOAT, GL_FALSE, 0, obj.vtx);
//	glVertexAttribPointer(hyp->pg.clr, 4, GL_FLOAT, GL_FALSE, 0, obj.clr);
/*
unsigned int nb = (unsigned int)(hyp->var.smootha*obj.nbvtx);
	glDrawArrays(GL_LINES, 0, nb);
fprintf(stderr, "%d ", nb); */
glDisable(GL_CULL_FACE);


/* show normals */

goto END;

float nrmlines[10000*6];
float clrlines[10000*8];
float x, y, z;
int i;

for(i = 0; i < obj.nbvtx; i++) {
	x = obj.vtx[i*3];
	y = obj.vtx[i*3+1];
	z = obj.vtx[i*3+2];

	nrmlines[i*6] = x;
	nrmlines[i*6+1] = y;
	nrmlines[i*6+2] = z;

	clrlines[i*8] = clrlines[i*8+1] = clrlines[i*8+2] = clrlines[i*8+3] = 1.0;

	nrmlines[i*6+3] = x + obj.nrm[i*3] * 0.4;
	nrmlines[i*6+4] = y + obj.nrm[i*3+1] * 0.4;
	nrmlines[i*6+5] = z + obj.nrm[i*3+2] * 0.4;

	clrlines[i*8+4] = clrlines[i*8+5] = clrlines[i*8+6] = clrlines[i*8+7] = 1.0;
}
glVertexAttribPointer(hyp->pg.pos, 3, GL_FLOAT, GL_FALSE, 0, nrmlines);
glVertexAttribPointer(hyp->pg.clr, 4, GL_FLOAT, GL_FALSE, 0, clrlines);
glDrawArrays(GL_LINES, 0, obj.nbvtx*2); 
/* end show normals */

END:

	glDisableVertexAttribArray(hyp->pg.nrm);
	glDisableVertexAttribArray(hyp->pg.clr);
	glDisableVertexAttribArray(hyp->pg.pos);
//glDisable(GL_VERTEX_PROGRAM_POINT_SIZE);
}