From fc8ef197d6e3088b7716545b06668ee6133e7753 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Wed, 13 Jan 2016 11:33:27 +0300 Subject: [PATCH] OpenGL example update --- examples/opengl/src/openglexample.d | 27 ++++++++++++++++----------- src/dlangui/core/math3d.d | 15 +++++++++++++++ src/dlangui/graphics/gldrawbuf.d | 23 ++++++++++++++--------- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/examples/opengl/src/openglexample.d b/examples/opengl/src/openglexample.d index 97c71726..5e469f48 100644 --- a/examples/opengl/src/openglexample.d +++ b/examples/opengl/src/openglexample.d @@ -257,26 +257,33 @@ static if (ENABLE_OPENGL) { checkgl!glDisable(GL_DEPTH_TEST); //import gl3n.linalg; - //glSupport.setPerspectiveProjection(windowRect, rc, 45.0f, 0.5f, 100.0f); + glSupport.setPerspectiveProjection(windowRect, rc, 45.0f, 0.5f, 100.0f); //mat4 projectionMatrix = glSupport.projectionMatrix; //mat4.perspective(rc.width, rc.height, 90.0f, 0.5f, 100.0f); // ======== Projection Matrix ================== mat4 projectionMatrix; // = glSupport.projectionMatrix; projectionMatrix.setIdentity(); float aspectRatio = cast(float)rc.width / cast(float)rc.height; projectionMatrix.setPerspective(45.0f, aspectRatio, 0.1f, 100.0f); + //projectionMatrix.setOrtho(-2.0f, 2.0f, 2.0f, -2.0f, -2.0f, 2.0f); // ======== View Matrix ================== mat4 viewMatrix; viewMatrix.setIdentity(); - viewMatrix.lookAt(vec3(2, 0, 0), vec3(0, 0, 0), vec3(0, 1, 0));//translation(0.0f, 0.0f, 4.0f).rotatez(angle); + viewMatrix.translate(0, 0, -4); + //viewMatrix.rotatez(30.0f); + //viewMatrix.rotatey(15.0f); + //viewMatrix.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 ================== mat4 modelMatrix; modelMatrix.setIdentity(); - modelMatrix.scale(0.3f); + //modelMatrix.scale(0.3f); + modelMatrix.rotatez(30.0f); + modelMatrix.rotatey(angle); //modelMatrix.translate(3, 0, 0); - mat4 m = (projectionMatrix * viewMatrix) * modelMatrix; + mat4 m = projectionMatrix * viewMatrix * modelMatrix; //mat4 m = modelMatrix * viewMatrix * projectionMatrix; //float[16] matrix; @@ -326,17 +333,14 @@ static if (ENABLE_OPENGL) { class MyProgram : GLProgram { @property override string vertexSource() { return q{ - uniform mat4 matrix; in vec4 vertex; in vec4 colAttr; in vec4 texCoord; - out vec4 col; out vec4 texc; - + uniform mat4 matrix; void main(void) { - //gl_Position = vertex * matrix; gl_Position = matrix * vertex; col = colAttr; texc = texCoord; @@ -352,8 +356,8 @@ static if (ENABLE_OPENGL) { out vec4 outColor; void main(void) { - outColor = col; //texture(tex, texc.st); // * col; - //outColor = col; //texture(tex, texc.st) * col; + //outColor = texture(tex, texc.st) * col; + outColor = col; } }; } @@ -396,7 +400,8 @@ static if (ENABLE_OPENGL) { glEnableVertexAttribArray(colAttrLocation); glEnableVertexAttribArray(texCoordLocation); - checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length/3); + Log.d("Drawing ", vertices.length / 9, " triangles"); + checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length / 3); glDisableVertexAttribArray(vertexLocation); glDisableVertexAttribArray(colAttrLocation); diff --git a/src/dlangui/core/math3d.d b/src/dlangui/core/math3d.d index 0e409240..a93db7ef 100644 --- a/src/dlangui/core/math3d.d +++ b/src/dlangui/core/math3d.d @@ -1004,6 +1004,21 @@ struct mat4 { return this; } + /// inplace rotate around Z axis + ref mat4 rotatez(float angle) { + return rotate(angle, 0, 0, 1); + } + + /// inplace rotate around X axis + ref mat4 rotatex(float angle) { + return rotate(angle, 1, 0, 0); + } + + /// inplace rotate around Y axis + ref mat4 rotatey(float angle) { + return rotate(angle, 0, 1, 0); + } + ref mat4 rotate(float angle, const vec3 axis) { return rotate(angle, axis.x, axis.y, axis.z); } diff --git a/src/dlangui/graphics/gldrawbuf.d b/src/dlangui/graphics/gldrawbuf.d index 36cb5f1a..4db1038f 100644 --- a/src/dlangui/graphics/gldrawbuf.d +++ b/src/dlangui/graphics/gldrawbuf.d @@ -791,14 +791,14 @@ static class GLTexture { /// image coords to UV float[2] uv(int x, int y) { float[2] res; - res[0] = x * cast(float) _dx / _tdx; - res[1] = y * cast(float) _dy / _tdy; + res[0] = cast(float)x / _tdx; + res[1] = cast(float)y / _tdy; return res; } float[2] uv(Point pt) { float[2] res; - res[0] = pt.x * cast(float) _dx / _tdx; - res[1] = pt.y * cast(float) _dy / _tdy; + res[0] = cast(float)pt.x / _tdx; + res[1] = cast(float)pt.y / _tdy; return res; } /// return UV coords for bottom right corner @@ -819,14 +819,19 @@ static class GLTexture { _tdx = nearestPOT(_dx); _tdy = nearestPOT(_dy); _texture = new Tex2D(); - if (!_texture.ID) - return; - uint * pixels = buf.scanLine(0); - if (!glSupport.setTextureImage(_texture, buf.width, buf.height, cast(ubyte*)pixels)) { - destroy(_texture); + if (!_texture.ID) { _texture = null; return; } + uint * pixels = buf.scanLine(0); + buf.invertAlpha(); + if (!glSupport.setTextureImage(_texture, buf.width, buf.height, cast(ubyte*)pixels)) { + destroy(_texture); + _texture = null; + buf.invertAlpha(); + return; + } + buf.invertAlpha(); } } ~this() {