3d scene - better scene for testing - #183

This commit is contained in:
Vadim Lopatin 2016-03-10 16:15:31 +03:00
parent 9661eb12dd
commit b8cadad8fb
4 changed files with 108 additions and 34 deletions

View File

@ -41,11 +41,15 @@ class UiWidget : VerticalLayout {
margins: 10 margins: 10
padding: 10 padding: 10
backgroundImageId: "tx_fabric.tiled" backgroundImageId: "tx_fabric.tiled"
layoutWidth: fill
layoutHeight: fill
VerticalLayout { VerticalLayout {
id: glView id: glView
margins: 10 margins: 10
padding: 10 padding: 10
layoutWidth: fill
layoutHeight: fill
TextWidget { text: "There should be OpenGL animation on background"; textColor: "red"; fontSize: 150%; fontWeight: 800; fontFace: "Arial" } TextWidget { text: "There should be OpenGL animation on background"; textColor: "red"; fontSize: 150%; fontWeight: 800; fontFace: "Arial" }
TextWidget { text: "Do you see it? If no, there is some bug in Mesh rendering code..."; fontSize: 120% } TextWidget { text: "Do you see it? If no, there is some bug in Mesh rendering code..."; fontSize: 120% }
HorizontalLayout { HorizontalLayout {
@ -80,6 +84,7 @@ class UiWidget : VerticalLayout {
CheckBox { id: cb2; text: "checkbox 2" } CheckBox { id: cb2; text: "checkbox 2" }
} }
} }
VSpacer { layoutWeight: 30 }
HorizontalLayout { HorizontalLayout {
TextWidget { text: "Some buttons:" } TextWidget { text: "Some buttons:" }
Button { id: btnOk; text: "Ok"; fontSize: 27px } Button { id: btnOk; text: "Ok"; fontSize: 27px }
@ -99,7 +104,18 @@ class UiWidget : VerticalLayout {
_scene.activeCamera = _cam; _scene.activeCamera = _cam;
_mesh = Mesh.createCubeMesh(vec3(0, 0, -0.9)); _mesh = Mesh.createCubeMesh(vec3(0, 0, 0), 0.3f);
for (int i = 0; i < 10; i++) {
_mesh.addCubeMesh(vec3(0, 0, i * 2 + 1.0f), 0.2f, vec4(i / 12, 1, 1, 1));
_mesh.addCubeMesh(vec3(i * 2 + 1.0f, 0, 0), 0.2f, vec4(1, i / 12, 1, 1));
_mesh.addCubeMesh(vec3(-i * 2 - 1.0f, 0, 0), 0.2f, vec4(1, i / 12, 1, 1));
_mesh.addCubeMesh(vec3(0, i * 2 + 1.0f, 0), 0.2f, vec4(1, 1, i / 12 + 0.1, 1));
_mesh.addCubeMesh(vec3(0, -i * 2 - 1.0f, 0), 0.2f, vec4(1, 1, i / 12 + 0.1, 1));
_mesh.addCubeMesh(vec3(i * 2 + 1.0f, i * 2 + 1.0f, i * 2 + 1.0f), 0.2f, vec4(i / 12, i / 12, i / 12, 1));
_mesh.addCubeMesh(vec3(-i * 2 + 1.0f, i * 2 + 1.0f, i * 2 + 1.0f), 0.2f, vec4(i / 12, i / 12, 1 - i / 12, 1));
_mesh.addCubeMesh(vec3( i * 2 + 1.0f, -i * 2 + 1.0f, i * 2 + 1.0f), 0.2f, vec4(i / 12, 1 - i / 12, i / 12, 1));
_mesh.addCubeMesh(vec3(-i * 2 - 1.0f, -i * 2 - 1.0f, -i * 2 - 1.0f), 0.2f, vec4(1 - i / 12, i / 12, i / 12, 1));
}
} }
/// 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
@ -134,27 +150,37 @@ class UiWidget : VerticalLayout {
Log.e("Invalid texture"); Log.e("Invalid texture");
return; return;
} }
_cam.setPerspective(4.0f, 3.0f, 45.0f, 0.1f, 100.0f); _cam.setPerspective(4.0f, 3.0f, 45.0f, 1.1f, 100.0f);
mat4 projectionViewModelMatrix; mat4 projectionViewModelMatrix;
if (true) { if (true) {
// ======== Projection Matrix ================== // ======== Projection Matrix ==================
mat4 projectionMatrix; mat4 projectionMatrix;
float aspectRatio = cast(float)rc.width / cast(float)rc.height; float aspectRatio = cast(float)rc.width / cast(float)rc.height;
projectionMatrix.setPerspective(45.0f, aspectRatio, 0.1f, 100.0f); projectionMatrix.setPerspective(
45.0f, //angle
aspectRatio, // aspect
1.1f, // near
1000.0f // far
);
// ======== View Matrix ================== // ======== View Matrix ==================
mat4 viewMatrix; mat4 viewMatrix;
//viewMatrix.translate(0, 0, 1 - angle / 100); // + angle //viewMatrix.translate(0, 0, -1 + angle / 100); // + angle
//viewMatrix.rotatex(-15.0f + angle); //viewMatrix.translate(0, 0, -4); // + angle
//viewMatrix.rotatez(-15.0f + angle);
//viewMatrix.rotatez(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(0, 0, -1 + angle / 100), vec3(0, 0, 0), vec3(0, 1, 0));//translation(0.0f, 0.0f, 4.0f).rotatez(angle);
//viewMatrix.lookAt(vec3(0, angle / 1000, -2 - angle / 100), vec3(0, 0, 0), vec3(0, 1, 0));//translation(0.0f, 0.0f, 4.0f).rotatez(angle);
viewMatrix.lookAt(vec3(0, 1, -3), 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(0.2f); //modelMatrix.scale(0.1f);
modelMatrix.rotatez(30.0f + angle * 0.3456778); modelMatrix.rotatez(30.0f + angle * 0.3456778);
//modelMatrix.rotatey(25);
//modelMatrix.rotatex(15);
modelMatrix.rotatey(angle); modelMatrix.rotatey(angle);
modelMatrix.rotatez(angle * 1.98765f); modelMatrix.rotatex(angle * 1.98765f);
// ======= PMV matrix ===================== // ======= PMV matrix =====================
projectionViewModelMatrix = projectionMatrix * viewMatrix * modelMatrix; projectionViewModelMatrix = projectionMatrix * viewMatrix * modelMatrix;
@ -162,11 +188,12 @@ class UiWidget : VerticalLayout {
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); glEnable(GL_BLEND);
checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
checkgl!glEnable(GL_CULL_FACE); checkgl!glEnable(GL_CULL_FACE);
//checkgl!glDisable(GL_CULL_FACE);
checkgl!glEnable(GL_DEPTH_TEST); checkgl!glEnable(GL_DEPTH_TEST);
checkgl!glCullFace(GL_BACK); checkgl!glCullFace(GL_BACK);

View File

@ -924,6 +924,34 @@ struct mat4 {
} }
void setPerspective(float angle, float aspect, float nearPlane, float farPlane) void setPerspective(float angle, float aspect, float nearPlane, float farPlane)
{
// Bail out if the projection volume is zero-sized.
float radians = (angle / 2.0f) * PI / 180.0f;
if (nearPlane == farPlane || aspect == 0.0f || radians < 0.0001f)
return;
float f = 1 / tan(radians);
float d = 1 / (nearPlane - farPlane);
// Construct the projection.
m[0*4 + 0] = f / aspect;
m[1*4 + 0] = 0.0f;
m[2*4 + 0] = 0.0f;
m[3*4 + 0] = 0.0f;
m[0*4 + 1] = 0.0f;
m[1*4 + 1] = f;
m[2*4 + 1] = 0.0f;
m[3*4 + 1] = 0.0f;
m[0*4 + 2] = 0.0f;
m[1*4 + 2] = 0.0f;
m[2*4 + 2] = (nearPlane + farPlane) * d;
m[3*4 + 2] = 2.0f * nearPlane * farPlane * d;
m[0*4 + 3] = 0.0f;
m[1*4 + 3] = 0.0f;
m[2*4 + 3] = -1.0f;
m[3*4 + 3] = 0.0f;
}
void setPerspective2(float angle, float aspect, float nearPlane, float farPlane)
{ {
// Bail out if the projection volume is zero-sized. // Bail out if the projection volume is zero-sized.
if (nearPlane == farPlane || aspect == 0.0f) if (nearPlane == farPlane || aspect == 0.0f)
@ -1043,67 +1071,83 @@ struct mat4 {
/// multiply two matrices /// multiply two matrices
static mat4 mul(const ref mat4 m1, const ref mat4 m2) { static mat4 mul(const ref mat4 m1, const ref mat4 m2) {
mat4 m; mat4 m;
m.m[0*4 + 0] = m1.m[0*4 + 0] * m2.m[0*4 + 0] + m.m[0*4 + 0] =
m1.m[0*4 + 0] * m2.m[0*4 + 0] +
m1.m[1*4 + 0] * m2.m[0*4 + 1] + m1.m[1*4 + 0] * m2.m[0*4 + 1] +
m1.m[2*4 + 0] * m2.m[0*4 + 2] + m1.m[2*4 + 0] * m2.m[0*4 + 2] +
m1.m[3*4 + 0] * m2.m[0*4 + 3]; m1.m[3*4 + 0] * m2.m[0*4 + 3];
m.m[0*4 + 1] = m1.m[0*4 + 1] * m2.m[0*4 + 0] + m.m[0*4 + 1] =
m1.m[0*4 + 1] * m2.m[0*4 + 0] +
m1.m[1*4 + 1] * m2.m[0*4 + 1] + m1.m[1*4 + 1] * m2.m[0*4 + 1] +
m1.m[2*4 + 1] * m2.m[0*4 + 2] + m1.m[2*4 + 1] * m2.m[0*4 + 2] +
m1.m[3*4 + 1] * m2.m[0*4 + 3]; m1.m[3*4 + 1] * m2.m[0*4 + 3];
m.m[0*4 + 2] = m1.m[0*4 + 2] * m2.m[0*4 + 0] + m.m[0*4 + 2] =
m1.m[0*4 + 2] * m2.m[0*4 + 0] +
m1.m[1*4 + 2] * m2.m[0*4 + 1] + m1.m[1*4 + 2] * m2.m[0*4 + 1] +
m1.m[2*4 + 2] * m2.m[0*4 + 2] + m1.m[2*4 + 2] * m2.m[0*4 + 2] +
m1.m[3*4 + 2] * m2.m[0*4 + 3]; m1.m[3*4 + 2] * m2.m[0*4 + 3];
m.m[0*4 + 3] = m1.m[0*4 + 3] * m2.m[0*4 + 0] + m.m[0*4 + 3] =
m1.m[0*4 + 3] * m2.m[0*4 + 0] +
m1.m[1*4 + 3] * m2.m[0*4 + 1] + m1.m[1*4 + 3] * m2.m[0*4 + 1] +
m1.m[2*4 + 3] * m2.m[0*4 + 2] + m1.m[2*4 + 3] * m2.m[0*4 + 2] +
m1.m[3*4 + 3] * m2.m[0*4 + 3]; m1.m[3*4 + 3] * m2.m[0*4 + 3];
m.m[1*4 + 0] = m1.m[0*4 + 0] * m2.m[1*4 + 0] + m.m[1*4 + 0] =
m1.m[0*4 + 0] * m2.m[1*4 + 0] +
m1.m[1*4 + 0] * m2.m[1*4 + 1] + m1.m[1*4 + 0] * m2.m[1*4 + 1] +
m1.m[2*4 + 0] * m2.m[1*4 + 2] + m1.m[2*4 + 0] * m2.m[1*4 + 2] +
m1.m[3*4 + 0] * m2.m[1*4 + 3]; m1.m[3*4 + 0] * m2.m[1*4 + 3];
m.m[1*4 + 1] = m1.m[0*4 + 1] * m2.m[1*4 + 0] + m.m[1*4 + 1] =
m1.m[0*4 + 1] * m2.m[1*4 + 0] +
m1.m[1*4 + 1] * m2.m[1*4 + 1] + m1.m[1*4 + 1] * m2.m[1*4 + 1] +
m1.m[2*4 + 1] * m2.m[1*4 + 2] + m1.m[2*4 + 1] * m2.m[1*4 + 2] +
m1.m[3*4 + 1] * m2.m[1*4 + 3]; m1.m[3*4 + 1] * m2.m[1*4 + 3];
m.m[1*4 + 2] = m1.m[0*4 + 2] * m2.m[1*4 + 0] + m.m[1*4 + 2] =
m1.m[0*4 + 2] * m2.m[1*4 + 0] +
m1.m[1*4 + 2] * m2.m[1*4 + 1] + m1.m[1*4 + 2] * m2.m[1*4 + 1] +
m1.m[2*4 + 2] * m2.m[1*4 + 2] + m1.m[2*4 + 2] * m2.m[1*4 + 2] +
m1.m[3*4 + 2] * m2.m[1*4 + 3]; m1.m[3*4 + 2] * m2.m[1*4 + 3];
m.m[1*4 + 3] = m1.m[0*4 + 3] * m2.m[1*4 + 0] + m.m[1*4 + 3] =
m1.m[0*4 + 3] * m2.m[1*4 + 0] +
m1.m[1*4 + 3] * m2.m[1*4 + 1] + m1.m[1*4 + 3] * m2.m[1*4 + 1] +
m1.m[2*4 + 3] * m2.m[1*4 + 2] + m1.m[2*4 + 3] * m2.m[1*4 + 2] +
m1.m[3*4 + 3] * m2.m[1*4 + 3]; m1.m[3*4 + 3] * m2.m[1*4 + 3];
m.m[2*4 + 0] = m1.m[0*4 + 0] * m2.m[2*4 + 0] + m.m[2*4 + 0] =
m1.m[0*4 + 0] * m2.m[2*4 + 0] +
m1.m[1*4 + 0] * m2.m[2*4 + 1] + m1.m[1*4 + 0] * m2.m[2*4 + 1] +
m1.m[2*4 + 0] * m2.m[2*4 + 2] + m1.m[2*4 + 0] * m2.m[2*4 + 2] +
m1.m[3*4 + 0] * m2.m[2*4 + 3]; m1.m[3*4 + 0] * m2.m[2*4 + 3];
m.m[2*4 + 1] = m1.m[0*4 + 1] * m2.m[2*4 + 0] + m.m[2*4 + 1] =
m1.m[0*4 + 1] * m2.m[2*4 + 0] +
m1.m[1*4 + 1] * m2.m[2*4 + 1] + m1.m[1*4 + 1] * m2.m[2*4 + 1] +
m1.m[2*4 + 1] * m2.m[2*4 + 2] + m1.m[2*4 + 1] * m2.m[2*4 + 2] +
m1.m[3*4 + 1] * m2.m[2*4 + 3]; m1.m[3*4 + 1] * m2.m[2*4 + 3];
m.m[2*4 + 2] = m1.m[0*4 + 2] * m2.m[2*4 + 0] + m.m[2*4 + 2] =
m1.m[0*4 + 2] * m2.m[2*4 + 0] +
m1.m[1*4 + 2] * m2.m[2*4 + 1] + m1.m[1*4 + 2] * m2.m[2*4 + 1] +
m1.m[2*4 + 2] * m2.m[2*4 + 2] + m1.m[2*4 + 2] * m2.m[2*4 + 2] +
m1.m[3*4 + 2] * m2.m[2*4 + 3]; m1.m[3*4 + 2] * m2.m[2*4 + 3];
m.m[2*4 + 3] = m1.m[0*4 + 3] * m2.m[2*4 + 0] + m.m[2*4 + 3] =
m1.m[0*4 + 3] * m2.m[2*4 + 0] +
m1.m[1*4 + 3] * m2.m[2*4 + 1] + m1.m[1*4 + 3] * m2.m[2*4 + 1] +
m1.m[2*4 + 3] * m2.m[2*4 + 2] + m1.m[2*4 + 3] * m2.m[2*4 + 2] +
m1.m[3*4 + 3] * m2.m[2*4 + 3]; m1.m[3*4 + 3] * m2.m[2*4 + 3];
m.m[3*4 + 0] = m1.m[0*4 + 0] * m2.m[3*4 + 0] + m.m[3*4 + 0] =
m1.m[0*4 + 0] * m2.m[3*4 + 0] +
m1.m[1*4 + 0] * m2.m[3*4 + 1] + m1.m[1*4 + 0] * m2.m[3*4 + 1] +
m1.m[2*4 + 0] * m2.m[3*4 + 2] + m1.m[2*4 + 0] * m2.m[3*4 + 2] +
m1.m[3*4 + 0] * m2.m[3*4 + 3]; m1.m[3*4 + 0] * m2.m[3*4 + 3];
m.m[3*4 + 1] = m1.m[0*4 + 1] * m2.m[3*4 + 0] + m.m[3*4 + 1] =
m1.m[0*4 + 1] * m2.m[3*4 + 0] +
m1.m[1*4 + 1] * m2.m[3*4 + 1] + m1.m[1*4 + 1] * m2.m[3*4 + 1] +
m1.m[2*4 + 1] * m2.m[3*4 + 2] + m1.m[2*4 + 1] * m2.m[3*4 + 2] +
m1.m[3*4 + 1] * m2.m[3*4 + 3]; m1.m[3*4 + 1] * m2.m[3*4 + 3];
m.m[3*4 + 2] = m1.m[0*4 + 2] * m2.m[3*4 + 0] + m.m[3*4 + 2] =
m1.m[0*4 + 2] * m2.m[3*4 + 0] +
m1.m[1*4 + 2] * m2.m[3*4 + 1] + m1.m[1*4 + 2] * m2.m[3*4 + 1] +
m1.m[2*4 + 2] * m2.m[3*4 + 2] + m1.m[2*4 + 2] * m2.m[3*4 + 2] +
m1.m[3*4 + 2] * m2.m[3*4 + 3]; m1.m[3*4 + 2] * m2.m[3*4 + 3];
m.m[3*4 + 3] = m1.m[0*4 + 3] * m2.m[3*4 + 0] + m.m[3*4 + 3] =
m1.m[0*4 + 3] * m2.m[3*4 + 0] +
m1.m[1*4 + 3] * m2.m[3*4 + 1] + m1.m[1*4 + 3] * m2.m[3*4 + 1] +
m1.m[2*4 + 3] * m2.m[3*4 + 2] + m1.m[2*4 + 3] * m2.m[3*4 + 2] +
m1.m[3*4 + 3] * m2.m[3*4 + 3]; m1.m[3*4 + 3] * m2.m[3*4 + 3];

View File

@ -1165,7 +1165,6 @@ class GLVertexBuffer : VertexBuffer {
override void draw(GraphicsEffect effect) { override void draw(GraphicsEffect effect) {
//bind(); //bind();
enableAttributes(effect); enableAttributes(effect);
Log.d("draw ", _indexFragments);
foreach (fragment; _indexFragments) { foreach (fragment; _indexFragments) {
checkgl!glDrawRangeElements(primitiveTypeToGL(fragment.type), checkgl!glDrawRangeElements(primitiveTypeToGL(fragment.type),
0, _vertexCount - 1, // The first to last vertex 0, _vertexCount - 1, // The first to last vertex

View File

@ -304,8 +304,7 @@ class Mesh {
cast(ushort)(startVertex + 0)]); cast(ushort)(startVertex + 0)]);
} }
static Mesh createCubeMesh(vec3 pos, float d=1, vec4 color = vec4(1,1,1,1)) { void addCubeMesh(vec3 pos, float d=1, vec4 color = vec4(1,1,1,1)) {
Mesh mesh = new Mesh(VertexFormat(VertexElementType.POSITION, VertexElementType.COLOR, VertexElementType.TEXCOORD0));
auto p000 = vec3(pos.x-d, pos.y-d, pos.z-d); auto p000 = vec3(pos.x-d, pos.y-d, pos.z-d);
auto p100 = vec3(pos.x+d, pos.y-d, pos.z-d); auto p100 = vec3(pos.x+d, pos.y-d, pos.z-d);
auto p010 = vec3(pos.x-d, pos.y+d, pos.z-d); auto p010 = vec3(pos.x-d, pos.y+d, pos.z-d);
@ -314,12 +313,17 @@ class Mesh {
auto p101 = vec3(pos.x+d, pos.y-d, pos.z+d); auto p101 = vec3(pos.x+d, pos.y-d, pos.z+d);
auto p011 = vec3(pos.x-d, pos.y+d, pos.z+d); auto p011 = vec3(pos.x-d, pos.y+d, pos.z+d);
auto p111 = vec3(pos.x+d, pos.y+d, pos.z+d); auto p111 = vec3(pos.x+d, pos.y+d, pos.z+d);
mesh.addQuad(p000, p010, p110, p100, color); // front face addQuad(p000, p010, p110, p100, color); // front face
mesh.addQuad(p101, p111, p011, p001, color); // back face addQuad(p101, p111, p011, p001, color); // back face
mesh.addQuad(p100, p110, p111, p101, color); // right face addQuad(p100, p110, p111, p101, color); // right face
mesh.addQuad(p001, p011, p010, p000, color); // left face addQuad(p001, p011, p010, p000, color); // left face
mesh.addQuad(p010, p011, p111, p110, color); // top face addQuad(p010, p011, p111, p110, color); // top face
mesh.addQuad(p001, p000, p100, p101, color); // bottom face addQuad(p001, p000, p100, p101, color); // bottom face
}
static Mesh createCubeMesh(vec3 pos, float d=1, vec4 color = vec4(1,1,1,1)) {
Mesh mesh = new Mesh(VertexFormat(VertexElementType.POSITION, VertexElementType.COLOR, VertexElementType.TEXCOORD0));
mesh.addCubeMesh(pos, d, color);
return mesh; return mesh;
} }
} }