This commit is contained in:
Vadim Lopatin 2016-03-10 13:38:32 +03:00
parent 5d733dd8c3
commit 5107982579
2 changed files with 62 additions and 12 deletions

View File

@ -103,18 +103,54 @@ class UiWidget : VerticalLayout {
_mesh = new Mesh(vfmt); _mesh = new Mesh(vfmt);
// square // square
float x0 = 0.3; float x0 = 0;
float y0 = 0.2; float y0 = 0;
float z0 = -0.9; float z0 = -0.9;
_mesh.addVertex([x0-1,y0-1,z0+0, 1,0,1,1, 0,0]); addCube(x0, y0, z0, 1);
_mesh.addVertex([x0-1,y0+1,z0+0, 1,1,1,1, 1,0]); //_mesh.addVertex([x0-1,y0-1,z0+0, 1,0,1,1, 0,0]);
_mesh.addVertex([x0+1,y0+1,z0+0, 0,1,1,1, 1,1]); //_mesh.addVertex([x0-1,y0+1,z0+0, 1,1,1,1, 1,0]);
_mesh.addVertex([x0+1,y0-1,z0+0, 1,1,0,1, 0,1]); //_mesh.addVertex([x0+1,y0+1,z0+0, 0,1,1,1, 1,1]);
_mesh.addPart(PrimitiveType.triangles, [0, 1, 2, 2, 3, 0]); //_mesh.addVertex([x0+1,y0-1,z0+0, 1,1,0,1, 0,1]);
//_mesh.addPart(PrimitiveType.triangles, [0, 1, 2, 2, 3, 0]);
} }
void addCube(float x0, float y0, float z0, float d) {
auto p000 = [x0-d, y0-d, z0-d];
auto p100 = [x0+d, y0-d, z0-d];
auto p010 = [x0-d, y0+d, z0-d];
auto p110 = [x0+d, y0+d, z0-d];
auto p001 = [x0-d, y0-d, z0+d];
auto p101 = [x0+d, y0-d, z0+d];
auto p011 = [x0-d, y0+d, z0+d];
auto p111 = [x0+d, y0+d, z0+d];
addQuad(p000 ~ p010 ~ p110 ~ p100); // front face
addQuad(p101 ~ p111 ~ p011 ~ p001); // back face
addQuad(p100 ~ p110 ~ p111 ~ p101); // right face
addQuad(p001 ~ p011 ~ p010 ~ p000); // left face
addQuad(p010 ~ p011 ~ p111 ~ p110); // top face
addQuad(p001 ~ p000 ~ p100 ~ p101); // bottom face
}
void addQuad(float[] vertexes) {
static float[] txcoords = [0, 0, 1, 0, 1, 1, 0, 1];
ushort startVertex = cast(ushort)_mesh.vertexCount;
for (int i = 0; i < 4; i++) {
_mesh.addVertex([
vertexes[i*3 + 0], vertexes[i*3 + 1], vertexes[i*3 + 2],
1,1,1,1,
txcoords[i*2 + 0], txcoords[i*2 + 1]]);
}
_mesh.addPart(PrimitiveType.triangles, [
cast(ushort)(startVertex + 0),
cast(ushort)(startVertex + 1),
cast(ushort)(startVertex + 2),
cast(ushort)(startVertex + 2),
cast(ushort)(startVertex + 3),
cast(ushort)(startVertex + 0)]);
}
/// returns true is widget is being animated - need to call animate() and redraw /// returns true is widget is being animated - need to call animate() and redraw
@property override bool animating() { return true; } @property override bool animating() { return true; }
/// animates window; interval is time left from previous draw, in hnsecs (1/10000000 of second) /// animates window; interval is time left from previous draw, in hnsecs (1/10000000 of second)
@ -157,13 +193,14 @@ class UiWidget : VerticalLayout {
// ======== View Matrix ================== // ======== View Matrix ==================
mat4 viewMatrix; mat4 viewMatrix;
viewMatrix.translate(0, 0, -6); //viewMatrix.translate(0, 0, 1 - angle / 100); // + angle
viewMatrix.rotatex(-15.0f); //viewMatrix.rotatex(-15.0f + angle);
//viewMatrix.rotatez(angle);
//viewMatrix.lookAt(vec3(-10, 0, 0), vec3(0, 0, 0), vec3(0, 1, 0));//translation(0.0f, 0.0f, 4.0f).rotatez(angle); //viewMatrix.lookAt(vec3(-10, 0, 0), vec3(0, 0, 0), vec3(0, 1, 0));//translation(0.0f, 0.0f, 4.0f).rotatez(angle);
// ======== Model Matrix ================== // ======== Model Matrix ==================
mat4 modelMatrix; mat4 modelMatrix;
modelMatrix.scale(1.5f); modelMatrix.scale(0.2f);
modelMatrix.rotatez(30.0f + angle * 0.3456778); modelMatrix.rotatez(30.0f + angle * 0.3456778);
modelMatrix.rotatey(angle); modelMatrix.rotatey(angle);
modelMatrix.rotatez(angle * 1.98765f); modelMatrix.rotatez(angle * 1.98765f);
@ -173,9 +210,15 @@ class UiWidget : VerticalLayout {
} else { } else {
projectionViewModelMatrix = _scene.viewProjectionMatrix; projectionViewModelMatrix = _scene.viewProjectionMatrix;
} }
projectionViewModelMatrix.setIdentity(); //projectionViewModelMatrix.setIdentity();
Log.d("matrix uniform: ", projectionViewModelMatrix.m); Log.d("matrix uniform: ", projectionViewModelMatrix.m);
glEnable(GL_BLEND);
checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
checkgl!glEnable(GL_CULL_FACE);
checkgl!glEnable(GL_DEPTH_TEST);
checkgl!glCullFace(GL_BACK);
_program.bind(); _program.bind();
_program.setUniform("matrix", projectionViewModelMatrix); _program.setUniform("matrix", projectionViewModelMatrix);
_tx.texture.setup(); _tx.texture.setup();
@ -185,6 +228,8 @@ class UiWidget : VerticalLayout {
_tx.texture.unbind(); _tx.texture.unbind();
_program.unbind(); _program.unbind();
checkgl!glDisable(GL_DEPTH_TEST);
checkgl!glDisable(GL_CULL_FACE);
} }
~this() { ~this() {

View File

@ -243,8 +243,13 @@ class Mesh {
return meshPart; return meshPart;
} }
/// add new mesh part or append indexes to existing part (if type matches)
MeshPart addPart(PrimitiveType type, ushort[] indexes) { MeshPart addPart(PrimitiveType type, ushort[] indexes) {
MeshPart lastPart = _parts.length > 0 ? _parts[$ - 1] : null;
if (!lastPart || lastPart.type != type)
return addPart(new MeshPart(type, indexes)); return addPart(new MeshPart(type, indexes));
lastPart.add(indexes);
return lastPart;
} }
/// adds single vertex /// adds single vertex