This commit is contained in:
Vadim Lopatin 2014-03-10 23:45:44 +04:00
parent 091f75db13
commit 013b79b4a6
3 changed files with 83 additions and 71 deletions

View File

@ -24,13 +24,14 @@ private void LVGLFillColor(uint color, float * buf, int count) {
private bool checkError(string context, string file = __FILE__, int line = __LINE__) { private bool checkError(string context, string file = __FILE__, int line = __LINE__) {
int err = glGetError(); int err = glGetError();
if (err != GL_NO_ERROR) { if (err != GL_NO_ERROR) {
//string errorString = fromStringz(gluErrorString());
Log.e("OpenGL error ", err, " at ", file, ":", line, " -- ", context); Log.e("OpenGL error ", err, " at ", file, ":", line, " -- ", context);
return true; return true;
} }
return false; return false;
} }
immutable float Z_2D = -1.0f; immutable float Z_2D = -2.0f;
void drawSolidFillRect(Rect rc, uint color1, uint color2, uint color3, uint color4) { void drawSolidFillRect(Rect rc, uint color1, uint color2, uint color3, uint color4) {
float[6 * 4] colors; float[6 * 4] colors;
LVGLFillColor(color1, colors.ptr + 4*0, 1); LVGLFillColor(color1, colors.ptr + 4*0, 1);
@ -57,7 +58,11 @@ void drawSolidFillRect(Rect rc, uint color1, uint color2, uint color3, uint colo
x0,y0,Z_2D, x0,y0,Z_2D,
x1,y1,Z_2D, x1,y1,Z_2D,
x1,y0,Z_2D]; x1,y0,Z_2D];
if (_solidFillProgram !is null) {
Log.d("solid fill: vertices ", vertices, " colors ", colors);
_solidFillProgram.execute(vertices, colors); _solidFillProgram.execute(vertices, colors);
} else
Log.e("No program");
//drawSolidFillRect(vertices, colors); //drawSolidFillRect(vertices, colors);
} }
@ -282,7 +287,10 @@ void setOrthoProjection(int dx, int dy) {
bufferDy = dy; bufferDy = dy;
//myGlOrtho(0, dx, 0, dy, -0.1f, 5.0f); //myGlOrtho(0, dx, 0, dy, -0.1f, 5.0f);
m = mat4.orthographic(0, dx, 0, dy, 0.5f, 5.0f); Log.d("Ortho ", dx, "x", dy);
m = mat4.orthographic(0, dx, 0, dy, 0.5f, 50.0f);
Log.d("Matrix: ", m);
glViewport(0, 0, dx, dy); glViewport(0, 0, dx, dy);
checkError("glViewport"); checkError("glViewport");
} }
@ -290,16 +298,20 @@ void setOrthoProjection(int dx, int dy) {
class GLProgram { class GLProgram {
@property abstract string vertexSource(); @property abstract string vertexSource();
@property abstract string fragmentSource(); @property abstract string fragmentSource();
GLuint vertexShader; protected GLuint vertexShader;
GLuint fragmentShader; protected GLuint fragmentShader;
GLuint program; protected GLuint program;
bool initialized; protected bool initialized;
bool error; protected bool error;
protected string glslversion;
this() { this() {
} }
private GLuint compileShader(string src, GLuint type) { private GLuint compileShader(string src, GLuint type) {
import core.stdc.stdlib; import core.stdc.stdlib;
import std.string; import std.string;
Log.d("compileShader glsl=", glslversion, " code: ", src);
GLuint shader = glCreateShader(type);//GL_VERTEX_SHADER GLuint shader = glCreateShader(type);//GL_VERTEX_SHADER
const char * psrc = src.toStringz; const char * psrc = src.toStringz;
GLuint len = src.length; GLuint len = src.length;
@ -325,6 +337,7 @@ class GLProgram {
} }
} }
bool compile() { bool compile() {
glslversion = fromStringz(glGetString(GL_SHADING_LANGUAGE_VERSION));
vertexShader = compileShader(vertexSource, GL_VERTEX_SHADER); vertexShader = compileShader(vertexSource, GL_VERTEX_SHADER);
fragmentShader = compileShader(fragmentSource, GL_FRAGMENT_SHADER); fragmentShader = compileShader(fragmentSource, GL_FRAGMENT_SHADER);
if (!vertexShader || !fragmentShader) { if (!vertexShader || !fragmentShader) {
@ -348,8 +361,8 @@ class GLProgram {
return false; return false;
} }
Log.d("Program compiled successfully"); Log.d("Program compiled successfully");
glDetachShader(program, vertexShader); //glDetachShader(program, vertexShader);
glDetachShader(program, fragmentShader); //glDetachShader(program, fragmentShader);
glUseProgram(program); glUseProgram(program);
checkError("glUseProgram " ~ to!string(program)); checkError("glUseProgram " ~ to!string(program));
if (!initLocations()) { if (!initLocations()) {
@ -362,9 +375,11 @@ class GLProgram {
bool initLocations() { bool initLocations() {
return true; return true;
} }
bool use() { bool bind() {
if (!initialized) if (!initialized)
return false; return false;
if (!glIsProgram(program))
Log.e("!glIsProgram(program)");
glUseProgram(program); glUseProgram(program);
checkError("glUseProgram " ~ to!string(program)); checkError("glUseProgram " ~ to!string(program));
return true; return true;
@ -389,13 +404,17 @@ class GLProgram {
} }
} }
immutable string HIGHP = "";
immutable string LOWP = "";
immutable string MEDIUMP = "";
class SolidFillProgram : GLProgram { class SolidFillProgram : GLProgram {
@property override string vertexSource() { @property override string vertexSource() {
return return
"attribute highp vec4 vertex;\n" "attribute " ~ HIGHP ~ " vec4 vertex;\n"
"attribute lowp vec4 colAttr;\n" "attribute " ~ LOWP ~ " vec4 colAttr;\n"
"varying lowp vec4 col;\n" "varying " ~ LOWP ~ " vec4 col;\n"
"uniform mediump mat4 matrix;\n" "uniform " ~ MEDIUMP ~ " mat4 matrix;\n"
"void main(void)\n" "void main(void)\n"
"{\n" "{\n"
" gl_Position = matrix * vertex;\n" " gl_Position = matrix * vertex;\n"
@ -405,8 +424,7 @@ class SolidFillProgram : GLProgram {
} }
@property override string fragmentSource() { @property override string fragmentSource() {
return return
"uniform sampler2D texture;\n" "varying " ~ LOWP ~ " vec4 col;\n"
"varying lowp vec4 col;\n"
"void main(void)\n" "void main(void)\n"
"{\n" "{\n"
" gl_FragColor = col;\n" " gl_FragColor = col;\n"
@ -419,9 +437,7 @@ class SolidFillProgram : GLProgram {
checkError("glDisable(GL_CULL_FACE)"); checkError("glDisable(GL_CULL_FACE)");
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
checkError("glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)"); checkError("glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)");
use(); bind();
glDisable(GL_CULL_FACE);
checkError("glDisable(GL_CULL_FACE)");
glUniformMatrix4fv(matrixLocation, 1, false, m.value_ptr); glUniformMatrix4fv(matrixLocation, 1, false, m.value_ptr);
checkError("glUniformMatrix4fv"); checkError("glUniformMatrix4fv");
} }
@ -433,11 +449,24 @@ class SolidFillProgram : GLProgram {
protected GLint matrixLocation; protected GLint matrixLocation;
protected GLint vertexLocation; protected GLint vertexLocation;
protected GLint colAttrLocation; protected GLint colAttrLocation;
protected GLuint vertexBuffer;
protected GLuint colAttrBuffer;
override bool initLocations() { override bool initLocations() {
bool res = super.initLocations(); bool res = super.initLocations();
//glGenBuffers(1, &vertexBuffer);
//glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
//glBufferData(GL_ARRAY_BUFFER, float.sizeof * 3 * 6, null, GL_DYNAMIC_DRAW);
//glGenBuffers(1, &colAttrBuffer);
//glBindBuffer(GL_ARRAY_BUFFER, colAttrBuffer);
//glBufferData(GL_ARRAY_BUFFER, float.sizeof * 4 * 6, null, GL_DYNAMIC_DRAW);
matrixLocation = glGetUniformLocation(program, "matrix"); matrixLocation = glGetUniformLocation(program, "matrix");
checkError("glGetUniformLocation matrix");
vertexLocation = glGetAttribLocation(program, "vertex"); vertexLocation = glGetAttribLocation(program, "vertex");
checkError("glGetAttribLocation vertex");
colAttrLocation = glGetAttribLocation(program, "colAttr"); colAttrLocation = glGetAttribLocation(program, "colAttr");
checkError("glGetAttribLocation colAttr");
return res && matrixLocation >= 0 && vertexLocation >= 0 && colAttrLocation >= 0; return res && matrixLocation >= 0 && vertexLocation >= 0 && colAttrLocation >= 0;
} }
@ -451,24 +480,23 @@ class SolidFillProgram : GLProgram {
glEnableVertexAttribArray(vertexLocation); glEnableVertexAttribArray(vertexLocation);
checkError("glEnableVertexAttribArray"); checkError("glEnableVertexAttribArray");
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, float.sizeof * 3, vertices.ptr);
checkError("glVertexAttribPointer");
glEnableVertexAttribArray(colAttrLocation); glEnableVertexAttribArray(colAttrLocation);
checkError("glEnableVertexAttribArray"); checkError("glEnableVertexAttribArray");
glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, float.sizeof * 4, colors.ptr);
glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, vertices.ptr);
checkError("glVertexAttribPointer");
glVertexAttribPointer(colAttrLocation, 4, GL_FLOAT, GL_FALSE, 0, colors.ptr);
checkError("glVertexAttribPointer"); checkError("glVertexAttribPointer");
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
checkError("glDrawArrays"); checkError("glDrawArrays");
glDisableVertexAttribArray(vertexLocation); glDisableVertexAttribArray(vertexLocation);
checkError("glDisableVertexAttribArray"); checkError("glDisableVertexAttribArray");
glDisableVertexAttribArray(colAttrLocation); glDisableVertexAttribArray(colAttrLocation);
checkError("glDisableVertexAttribArray"); checkError("glDisableVertexAttribArray");
afterExecute(); afterExecute();
glBindTexture(GL_TEXTURE_2D, 0);
checkError("glBindTexture");
return true; return true;
} }
} }
@ -476,12 +504,12 @@ class SolidFillProgram : GLProgram {
class TextureProgram : SolidFillProgram { class TextureProgram : SolidFillProgram {
@property override string vertexSource() { @property override string vertexSource() {
return return
"attribute highp vec4 vertex;\n" "attribute " ~ HIGHP ~ " vec4 vertex;\n"
"attribute lowp vec4 colAttr;\n" "attribute " ~ LOWP ~ " vec4 colAttr;\n"
"attribute mediump vec4 texCoord;\n" "attribute " ~ MEDIUMP ~ " vec4 texCoord;\n"
"varying lowp vec4 col;\n" "varying " ~ LOWP ~ " vec4 col;\n"
"varying mediump vec4 texc;\n" "varying " ~ MEDIUMP ~ " vec4 texc;\n"
"uniform mediump mat4 matrix;\n" "uniform " ~ MEDIUMP ~ " mat4 matrix;\n"
"void main(void)\n" "void main(void)\n"
"{\n" "{\n"
" gl_Position = matrix * vertex;\n" " gl_Position = matrix * vertex;\n"
@ -493,8 +521,8 @@ class TextureProgram : SolidFillProgram {
@property override string fragmentSource() { @property override string fragmentSource() {
return return
"uniform sampler2D texture;\n" "uniform sampler2D texture;\n"
"varying lowp vec4 col;\n" "varying " ~ LOWP ~ " vec4 col;\n"
"varying mediump vec4 texc;\n" "varying " ~ MEDIUMP ~ " vec4 texc;\n"
"void main(void)\n" "void main(void)\n"
"{\n" "{\n"
" gl_FragColor = texture2D(texture, texc.st) * col;\n" " gl_FragColor = texture2D(texture, texc.st) * col;\n"
@ -563,3 +591,16 @@ bool initShaders() {
Log.d("Shaders compiled successfully"); Log.d("Shaders compiled successfully");
return true; return true;
} }
bool uninitShaders() {
Log.d("Uniniting shaders");
if (_textureProgram !is null) {
destroy(_textureProgram);
_textureProgram = null;
}
if (_solidFillProgram !is null) {
destroy(_solidFillProgram);
_solidFillProgram = null;
}
return true;
}

View File

@ -167,7 +167,12 @@ class Win32Window : Window {
Log.e("Derelict exception", e); Log.e("Derelict exception", e);
} }
} else { } else {
if (initShaders()) {
setOpenglEnabled();
useOpengl = true; useOpengl = true;
} else {
Log.e("Failed to compile shaders");
}
} }
} }
} else { } else {
@ -181,6 +186,7 @@ class Win32Window : Window {
Log.d("Window destructor"); Log.d("Window destructor");
import derelict.opengl3.wgl; import derelict.opengl3.wgl;
if (_hGLRC) { if (_hGLRC) {
uninitShaders();
wglMakeCurrent (null, null) ; wglMakeCurrent (null, null) ;
wglDeleteContext(_hGLRC); wglDeleteContext(_hGLRC);
_hGLRC = null; _hGLRC = null;
@ -237,6 +243,7 @@ class Win32Window : Window {
GLDrawBuf buf = new GLDrawBuf(_dx, _dy, false); GLDrawBuf buf = new GLDrawBuf(_dx, _dy, false);
buf.beforeDrawing(); buf.beforeDrawing();
buf.fillRect(Rect(100, 100, 200, 200), 0x704020); buf.fillRect(Rect(100, 100, 200, 200), 0x704020);
buf.fillRect(Rect(40, 70, 100, 120), 0x000000);
buf.afterDrawing(); buf.afterDrawing();
//Log.d("onPaint() end drawing opengl"); //Log.d("onPaint() end drawing opengl");
SwapBuffers(hdc); SwapBuffers(hdc);

View File

@ -111,42 +111,6 @@ class LinearLayout : WidgetGroup {
pheight -= m.top + m.bottom + p.top + p.bottom; pheight -= m.top + m.bottom + p.top + p.bottom;
// measure children // measure children
Point sz = _layoutItems.measure(orientation, _children, pwidth, pheight); Point sz = _layoutItems.measure(orientation, _children, pwidth, pheight);
/*
int contentWidth = 0;
int contentHeight = 0;
if (orientation == Orientation.Vertical) {
// Vertical
int max = 0;
int total = 0;
for (int i = 0; i < _children.count; i++) {
Widget item = _children.get(i);
if (item.visibility == Visibility.Gone)
continue;
item.measure(pwidth, pheight);
if (max < item.measuredWidth)
max = item.measuredWidth;
total += item.measuredHeight;
}
contentWidth = max;
contentHeight = total;
} else {
// Horizontal
int max = 0;
int total = 0;
for (int i = 0; i < _children.count; i++) {
Widget item = _children.get(i);
if (item.visibility == Visibility.Gone)
continue;
item.measure(pwidth, pheight);
if (max < item.measuredHeight)
max = item.measuredHeight;
total += item.measuredWidth;
}
contentWidth = total;
contentHeight = max;
}
measuredContent(parentWidth, parentHeight, contentWidth, contentHeight);
*/
measuredContent(parentWidth, parentHeight, sz.x, sz.y); measuredContent(parentWidth, parentHeight, sz.x, sz.y);
} }