@@ -26,8 +26,6 @@ import com.github.lookupgroup27.lookup.util.opengl.TextureManager
26
26
* @property name The name of the planet (e.g., "Earth"). Defaults to "Planet".
27
27
* @property position The planet's position in 3D space, represented as a float array [x, y, z].
28
28
* @property textureId The resource ID of the texture applied to the planet's surface.
29
- * @property vertexShaderCode The custom vertex shader code used for rendering the planet.
30
- * @property fragmentShaderCode The custom fragment shader code used for rendering the planet.
31
29
* @property scale The scaling factor applied to the planet's geometry. Defaults to 0.3.
32
30
*/
33
31
open class Planet (
@@ -38,6 +36,7 @@ open class Planet(
38
36
numBands : Int = SphereRenderer .DEFAULT_NUM_BANDS ,
39
37
stepsPerBand : Int = SphereRenderer .DEFAULT_STEPS_PER_BAND ,
40
38
private val scale : Float = 0.02f ,
39
+ private val rotationSpeed : Float = 50f ,
41
40
val funFact : String = " "
42
41
) : Object() {
43
42
@@ -52,7 +51,6 @@ open class Planet(
52
51
53
52
// New properties for rotation
54
53
private var rotationAngle: Float = 0f // Current rotation angle in degrees
55
- private val rotationSpeed: Float = 50f // Rotation speed in degrees per second (constant for all)
56
54
57
55
/* * Initializes the planet's geometry, shaders, and texture. */
58
56
init {
@@ -94,7 +92,10 @@ open class Planet(
94
92
fun draw (camera : Camera , transformMatrix : FloatArray? = null) {
95
93
label.draw(camera)
96
94
val modelMatrix = FloatArray (16 )
95
+ val billboardMatrix = FloatArray (16 )
96
+ val mvpMatrix = FloatArray (16 )
97
97
Matrix .setIdentityM(modelMatrix, 0 )
98
+ Matrix .setIdentityM(billboardMatrix, 0 )
98
99
99
100
// Apply the provided transform matrix or use the default planet position
100
101
if (transformMatrix != null ) {
@@ -104,19 +105,46 @@ open class Planet(
104
105
Matrix .scaleM(modelMatrix, 0 , scale, scale, scale)
105
106
}
106
107
107
- val viewMatrix = camera.viewMatrix
108
- val projMatrix = camera.projMatrix
109
- val mvpMatrix = FloatArray (16 )
108
+ // Extract camera look direction from view matrix
109
+ val lookX = - camera.viewMatrix[2 ] // Third column of view matrix
110
+ val lookY = - camera.viewMatrix[6 ] // is the look direction
111
+ val lookZ = - camera.viewMatrix[10 ]
112
+
113
+ // Create billboard rotation
114
+ val upX = camera.viewMatrix[1 ] // Second column is up vector
115
+ val upY = camera.viewMatrix[5 ]
116
+ val upZ = camera.viewMatrix[9 ]
117
+
118
+ // Calculate right vector (cross product)
119
+ val rightX = upY * lookZ - upZ * lookY
120
+ val rightY = upZ * lookX - upX * lookZ
121
+ val rightZ = upX * lookY - upY * lookX
122
+
123
+ // Set billboard matrix
124
+ fun setMatrixRow (matrix : FloatArray , startIndex : Int , x : Float , y : Float , z : Float , w : Float ) {
125
+ matrix[startIndex] = x
126
+ matrix[startIndex + 1 ] = y
127
+ matrix[startIndex + 2 ] = z
128
+ matrix[startIndex + 3 ] = w
129
+ }
130
+
131
+ // Set billboard matrix
132
+ setMatrixRow(billboardMatrix, 0 , rightX, rightY, rightZ, 0f )
133
+ setMatrixRow(billboardMatrix, 4 , upX, upY, upZ, 0f )
134
+ setMatrixRow(billboardMatrix, 8 , lookX, lookY, lookZ, 0f )
135
+ setMatrixRow(billboardMatrix, 12 , 0f , 0f , 0f , 1f )
110
136
111
137
// Combine matrices: Projection * View * Model
112
138
113
139
// Apply rotation transformation
114
- Matrix .rotateM(modelMatrix, 0 , rotationAngle, 0f , 1f , 0f ) // Rotate around the Y-axis
140
+ val rotatedMatrix = FloatArray (16 )
141
+ Matrix .rotateM(billboardMatrix, 0 , rotationAngle, 0f , 1f , 0f )
142
+ Matrix .multiplyMM(rotatedMatrix, 0 , modelMatrix, 0 , billboardMatrix, 0 )
115
143
116
144
// Combine model, view, and projection matrices in correct order
117
145
val viewModelMatrix = FloatArray (16 )
118
- Matrix .multiplyMM(viewModelMatrix, 0 , viewMatrix, 0 , modelMatrix , 0 )
119
- Matrix .multiplyMM(mvpMatrix, 0 , projMatrix, 0 , viewModelMatrix, 0 )
146
+ Matrix .multiplyMM(viewModelMatrix, 0 , camera. viewMatrix, 0 , rotatedMatrix , 0 )
147
+ Matrix .multiplyMM(mvpMatrix, 0 , camera. projMatrix, 0 , viewModelMatrix, 0 )
120
148
121
149
// Pass final MVP matrix to the renderer
122
150
sphereRenderer.bindShaderAttributes(mvpMatrix)
0 commit comments