mirror of https://github.com/buggins/dlangui.git
custom OpenGL drawing support fixed
This commit is contained in:
parent
d3b2c9bedf
commit
73035e925d
|
@ -27,7 +27,7 @@ extern (C) int UIAppMain(string[] args) {
|
|||
return Platform.instance.enterMessageLoop();
|
||||
}
|
||||
|
||||
static if (ENABLE_OPENGL) {
|
||||
static if (ENABLE_OPENGL):
|
||||
|
||||
import derelict.opengl3.gl3;
|
||||
import derelict.opengl3.gl;
|
||||
|
@ -54,9 +54,8 @@ static if (ENABLE_OPENGL) {
|
|||
padding: 20
|
||||
layoutWidth: fill; layoutHeight: fill
|
||||
|
||||
//backgroundColor: "#C0E0E070" // semitransparent yellow background
|
||||
// red bold text with size = 150% of base style size and font face Arial
|
||||
TextWidget { text: "Some controls to draw on top of OpenGL scene"; textColor: "red"; fontSize: 150%; fontWeight: 800; fontFace: "Arial" }
|
||||
|
||||
// arrange controls as form - table with two columns
|
||||
TableLayout {
|
||||
colCount: 2
|
||||
|
@ -78,6 +77,11 @@ static if (ENABLE_OPENGL) {
|
|||
CheckBox { id: cb2; text: "checkbox 2" }
|
||||
}
|
||||
}
|
||||
TextWidget { text: "Choose OpenGL example:" }
|
||||
VerticalLayout {
|
||||
RadioButton { id: rbExample1; text: "Shaders based example - Cube"; checked: true }
|
||||
RadioButton { id: rbExample2; text: "Legacy OpenGL API example - glxGears" }
|
||||
}
|
||||
VSpacer { layoutWeight: 10 }
|
||||
HorizontalLayout {
|
||||
Button { id: btnOk; text: "Ok" }
|
||||
|
@ -88,10 +92,35 @@ static if (ENABLE_OPENGL) {
|
|||
});
|
||||
// assign OpenGL drawable to child widget background
|
||||
w.childById("glView").backgroundDrawable = DrawableRef(new OpenGLDrawable(&doDraw));
|
||||
|
||||
w.childById("rbExample1").click = delegate(Widget w) {
|
||||
_exampleIndex = 0; // new API
|
||||
return true;
|
||||
};
|
||||
w.childById("rbExample2").click = delegate(Widget w) {
|
||||
_exampleIndex = 1; // old API
|
||||
return true;
|
||||
};
|
||||
|
||||
addChild(w);
|
||||
}
|
||||
|
||||
bool _oldApi;
|
||||
int _exampleIndex = 0;
|
||||
|
||||
/// returns true is widget is being animated - need to call animate() and redraw
|
||||
@property override bool animating() { return true; }
|
||||
/// animates window; interval is time left from previous draw, in hnsecs (1/10000000 of second)
|
||||
override void animate(long interval) {
|
||||
if (_exampleIndex == 1) {
|
||||
// animate legacy API example
|
||||
// rotate gears
|
||||
angle += interval * 0.000002f;
|
||||
} else {
|
||||
// TODO: animate new API example
|
||||
angle += interval * 0.000002f;
|
||||
}
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/// this is OpenGLDrawableDelegate implementation
|
||||
private void doDraw(Rect windowRect, Rect rc) {
|
||||
|
@ -99,15 +128,15 @@ static if (ENABLE_OPENGL) {
|
|||
Log.v("GlGears: OpenGL is disabled");
|
||||
return;
|
||||
}
|
||||
_oldApi = glSupport.legacyMode; // !!glLightfv;
|
||||
if (_oldApi) {
|
||||
drawUsingOldAPI(windowRect, rc);
|
||||
} else {
|
||||
bool canUseOldApi = !!glLightfv;
|
||||
bool canUseNewApi = !glSupport.legacyMode;
|
||||
if (_exampleIndex == 0 || !canUseOldApi)
|
||||
drawUsingNewAPI(windowRect, rc);
|
||||
}
|
||||
else if (_exampleIndex == 1 || !canUseNewApi)
|
||||
drawUsingOldAPI(windowRect, rc);
|
||||
}
|
||||
|
||||
/// Legacy API example (glBegin/glEnd)
|
||||
/// Legacy API example (glBegin/glEnd) - glxGears
|
||||
void drawUsingOldAPI(Rect windowRect, Rect rc) {
|
||||
static bool _initCalled;
|
||||
if (!_initCalled) {
|
||||
|
@ -127,108 +156,6 @@ static if (ENABLE_OPENGL) {
|
|||
glDisable(GL_DEPTH_TEST);
|
||||
}
|
||||
|
||||
MyProgram _program;
|
||||
|
||||
GLTexture _tx;
|
||||
float[] vertices;
|
||||
float[] texcoords;
|
||||
float[4*6*6] colors;
|
||||
void createMesh() {
|
||||
if (!_tx)
|
||||
_tx = new GLTexture("crate");
|
||||
// define Cube mesh
|
||||
vertices = [
|
||||
-1.0f,-1.0f,-1.0f, // triangle 1 : begin
|
||||
-1.0f,-1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 1.0f, // triangle 1 : end
|
||||
1.0f, 1.0f,-1.0f, // triangle 2 : begin
|
||||
-1.0f,-1.0f,-1.0f,
|
||||
-1.0f, 1.0f,-1.0f, // triangle 2 : end
|
||||
1.0f,-1.0f, 1.0f,
|
||||
-1.0f,-1.0f,-1.0f,
|
||||
1.0f,-1.0f,-1.0f,
|
||||
1.0f, 1.0f,-1.0f,
|
||||
1.0f,-1.0f,-1.0f,
|
||||
-1.0f,-1.0f,-1.0f,
|
||||
-1.0f,-1.0f,-1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f,-1.0f,
|
||||
1.0f,-1.0f, 1.0f,
|
||||
-1.0f,-1.0f, 1.0f,
|
||||
-1.0f,-1.0f,-1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
-1.0f,-1.0f, 1.0f,
|
||||
1.0f,-1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f,-1.0f,-1.0f,
|
||||
1.0f, 1.0f,-1.0f,
|
||||
1.0f,-1.0f,-1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f,-1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f,-1.0f,
|
||||
-1.0f, 1.0f,-1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f,-1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 1.0f,
|
||||
1.0f,-1.0f, 1.0f
|
||||
];
|
||||
float[2] uv = _tx.uv;
|
||||
float tx0 = 0.0f;
|
||||
float tx1 = uv[0];
|
||||
float ty0 = 0.0f;
|
||||
float ty1 = uv[1];
|
||||
texcoords = [
|
||||
//
|
||||
tx0, ty1,
|
||||
tx0, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty1,
|
||||
tx0, ty1,
|
||||
//
|
||||
tx0, ty1,
|
||||
tx0, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty1,
|
||||
tx0, ty1,
|
||||
//
|
||||
tx0, ty1,
|
||||
tx0, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty1,
|
||||
tx0, ty1,
|
||||
//
|
||||
tx0, ty1,
|
||||
tx0, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty1,
|
||||
tx0, ty1,
|
||||
//
|
||||
tx0, ty1,
|
||||
tx0, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty1,
|
||||
tx0, ty1,
|
||||
//
|
||||
tx0, ty1,
|
||||
tx0, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty0,
|
||||
tx1, ty1,
|
||||
tx0, ty1,
|
||||
];
|
||||
// init with white color
|
||||
foreach(ref cl; colors)
|
||||
cl = 1.0f;
|
||||
}
|
||||
|
||||
~this() {
|
||||
if (_program)
|
||||
destroy(_program);
|
||||
|
@ -236,11 +163,13 @@ static if (ENABLE_OPENGL) {
|
|||
destroy(_tx);
|
||||
}
|
||||
|
||||
MyGLProgram _program;
|
||||
GLTexture _tx;
|
||||
|
||||
/// New API example (OpenGL3+, shaders)
|
||||
void drawUsingNewAPI(Rect windowRect, Rect rc) {
|
||||
// TODO: put some sample code here
|
||||
if (!_program) {
|
||||
_program = new MyProgram();
|
||||
_program = new MyGLProgram();
|
||||
createMesh();
|
||||
}
|
||||
if (!_program.check())
|
||||
|
@ -251,84 +180,84 @@ static if (ENABLE_OPENGL) {
|
|||
return;
|
||||
}
|
||||
|
||||
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
checkgl!glDisable(GL_CULL_FACE);
|
||||
checkgl!glEnable(GL_CULL_FACE);
|
||||
checkgl!glEnable(GL_DEPTH_TEST);
|
||||
checkgl!glCullFace(GL_BACK);
|
||||
|
||||
// ======== Projection Matrix ==================
|
||||
mat4 projectionMatrix; // = glSupport.projectionMatrix;
|
||||
projectionMatrix.setIdentity();
|
||||
mat4 projectionMatrix;
|
||||
float aspectRatio = cast(float)rc.width / cast(float)rc.height;
|
||||
projectionMatrix.setPerspective(45.0f, aspectRatio, 0.1f, 100.0f);
|
||||
|
||||
// ======== View Matrix ==================
|
||||
mat4 viewMatrix;
|
||||
viewMatrix.setIdentity();
|
||||
viewMatrix.translate(0, 0, -6);
|
||||
//viewMatrix.rotatez(30.0f);
|
||||
//viewMatrix.rotatey(15.0f);
|
||||
//viewMatrix.translation(0.0f, 0.0f, 4.0f).rotatez(angle);
|
||||
viewMatrix.rotatex(-15.0f);
|
||||
//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(1.5f);
|
||||
modelMatrix.rotatez(30.0f + angle * 0.3456778);
|
||||
modelMatrix.rotatey(angle);
|
||||
modelMatrix.rotatez(angle * 1.98765f);
|
||||
|
||||
//modelMatrix.translate(3, 0, 0);
|
||||
mat4 m = projectionMatrix * viewMatrix * modelMatrix;
|
||||
//mat4 m = modelMatrix * viewMatrix * projectionMatrix;
|
||||
// ======= PMV matrix =====================
|
||||
mat4 projectionViewModelMatrix = projectionMatrix * viewMatrix * modelMatrix;
|
||||
|
||||
//float[16] matrix;
|
||||
//for (int y = 0; y < 4; y++)
|
||||
// for (int x = 0; x < 4; x++)
|
||||
// matrix[y * 4 + x] = m[x][y];
|
||||
//matrix[x * 4 + y] = m[y][x];
|
||||
_program.execute(vertices, colors, texcoords, _tx.texture, true, projectionViewModelMatrix.m);
|
||||
|
||||
Log.d("projectionMatrix qt: ", glSupport.projectionMatrix);
|
||||
Log.d("projectionMatrix 2 : ", projectionMatrix);
|
||||
Log.d("projectionViewModelMatrix: ", cast(float[16])m.m);
|
||||
Log.d("(-1,-1,-1) * matrix: ", (m * vec3(-1, -1, -1)).vec);
|
||||
Log.d("(1,1,1) * matrix: ", (m * vec3(1, 1, 1)).vec);
|
||||
Log.d("(0,3,0) * matrix: ", (m * vec3(0, 3, 0)).vec);
|
||||
for (int i = 0; i < vertices.length - 2; i+=3) {
|
||||
vec3 v = vec3(vertices[i], vertices[i + 1], vertices[i + 2]);
|
||||
vec3 v_mul_m = (v * m);
|
||||
vec3 m_mul_v = (m * v);
|
||||
Log.d("pvmmatrix * ", v.vec, " = ", v_mul_m.vec);
|
||||
}
|
||||
//Log.d("(1,1,1) * matrix: ", m * vec4(1, 1, 1, 1));
|
||||
//Log.d("(0,1,0) * matrix: ", m * vec4(0, 1, 0, 1));
|
||||
//Log.d("(1,0,0) * matrix: ", m * vec4(1, 0, 0, 1));
|
||||
//Log.d("(0,0,0) * matrix: ", m * vec4(0, 0, 0, 1));
|
||||
|
||||
_program.execute(vertices, colors, texcoords, _tx.texture, true, m.m);
|
||||
checkgl!glDisable(GL_CULL_FACE);
|
||||
checkgl!glDisable(GL_DEPTH_TEST);
|
||||
}
|
||||
/// returns true is widget is being animated - need to call animate() and redraw
|
||||
@property override bool animating() { return true; }
|
||||
/// animates window; interval is time left from previous draw, in hnsecs (1/10000000 of second)
|
||||
override void animate(long interval) {
|
||||
if (_oldApi) {
|
||||
// animate legacy API example
|
||||
// rotate gears
|
||||
angle += interval * 0.000002f;
|
||||
} else {
|
||||
// TODO: animate new API example
|
||||
angle += interval * 0.000002f;
|
||||
}
|
||||
invalidate();
|
||||
|
||||
// Cube mesh
|
||||
float[] vertices;
|
||||
float[] texcoords;
|
||||
float[4*6*6] colors;
|
||||
void createMesh() {
|
||||
if (!_tx)
|
||||
_tx = new GLTexture("crate");
|
||||
// define Cube mesh
|
||||
auto p000 = [-1.0f, -1.0f, -1.0f];
|
||||
auto p100 = [ 1.0f, -1.0f, -1.0f];
|
||||
auto p010 = [-1.0f, 1.0f, -1.0f];
|
||||
auto p110 = [ 1.0f, 1.0f, -1.0f];
|
||||
auto p001 = [-1.0f, -1.0f, 1.0f];
|
||||
auto p101 = [ 1.0f, -1.0f, 1.0f];
|
||||
auto p011 = [-1.0f, 1.0f, 1.0f];
|
||||
auto p111 = [ 1.0f, 1.0f, 1.0f];
|
||||
vertices = p000 ~ p010 ~ p110 ~ p110 ~ p100 ~ p000 // front face
|
||||
~ p101 ~ p111 ~ p011 ~ p011 ~ p001 ~ p101 // back face
|
||||
~ p100 ~ p110 ~ p111 ~ p111 ~ p101 ~ p100 // right face
|
||||
~ p001 ~ p011 ~ p010 ~ p010 ~ p000 ~ p001 // left face
|
||||
~ p010 ~ p011 ~ p111 ~ p111 ~ p110 ~ p010 // top face
|
||||
~ p001 ~ p000 ~ p100 ~ p100 ~ p101 ~ p001 // bottom face
|
||||
;
|
||||
// texture coordinates
|
||||
float[2] uv = _tx.uv;
|
||||
float tx0 = 0.0f;
|
||||
float tx1 = uv[0];
|
||||
float ty0 = 0.0f;
|
||||
float ty1 = uv[1];
|
||||
float[12] facetx = [tx1, ty1,
|
||||
tx0, ty0,
|
||||
tx0, ty1,
|
||||
tx0, ty1,
|
||||
tx1, ty0,
|
||||
tx1, ty1];
|
||||
texcoords = facetx ~ facetx ~ facetx ~ facetx ~ facetx ~ facetx;
|
||||
// init with white color (1, 1, 1, 1)
|
||||
foreach(ref cl; colors)
|
||||
cl = 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
// ====================================================================================
|
||||
// Shaders based example
|
||||
|
||||
class MyProgram : GLProgram {
|
||||
// Simple texture + color shader
|
||||
class MyGLProgram : GLProgram {
|
||||
@property override string vertexSource() {
|
||||
return q{
|
||||
in vec4 vertex;
|
||||
|
@ -355,15 +284,16 @@ static if (ENABLE_OPENGL) {
|
|||
void main(void)
|
||||
{
|
||||
outColor = texture(tex, texc.st) * col;
|
||||
//outColor = col;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// attribute locations
|
||||
protected GLint matrixLocation;
|
||||
protected GLint vertexLocation;
|
||||
protected GLint colAttrLocation;
|
||||
protected GLint texCoordLocation;
|
||||
|
||||
override bool initLocations() {
|
||||
matrixLocation = getUniformLocation("matrix");
|
||||
vertexLocation = getAttribLocation("vertex");
|
||||
|
@ -377,7 +307,7 @@ static if (ENABLE_OPENGL) {
|
|||
return false;
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
checkgl!glDisable(GL_CULL_FACE);
|
||||
//checkgl!glDisable(GL_CULL_FACE);
|
||||
checkgl!glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
bind();
|
||||
checkgl!glUniformMatrix4fv(matrixLocation, 1, false, matrix.ptr);
|
||||
|
@ -398,7 +328,6 @@ static if (ENABLE_OPENGL) {
|
|||
glEnableVertexAttribArray(colAttrLocation);
|
||||
glEnableVertexAttribArray(texCoordLocation);
|
||||
|
||||
Log.d("Drawing ", vertices.length / 9, " triangles");
|
||||
checkgl!glDrawArrays(GL_TRIANGLES, 0, cast(int)vertices.length / 3);
|
||||
|
||||
glDisableVertexAttribArray(vertexLocation);
|
||||
|
@ -421,6 +350,7 @@ static if (ENABLE_OPENGL) {
|
|||
//=====================================================================================
|
||||
// Legacy OpenGL API example
|
||||
// GlxGears
|
||||
//=====================================================================================
|
||||
|
||||
import std.math;
|
||||
static __gshared GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
|
||||
|
@ -648,6 +578,3 @@ static if (ENABLE_OPENGL) {
|
|||
|
||||
glEnable(GL_NORMALIZE);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -627,7 +627,7 @@ bool fuzzyNull(float v) {
|
|||
|
||||
/// float matrix 4 x 4
|
||||
struct mat4 {
|
||||
float[16] m;
|
||||
float[16] m = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
||||
|
||||
//alias m this;
|
||||
|
||||
|
|
|
@ -761,13 +761,13 @@ public:
|
|||
override void draw() {
|
||||
if (_handler) {
|
||||
glSupport.setOrthoProjection(_windowRect, _rc);
|
||||
glSupport.clearDepthBuffer();
|
||||
_handler(_windowRect, _rc);
|
||||
glSupport.setOrthoProjection(_windowRect, _windowRect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// GL Texture object from image
|
||||
static class GLTexture {
|
||||
protected int _dx;
|
||||
|
|
|
@ -860,6 +860,11 @@ class GLSupport {
|
|||
return _projectionMatrix;
|
||||
}
|
||||
|
||||
/// clear depth buffer
|
||||
void clearDepthBuffer() {
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void setOrthoProjection(Rect windowRect, Rect view) {
|
||||
flushGL();
|
||||
bufferDx = windowRect.width;
|
||||
|
|
Loading…
Reference in New Issue