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 6cbabd2

Browse filesBrowse files
committed
working
1 parent 64da59d commit 6cbabd2
Copy full SHA for 6cbabd2

File tree

Expand file treeCollapse file tree

4 files changed

+143
-40
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+143
-40
lines changed

‎build.gradle

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

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

25-
compile 'com.github.kotlin-graphics:glm:4711d0f44930262d53f4c3b9a799edb4f6a4401d'
26-
compile 'com.github.kotlin-graphics:gli:4f94c22fac4a6fad4cf84a8d3a436b2ade10453c'
27-
compile 'com.github.kotlin-graphics:uno-sdk:3faa2cb28044564b80e8e2d2b4342a007142a083'
28-
compile 'com.github.java-graphics:assimp:ab7cf7e5a6ead86e3ef726e17b053164b3312f1c'
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'
2929

3030
// testCompile("io.kotlintest:kotlintest:2.0.0")
3131

+96Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package learnOpenGL.common
2+
3+
import glm.BYTES
4+
import glm.vec2.Vec2
5+
import glm.vec3.Vec3
6+
import org.lwjgl.opengl.GL15.*
7+
import org.lwjgl.opengl.GL30.glGenVertexArrays
8+
import uno.buffer.byteBufferBig
9+
import uno.buffer.intBufferBig
10+
import uno.gln.glBindBuffer
11+
import uno.gln.glBindVertexArray
12+
import uno.gln.intBuffer
13+
14+
/**
15+
* Created by GBarbieri on 02.05.2017.
16+
*/
17+
18+
class Vertex(
19+
// Position
20+
val position: Vec3,
21+
// Normal
22+
val normal: Vec3,
23+
// TexCoords
24+
val texCoords: Vec2 = Vec2()
25+
// Tangent
26+
// val tangent: Vec3,
27+
// // Bitangent
28+
// val bitangent: Vec3
29+
) {
30+
companion object {
31+
val size = 2 * Vec3.size + Vec2.size
32+
}
33+
}
34+
35+
class Texture(
36+
val id: Int,
37+
val type: String,
38+
val path: String)
39+
40+
class Mesh(
41+
val vertices: List<Vertex>,
42+
val indices: List<Int>,
43+
val textures: List<Texture>
44+
) {
45+
val vao = intBufferBig(1)
46+
47+
object Buffer {
48+
val VERTEX = 0
49+
val ELEMENT = 1
50+
val MAX = 2
51+
}
52+
53+
val buffers = intBufferBig(Buffer.MAX)
54+
55+
init { // Now that we have all the required data, set the vertex buffers and its attribute pointers.
56+
57+
// Create buffers/arrays
58+
glGenVertexArrays(vao)
59+
glGenBuffers(buffers)
60+
61+
glBindVertexArray(vao)
62+
// Load data into vertex buffers
63+
glBindBuffer(GL_ARRAY_BUFFER, buffers[Buffer.VERTEX])
64+
val vertexBuffer = byteBufferBig(Vertex.size * vertices.size)
65+
vertices.forEachIndexed { i, it ->
66+
it.position.to(vertexBuffer, i * Vertex.size)
67+
it.normal.to(vertexBuffer, i * Vertex.size + Vec3.size)
68+
it.texCoords.to(vertexBuffer, i * Vertex.size + Vec3.size * 2)
69+
}
70+
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW)
71+
72+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[Buffer.ELEMENT])
73+
val indexBuffer = byteBufferBig(indices.size)
74+
repeat(indices.size) { indexBuffer.putInt(it * Int.BYTES, indices[it])}
75+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_STATIC_DRAW)
76+
77+
// Set the vertex attribute pointers
78+
// Vertex Positions
79+
glEnableVertexAttribArray(0)
80+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)0)
81+
// Vertex Normals
82+
glEnableVertexAttribArray(1)
83+
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) offsetof (Vertex, Normal))
84+
// Vertex Texture Coords
85+
glEnableVertexAttribArray(2)
86+
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) offsetof (Vertex, TexCoords))
87+
// Vertex Tangent
88+
glEnableVertexAttribArray(3)
89+
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) offsetof (Vertex, Tangent))
90+
// Vertex Bitangent
91+
glEnableVertexAttribArray(4)
92+
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *) offsetof (Vertex, Bitangent))
93+
94+
glBindVertexArray(0)
95+
}
96+
}

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

Copy file name to clipboardExpand all lines: src/main/kotlin/learnOpenGL/common/Model.kt
+43-9Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package learnOpenGL.common
33
import assimp.*
44
import assimp.AiPostProcessSteps.*
55
import glm.vec2.Vec2
6+
import org.lwjgl.opengl.GL11.*
7+
import org.lwjgl.opengl.GL30.glGenerateMipmap
68

79
/**
810
* Created by GBarbieri on 02.05.2017.
@@ -11,7 +13,9 @@ import glm.vec2.Vec2
1113
class Model(path: String, val gammaCorrection: Boolean = false) {
1214

1315
/* Model Data */
14-
val meshes = ArrayList<mesh>()
16+
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>()
1519

1620
/** Loads a model with supported ASSIMP extensions from file and stores the resulting meshes in the meshes vector. */
1721
init {
@@ -43,11 +47,11 @@ class Model(path: String, val gammaCorrection: Boolean = false) {
4347
scene.mMeshes.forEach {
4448
// The node object only contains indices to index the actual objects in the scene.
4549
// The scene contains all the data, node is just to keep stuff organized (like relations between nodes).
46-
meshes +=
50+
meshes += processMesh(it, scene)
4751
}
4852
}
4953

50-
fun processMesh(mesh: AiMesh, scene: AiScene): mesh {
54+
fun processMesh(mesh: AiMesh, scene: AiScene): Mesh {
5155

5256
// Data to fill
5357
val vertices = ArrayList<Vertex>()
@@ -76,7 +80,7 @@ class Model(path: String, val gammaCorrection: Boolean = false) {
7680
mesh.mFaces.forEach { indices += it } // Retrieve all indices of the face and store them in the indices vector
7781

7882
// Process materials
79-
if(mesh.mMaterialIndex >= 0) {
83+
if (mesh.mMaterialIndex >= 0) {
8084

8185
val material = scene.mMaterials[mesh.mMaterialIndex]
8286

@@ -87,19 +91,49 @@ class Model(path: String, val gammaCorrection: Boolean = false) {
8791
Specular: texture_specularN
8892
Normal: texture_normalN */
8993

90-
// Load all Diffuse, Specular, Normal and Height maps
91-
textures.addAll(loadMaterialTextures(material, AiTexture.Type.diffuse, "texture_diffuse"))
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"))
92102
}
103+
104+
// Return a mesh object created from the extracted mesh data
105+
return Mesh(vertices, indices, textures)
93106
}
94107

95108
/**
96109
* Checks all material textures of a given type and loads the textures if they're not loaded yet.
97110
* The required info is returned as a Texture struct.
98111
*/
99-
fun loadMaterialTextures(mat: AiMaterial, type: AiTexture.Type, typeName: String): List<Texture> {
112+
fun loadMaterialTextures(scene: AiScene, mat: AiMaterial, type: AiTexture.Type, typeName: String) =
100113

101-
val textures = ArrayList<Texture>()
114+
mat.textures.filter { it.type == type && !textures_loaded.contains(it.file) }.map {
102115

116+
val textureID = glGenTextures()
103117

104-
}
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+
}
105139
}

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

Copy file name to clipboardExpand all lines: src/main/kotlin/learnOpenGL/common/mesh.kt
-27Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

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