OpenGL example update

This commit is contained in:
Vadim Lopatin 2016-01-13 11:33:27 +03:00
parent 007784cfb4
commit fc8ef197d6
3 changed files with 45 additions and 20 deletions

View File

@ -257,26 +257,33 @@ static if (ENABLE_OPENGL) {
checkgl!glDisable(GL_DEPTH_TEST); checkgl!glDisable(GL_DEPTH_TEST);
//import gl3n.linalg; //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); //mat4 projectionMatrix = glSupport.projectionMatrix; //mat4.perspective(rc.width, rc.height, 90.0f, 0.5f, 100.0f);
// ======== Projection Matrix ================== // ======== Projection Matrix ==================
mat4 projectionMatrix; // = glSupport.projectionMatrix; mat4 projectionMatrix; // = glSupport.projectionMatrix;
projectionMatrix.setIdentity(); projectionMatrix.setIdentity();
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, aspectRatio, 0.1f, 100.0f);
//projectionMatrix.setOrtho(-2.0f, 2.0f, 2.0f, -2.0f, -2.0f, 2.0f);
// ======== View Matrix ================== // ======== View Matrix ==================
mat4 viewMatrix; mat4 viewMatrix;
viewMatrix.setIdentity(); 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 ================== // ======== Model Matrix ==================
mat4 modelMatrix; mat4 modelMatrix;
modelMatrix.setIdentity(); modelMatrix.setIdentity();
modelMatrix.scale(0.3f); //modelMatrix.scale(0.3f);
modelMatrix.rotatez(30.0f);
modelMatrix.rotatey(angle);
//modelMatrix.translate(3, 0, 0); //modelMatrix.translate(3, 0, 0);
mat4 m = (projectionMatrix * viewMatrix) * modelMatrix; mat4 m = projectionMatrix * viewMatrix * modelMatrix;
//mat4 m = modelMatrix * viewMatrix * projectionMatrix; //mat4 m = modelMatrix * viewMatrix * projectionMatrix;
//float[16] matrix; //float[16] matrix;
@ -326,17 +333,14 @@ static if (ENABLE_OPENGL) {
class MyProgram : GLProgram { class MyProgram : GLProgram {
@property override string vertexSource() { @property override string vertexSource() {
return q{ return q{
uniform mat4 matrix;
in vec4 vertex; in vec4 vertex;
in vec4 colAttr; in vec4 colAttr;
in vec4 texCoord; in vec4 texCoord;
out vec4 col; out vec4 col;
out vec4 texc; out vec4 texc;
uniform mat4 matrix;
void main(void) void main(void)
{ {
//gl_Position = vertex * matrix;
gl_Position = matrix * vertex; gl_Position = matrix * vertex;
col = colAttr; col = colAttr;
texc = texCoord; texc = texCoord;
@ -352,8 +356,8 @@ static if (ENABLE_OPENGL) {
out vec4 outColor; out vec4 outColor;
void main(void) void main(void)
{ {
outColor = col; //texture(tex, texc.st); // * col; //outColor = texture(tex, texc.st) * col;
//outColor = col; //texture(tex, texc.st) * col; outColor = col;
} }
}; };
} }
@ -396,7 +400,8 @@ static if (ENABLE_OPENGL) {
glEnableVertexAttribArray(colAttrLocation); glEnableVertexAttribArray(colAttrLocation);
glEnableVertexAttribArray(texCoordLocation); 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(vertexLocation);
glDisableVertexAttribArray(colAttrLocation); glDisableVertexAttribArray(colAttrLocation);

View File

@ -1004,6 +1004,21 @@ struct mat4 {
return this; 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) { ref mat4 rotate(float angle, const vec3 axis) {
return rotate(angle, axis.x, axis.y, axis.z); return rotate(angle, axis.x, axis.y, axis.z);
} }

View File

@ -791,14 +791,14 @@ static class GLTexture {
/// image coords to UV /// image coords to UV
float[2] uv(int x, int y) { float[2] uv(int x, int y) {
float[2] res; float[2] res;
res[0] = x * cast(float) _dx / _tdx; res[0] = cast(float)x / _tdx;
res[1] = y * cast(float) _dy / _tdy; res[1] = cast(float)y / _tdy;
return res; return res;
} }
float[2] uv(Point pt) { float[2] uv(Point pt) {
float[2] res; float[2] res;
res[0] = pt.x * cast(float) _dx / _tdx; res[0] = cast(float)pt.x / _tdx;
res[1] = pt.y * cast(float) _dy / _tdy; res[1] = cast(float)pt.y / _tdy;
return res; return res;
} }
/// return UV coords for bottom right corner /// return UV coords for bottom right corner
@ -819,14 +819,19 @@ static class GLTexture {
_tdx = nearestPOT(_dx); _tdx = nearestPOT(_dx);
_tdy = nearestPOT(_dy); _tdy = nearestPOT(_dy);
_texture = new Tex2D(); _texture = new Tex2D();
if (!_texture.ID) if (!_texture.ID) {
return;
uint * pixels = buf.scanLine(0);
if (!glSupport.setTextureImage(_texture, buf.width, buf.height, cast(ubyte*)pixels)) {
destroy(_texture);
_texture = null; _texture = null;
return; 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() { ~this() {