Textures
This commit is contained in:
parent
6d2a0e4d04
commit
072593bb22
@ -4,11 +4,9 @@ import kotlinx.browser.document
|
||||
import kotlinx.browser.window
|
||||
import org.khronos.webgl.Float32Array
|
||||
import org.khronos.webgl.Int32Array
|
||||
import org.khronos.webgl.WebGLRenderingContext
|
||||
import org.w3c.dom.HTMLCanvasElement
|
||||
import ru.landgrafhomyak.bgtu.computer_graphics1.model_builder.Matrix4x4
|
||||
import ru.landgrafhomyak.bgtu.computer_graphics1.model_builder.Point2F
|
||||
import ru.landgrafhomyak.bgtu.computer_graphics1.model_builder.Point3F
|
||||
import ru.landgrafhomyak.bgtu.computer_graphics1.model_builder.Point4F
|
||||
|
||||
fun main() {
|
||||
@ -63,7 +61,7 @@ fun main() {
|
||||
gl.STATIC_DRAW
|
||||
)
|
||||
val tpa = gl.getAttribLocation(shaderProgram, "texture_pos")
|
||||
gl.vertexAttribPointer(tpa, 2, gl.FLOAT, false, 2 * 4, 0)
|
||||
gl.vertexAttribPointer(tpa, 2, gl.FLOAT, false, 0, 0)
|
||||
gl.enableVertexAttribArray(tpa)
|
||||
|
||||
gl.bindBuffer(gl.ARRAY_BUFFER, textureTypesBuffer)
|
||||
@ -73,11 +71,14 @@ fun main() {
|
||||
gl.STATIC_DRAW
|
||||
)
|
||||
val tta = gl.getAttribLocation(shaderProgram, "texture_type")
|
||||
gl.vertexAttribIPointer(tta, 2, gl.INT, false, 1 * 4, 0)
|
||||
gl.vertexAttribIPointer(tta, 1, gl.INT, false, 0, 0)
|
||||
gl.enableVertexAttribArray(tta)
|
||||
|
||||
gl.enable(gl.DEPTH_TEST)
|
||||
|
||||
gl.enable(gl.BLEND);
|
||||
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
var xAngle = 0.0f
|
||||
var yAngle = 0.0f
|
||||
var distance = 0.0f
|
||||
@ -87,13 +88,12 @@ fun main() {
|
||||
|
||||
|
||||
val redraw = {
|
||||
val radius = ((if (canvas.width < canvas.height) canvas.width else canvas.height) * 7f / 16f);
|
||||
gl.viewport(0, 0, canvas.width, canvas.height);
|
||||
gl.clearColor(0f, 0f, 0f, 1f);
|
||||
gl.clear(gl.COLOR_BUFFER_BIT or gl.DEPTH_BUFFER_BIT);
|
||||
val t = Matrix4x4.shift(0f, 0f, distance) * Matrix4x4.rotateY(yAngle) * Matrix4x4.rotateX(xAngle)
|
||||
val po = Matrix4x4.projectionOrtho(-2f, 2f, -2f, 2f, -2f, 2f);
|
||||
val pc = Matrix4x4.projectionCentralInfinite(-2f, 2f, -2f, 2f, 1f) * Matrix4x4.shift(0f, 0f, -3f) * Matrix4x4.scale(3f, 3f, -1f);
|
||||
val pc = Matrix4x4.projectionCentralInfinite(-2f, 2f, -2f, 2f, 1f) * Matrix4x4.shift(0f, 0f, -3.3f) * Matrix4x4.scale(3f, 3f, -1f);
|
||||
val s: Point2F
|
||||
if (canvas.width > canvas.height) {
|
||||
s = Point2F(canvas.height.toFloat() / canvas.width.toFloat(), 1f)
|
||||
|
@ -3,12 +3,13 @@ package ru.landgrafhomyak.bgtu.computer_graphics1
|
||||
import kotlin.math.PI
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.sin
|
||||
import kotlin.math.sqrt
|
||||
import ru.landgrafhomyak.bgtu.computer_graphics1.model_builder.ModelTriangles
|
||||
import ru.landgrafhomyak.bgtu.computer_graphics1.model_builder.Point2F
|
||||
import ru.landgrafhomyak.bgtu.computer_graphics1.model_builder.Polygon
|
||||
import ru.landgrafhomyak.bgtu.computer_graphics1.model_builder.TextureType
|
||||
|
||||
private fun rot_n(degrees: Int, length: Int): Point2F {
|
||||
private fun rot_n(degrees: Int, length: Float): Point2F {
|
||||
val radians = degrees.toLong().toDouble() * PI / 180;
|
||||
return Point2F(
|
||||
x = cos(radians).times(length).toFloat(),
|
||||
@ -20,106 +21,53 @@ private fun rot_n(degrees: Int, length: Int): Point2F {
|
||||
val model = ModelTriangles.fromPolygons(
|
||||
Polygon(
|
||||
TextureType.HSV,
|
||||
Polygon.PolygonVertex(0f, 1f, 1f, rot_n(0, 1)),
|
||||
Polygon.PolygonVertex(1f, 1f, 0f, rot_n(240, 1)),
|
||||
Polygon.PolygonVertex(1f, 0f, 1f, rot_n(120, 1))
|
||||
Polygon.PolygonVertex(0f, 1f, 1f, rot_n(0, 1f)),
|
||||
Polygon.PolygonVertex(-1f, 1f, 1f, rot_n(0, 2f)),
|
||||
Polygon.PolygonVertex(-1f, -1f, 1f, rot_n(300, 2f * sqrt(2f))),
|
||||
Polygon.PolygonVertex(1f, -1f, 1f, rot_n(240, 2f)),
|
||||
Polygon.PolygonVertex(1f, 0f, 1f, rot_n(240, 1f)),
|
||||
),
|
||||
Polygon(
|
||||
TextureType.HSV,
|
||||
Polygon.PolygonVertex(-1f, -1f, 1f, rot_n(60, 2)),
|
||||
Polygon.PolygonVertex(-1f, 1f, 1f, rot_n(0, 2)),
|
||||
Polygon.PolygonVertex(0f, 1f, 1f, rot_n(0, 1))
|
||||
Polygon.PolygonVertex(0f, 1f, 1f, rot_n(0, 1f)),
|
||||
Polygon.PolygonVertex(-1f, 1f, 1f, rot_n(0, 2f)),
|
||||
Polygon.PolygonVertex(-1f, 1f, -1f, rot_n(60, 2f * sqrt(2f))),
|
||||
Polygon.PolygonVertex(1f, 1f, -1f, rot_n(120, 2f)),
|
||||
Polygon.PolygonVertex(1f, 1f, 0f, rot_n(120, 1f))
|
||||
),
|
||||
Polygon(
|
||||
TextureType.HSV,
|
||||
Polygon.PolygonVertex(-1f, -1f, 1f, rot_n(60, 2)),
|
||||
Polygon.PolygonVertex(1f, 0f, 1f, rot_n(120, 1)),
|
||||
Polygon.PolygonVertex(0f, 1f, 1f, rot_n(0, 1))
|
||||
),
|
||||
|
||||
Polygon(
|
||||
TextureType.HSV,
|
||||
Polygon.PolygonVertex(-1f, -1f, 1f, rot_n(60, 2)),
|
||||
Polygon.PolygonVertex(1f, 0f, 1f, rot_n(120, 1)),
|
||||
Polygon.PolygonVertex(1f, -1f, 1f, rot_n(120, 1))
|
||||
Polygon.PolygonVertex(1f, 0f, 1f, rot_n(240, 1f)),
|
||||
Polygon.PolygonVertex(1f, -1f, 1f, rot_n(240, 2f)),
|
||||
Polygon.PolygonVertex(1f, -1f, -1f, rot_n(180, 2f * sqrt(2f))),
|
||||
Polygon.PolygonVertex(1f, 1f, -1f, rot_n(120, 2f)),
|
||||
Polygon.PolygonVertex(1f, 1f, 0f, rot_n(120, 1f))
|
||||
),
|
||||
Polygon(
|
||||
TextureType.HSV,
|
||||
Polygon.PolygonVertex(1f, -1f, -1f, rot_n(180, 2)),
|
||||
Polygon.PolygonVertex(1f, 1f, -1f, rot_n(240, 2)),
|
||||
Polygon.PolygonVertex(1f, 1f, 0f, rot_n(240, 1))
|
||||
Polygon.PolygonVertex(1f, 0f, 1f, rot_n(240, 1f)),
|
||||
Polygon.PolygonVertex(1f, 1f, 0f, rot_n(120, 1f)),
|
||||
Polygon.PolygonVertex(0f, 1f, 1f, rot_n(0, 1f))
|
||||
),
|
||||
Polygon(
|
||||
TextureType.HSV,
|
||||
Polygon.PolygonVertex(1f, -1f, -1f, rot_n(180, 2)),
|
||||
Polygon.PolygonVertex(1f, 0f, 1f, rot_n(120, 1)),
|
||||
Polygon.PolygonVertex(1f, 1f, 0f, rot_n(240, 1))
|
||||
),
|
||||
|
||||
Polygon(
|
||||
TextureType.HSV,
|
||||
Polygon.PolygonVertex(1f, -1f, -1f, rot_n(180, 2)),
|
||||
Polygon.PolygonVertex(1f, 0f, 1f, rot_n(120, 1)),
|
||||
Polygon.PolygonVertex(1f, -1f, 1f, rot_n(120, 2))
|
||||
),
|
||||
Polygon(
|
||||
TextureType.HSV,
|
||||
Polygon.PolygonVertex(-1f, 1f, -1f, rot_n(300, 2)),
|
||||
Polygon.PolygonVertex(1f, 1f, -1f, rot_n(240, 2)),
|
||||
Polygon.PolygonVertex(1f, 1f, 0f, rot_n(240, 1))
|
||||
),
|
||||
Polygon(
|
||||
TextureType.HSV,
|
||||
Polygon.PolygonVertex(-1f, 1f, -1f, rot_n(300, 2)),
|
||||
Polygon.PolygonVertex(0f, 1f, 1f, rot_n(0, 1)),
|
||||
Polygon.PolygonVertex(1f, 1f, 0f, rot_n(240, 1))
|
||||
),
|
||||
|
||||
Polygon(
|
||||
TextureType.HSV,
|
||||
Polygon.PolygonVertex(-1f, 1f, -1f, rot_n(300, 2)),
|
||||
Polygon.PolygonVertex(0f, 1f, 1f, rot_n(0, 1)),
|
||||
Polygon.PolygonVertex(-1f, 1f, 1f, rot_n(0, 2))
|
||||
),
|
||||
|
||||
Polygon(
|
||||
TextureType.WHITE_TRANSPARENCY,
|
||||
TextureType.GRADIENT_CHESS,
|
||||
Polygon.PolygonVertex(-1f, -1f, 1f, 0f, 0f),
|
||||
Polygon.PolygonVertex(-1f, -1f, -1f, 0f, 1f),
|
||||
Polygon.PolygonVertex(-1f, 1f, 1f, 1f, 0f)
|
||||
),
|
||||
|
||||
Polygon(
|
||||
TextureType.WHITE_TRANSPARENCY,
|
||||
Polygon.PolygonVertex(-1f, 1f, -1f, 1f, 1f),
|
||||
Polygon.PolygonVertex(-1f, -1f, -1f, 0f, 1f),
|
||||
Polygon.PolygonVertex(-1f, 1f, 1f, 1f, 0f)
|
||||
Polygon.PolygonVertex(-1f, 1f, 1f, 1f, 0f),
|
||||
),
|
||||
Polygon(
|
||||
TextureType.CHESS,
|
||||
Polygon.PolygonVertex(1f, -1f, -1f, 0f, 1f),
|
||||
Polygon.PolygonVertex(-1f, -1f, -1f, 0f, 0f),
|
||||
Polygon.PolygonVertex(-1f, 1f, -1f, 1f, 0f),
|
||||
Polygon.PolygonVertex(1f, 1f, -1f, 1f, 1f),
|
||||
),
|
||||
|
||||
Polygon(
|
||||
TextureType.WHITE_TRANSPARENCY,
|
||||
Polygon.PolygonVertex(-1f, -1f, -1f, 0f, 0f),
|
||||
Polygon.PolygonVertex(-1f, 1f, -1f, 0f, 1f),
|
||||
Polygon.PolygonVertex(1f, 1f, -1f, 1f, 1f)
|
||||
),
|
||||
|
||||
Polygon(
|
||||
TextureType.WHITE_TRANSPARENCY,
|
||||
Polygon.PolygonVertex(-1f, -1f, -1f, 0f, 0f),
|
||||
Polygon.PolygonVertex(-1f, -1f, 1f, 0f, 1f),
|
||||
Polygon.PolygonVertex(1f, -1f, 1f, 1f, 1f),
|
||||
Polygon.PolygonVertex(1f, -1f, -1f, 1f, 0f),
|
||||
Polygon.PolygonVertex(1f, 1f, -1f, 1f, 1f)
|
||||
),
|
||||
|
||||
Polygon(
|
||||
TextureType.CHESS,
|
||||
Polygon.PolygonVertex(1f, -1f, 1f, 0f, 0f),
|
||||
Polygon.PolygonVertex(-1f, -1f, 1f, 0f, 1f),
|
||||
Polygon.PolygonVertex(1f, -1f, -1f, 1f, 0f)
|
||||
),
|
||||
Polygon(
|
||||
TextureType.CHESS,
|
||||
Polygon.PolygonVertex(-1f, -1f, -1f, 1f, 1f),
|
||||
Polygon.PolygonVertex(-1f, -1f, 1f, 0f, 1f),
|
||||
Polygon.PolygonVertex(1f, -1f, -1f, 1f, 0f)
|
||||
Polygon.PolygonVertex(-1f, -1f, -1f, 0f, 0f),
|
||||
)
|
||||
)
|
@ -1,7 +1,9 @@
|
||||
package ru.landgrafhomyak.bgtu.computer_graphics1.model_builder
|
||||
|
||||
enum class TextureType {
|
||||
CHESS,
|
||||
HSV,
|
||||
NULL,
|
||||
WHITE_TRANSPARENCY,
|
||||
HSV,
|
||||
CHESS,
|
||||
GRADIENT_CHESS,
|
||||
}
|
@ -35,6 +35,8 @@ internal val fragmentShaderSource = """
|
||||
|
||||
vec3 hsv2rgb(float, float, float);
|
||||
vec3 hsv_texture(float, float);
|
||||
float atan_gradient(float k, float radius, float lo, float hi);
|
||||
float linear_gradient(float k, float lo, float hi);
|
||||
|
||||
void main() {
|
||||
switch (texture_type_v2f) {
|
||||
@ -51,6 +53,26 @@ internal val fragmentShaderSource = """
|
||||
case ${TextureType.HSV.ordinal}:
|
||||
FragColor = vec4(hsv_texture(texture_pos_v2f.x, texture_pos_v2f.y), 1.0);
|
||||
break;
|
||||
case ${TextureType.GRADIENT_CHESS.ordinal}:
|
||||
{
|
||||
float radius = 0.2;
|
||||
if (int( round(floor((texture_pos_v2f.x - 0.5) / 0.033)) + round(floor((texture_pos_v2f.y - 0.5) / 0.033)) ) % 2 == 0) {
|
||||
FragColor = vec4(
|
||||
0.0 + linear_gradient(texture_pos_v2f.x, 0.0, 0.5),
|
||||
0.0 + linear_gradient(texture_pos_v2f.y, 0.0, 0.5),
|
||||
0.5 - min(linear_gradient(texture_pos_v2f.x, 0.0, 0.35) + linear_gradient(texture_pos_v2f.y, 0.0, 0.35), 0.5),
|
||||
1.0
|
||||
);
|
||||
} else {
|
||||
FragColor = vec4(
|
||||
0.0 + linear_gradient(texture_pos_v2f.x, 0.0, 1.0),
|
||||
0.0 + linear_gradient(texture_pos_v2f.y, 0.0, 1.0),
|
||||
1.0 - min(linear_gradient(texture_pos_v2f.x, 0.0, 0.8) + linear_gradient(texture_pos_v2f.y, 0.0, 0.8), 1.0),
|
||||
1.0
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
FragColor = vec4(0.0, 0.0, 0.0, 0.0);
|
||||
}
|
||||
@ -61,6 +83,12 @@ internal val fragmentShaderSource = """
|
||||
return hsv2rgb(mod(mod(angle / PI * 180.0, 360.0) + 360.0, 360.0), 1.0, 1.0);
|
||||
}
|
||||
|
||||
float atan_gradient(float k, float radius, float lo, float hi) {
|
||||
return ((atan((k - 0.5) * 2.0 * radius) / atan(radius)) + 0.5) * (hi - lo) + lo;
|
||||
}
|
||||
float linear_gradient(float k, float lo, float hi) {
|
||||
return k * (hi - lo) + lo;
|
||||
}
|
||||
vec3 hsv2rgb(float hue, float saturation, float value) {
|
||||
int hi = int(floor(hue / 60.0)) % 6;
|
||||
float f = hue / 60.0 - floor(hue / 60.0);
|
||||
|
Loading…
Reference in New Issue
Block a user