Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f803fbd

Browse filesBrowse files
committed
working
1 parent ad29157 commit f803fbd
Copy full SHA for f803fbd

File tree

Expand file treeCollapse file tree

4 files changed

+70
-138
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+70
-138
lines changed

‎build.gradle

Copy file name to clipboardExpand all lines: build.gradle
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ dependencies {
2222

2323
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
2424

25-
compile 'com.github.kotlin-graphics:glm:f0580a46e8677001b2b49b36e0a255ff52fb05fe'
26-
compile 'com.github.kotlin-graphics:gli:97b39116024dddceb2e09106198321049dc3a85f'
27-
compile 'com.github.kotlin-graphics:uno-sdk:64e5bb942b681326e63fbe91683c8ce7ba1aaeb6'
28-
compile 'com.github.java-graphics:assimp:993a4d963c5b39d0b2f43d1726d71804522e7207'
25+
compile 'com.github.kotlin-graphics:uno-sdk:8c4a4b1c1a3a844b8c299a6883e6766ccdfbaec6'
26+
compile 'com.github.java-graphics:assimp:ecd4edfc5ba0d2a34ad13a3c86a6ec5a8fc9870c'
2927

3028
// testCompile("io.kotlintest:kotlintest:2.0.0")
3129

‎src/main/kotlin/learnOpenGL/common/GlfwWindow.kt

Copy file name to clipboardExpand all lines: src/main/kotlin/learnOpenGL/common/GlfwWindow.kt
+34-3Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package learnOpenGL.common
22

3+
import glm.bool
34
import glm.f
45
import glm.i
56
import glm.vec2.Vec2i
7+
import glm.vec4.Vec4i
68
import org.lwjgl.glfw.Callbacks
79
import org.lwjgl.glfw.GLFW.*
810
import org.lwjgl.glfw.GLFWCursorPosCallbackI
@@ -23,6 +25,8 @@ class GlfwWindow(width: Int, height: Int, title: String) {
2325

2426
private val x = intBufferBig(1)
2527
private val y = intBufferBig(1)
28+
private val z = intBufferBig(1)
29+
private val w = intBufferBig(1)
2630

2731
val handle = glfwCreateWindow(width, height, title, 0L, 0L)
2832

@@ -71,10 +75,37 @@ class GlfwWindow(width: Int, height: Int, title: String) {
7175
val framebufferSize: Vec2i
7276
get() {
7377
glfwGetFramebufferSize(handle, x, y)
74-
return Vec2i(x[0], y[0])
78+
return Vec2i(x, y)
79+
}
80+
81+
val frameSize: Vec4i
82+
get() {
83+
glfwGetWindowFrameSize(handle, x, y, z, w)
84+
return Vec4i(x[0], y[0], z[0], w[0])
7585
}
7686

77-
// val frameSize:
87+
fun iconify() = glfwIconifyWindow(handle)
88+
fun restore() = glfwRestoreWindow(handle)
89+
fun maximize() = glfwMaximizeWindow(handle)
90+
fun show() = glfwShowWindow(handle)
91+
fun hide() = glfwHideWindow(handle)
92+
fun focus() = glfwFocusWindow(handle)
93+
94+
val monitor get() = glfwGetWindowMonitor(handle)
95+
fun monitor(monitor: Long, xPos: Int, yPos: Int, width: Int, height: Int) =
96+
monitor(monitor, xPos, yPos, width, height, GLFW_DONT_CARE)
97+
fun monitor(monitor: Long, xPos: Int, yPos: Int, width: Int, height: Int, refreshRate: Int) =
98+
glfwSetWindowMonitor(handle, monitor, xPos, yPos, width, height, refreshRate)
99+
100+
val focused get() = glfwGetWindowAttrib(handle, GLFW_FOCUSED).bool
101+
val iconified get() = glfwGetWindowAttrib(handle, GLFW_ICONIFIED).bool
102+
val maximized get() = glfwGetWindowAttrib(handle, GLFW_MAXIMIZED).bool
103+
val visible get() = glfwGetWindowAttrib(handle, GLFW_VISIBLE).bool
104+
val resizable get() = glfwGetWindowAttrib(handle, GLFW_RESIZABLE).bool
105+
val decorated get() = glfwGetWindowAttrib(handle, GLFW_DECORATED).bool
106+
val floating get() = glfwGetWindowAttrib(handle, GLFW_FLOATING).bool
107+
108+
78109

79110
fun makeContextCurrent() = glfwMakeContextCurrent(handle)
80111

@@ -87,7 +118,7 @@ class GlfwWindow(width: Int, height: Int, title: String) {
87118
glfwDestroyWindow(handle)
88119
}
89120

90-
fun show() = glfwShowWindow(handle)
121+
91122
fun swapBuffers() = glfwSwapBuffers(handle)
92123

93124

+26-37Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package learnOpenGL.common
22

3+
import assimp.AiMesh
4+
import assimp.AiScene
35
import glm.BYTES
6+
import glm.set
47
import glm.vec2.Vec2
58
import glm.vec3.Vec3
69
import org.lwjgl.opengl.GL11.GL_FLOAT
@@ -9,6 +12,7 @@ import org.lwjgl.opengl.GL20.glEnableVertexAttribArray
912
import org.lwjgl.opengl.GL20.glVertexAttribPointer
1013
import org.lwjgl.opengl.GL30.glGenVertexArrays
1114
import uno.buffer.byteBufferBig
15+
import uno.buffer.floatBufferBig
1216
import uno.buffer.intBufferBig
1317
import uno.glf.glf
1418
import uno.glf.semantic
@@ -18,32 +22,9 @@ import uno.gln.*
1822
* Created by GBarbieri on 02.05.2017.
1923
*/
2024

21-
class Vertex(
22-
// Position
23-
val position: Vec3,
24-
// Normal
25-
val normal: Vec3,
26-
// TexCoords
27-
val texCoords: Vec2 = Vec2()
28-
// Tangent
29-
// val tangent: Vec3,
30-
// // Bitangent
31-
// val bitangent: Vec3
32-
) {
33-
companion object {
34-
val size = 2 * Vec3.size + Vec2.size
35-
}
36-
}
37-
38-
class Texture(
39-
val id: Int,
40-
val type: String,
41-
val path: String)
42-
4325
class Mesh(
44-
val vertices: List<Vertex>,
45-
val indices: List<Int>,
46-
val textures: List<Texture>
26+
assimpMesh: AiMesh,
27+
scene: AiScene
4728
) {
4829
val vao = intBufferBig(1)
4930

@@ -64,29 +45,35 @@ class Mesh(
6445
glBindVertexArray(vao)
6546
// Load data into vertex buffers
6647
glBindBuffer(GL_ARRAY_BUFFER, buffers[Buffer.VERTEX])
67-
val vertexBuffer = byteBufferBig(Vertex.size * vertices.size)
68-
vertices.forEachIndexed { i, it ->
69-
it.position.to(vertexBuffer, i * Vertex.size)
70-
it.normal.to(vertexBuffer, i * Vertex.size + Vec3.size)
71-
it.texCoords.to(vertexBuffer, i * Vertex.size + Vec3.size * 2)
48+
val vertexSize = 3 + 3 + 2
49+
val vertices = floatBufferBig(vertexSize * assimpMesh.mNumVertices)
50+
assimpMesh.mVertices.forEachIndexed { i, v ->
51+
val n = assimpMesh.mNormals[i]
52+
v.to(vertices, i * vertexSize)
53+
n.to(vertices, i * vertexSize + 3)
54+
if (assimpMesh.mTextureCoords[0].isNotEmpty()) {
55+
val tc = assimpMesh.mTextureCoords[0][i]
56+
vertices[i * vertexSize + 3 + 3] = tc[0]
57+
vertices[i * vertexSize + 3 + 3 + 1] = tc[1]
58+
}
7259
}
73-
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW)
60+
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW)
7461

7562
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Buffer.ELEMENT])
76-
val indexBuffer = byteBufferBig(indices.size)
77-
repeat(indices.size) { indexBuffer.putInt(it * Int.BYTES, indices[it])}
78-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_STATIC_DRAW)
63+
val indices = intBufferBig(assimpMesh.mNumFaces * 3)
64+
repeat(assimpMesh.mNumFaces * 3) { indices[it] = assimpMesh.mFaces[it / 3][it % 3] }
65+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW)
7966

8067
// Set the vertex attribute pointers
8168
// Vertex Positions
8269
glEnableVertexAttribArray(semantic.attr.POSITION)
83-
glVertexAttribPointer(semantic.attr.POSITION, Vec3.length, GL_FLOAT, false, Vertex.size, 0)
70+
glVertexAttribPointer(glf.pos3_nor3_tc2)
8471
// Vertex Normals
8572
glEnableVertexAttribArray(semantic.attr.NORMAL)
86-
glVertexAttribPointer(semantic.attr.NORMAL, Vec3.length, GL_FLOAT, false, Vertex.size, Vec3.size)
73+
glVertexAttribPointer(glf.pos3_nor3_tc2[1])
8774
// Vertex Texture Coords
8875
glEnableVertexAttribArray(semantic.attr.TEX_COORD)
89-
glVertexAttribPointer(semantic.attr.TEX_COORD, Vec2.length, GL_FLOAT, false, Vertex.size, Vec3.size * 2)
76+
glVertexAttribPointer(glf.pos3_nor3_tc2[2])
9077
// Vertex Tangent
9178
// glEnableVertexAttribArray(3)
9279
// glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) offsetof (Vertex, Tangent))
@@ -95,5 +82,7 @@ class Mesh(
9582
// glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) offsetof (Vertex, Bitangent))
9683

9784
glBindVertexArray()
85+
86+
9887
}
9988
}

‎src/main/kotlin/learnOpenGL/common/Model.kt

Copy file name to clipboardExpand all lines: src/main/kotlin/learnOpenGL/common/Model.kt
+8-94Lines changed: 8 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import assimp.AiPostProcessSteps.*
55
import glm.vec2.Vec2
66
import org.lwjgl.opengl.GL11.*
77
import org.lwjgl.opengl.GL30.glGenerateMipmap
8+
import uno.gln.glTexImage2D
89

910
/**
1011
* Created by GBarbieri on 02.05.2017.
1112
*/
1213

13-
class Model(path: String, val gammaCorrection: Boolean = false) {
14+
class Model(
15+
path: String,
16+
val program: Int,
17+
val diffuseUnit: Int? = null,
18+
val specularUnit: Int? = null,
19+
val gammaCorrection: Boolean = false) {
1420

1521
/* Model Data */
1622
val meshes = ArrayList<Mesh>()
17-
// Stores all the textures loaded so far, optimization to make sure textures aren't loaded more than once.
18-
var textures_loaded = ArrayList<String>()
1923

2024
/** Loads a model with supported ASSIMP extensions from file and stores the resulting meshes in the meshes vector. */
2125
init {
@@ -34,106 +38,16 @@ class Model(path: String, val gammaCorrection: Boolean = false) {
3438
//
3539
// // Process ASSIMP's root node recursively
3640
processNode(scene.mRootNode, scene)
37-
38-
// Load all Diffuse, Specular, Normal and Height maps
39-
4041
}
4142

4243
/** Processes a node in a recursive fashion. Processes each individual mesh located at the node and repeats this
4344
* process on its children nodes (if any). */
4445
fun processNode(node: AiNode, scene: AiScene) {
45-
4646
// Process each mesh located at the current node
4747
scene.mMeshes.forEach {
4848
// The node object only contains indices to index the actual objects in the scene.
4949
// The scene contains all the data, node is just to keep stuff organized (like relations between nodes).
50-
meshes += processMesh(it, scene)
50+
meshes += Mesh(it, scene)
5151
}
5252
}
53-
54-
fun processMesh(mesh: AiMesh, scene: AiScene): Mesh {
55-
56-
// Data to fill
57-
val vertices = ArrayList<Vertex>()
58-
val indices = ArrayList<Int>()
59-
val textures = ArrayList<Texture>()
60-
61-
// Walk through each of the mesh's vertices
62-
for (i in 0 until mesh.mNumVertices)
63-
64-
vertices += Vertex(
65-
position = mesh.mVertices[i],
66-
normal = mesh.mVertices[i],
67-
/* A vertex can contain up to 8 different texture coordinates. We thus make the assumption that we
68-
won't use models where a vertex can have multiple texture coordinates so we always take the
69-
first set (0). */
70-
texCoords =
71-
if (mesh.mTextureCoords.isNotEmpty())
72-
Vec2(mesh.mTextureCoords[0][i])
73-
else
74-
Vec2())
75-
// tangent = mesh.mTangents[i],
76-
// bitangent = mesh.mBitangents[i])
77-
78-
/* Now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding
79-
* vertex indices. */
80-
mesh.mFaces.forEach { indices += it } // Retrieve all indices of the face and store them in the indices vector
81-
82-
// Process materials
83-
if (mesh.mMaterialIndex >= 0) {
84-
85-
val material = scene.mMaterials[mesh.mMaterialIndex]
86-
87-
/* We assume a convention for sampler names in the shaders. Each diffuse texture should be named as
88-
'texture_diffuseN' where N is a sequential number ranging from 1 to MAX_SAMPLER_NUMBER.
89-
Same applies to other texture as the following list summarizes:
90-
Diffuse: texture_diffuseN
91-
Specular: texture_specularN
92-
Normal: texture_normalN */
93-
94-
// 1. Diffuse maps
95-
textures.addAll(loadMaterialTextures(scene, material, AiTexture.Type.diffuse, "texture_diffuse"))
96-
// 1. Specular maps
97-
// textures.addAll(loadMaterialTextures(scene, material, AiTexture.Type.specular, "texture_specular"))
98-
// // 1. Normal maps
99-
// textures.addAll(loadMaterialTextures(scene, material, AiTexture.Type.height, "texture_normal"))
100-
// // 1. Normal maps
101-
// textures.addAll(loadMaterialTextures(scene, material, AiTexture.Type.height, "texture_normal"))
102-
}
103-
104-
// Return a mesh object created from the extracted mesh data
105-
return Mesh(vertices, indices, textures)
106-
}
107-
108-
/**
109-
* Checks all material textures of a given type and loads the textures if they're not loaded yet.
110-
* The required info is returned as a Texture struct.
111-
*/
112-
fun loadMaterialTextures(scene: AiScene, mat: AiMaterial, type: AiTexture.Type, typeName: String) =
113-
114-
mat.textures.filter { it.type == type && !textures_loaded.contains(it.file) }.map {
115-
116-
val textureID = glGenTextures()
117-
118-
val gliTexture = scene.textures[it.file]!!
119-
120-
glBindTexture(GL_TEXTURE_2D, textureID)
121-
val format = gli.gl.translate(gliTexture.format, gliTexture.swizzles)
122-
// TODO gln
123-
glTexImage2D(GL_TEXTURE_2D, 0,
124-
format.internal.i,
125-
gliTexture.extent().x, gliTexture.extent().y, 0,
126-
format.external.i, format.type.i,
127-
gliTexture.data())
128-
glGenerateMipmap(GL_TEXTURE_2D)
129-
130-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
131-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
132-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
133-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
134-
135-
textures_loaded.add(it.file!!)
136-
137-
Texture(textureID, typeName, it.file!!)
138-
}
13953
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.