├── .classpath
├── .gitignore
├── .project
├── README.md
├── dependencies
├── joml-1.5.0.jar
└── lwjgl 3
│ ├── doc
│ ├── 3rdparty
│ │ ├── glfw_license.txt
│ │ ├── jemalloc_license.txt
│ │ ├── khronos_license.txt
│ │ ├── libffi_license.txt
│ │ └── oculus_license.txt
│ ├── LICENSE.txt
│ └── javadoc.zip
│ ├── jar
│ └── lwjgl.jar
│ └── native
│ ├── OpenAL.dll
│ ├── OpenAL32.dll
│ ├── glfw.dll
│ ├── glfw32.dll
│ ├── jemalloc.dll
│ ├── jemalloc32.dll
│ ├── libglfw.dylib
│ ├── libglfw.so
│ ├── libglfw32.so
│ ├── libjemalloc.dylib
│ ├── libjemalloc.so
│ ├── libjemalloc32.so
│ ├── liblwjgl.dylib
│ ├── liblwjgl.so
│ ├── liblwjgl32.so
│ ├── libopenal.dylib
│ ├── libopenal.so
│ ├── libopenal32.so
│ ├── lwjgl.dll
│ └── lwjgl32.dll
├── resources
├── models
│ └── block.json
├── shaders
│ ├── fragment.fs
│ ├── rainbow.fs
│ └── vertex.vs
└── textures
│ ├── dirt.png
│ ├── grass.png
│ ├── grassblock - Copy.png
│ ├── grassblock.png
│ └── white.png
└── src
├── cm
└── cubestruct
│ ├── block
│ ├── Block.java
│ ├── DirtBlock.java
│ ├── EmptyBlock.java
│ └── GrassBlock.java
│ ├── engine
│ ├── GameEngine.java
│ ├── GameItem.java
│ ├── IGameLogic.java
│ ├── Timer.java
│ ├── Utils.java
│ ├── Window.java
│ └── render
│ │ ├── Camera.java
│ │ ├── Cube.java
│ │ ├── IRenderObject.java
│ │ ├── Mesh.java
│ │ ├── ShaderProgram.java
│ │ ├── Texture.java
│ │ └── Transformation.java
│ └── world
│ ├── BasicGen.java
│ ├── Chunk.java
│ ├── FastNoise.java
│ ├── Generator.java
│ ├── SimplexNoiseOctave.java
│ ├── SuperFlatGen.java
│ └── World.java
├── de
└── matthiasmann
│ └── twl
│ └── utils
│ └── PNGDecoder.java
└── org
└── cubestruct
└── game
├── CubeStruct.java
├── Main.java
└── Renderer.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Cubestruct
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Cubestruct
2 | A Minecraft Clone in LWJGL 3
3 |
4 | For help with LWJGL 3, check out https://lwjglgamedev.gitbooks.io/3d-game-development-with-lwjgl/content/.
5 | (We need to add the lighting directional light from here!)
6 |
7 | # Usage
8 |
9 | 1. Download Eclipse and the latest Java version
10 | 2. Create a folder to be the Eclipse workspace
11 | 2. Download and drag the Cubestruct folder into the workspace
12 | 3. Open Eclipse and select the location of your workspace
13 | 4. In the Eclipse navbar go to File>Import>General>Existing Projects into Workspace
14 | 5. Make sure LWJGL Lirbary paths are correct in configurations
15 | 6. ESC to close
16 |
17 | ###If on a mac:
18 |
19 | 1. Go to Run>Run Configurations>Java Application.
20 | 2. Select the create new and put org.cubestruct.game.Main as the main class.
21 | 3. Go to arguments and put `-XstartOnFirstThread` in the VM argument box.
22 | 4. Click Apply
23 | 5. Click Run
24 |
25 | #Features
26 |
27 | * ModderMe123 added rainbow blocks
28 |
29 | * Minecraft Camera Controls
30 |
--------------------------------------------------------------------------------
/dependencies/joml-1.5.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/joml-1.5.0.jar
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/doc/3rdparty/glfw_license.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2002-2006 Marcus Geelnard
2 | Copyright (c) 2006-2010 Camilla Berglund
3 |
4 | This software is provided 'as-is', without any express or implied
5 | warranty. In no event will the authors be held liable for any damages
6 | arising from the use of this software.
7 |
8 | Permission is granted to anyone to use this software for any purpose,
9 | including commercial applications, and to alter it and redistribute it
10 | freely, subject to the following restrictions:
11 |
12 | 1. The origin of this software must not be misrepresented; you must not
13 | claim that you wrote the original software. If you use this software
14 | in a product, an acknowledgment in the product documentation would
15 | be appreciated but is not required.
16 |
17 | 2. Altered source versions must be plainly marked as such, and must not
18 | be misrepresented as being the original software.
19 |
20 | 3. This notice may not be removed or altered from any source
21 | distribution.
22 |
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/doc/3rdparty/jemalloc_license.txt:
--------------------------------------------------------------------------------
1 | Copyright (C) 2002-2014 Jason Evans .
2 | All rights reserved.
3 | Copyright (C) 2007-2012 Mozilla Foundation. All rights reserved.
4 | Copyright (C) 2009-2014 Facebook, Inc. All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 | 1. Redistributions of source code must retain the above copyright notice(s),
9 | this list of conditions and the following disclaimer.
10 | 2. Redistributions in binary form must reproduce the above copyright notice(s),
11 | this list of conditions and the following disclaimer in the documentation
12 | and/or other materials provided with the distribution.
13 |
14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
15 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 | EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/doc/3rdparty/khronos_license.txt:
--------------------------------------------------------------------------------
1 | /*
2 | ** Copyright (c) 2013-2014 The Khronos Group Inc.
3 | **
4 | ** Permission is hereby granted, free of charge, to any person obtaining a
5 | ** copy of this software and/or associated documentation files (the
6 | ** "Materials"), to deal in the Materials without restriction, including
7 | ** without limitation the rights to use, copy, modify, merge, publish,
8 | ** distribute, sublicense, and/or sell copies of the Materials, and to
9 | ** permit persons to whom the Materials are furnished to do so, subject to
10 | ** the following conditions:
11 | **
12 | ** The above copyright notice and this permission notice shall be included
13 | ** in all copies or substantial portions of the Materials.
14 | **
15 | ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 | ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
22 | */
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/doc/3rdparty/libffi_license.txt:
--------------------------------------------------------------------------------
1 | libffi - Copyright (c) 1996-2014 Anthony Green, Red Hat, Inc and others.
2 | See source files for details.
3 |
4 | Permission is hereby granted, free of charge, to any person obtaining
5 | a copy of this software and associated documentation files (the
6 | ``Software''), to deal in the Software without restriction, including
7 | without limitation the rights to use, copy, modify, merge, publish,
8 | distribute, sublicense, and/or sell copies of the Software, and to
9 | permit persons to whom the Software is furnished to do so, subject to
10 | the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be
13 | included in all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/doc/3rdparty/oculus_license.txt:
--------------------------------------------------------------------------------
1 | Oculus VR, LLC Software Development Kit License Agreement
2 |
3 | Copyright © 2014 Oculus VR, LLC All rights reserved.
4 |
5 | The text of this may be found at: http://www.oculusvr.com/licenses/LICENSE-3.2
6 |
7 | Human-Readable Summary*:
8 |
9 | You are Free to:
10 |
11 | Use, modify, and distribute the Oculus VR Rift SDK in source and binary
12 | form with your applications/software.
13 |
14 | With the Following Restrictions:
15 |
16 | You can only distribute or re-distribute the source code to LibOVR in
17 | whole, not in part.
18 |
19 | Modifications to the Oculus VR Rift SDK in source or binary form must
20 | be shared with Oculus VR.
21 |
22 | If your applications cause health and safety issues, you may lose your
23 | right to use the Oculus VR Rift SDK, including LibOVR.
24 |
25 | The Oculus VR Rift SDK may not be used to interface with unapproved commercial
26 | virtual reality mobile or non-mobile products or hardware.
27 |
28 | * - This human-readable Summary is not a license. It is simply a convenient
29 | reference for understanding the full Oculus VR Rift SDK License Agreement.
30 | The Summary is written as a user-friendly interface to the full Oculus VR Rift
31 | SDK License below. This Summary itself has no legal value, and its contents do
32 | not appear in the actual license.
33 |
34 | Full-length Legal Copy:
35 |
36 | 1. Subject to the terms and conditions of this License Agreement (the "License"),
37 | Oculus VR, LLC ("Oculus VR") hereby grants to you a perpetual, worldwide,
38 | non-exclusive, no-charge, royalty-free, sublicenseable copyright license to use,
39 | reproduce, redistribute (subject to restrictions below), modify, and improve the
40 | software contained in this Oculus VR Rift Software Development Kit ("RIFT SDK"),
41 | including, but not limited to, the samples, headers, LibOVR headers, and LibOVR
42 | source. This license is subject to the following terms and conditions:
43 |
44 | 1.1. This license includes the non-exclusive license and right to use (i) the RIFT
45 | SDK to make applications, content, games and demos (collectively and generally
46 | referred to as "Developer Content") that run on the Oculus VR approved mobile hardware
47 | and software products ("Oculus Approved Rift Products") and which may incorporate
48 | the RIFT SDK in whole or in part in binary or object code; and (ii) to use the
49 | RIFT SDK to create derivative works of the RIFT SDK itself ("RIFT SDK Derivatives"),
50 | whether in source, binary, or object form, in whole or in part, including third
51 | party software unless otherwise noted.
52 |
53 | 1.2. RIFT SDK Derivatives are further defined as source, binary or object code
54 | derived exclusively from the RIFT SDK by you; provided, however, that RIFT SDK
55 | Derivatives do not include the Developer Content (engines, utilities, applications,
56 | content, games or demos) which may be developed using the RIFT SDK. By way of example
57 | a mobile application or game or demo that is developed using the RIFT SDK would not
58 | be a RIFT SDK Derivative , nor would a utility or tool set in a pre-existing game
59 | engine that is adapted to work with the RIFT SDK be a RIFT SDK Derivative.
60 | By way of example, but not limitation, a RIFT SDK Derivative is or would be: either (i)
61 | an adaptation of a utility or piece of code from the RIFT SDK to improve efficiency;
62 | or (ii) an addition of code or improvement to the RIFT SDK that adds functionality.
63 |
64 | 1.3 For the sake of clarification when you use the RIFT SDK (including RIFT SDK
65 | Derivatives) in or with Developer Content, you retain all rights to your Developer
66 | Content, and you have no obligations to share or license Developer Content (including
67 | your source and object code) to Oculus VR or any third parties; provided, however,
68 | Oculus VR retains all rights to the RIFT SDK and the RIFT SDK Derivatives that may
69 | be incorporated into your Developer Content.
70 |
71 | 1.4 You agree to and you will use the Flash Screen Warning and the Health and
72 | Safety Warnings (collectively the "Oculus Warnings") and the Oculus VR health and
73 | safety protocols found in the Oculus Best Practices Guide ("Oculus H&S Protocols"),
74 | and your use of the Oculus Warnings and the Oculus end user license agreement
75 | ("Oculus EULA") with your Developer Content as provided for in the Oculus Developer
76 | Center, all of which can be found at the following link:
77 | https://developer.oculusvr.com/?action=doc.
78 |
79 | 2. You, the recipient and user of the RIFT SDK, hereby agree and accept that that
80 | Oculus VR shall own all right, title and interest to the intellectual property
81 | rights, including, but limited to copyright, trademark and patent rights, to any
82 | RIFT SDK Derivatives that you may create, and you hereby assign any and all such
83 | rights to such RIFT SDK Derivatives to Oculus VR, subject to the following.
84 |
85 | 2.1 We hereby grant to you the a fully paid up, no-charge, royalty-free,
86 | world-wide, in perpetuity, non-exclusive right and license back to use these RIFT
87 | SDK Derivatives solely in conjunction with the RIFT SDK (or any components of the
88 | RIFT SDK) and/or Developer Content on Oculus Rift Products as set forth herein.
89 |
90 | 2.2 Furthermore, for the sake of clarification, Oculus VR and its assignees and
91 | licensees shall be free to use such RIFT SDK Derivatives without any approval
92 | from you and without compensation or attribution to you.
93 |
94 | 2.3 You also agree upon Oculus VR's request to provide the source and binary code
95 | of any RIFT SDK Derivatives to Oculus VR. FAILURE TO COMPLY WITH THIS REQUEST
96 | IS THE BASIS FOR AUTOMATIC TERMINATION OF THIS LICENSE BY OCULUS VR.
97 |
98 | 3. Subject to the terms and conditions of this License, your license to redistribute
99 | and sublicense the RIFT SDK and RIFT SDK Derivatives is also expressly made
100 | subject to the following conditions:
101 |
102 | 3.1. You may sublicense and redistribute the source, binary, or object code of
103 | the RIFT SDK in whole or in part by itself for no charge or as part of a for charge
104 | piece of Developer Content; provided, however, you may only license, sublicense
105 | or redistribute the source, binary or object code of LibOVR in whole, and you may
106 | not license, sublicense or redistribute any portion or element of LibOVR separately
107 | or in part (in either source, binary or object form). If you license, sublicense
108 | or redistribute RIFT SDK Derivatives in and of themselves (not as a part of a
109 | piece of Developer Content) then you may only do that solely with and in conjunction
110 | with either the RIFT SDK or LibOVR. The RIFT SDK (including, but not limited to
111 | LibOVR), any RIFT SDK Derivatives, and any Developer Content may only be used
112 | with Oculus Approved Rift Products and may not be used, licensed, or sublicensed
113 | to interface with mobile software or hardware or other commercial headsets,
114 | mobile tablets or phones that are not authorized and approved by Oculus VR;
115 |
116 | 3.2. You must include with all such redistributed or sublicensed RIFT SDK
117 | or RIFT SDK Derivatives code the following copyright notice:
118 | "Copyright © 2014 Oculus VR, LLC. All rights reserved," and include the
119 | list of conditions contained in this Section 3, including the full text of
120 | the disclaimer in Section 3.6 below;
121 |
122 | 3.3. Neither the name of Oculus VR, LLC nor the names of Oculus VR, LLC's
123 | contributors, licensors, employees, or contractors, may be used to endorse or promote
124 | products derived from this RIFT SDK without specific prior written permission
125 | of Oculus VR, LLC;
126 |
127 | 3.4. You must give any other recipients of the RIFT SDK or any elements thereof,
128 | including LibOVR or RIFT SDK Derivatives, a copy of this License as such recipients,
129 | licensees or sublicensees may only use the RIFT SDK or any RIFT SDK Derivatives
130 | or any elements thereof subject to the terms of this Licence and such recipients,
131 | licensees or sublicensees agreement and acceptance of this License with Oculus VR
132 | (which will convey all rights to the recipients’ or licensees’ or sublicensees’
133 | RIFT SDK Derivatives to Oculus VR), and you must cause any modified files to
134 | carry prominent notices stating that you changed the files;
135 |
136 | 3.5. If the RIFT SDK or a specific element thereof such as LibOVR includes a
137 | "LICENSE" text file as part of its distribution (the "License Notice"), then
138 | any RIFT SDK Derivatives that you distribute with the RIFT SDK in whole or in
139 | part must include a readable copy of such attribution notices as are contained
140 | within the applicable License Notice file (excluding those notices that do not
141 | pertain to any part of the RIFT SDK Derivatives), in at least one of the following
142 | places: within a License Notice text file distributed as part of the RIFT SDK
143 | Derivatives; within the source form or documentation, if provided along with
144 | the RIFT SDK Derivatives; or, within a display generated by the RIFT SDK Derivatives,
145 | if and wherever such third-party notices normally appear. You must also include
146 | in the License Notice file for all RIFT SDK Derivatives a copy of all notices
147 | (including any product liability or health and safety notices). The contents
148 | of the License Notice file are for informational purposes only and do not modify
149 | the License. You may add your own attribution notices within RIFT SDK Derivatives
150 | that you distribute, alongside or as an addendum to the License Notice text from
151 | the RIFT SDK or any part thereof, provided that such additional attribution notices
152 | cannot be construed as modifying the License.
153 |
154 | 3.6. THIS RIFT SDK AND ANY COMPONENT THEREOF IS PROVIDED BY OCULUS VR AND
155 | ITS CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
156 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
157 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OCULUS VR AS THE
158 | COPYRIGHT OWNER OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
159 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
160 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
161 | OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
162 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
163 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS RIFT
164 | SDK OR THE RIFT SDK DERIVATIVES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
165 |
166 | 4. This License does not grant permission to use the trade names, trademarks,
167 | service marks, or product names of Oculus VR, except as required for reasonable
168 | and customary use in describing the origin of the RIFT SDK, LibOVR, or any
169 | element thereof, and reproducing the content of the License Notice file.
170 | Oculus VR reserves all rights not expressly granted to you under this License.
171 |
172 | 5. In no event and under no legal theory, whether in tort (including negligence),
173 | contract, or otherwise, unless required by applicable law (such as
174 | deliberate and grossly negligent acts) or agreed to in writing, shall Oculus VR
175 | or any contributor be liable to you or your licensees or sublicensees for
176 | damages, including any direct, indirect, special, incidental, or consequential
177 | damages of any character arising as a result of this License or out of the use
178 | or inability to use the RIFT SDK, LibOVR, any element thereof or any RIFT SDK
179 | Derivatives (including but not limited to damages for loss of goodwill, work
180 | stoppage, computer failure or malfunction, or any and all other commercial
181 | damages or losses), even if you or such contributor has been advised of the
182 | possibility of such damages.
183 |
184 | 6. Your acceptance of the terms and conditions of this License in and of
185 | itself and for all Developer Content created as of March 1, 2014, may be
186 | evidenced by any of the following: your usage of the RIFT SDK or any element
187 | thereof, acceptance of the click-through agreement, or opening the packaging
188 | of the CD-ROM containing the RIFT SDK or any element thereof, including LibOVR.
189 | As this License is updated for future releases of the RIFT SDK and/or LibOVR,
190 | you agree to abide by and meet all requirements of future updates of this
191 | License for those future RIFT SDK releases as evidenced by the same usage of
192 | the RIFT SDK or any element thereof and the future updates of this License
193 | will apply for that future Developer Content that may developed for or with
194 | that future RIFT SDK or any element thereof (i.e., you cannot sidestep out
195 | of the requirements of future updates of the License by developing against
196 | an older release of the RIFT SDK or License).
197 |
198 | 7. Oculus VR reserves the right to terminate this License and all your
199 | rights hereunder in the event you materially breach this License and fail
200 | to cure such breach within ten (10) business days after notice of breach
201 | from Oculus VR.
202 |
203 | 8. Furthermore, Oculus VR also reserves the right to cancel or terminate
204 | this License for any of the following reasons upon notice to you, subject
205 | to the appeal process set forth in Section 14 for a wrongful termination:
206 |
207 | a) Intellectual property infringement by you with Developer Content
208 | or RIFT SDK Derivatives created by you that is used with or by the
209 | RIFT SDK or any part thereof, or any of the RIFT SDK Derivatives;
210 |
211 | b) Developer Content that violates or infringes upon applicable law;
212 |
213 | c) Health and safety issues associated with your Developer Content;
214 |
215 | d) Failure to comply with or use properly the Oculus Warnings,
216 | Oculus H&S Protocols, or Oculus EULA;
217 |
218 | e) Use of the RIFT SDK, RIFT SDK Derivatives or LibOVR with a
219 | commercial product other than an Oculus Approved Product; and
220 |
221 | f) Failure to provide required notices or deliver source code
222 | and/or binary of RIFT SDK Derivatives as set forth above.
223 |
224 | If you believe that you have been wrongfully terminated under this Section 8
225 | with respect to material breach or with respect to these above conditions,
226 | you have the right to appeal the termination of this License under Section 14.
227 |
228 | 9. This License may be amended by Oculus VR on a prospective basis, and your
229 | usage of the License after such amendments or changes signifies your consent
230 | to and acceptance of any such amendments or changes on a going forward basis.
231 |
232 | 10. In the event any provision of this License is determined to be invalid,
233 | prohibited or unenforceable by a court or other body of competent jurisdiction,
234 | this License shall be construed as if such invalid, prohibited or unenforceable
235 | provision has been more narrowly drawn so as not to be invalid, prohibited or
236 | unenforceable.
237 |
238 | 11. You may not assign any rights or obligations under this License without
239 | the advance written consent of Oculus VR, which may be withheld in its sole
240 | discretion. Oculus VR may assign its rights or obligations under this License
241 | in its sole discretion.
242 |
243 | 12. Failure of either party at any time to enforce any of the provisions of
244 | this License will not be construed as a waiver of such provisions or in any way
245 | affect the validity of this License or parts thereof.
246 |
247 | 13. Your remedies under this License shall be limited to the right to collect
248 | money damages, if any, and you hereby waive your right to injunctive or other
249 | equitable relief.
250 |
251 | 14. This License shall be governed by the laws of the State of California,
252 | without giving effect to choice of law principles. All disputes relating to
253 | this License shall be resolved by binding non-appearance-based arbitration
254 | before a neutral arbitrator in Orange County, California. If your License
255 | has been terminated hereunder by Oculus, you may appeal your termination
256 | through this arbitration process on an expedited basis with an arbitration
257 | within thirty days of your giving Oculus VR notice of the appeal. The
258 | arbitration shall be conducted in accordance with the rules and procedures
259 | of JAMS then in effect, and the judgment of the arbitrator shall be final
260 | and capable of entry in any court of competent jurisdiction. You agree
261 | to submit to the personal jurisdiction of the courts located within Orange
262 | County, California in connection with any entrance of an arbitrator’s judgment
263 | or decision or any dispute with respect to the arbitration process or procedure
264 | or Oculus VR’s exercise of its equitable rights or remedies.
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/doc/LICENSE.txt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012-2014 Lightweight Java Game Library Project
3 | * All rights reserved.
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions are
7 | * met:
8 | *
9 | * * Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | *
12 | * * Redistributions in binary form must reproduce the above copyright
13 | * notice, this list of conditions and the following disclaimer in the
14 | * documentation and/or other materials provided with the distribution.
15 | *
16 | * * Neither the name of 'Light Weight Java Game Library' nor the names of
17 | * its contributors may be used to endorse or promote products derived
18 | * from this software without specific prior written permission.
19 | *
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 | */
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/doc/javadoc.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/doc/javadoc.zip
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/jar/lwjgl.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/jar/lwjgl.jar
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/OpenAL.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/OpenAL.dll
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/OpenAL32.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/OpenAL32.dll
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/glfw.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/glfw.dll
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/glfw32.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/glfw32.dll
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/jemalloc.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/jemalloc.dll
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/jemalloc32.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/jemalloc32.dll
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/libglfw.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/libglfw.dylib
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/libglfw.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/libglfw.so
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/libglfw32.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/libglfw32.so
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/libjemalloc.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/libjemalloc.dylib
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/libjemalloc.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/libjemalloc.so
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/libjemalloc32.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/libjemalloc32.so
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/liblwjgl.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/liblwjgl.dylib
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/liblwjgl.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/liblwjgl.so
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/liblwjgl32.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/liblwjgl32.so
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/libopenal.dylib:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/libopenal.dylib
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/libopenal.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/libopenal.so
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/libopenal32.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/libopenal32.so
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/lwjgl.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/lwjgl.dll
--------------------------------------------------------------------------------
/dependencies/lwjgl 3/native/lwjgl32.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/dependencies/lwjgl 3/native/lwjgl32.dll
--------------------------------------------------------------------------------
/resources/models/block.json:
--------------------------------------------------------------------------------
1 | {
2 | "textures":[
3 | {"id":"0", "path":"grass.png"}
4 | ],
5 | "employees":[
6 | {"firstName":"John", "lastName":"Doe"},
7 | {"firstName":"Anna", "lastName":"Smith"},
8 | {"firstName":"Peter", "lastName":"Jones"}
9 | ]
10 | }
--------------------------------------------------------------------------------
/resources/shaders/fragment.fs:
--------------------------------------------------------------------------------
1 | #version 330
2 |
3 | in vec2 outTexCoord;
4 | out vec4 fragColor;
5 |
6 | uniform sampler2D texture_sampler;
7 |
8 | vec4 hsv_to_rgb(float h, float s, float v, float a)
9 | {
10 | float c = v * s;
11 | h = mod((h * 6.0), 6.0);
12 | float x = c * (1.0 - abs(mod(h, 2.0) - 1.0));
13 | vec4 color;
14 |
15 | if (0.0 <= h && h < 1.0) {
16 | color = vec4(c, x, 0.0, a);
17 | } else if (1.0 <= h && h < 2.0) {
18 | color = vec4(x, c, 0.0, a);
19 | } else if (2.0 <= h && h < 3.0) {
20 | color = vec4(0.0, c, x, a);
21 | } else if (3.0 <= h && h < 4.0) {
22 | color = vec4(0.0, x, c, a);
23 | } else if (4.0 <= h && h < 5.0) {
24 | color = vec4(x, 0.0, c, a);
25 | } else if (5.0 <= h && h < 6.0) {
26 | color = vec4(c, 0.0, x, a);
27 | } else {
28 | color = vec4(0.0, 0.0, 0.0, a);
29 | }
30 |
31 | color.rgb += v - c;
32 |
33 | return color;
34 | }
35 |
36 | void main()
37 | {
38 | fragColor = texture(texture_sampler, outTexCoord);
39 | }
40 |
--------------------------------------------------------------------------------
/resources/shaders/rainbow.fs:
--------------------------------------------------------------------------------
1 | #version 330
2 |
3 | in vec2 outTexCoord;
4 | out vec4 fragColor;
5 |
6 | uniform sampler2D texture_sampler;
7 |
8 | /*
9 | * GLSL HSV to RGB+A conversion. Useful for many effects and shader debugging.
10 | *
11 | * Copyright (c) 2012 Corey Tabaka
12 | *
13 | * Hue is in the range [0.0, 1.0] instead of degrees or radians.
14 | * Alpha is simply passed through for convenience.
15 | */
16 |
17 | vec4 hue_to_rgb(float h)
18 | {
19 | h = mod((h * 6.), 6.);
20 | float x = (1. - abs(mod(h, 2.) - 1.));
21 | vec4 color;
22 |
23 | if (0. <= h && h < 1.) {
24 | color = vec4(1., x, 0., 1.);
25 | } else if (1. <= h && h < 2.) {
26 | color = vec4(x, 1., 0., 1.);
27 | } else if (2. <= h && h < 3.) {
28 | color = vec4(0., 1., x, 1.);
29 | } else if (3. <= h && h < 4.) {
30 | color = vec4(0., x, 1., 1.);
31 | } else if (4. <= h && h < 5.) {
32 | color = vec4(x, 0., 1., 1.);
33 | } else if (5. <= h && h < 6.) {
34 | color = vec4(1., 0., x, 1.);
35 | } else {
36 | color = vec4(0., 0., 0., 1.);
37 | }
38 |
39 | return color;
40 | }
41 |
42 | void main()
43 | {
44 | fragColor = texture(texture_sampler, outTexCoord)+hue_to_rgb(outTexCoord.x-outTexCoord.y);
45 | }
46 |
--------------------------------------------------------------------------------
/resources/shaders/vertex.vs:
--------------------------------------------------------------------------------
1 | #version 330
2 |
3 | layout (location=0) in vec3 position;
4 | layout (location=1) in vec2 texCoord;
5 |
6 | out vec2 outTexCoord;
7 |
8 | uniform mat4 worldMatrix;
9 | uniform mat4 projectionMatrix;
10 |
11 | void main()
12 | {
13 | gl_Position = projectionMatrix * worldMatrix * vec4(position, 1.0);
14 | outTexCoord = texCoord;
15 | }
--------------------------------------------------------------------------------
/resources/textures/dirt.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/resources/textures/dirt.png
--------------------------------------------------------------------------------
/resources/textures/grass.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/resources/textures/grass.png
--------------------------------------------------------------------------------
/resources/textures/grassblock - Copy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/resources/textures/grassblock - Copy.png
--------------------------------------------------------------------------------
/resources/textures/grassblock.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/resources/textures/grassblock.png
--------------------------------------------------------------------------------
/resources/textures/white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CM-Tech/Cubestruct/41afb8891eab0123e9eba194339b719564f5c0b9/resources/textures/white.png
--------------------------------------------------------------------------------
/src/cm/cubestruct/block/Block.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.block;
2 |
3 | import org.joml.Vector3f;
4 |
5 | import cm.cubestruct.engine.GameItem;
6 | import cm.cubestruct.engine.render.Cube;
7 | import cm.cubestruct.engine.render.IRenderObject;
8 |
9 | public class Block {
10 | private final Vector3f position;
11 | public GameItem model;
12 | public Block(int bX,int bY,int bZ,String texture){
13 | position=new Vector3f(bX,bY,bZ);
14 | try {
15 | Cube c=new Cube(new Vector3f(1.0f), texture);
16 | c.setPosition(bX, bY, bZ);
17 | model=c;
18 | } catch (Exception e) {
19 | // TODO Auto-generated catch block
20 | e.printStackTrace();
21 | }
22 |
23 | }
24 | public Block(int bX,int bY,int bZ){
25 |
26 | this( bX, bY, bZ,"/textures/white.png");
27 |
28 |
29 | }
30 | public void render(){
31 | model.render();
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/block/DirtBlock.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.block;
2 |
3 | public class DirtBlock extends Block {
4 |
5 | public DirtBlock(int bX, int bY, int bZ) {
6 | super(bX, bY, bZ,"/textures/dirt.png");
7 |
8 | // TODO Auto-generated constructor stub
9 | }
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/block/EmptyBlock.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.block;
2 |
3 | public class EmptyBlock extends Block {
4 | public EmptyBlock(int bX, int bY, int bZ) {
5 | super(bX, bY, bZ);
6 |
7 | // TODO Auto-generated constructor stub
8 | }
9 | @Override
10 | public void render(){
11 |
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/block/GrassBlock.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.block;
2 |
3 | public class GrassBlock extends Block {
4 | public GrassBlock(int bX, int bY, int bZ) {
5 | super(bX, bY, bZ,"/textures/grass.png");
6 |
7 | // TODO Auto-generated constructor stub
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/engine/GameEngine.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.engine;
2 |
3 |
4 |
5 | public class GameEngine implements Runnable {
6 |
7 | public static final int TARGET_FPS = 75;
8 |
9 | public static final int TARGET_UPS = 30;
10 |
11 | private final Window window;
12 |
13 | private final Thread gameLoopThread;
14 |
15 | private final Timer timer;
16 |
17 | private final IGameLogic gameLogic;
18 |
19 | public GameEngine(String windowTitle, int width, int height, boolean vSync, IGameLogic gameLogic) throws Exception {
20 | gameLoopThread = new Thread(this, "GAME_LOOP_THREAD");
21 | window = new Window(windowTitle, width, height, vSync);
22 | this.gameLogic = gameLogic;
23 | timer = new Timer();
24 | }
25 |
26 | public void start() {
27 | String osName = System.getProperty("os.name");
28 | if ( osName.contains("Mac") ) {
29 | gameLoopThread.run();
30 | } else {
31 | gameLoopThread.start();
32 | }
33 | }
34 |
35 | @Override
36 | public void run() {
37 | try {
38 | init();
39 | gameLoop();
40 | } catch (Exception excp) {
41 | excp.printStackTrace();
42 | }finally{
43 | cleanup();
44 | }
45 | }
46 |
47 | protected void init() throws Exception {
48 | window.init();
49 | timer.init();
50 | gameLogic.init(window);
51 | }
52 |
53 | protected void gameLoop() {
54 | float ellapsedTime;
55 | float accumulator = 0f;
56 | float interval = 1f / TARGET_UPS;
57 |
58 | boolean running = true;
59 | while (running && !window.windowShouldClose()) {
60 | ellapsedTime = timer.getEllapsedTime();
61 | accumulator += ellapsedTime;
62 |
63 | input();
64 |
65 | while (accumulator >= interval) {
66 | update(interval);
67 | accumulator -= interval;
68 | }
69 |
70 | render();
71 |
72 | if (!window.isvSync()) {
73 | sync();
74 | }
75 | }
76 | }
77 |
78 | private void sync() {
79 | float loopSlot = 1f / TARGET_FPS;
80 | double endTime = timer.getLastLoopTime() + loopSlot;
81 | while (timer.getTime() < endTime) {
82 | try {
83 | Thread.sleep(1);
84 | } catch (InterruptedException ie) {
85 | }
86 | }
87 | }
88 |
89 | protected void input() {
90 | gameLogic.input();
91 | }
92 |
93 | protected void update(float interval) {
94 | gameLogic.update(interval);
95 | }
96 | protected void cleanup() {
97 | gameLogic.cleanup();
98 | }
99 | protected void render() {
100 | gameLogic.render();
101 | window.update();
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/engine/GameItem.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.engine;
2 |
3 | import org.joml.Vector3f;
4 | import org.lwjgl.opengl.GL11;
5 |
6 | import cm.cubestruct.engine.render.IRenderObject;
7 | import cm.cubestruct.engine.render.Mesh;
8 |
9 | public class GameItem implements IRenderObject {
10 |
11 | private final Mesh mesh;
12 |
13 | private final Vector3f position;
14 |
15 | private float scale;
16 |
17 | private final Vector3f rotation;
18 |
19 | public GameItem(Mesh mesh) {
20 | this.mesh = mesh;
21 | position = new Vector3f(0, 0, 0);
22 | scale = 1;
23 | rotation = new Vector3f(0, 0, 0);
24 | }
25 |
26 | public Vector3f getPosition() {
27 | return position;
28 | }
29 |
30 | public void setPosition(float x, float y, float z) {
31 | this.position.x = x;
32 | this.position.y = y;
33 | this.position.z = z;
34 | }
35 |
36 | public float getScale() {
37 | return scale;
38 | }
39 |
40 | public void setScale(float scale) {
41 | this.scale = scale;
42 | }
43 |
44 | public Vector3f getRotation() {
45 | return rotation;
46 | }
47 |
48 | public void setRotation(float x, float y, float z) {
49 | this.rotation.x = x;
50 | this.rotation.y = y;
51 | this.rotation.z = z;
52 | }
53 |
54 | public Mesh getMesh() {
55 | return mesh;
56 | }
57 |
58 | @Override
59 | public void render() {
60 | mesh.render();
61 |
62 | }
63 |
64 | @Override
65 | public int shouldRender() {
66 | // TODO Auto-generated method stub
67 | return GL11.GL_TRUE;
68 | }
69 |
70 | @Override
71 | public void cleanup() {
72 | mesh.cleanUp();
73 |
74 | }
75 | }
--------------------------------------------------------------------------------
/src/cm/cubestruct/engine/IGameLogic.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.engine;
2 |
3 | import cm.cubestruct.engine.Window;
4 |
5 | public interface IGameLogic {
6 |
7 | void init(Window window) throws Exception;
8 |
9 | void input();
10 |
11 | void keyPressed(int key);
12 |
13 | void update(float interval);
14 |
15 | void render();
16 | void cleanup();
17 |
18 | }
--------------------------------------------------------------------------------
/src/cm/cubestruct/engine/Timer.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.engine;
2 |
3 | public class Timer {
4 |
5 | private double lastLoopTime;
6 |
7 | public void init() {
8 | lastLoopTime = getTime();
9 | }
10 |
11 | public double getTime() {
12 | return System.nanoTime() / 1000_000_000.0;
13 | }
14 |
15 | public float getEllapsedTime() {
16 | double time = getTime();
17 | float ellapsedTime = (float) (time - lastLoopTime);
18 | lastLoopTime = time;
19 | return ellapsedTime;
20 | }
21 |
22 | public double getLastLoopTime() {
23 | return lastLoopTime;
24 | }
25 | }
--------------------------------------------------------------------------------
/src/cm/cubestruct/engine/Utils.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.engine;
2 |
3 | import java.io.InputStream;
4 | import java.util.Scanner;
5 |
6 | public class Utils {
7 |
8 | public static String loadResource(String fileName) throws Exception {
9 | String result = "";
10 | try (InputStream in = Utils.class.getClass().getResourceAsStream(fileName)) {
11 | result = new Scanner(in, "UTF-8").useDelimiter("\\A").next();
12 | }
13 | return result;
14 | }
15 |
16 | }
--------------------------------------------------------------------------------
/src/cm/cubestruct/engine/Window.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.engine;
2 |
3 | import static org.lwjgl.glfw.GLFW.*;
4 | import org.lwjgl.glfw.GLFW;
5 | import org.lwjgl.glfw.GLFWErrorCallback;
6 | import org.lwjgl.glfw.GLFWKeyCallback;
7 | import org.lwjgl.glfw.GLFWVidMode;
8 | import org.lwjgl.glfw.GLFWWindowSizeCallback;
9 | import org.lwjgl.opengl.GL;
10 | import org.lwjgl.opengl.GL11;
11 |
12 | import static org.lwjgl.opengl.GL11.GL_FALSE;
13 | import static org.lwjgl.opengl.GL11.GL_TRUE;
14 | import static org.lwjgl.opengl.GL11.glClearColor;
15 | import static org.lwjgl.system.MemoryUtil.NULL;
16 |
17 | import java.util.ArrayList;
18 |
19 | public class Window {
20 |
21 | private final String title;
22 |
23 | private int width;
24 |
25 | private int height;
26 |
27 | public long windowHandle;
28 |
29 | private GLFWErrorCallback errorCallback;
30 |
31 | private GLFWKeyCallback keyCallback;
32 |
33 | private GLFWWindowSizeCallback windowSizeCallback;
34 | public IGameLogic gl;
35 |
36 | private boolean resized;
37 |
38 | private boolean vSync;
39 | private ArrayList keysPressed = new ArrayList();
40 |
41 | public Window(String title, int width, int height, boolean vSync) {
42 | this.title = title;
43 | this.width = width;
44 | this.height = height;
45 | this.vSync = vSync;
46 | this.resized = false;
47 | }
48 |
49 | public void init() {
50 | // Setup an error callback. The default implementation
51 | // will print the error message in System.err.
52 | glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
53 |
54 | // Initialize GLFW. Most GLFW functions will not work before doing this.
55 | if (glfwInit() == GL_FALSE) {
56 | throw new IllegalStateException("Unable to initialize GLFW");
57 | }
58 |
59 | glfwDefaultWindowHints(); // optional, the current window hints are
60 | // already the default
61 | glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden
62 | // after creation
63 | glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable
64 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
65 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
66 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
67 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
68 |
69 | // Create the window
70 | windowHandle = glfwCreateWindow(width, height, title, NULL, NULL);
71 | if (windowHandle == NULL) {
72 | throw new RuntimeException("Failed to create the GLFW window");
73 | }
74 |
75 | // Setup resize callback
76 | glfwSetWindowSizeCallback(windowHandle, windowSizeCallback = new GLFWWindowSizeCallback() {
77 | @Override
78 | public void invoke(long window, int width, int height) {
79 | Window.this.width = width;
80 | Window.this.height = height;
81 | Window.this.setResized(true);
82 | }
83 | });
84 |
85 | // Setup a key callback. It will be called every time a key is pressed,
86 | // repeated or released.
87 | glfwSetKeyCallback(windowHandle, keyCallback = new GLFWKeyCallback() {
88 | @Override
89 | public void invoke(long window, int key, int scancode, int action, int mods) {
90 |
91 | if (action == GLFW_PRESS) {
92 | //System.out.println("pre");
93 |
94 | if (!Window.this.keysPressed.contains(new Integer(key))) {
95 | Window.this.keysPressed.add(new Integer(key));
96 | }
97 | if(Window.this.gl!=null){
98 | Window.this.gl.keyPressed(key);
99 | }
100 | }
101 | if (action == GLFW_REPEAT) {
102 | //System.out.println("rep");
103 | if (!Window.this.keysPressed.contains(new Integer(key))) {
104 | Window.this.keysPressed.add(new Integer(key));
105 | }
106 | if(Window.this.gl!=null){
107 | Window.this.gl.keyPressed(key);
108 | }
109 | }
110 | if (action == GLFW.GLFW_RELEASE) {
111 | System.out.println("rel");
112 | if (Window.this.keysPressed.contains(new Integer(key))) {
113 | Window.this.keysPressed.remove(new Integer(key));
114 | }
115 | }
116 | //System.out.println(Window.this.keysPressed);
117 | if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
118 | glfwSetWindowShouldClose(window, GL_TRUE);
119 | }
120 | if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
121 | glfwSetWindowShouldClose(window, GL_TRUE);
122 | }
123 | }
124 | });
125 |
126 | // Get the resolution of the primary monitor
127 | GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
128 | // Center our window
129 | glfwSetWindowPos(windowHandle, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
130 |
131 | // Make the OpenGL context current
132 | glfwMakeContextCurrent(windowHandle);
133 |
134 | if (isvSync()) {
135 | // Enable v-sync
136 | glfwSwapInterval(1);
137 | }
138 |
139 | // Make the window visible
140 | glfwShowWindow(windowHandle);
141 |
142 | GL.createCapabilities();
143 |
144 |
145 | // Set the clear color
146 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
147 | GL11.glEnable(GL11.GL_CULL_FACE);
148 | GL11.glCullFace(GL11.GL_BACK);
149 | }
150 |
151 | public ArrayList keysPressed() {
152 | /*int[] k = new int[this.keysPressed.size()];
153 | for (int i = 0; i < this.keysPressed.size(); i++) {
154 | k[i] = this.keysPressed.get(i);
155 | }*/
156 | return this.keysPressed;
157 | }
158 |
159 | public void setClearColor(float r, float g, float b, float alpha) {
160 | glClearColor(r, g, b, alpha);
161 | }
162 |
163 | public boolean isKeyPressed(int keyCode) {
164 | return glfwGetKey(windowHandle, keyCode) == GLFW_PRESS;
165 | }
166 |
167 | public boolean windowShouldClose() {
168 | return glfwWindowShouldClose(windowHandle) == GL_TRUE;
169 | }
170 |
171 | public String getTitle() {
172 | return title;
173 | }
174 |
175 | public int getWidth() {
176 | return width;
177 | }
178 |
179 | public int getHeight() {
180 | return height;
181 | }
182 |
183 | public boolean isResized() {
184 | return resized;
185 | }
186 |
187 | public void setResized(boolean resized) {
188 | this.resized = resized;
189 | }
190 |
191 | public boolean isvSync() {
192 | return vSync;
193 | }
194 |
195 | public void setvSync(boolean vSync) {
196 | this.vSync = vSync;
197 | }
198 |
199 | public void update() {
200 | glfwSwapBuffers(windowHandle);
201 | glfwPollEvents();
202 | }
203 | }
204 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/engine/render/Camera.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.engine.render;
2 |
3 | import org.joml.Vector3f;
4 |
5 | public class Camera {
6 | public Vector3f position;
7 |
8 |
9 | public Vector3f rotation;
10 | public Camera() {
11 | position = new Vector3f(0, 0, 0);
12 | //scale = 1;
13 | rotation = new Vector3f(0, 0, 0);
14 | // TODO Auto-generated constructor stub
15 | }
16 | public Vector3f getPosition() {
17 | return position;
18 | }
19 |
20 | public void setPosition(float x, float y, float z) {
21 | this.position.x = x;
22 | this.position.y = y;
23 | this.position.z = z;
24 | }
25 |
26 |
27 |
28 |
29 | public Vector3f getRotation() {
30 | return rotation;
31 | }
32 |
33 | public void setRotation(float x, float y, float z) {
34 | this.rotation.x = x;
35 | this.rotation.y = y;
36 | this.rotation.z = z;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/engine/render/Cube.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.engine.render;
2 |
3 | import org.joml.Vector3f;
4 | import org.lwjgl.opengl.GL11;
5 |
6 | import cm.cubestruct.engine.GameItem;
7 |
8 |
9 | public class Cube extends GameItem {
10 |
11 | private final Mesh mesh;
12 |
13 | private final Vector3f position;
14 |
15 | private float scale;
16 |
17 | private final Vector3f rotation;
18 |
19 | public Cube(Vector3f size,String texturePath) throws Exception {
20 | super(null);
21 | // Create the Mesh
22 | float[] positions = new float[] {
23 | // V0
24 | -0.5f, 0.5f, 0.5f,
25 | // V1
26 | -0.5f, -0.5f, 0.5f,
27 | // V2
28 | 0.5f, -0.5f, 0.5f,
29 | // V3
30 | 0.5f, 0.5f, 0.5f,
31 | // V4
32 | -0.5f, 0.5f, -0.5f,
33 | // V5
34 | 0.5f, 0.5f, -0.5f,
35 | // V6
36 | -0.5f, -0.5f, -0.5f,
37 | // V7
38 | 0.5f, -0.5f, -0.5f,
39 |
40 | // For text coords in top face
41 | // V8: V4 repeated
42 | -0.5f, 0.5f, -0.5f,
43 | // V9: V5 repeated
44 | 0.5f, 0.5f, -0.5f,
45 | // V10: V0 repeated
46 | -0.5f, 0.5f, 0.5f,
47 | // V11: V3 repeated
48 | 0.5f, 0.5f, 0.5f,
49 |
50 | // For text coords in right face
51 | // V12: V3 repeated
52 | 0.5f, 0.5f, 0.5f,
53 | // V13: V2 repeated
54 | 0.5f, -0.5f, 0.5f,
55 |
56 | // For text coords in left face
57 | // V14: V0 repeated
58 | -0.5f, 0.5f, 0.5f,
59 | // V15: V1 repeated
60 | -0.5f, -0.5f, 0.5f,
61 |
62 | // For text coords in bottom face
63 | // V16: V6 repeated
64 | -0.5f, -0.5f, -0.5f,
65 | // V17: V7 repeated
66 | 0.5f, -0.5f, -0.5f,
67 | // V18: V1 repeated
68 | -0.5f, -0.5f, 0.5f,
69 | // V19: V2 repeated
70 | 0.5f, -0.5f, 0.5f,
71 | };
72 | for(int i=0;i vboIdList;
21 |
22 | private final int vertexCount;
23 |
24 | private final Texture texture;
25 |
26 | public Mesh(float[] positions, float[] textCoords, int[] indices, Texture texture) {
27 | this.texture = texture;
28 | vertexCount = indices.length;
29 | vboIdList = new ArrayList();
30 |
31 | vaoId = glGenVertexArrays();
32 | glBindVertexArray(vaoId);
33 |
34 | // Position VBO
35 | int vboId = glGenBuffers();
36 | vboIdList.add(vboId);
37 | FloatBuffer posBuffer = BufferUtils.createFloatBuffer(positions.length);
38 | posBuffer.put(positions).flip();
39 | glBindBuffer(GL_ARRAY_BUFFER, vboId);
40 | glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW);
41 | glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
42 |
43 | // Texture coordinates VBO
44 | vboId = glGenBuffers();
45 | vboIdList.add(vboId);
46 | FloatBuffer textCoordsBuffer = BufferUtils.createFloatBuffer(textCoords.length);
47 | textCoordsBuffer.put(textCoords).flip();
48 | glBindBuffer(GL_ARRAY_BUFFER, vboId);
49 | glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW);
50 | glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
51 |
52 | // Index VBO
53 | vboId = glGenBuffers();
54 | vboIdList.add(vboId);
55 | IntBuffer indicesBuffer = BufferUtils.createIntBuffer(indices.length);
56 | indicesBuffer.put(indices).flip();
57 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboId);
58 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
59 |
60 | glBindBuffer(GL_ARRAY_BUFFER, 0);
61 | glBindVertexArray(0);
62 | }
63 |
64 | public int getVaoId() {
65 | return vaoId;
66 | }
67 |
68 | public int getVertexCount() {
69 | return vertexCount;
70 | }
71 |
72 | public void render() {
73 | // Activate firs texture bank
74 | glActiveTexture(GL_TEXTURE0);
75 | // Bind the texture
76 | glBindTexture(GL_TEXTURE_2D, texture.getId());
77 |
78 | // Draw the mesh
79 | glBindVertexArray(getVaoId());
80 | glEnableVertexAttribArray(0);
81 | glEnableVertexAttribArray(1);
82 | //GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
83 | //GL11.glCullFace(GL11.GL_FRONT_AND_BACK);
84 | //GL11.glPolygonMode(GL11.GL_BACK, GL11.GL_POINT);
85 | glEnable(GL_DEPTH_TEST);
86 | glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0);
87 |
88 |
89 | // Restore state
90 | glDisableVertexAttribArray(0);
91 | glDisableVertexAttribArray(1);
92 | glBindVertexArray(0);
93 | }
94 |
95 | public void cleanUp() {
96 | glDisableVertexAttribArray(0);
97 |
98 | // Delete the VBOs
99 | glBindBuffer(GL_ARRAY_BUFFER, 0);
100 | for (int vboId : vboIdList) {
101 | glDeleteBuffers(vboId);
102 | }
103 |
104 | // Delete the texture
105 | texture.cleanup();
106 |
107 | // Delete the VAO
108 | glBindVertexArray(0);
109 | glDeleteVertexArrays(vaoId);
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/engine/render/ShaderProgram.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.engine.render;
2 |
3 | import java.nio.FloatBuffer;
4 | import java.util.HashMap;
5 | import java.util.Map;
6 | import org.joml.Matrix4f;
7 | import org.lwjgl.BufferUtils;
8 | import org.lwjgl.opengl.GL11;
9 |
10 | import static org.lwjgl.opengl.GL20.*;
11 |
12 | public class ShaderProgram {
13 |
14 | private final int programId;
15 |
16 | private int vertexShaderId;
17 |
18 | private int fragmentShaderId;
19 |
20 | private final Map uniforms;
21 |
22 | public ShaderProgram() throws Exception {
23 | programId = glCreateProgram();
24 | if (programId == 0) {
25 | throw new Exception("Could not create Shader");
26 | }
27 | uniforms = new HashMap<>();
28 | }
29 |
30 | public void createUniform(String uniformName) throws Exception {
31 | int uniformLocation = glGetUniformLocation(programId, uniformName);
32 | if (uniformLocation < 0) {
33 | throw new Exception ("Could not find uniform:" + uniformName);
34 | }
35 | uniforms.put(uniformName, uniformLocation);
36 | }
37 |
38 | public void setUniform(String uniformName, Matrix4f value) {
39 | // Dump the matrix into a float buffer
40 | FloatBuffer fb = BufferUtils.createFloatBuffer(16);
41 | value.get(fb);
42 | glUniformMatrix4fv(uniforms.get(uniformName), false, fb);
43 |
44 | }
45 |
46 | public void setUniform(String uniformName, int value) {
47 | glUniform1i(uniforms.get(uniformName), value);
48 | }
49 |
50 | public void createVertexShader(String shaderCode) throws Exception {
51 | vertexShaderId = createShader(shaderCode, GL_VERTEX_SHADER);
52 | }
53 |
54 | public void createFragmentShader(String shaderCode) throws Exception {
55 | fragmentShaderId = createShader(shaderCode, GL_FRAGMENT_SHADER);
56 | }
57 |
58 | protected int createShader(String shaderCode, int shaderType) throws Exception {
59 | int shaderId = glCreateShader(shaderType);
60 | if (shaderId == 0) {
61 | throw new Exception("Error creating shader. Code: " + shaderId);
62 | }
63 |
64 | glShaderSource(shaderId, shaderCode);
65 | glCompileShader(shaderId);
66 |
67 | if (glGetShaderi(shaderId, GL_COMPILE_STATUS) == 0) {
68 | throw new Exception("Error compiling Shader code: " + glGetShaderInfoLog(shaderId, 1024));
69 | }
70 |
71 | glAttachShader(programId, shaderId);
72 |
73 | return shaderId;
74 | }
75 |
76 | public void link() throws Exception {
77 | glLinkProgram(programId);
78 | if (glGetProgrami(programId, GL_LINK_STATUS) == 0) {
79 | throw new Exception("Error linking Shader code: " + glGetShaderInfoLog(programId, 1024));
80 | }
81 |
82 | glValidateProgram(programId);
83 | if (glGetProgrami(programId, GL_VALIDATE_STATUS) == 0) {
84 | System.err.println("Warning validating Shader code: " + glGetShaderInfoLog(programId, 1024));
85 | }
86 |
87 | }
88 |
89 | public void bind() {
90 | glUseProgram(programId);
91 | }
92 |
93 | public void unbind() {
94 | glUseProgram(0);
95 | }
96 |
97 | public void cleanup() {
98 | unbind();
99 | if (programId != 0) {
100 | if (vertexShaderId != 0) {
101 | glDetachShader(programId, vertexShaderId);
102 | }
103 | if (fragmentShaderId != 0) {
104 | glDetachShader(programId, fragmentShaderId);
105 | }
106 | glDeleteProgram(programId);
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/engine/render/Texture.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.engine.render;
2 |
3 | import de.matthiasmann.twl.utils.PNGDecoder;
4 | import de.matthiasmann.twl.utils.PNGDecoder.Format;
5 | import java.nio.ByteBuffer;
6 | import static org.lwjgl.opengl.GL11.*;
7 | import static org.lwjgl.opengl.GL30.glGenerateMipmap;
8 |
9 | public class Texture {
10 |
11 | private final int id;
12 |
13 | public Texture(String fileName) throws Exception {
14 | this(loadTexture(fileName));
15 | }
16 |
17 | public Texture(int id) {
18 | this.id = id;
19 | }
20 |
21 | public void bind() {
22 | glBindTexture(GL_TEXTURE_2D, id);
23 | }
24 |
25 | public int getId() {
26 | return id;
27 | }
28 |
29 | private static int loadTexture(String fileName) throws Exception {
30 | // Load Texture file
31 | PNGDecoder decoder = new PNGDecoder(Texture.class.getResourceAsStream(fileName));
32 |
33 | // Load texture contents into a byte buffer
34 | ByteBuffer buf = ByteBuffer.allocateDirect(
35 | 4 * decoder.getWidth() * decoder.getHeight());
36 | decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
37 | buf.flip();
38 |
39 | // Create a new OpenGL texture
40 | int textureId = glGenTextures();
41 | // Bind the texture
42 | glBindTexture(GL_TEXTURE_2D, textureId);
43 |
44 | // Tell OpenGL how to unpack the RGBA bytes. Each component is 1 byte size
45 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
46 |
47 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
48 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
49 |
50 | // Upload the texture data
51 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0,
52 | GL_RGBA, GL_UNSIGNED_BYTE, buf);
53 | // Generate Mip Map
54 | glGenerateMipmap(GL_TEXTURE_2D);
55 | return textureId;
56 | }
57 |
58 | public void cleanup() {
59 | glDeleteTextures(id);
60 | }
61 | }
--------------------------------------------------------------------------------
/src/cm/cubestruct/engine/render/Transformation.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.engine.render;
2 |
3 | import org.joml.Matrix4f;
4 | import org.joml.Vector3f;
5 |
6 | public class Transformation {
7 |
8 | private final Matrix4f projectionMatrix;
9 |
10 | private final Matrix4f worldMatrix;
11 |
12 | public Transformation() {
13 | worldMatrix = new Matrix4f();
14 | projectionMatrix = new Matrix4f();
15 | }
16 | public final Matrix4f getCameraProjectionMatrix(float fov, float width, float height, float zNear, float zFar,Camera c) {
17 | float aspectRatio = width / height;
18 | projectionMatrix.identity();
19 | projectionMatrix.transform(c.getPosition());
20 | projectionMatrix.rotateY(c.getRotation().y);
21 | projectionMatrix.rotateX(c.getRotation().x);
22 | projectionMatrix.perspective(fov, aspectRatio, zNear, zFar);
23 |
24 | return projectionMatrix;
25 | }
26 | public final Matrix4f getProjectionMatrix(float fov, float width, float height, float zNear, float zFar) {
27 | float aspectRatio = width / height;
28 | projectionMatrix.identity();
29 | projectionMatrix.perspective(fov, aspectRatio, zNear, zFar);
30 |
31 | return projectionMatrix;
32 | }
33 | public Matrix4f getWorldMatrix(Vector3f offset, Vector3f rotation, float scale) {
34 | worldMatrix.identity().translate(offset).
35 | rotateX((float)Math.toRadians(rotation.x)).
36 | rotateY((float)Math.toRadians(rotation.y)).
37 | rotateZ((float)Math.toRadians(rotation.z)).
38 | scale(scale);
39 | return worldMatrix;
40 | }
41 | public Matrix4f getWorldMatrix(Vector3f offset, Vector3f rotation, float scale,Camera c) {
42 | worldMatrix.identity().rotateX(-(float)Math.toRadians(c.rotation.x)).
43 | rotateY(-(float)Math.toRadians(c.rotation.y)).
44 | translate(new Vector3f(c.getPosition().x,-c.getPosition().y,c.getPosition().z)).
45 | translate(offset).
46 | rotateX((float)Math.toRadians(rotation.x)).
47 | rotateY((float)Math.toRadians(rotation.y)).
48 | rotateZ((float)Math.toRadians(rotation.z)).
49 | scale(scale);
50 | return worldMatrix;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/world/BasicGen.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.world;
2 |
3 | import cm.cubestruct.block.Block;
4 | import cm.cubestruct.block.DirtBlock;
5 | import cm.cubestruct.block.EmptyBlock;
6 | import cm.cubestruct.block.GrassBlock;
7 |
8 | public class BasicGen implements Generator {
9 | public SimplexNoiseOctave simp;
10 | public BasicGen(int seed){
11 | simp=new SimplexNoiseOctave(seed);
12 | }
13 | @Override
14 | public Block genBlock(int wX, int wY,int wZ, int seed) {
15 | int height=(int) (simp.noise(wX/80.0,wZ/80.0)*5.0+10.0);
16 | Block b=wY>height+1?null:(wY>height?new GrassBlock(wX, wY,wZ):new DirtBlock(wX,wY,wZ));//new EmptyBlock(wX, wY,wZ):(wY>5?new GrassBlock(wX, wY,wZ):new DirtBlock(wX,wY,wZ));
17 | return b;
18 | }
19 | @Override
20 | public int maxHeight(int wX, int wZ, int seed) {
21 | // TODO Auto-generated method stub
22 | return (int) (simp.noise(wX/80.0,wZ/80.0)*5.0+10.0)+2;
23 | }
24 |
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/world/Chunk.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.world;
2 |
3 | import java.util.ArrayList;
4 |
5 | import org.joml.Vector3f;
6 |
7 | import cm.cubestruct.block.Block;
8 | import cm.cubestruct.engine.GameItem;
9 | import cm.cubestruct.engine.render.Cube;
10 | import cm.cubestruct.engine.render.IRenderObject;
11 |
12 | public class Chunk {
13 | public Block[][][] blocks;
14 | int cx = 0;
15 | int cz = 0;
16 | Generator g;
17 | private int genHeight=0;
18 | private boolean genFin = false;
19 |
20 | public Chunk(int cx, int cz, Generator g) {
21 | this.g = g;
22 | blocks = new Block[16][World.WORLD_HEIGHT][16];
23 | this.cx = cx;
24 | this.cz = cz;
25 | int wX = cx * 16;
26 | int wZ = cz * 16;
27 |
28 | for (int x = 0; x < 16; x++) {
29 | for (int z = 0; z < 16; z++) {
30 | boolean dirt=false;
31 | int mHeight= g.maxHeight(wX+x, wZ+z, 0);
32 | //System.out.println(mHeight);
33 | for (int y = 0; y renderItems() {
98 | ArrayList c = new ArrayList();
99 | //if (genFin) {
100 | for (int y = 0; y < World.WORLD_HEIGHT; y++) {
101 | for (int x = 0; x < 16; x++) {
102 | for (int z = 0; z < 16; z++) {
103 | if (blocks[x][y][z] != null) {
104 | c.add(blocks[x][y][z].model);
105 | }
106 | }
107 | }
108 | }
109 | //}
110 | return c;
111 | }
112 |
113 | public void cleanup() {
114 | for (int y = 0; y < World.WORLD_HEIGHT; y++) {
115 | for (int x = 0; x < 16; x++) {
116 | for (int z = 0; z < 16; z++) {
117 | if (blocks[x][y][z] != null) {
118 | blocks[x][y][z].model.cleanup();
119 | }
120 | }
121 | }
122 | }
123 |
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/world/FastNoise.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.world;
2 |
3 | /*
4 | * This file is part of 3DzzD http://dzzd.net/.
5 | *
6 | * Released under LGPL
7 | *
8 | * 3DzzD is free software: you can redistribute it and/or modify
9 | * it under the terms of the GNU Lesser General Public License as published by
10 | * the Free Software Foundation, either version 3 of the License, or
11 | * (at your option) any later version.
12 | *
13 | * 3DzzD is distributed in the hope that it will be useful,
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | * GNU Lesser General Public License for more details.
17 | *
18 | * You should have received a copy of the GNU Lesser General Public License
19 | * along with 3DzzD. If not, see .
20 | *
21 | * Copyright 2005 - 20010 Bruno Augier
22 | */
23 |
24 |
25 | /**
26 | * Fast perlin noise generation
27 | *
28 | * Generate a perlin noise with 8 octave and a persistence of 0.5
29 | *
30 | * NB:
31 | * - output range between 0 and 255
32 | * - maximum octave = 7
33 | *
34 | * you can change type of noise between grad & value noise by commenting/uncommenting block
35 | * you can change type of interpolation between bicubic/bilinear by commenting/uncommenting block
36 | */
37 | public class FastNoise
38 | {
39 | public static int noise(double x,double y,int nbOctave)
40 | {
41 | int result=0;
42 | int frequence256=256;
43 | int sx=(int)((x)*frequence256);
44 | int sy=(int)((y)*frequence256);
45 | int octave=nbOctave;
46 | while(octave!=0)
47 | {
48 | int bX=sx&0xFF;
49 | int bY=sy&0xFF;
50 |
51 | int sxp=sx>>8;
52 | int syp=sy>>8;
53 |
54 |
55 | //Compute noise for each corner of current cell
56 | int Y1376312589_00=syp*1376312589;
57 | int Y1376312589_01=Y1376312589_00+1376312589;
58 |
59 | int XY1376312589_00=sxp+Y1376312589_00;
60 | int XY1376312589_10=XY1376312589_00+1;
61 | int XY1376312589_01=sxp+Y1376312589_01;
62 | int XY1376312589_11=XY1376312589_01+1;
63 |
64 | int XYBASE_00=(XY1376312589_00<<13)^XY1376312589_00;
65 | int XYBASE_10=(XY1376312589_10<<13)^XY1376312589_10;
66 | int XYBASE_01=(XY1376312589_01<<13)^XY1376312589_01;
67 | int XYBASE_11=(XY1376312589_11<<13)^XY1376312589_11;
68 |
69 | int alt1=(XYBASE_00 * (XYBASE_00 * XYBASE_00 * 15731 + 789221) + 1376312589) ;
70 | int alt2=(XYBASE_10 * (XYBASE_10 * XYBASE_10 * 15731 + 789221) + 1376312589) ;
71 | int alt3=(XYBASE_01 * (XYBASE_01 * XYBASE_01 * 15731 + 789221) + 1376312589) ;
72 | int alt4=(XYBASE_11 * (XYBASE_11 * XYBASE_11 * 15731 + 789221) + 1376312589) ;
73 |
74 | /*
75 | *NOTE : on for true grandiant noise uncomment following block
76 | * for true gradiant we need to perform scalar product here, gradiant vector are created/deducted using
77 | * the above pseudo random values (alt1...alt4) : by cutting thoses values in twice values to get for each a fixed x,y vector
78 | * gradX1= alt1&0xFF
79 | * gradY1= (alt1&0xFF00)>>8
80 | *
81 | * the last part of the PRN (alt1&0xFF0000)>>8 is used as an offset to correct one of the gradiant problem wich is zero on cell edge
82 | *
83 | * source vector (sXN;sYN) for scalar product are computed using (bX,bY)
84 | *
85 | * each four values must be replaced by the result of the following
86 | * altN=(gradXN;gradYN) scalar (sXN;sYN)
87 | *
88 | * all the rest of the code (interpolation+accumulation) is identical for value & gradiant noise
89 | */
90 |
91 |
92 | /*START BLOCK FOR TRUE GRADIANT NOISE*/
93 |
94 | int grad1X=(alt1&0xFF)-128;
95 | int grad1Y=((alt1>>8)&0xFF)-128;
96 | int grad2X=(alt2&0xFF)-128;
97 | int grad2Y=((alt2>>8)&0xFF)-128;
98 | int grad3X=(alt3&0xFF)-128;
99 | int grad3Y=((alt3>>8)&0xFF)-128;
100 | int grad4X=(alt4&0xFF)-128;
101 | int grad4Y=((alt4>>8)&0xFF)-128;
102 |
103 |
104 | int sX1=bX>>1;
105 | int sY1=bY>>1;
106 | int sX2=128-sX1;
107 | int sY2=sY1;
108 | int sX3=sX1;
109 | int sY3=128-sY1;
110 | int sX4=128-sX1;
111 | int sY4=128-sY1;
112 | alt1=(grad1X*sX1+grad1Y*sY1)+16384+((alt1&0xFF0000)>>9); //to avoid seams to be 0 we use an offset
113 | alt2=(grad2X*sX2+grad2Y*sY2)+16384+((alt2&0xFF0000)>>9);
114 | alt3=(grad3X*sX3+grad3Y*sY3)+16384+((alt3&0xFF0000)>>9);
115 | alt4=(grad4X*sX4+grad4Y*sY4)+16384+((alt4&0xFF0000)>>9);
116 |
117 | /*END BLOCK FOR TRUE GRADIANT NOISE */
118 |
119 |
120 | /*START BLOCK FOR VALUE NOISE*/
121 | /*
122 | alt1&=0xFFFF;
123 | alt2&=0xFFFF;
124 | alt3&=0xFFFF;
125 | alt4&=0xFFFF;
126 | */
127 | /*END BLOCK FOR VALUE NOISE*/
128 |
129 |
130 | /*START BLOCK FOR LINEAR INTERPOLATION*/
131 | //BiLinear interpolation
132 | /*
133 | int f24=(bX*bY)>>8;
134 | int f23=bX-f24;
135 | int f14=bY-f24;
136 | int f13=256-f14-f23-f24;
137 |
138 | int val=(alt1*f13+alt2*f23+alt3*f14+alt4*f24);
139 | */
140 | /*END BLOCK FOR LINEAR INTERPOLATION*/
141 |
142 |
143 |
144 | //BiCubic interpolation ( in the form alt(bX) = alt[n] - (3*bX^2 - 2*bX^3) * (alt[n] - alt[n+1]) )
145 | /*START BLOCK FOR BICUBIC INTERPOLATION*/
146 | int bX2=(bX*bX)>>8;
147 | int bX3=(bX2*bX)>>8;
148 | int _3bX2=3*bX2;
149 | int _2bX3=2*bX3;
150 | int alt12= alt1 - (((_3bX2 - _2bX3) * (alt1-alt2)) >> 8);
151 | int alt34= alt3 - (((_3bX2 - _2bX3) * (alt3-alt4)) >> 8);
152 |
153 |
154 | int bY2=(bY*bY)>>8;
155 | int bY3=(bY2*bY)>>8;
156 | int _3bY2=3*bY2;
157 | int _2bY3=2*bY3;
158 | int val= alt12 - (((_3bY2 - _2bY3) * (alt12-alt34)) >> 8);
159 |
160 | val*=256;
161 | /*END BLOCK FOR BICUBIC INTERPOLATION*/
162 |
163 |
164 | //Accumulate in result
165 | result+=(val<>>(16+nbOctave+1);
173 | }
174 | }
--------------------------------------------------------------------------------
/src/cm/cubestruct/world/Generator.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.world;
2 |
3 | import cm.cubestruct.block.Block;
4 |
5 | public interface Generator {
6 | public Block genBlock(int wX,int wY,int wZ,int seed);
7 | public int maxHeight(int wX,int wZ,int seed);
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/world/SimplexNoiseOctave.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.world;
2 |
3 | import java.util.Random;
4 |
5 | /*
6 | * A speed-improved simplex noise algorithm for 2D, 3D and 4D in Java.
7 | *
8 | * Based on example code by Stefan Gustavson (stegu@itn.liu.se).
9 | * Optimisations by Peter Eastman (peastman@drizzle.stanford.edu).
10 | * Better rank ordering method by Stefan Gustavson in 2012.
11 | *
12 | * This could be speeded up even further, but it's useful as it is.
13 | *
14 | * Version 2012-03-09
15 | *
16 | * This code was placed in the public domain by its original author,
17 | * Stefan Gustavson. You may use it as you see fit, but
18 | * attribution is appreciated.
19 | *
20 | */
21 |
22 | public class SimplexNoiseOctave { // Simplex noise in 2D, 3D and 4D
23 |
24 | // Skewing and unskewing factors for 2, 3, and 4 dimensions
25 | private static final double F2 = 0.5 * (Math.sqrt(3.0) - 1.0);
26 | private static final double G2 = (3.0 - Math.sqrt(3.0)) / 6.0;
27 | private static final double F3 = 1.0 / 3.0;
28 | private static final double G3 = 1.0 / 6.0;
29 | private static final double F4 = (Math.sqrt(5.0) - 1.0) / 4.0;
30 | private static final double G4 = (5.0 - Math.sqrt(5.0)) / 20.0;
31 | public static int RANDOMSEED = 0;
32 | private static int NUMBEROFSWAPS = 400;
33 | private static Grad grad3[] = {new Grad(1, 1, 0), new Grad(-1, 1, 0),
34 | new Grad(1, -1, 0), new Grad(-1, -1, 0), new Grad(1, 0, 1),
35 | new Grad(-1, 0, 1), new Grad(1, 0, -1), new Grad(-1, 0, -1),
36 | new Grad(0, 1, 1), new Grad(0, -1, 1), new Grad(0, 1, -1),
37 | new Grad(0, -1, -1)};
38 | private static Grad grad4[] = {new Grad(0, 1, 1, 1),
39 | new Grad(0, 1, 1, -1), new Grad(0, 1, -1, 1),
40 | new Grad(0, 1, -1, -1), new Grad(0, -1, 1, 1),
41 | new Grad(0, -1, 1, -1), new Grad(0, -1, -1, 1),
42 | new Grad(0, -1, -1, -1), new Grad(1, 0, 1, 1),
43 | new Grad(1, 0, 1, -1), new Grad(1, 0, -1, 1),
44 | new Grad(1, 0, -1, -1), new Grad(-1, 0, 1, 1),
45 | new Grad(-1, 0, 1, -1), new Grad(-1, 0, -1, 1),
46 | new Grad(-1, 0, -1, -1), new Grad(1, 1, 0, 1),
47 | new Grad(1, 1, 0, -1), new Grad(1, -1, 0, 1),
48 | new Grad(1, -1, 0, -1), new Grad(-1, 1, 0, 1),
49 | new Grad(-1, 1, 0, -1), new Grad(-1, -1, 0, 1),
50 | new Grad(-1, -1, 0, -1), new Grad(1, 1, 1, 0),
51 | new Grad(1, 1, -1, 0), new Grad(1, -1, 1, 0),
52 | new Grad(1, -1, -1, 0), new Grad(-1, 1, 1, 0),
53 | new Grad(-1, 1, -1, 0), new Grad(-1, -1, 1, 0),
54 | new Grad(-1, -1, -1, 0)};
55 | private static short p_supply[] = {151, 160, 137, 91, 90, 15, 131, 13,
56 | 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8,
57 | 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26,
58 | 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237,
59 | 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71,
60 | 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60,
61 | 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143,
62 | 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89,
63 | 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198,
64 | 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118,
65 | 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189,
66 | 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70,
67 | 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108,
68 | 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251,
69 | 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145,
70 | 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184,
71 | 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205,
72 | 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61,
73 | 156, 180};
74 | private short p[] = new short[p_supply.length];
75 | // To remove the need for index wrapping, double the permutation table
76 | // length
77 | private short perm[] = new short[512];
78 | private short permMod12[] = new short[512];
79 | public SimplexNoiseOctave(int seed) {
80 | p = p_supply.clone();
81 |
82 | if (seed == RANDOMSEED) {
83 | Random rand = new Random();
84 | seed = rand.nextInt();
85 | }
86 |
87 | // the random for the swaps
88 | Random rand = new Random(seed);
89 |
90 | // the seed determines the swaps that occur between the default order
91 | // and the order we're actually going to use
92 | for (int i = 0; i < NUMBEROFSWAPS; i++) {
93 | int swapFrom = rand.nextInt(p.length);
94 | int swapTo = rand.nextInt(p.length);
95 |
96 | short temp = p[swapFrom];
97 | p[swapFrom] = p[swapTo];
98 | p[swapTo] = temp;
99 | }
100 |
101 | for (int i = 0; i < 512; i++) {
102 | perm[i] = p[i & 255];
103 | permMod12[i] = (short) (perm[i] % 12);
104 | }
105 | }
106 |
107 | // This method is a *lot* faster than using (int)Math.floor(x)
108 | private static int fastfloor(double x) {
109 | int xi = (int) x;
110 | return x < xi ? xi - 1 : xi;
111 | }
112 |
113 | private static double dot(Grad g, double x, double y) {
114 | return g.x * x + g.y * y;
115 | }
116 |
117 | private static double dot(Grad g, double x, double y, double z) {
118 | return g.x * x + g.y * y + g.z * z;
119 | }
120 |
121 | private static double dot(Grad g, double x, double y, double z, double w) {
122 | return g.x * x + g.y * y + g.z * z + g.w * w;
123 | }
124 |
125 | // 2D simplex noise
126 | public double noise(double xin, double yin) {
127 | double n0, n1, n2; // Noise contributions from the three corners
128 | // Skew the input space to determine which simplex cell we're in
129 | double s = (xin + yin) * F2; // Hairy factor for 2D
130 | int i = fastfloor(xin + s);
131 | int j = fastfloor(yin + s);
132 | double t = (i + j) * G2;
133 | double X0 = i - t; // Unskew the cell origin back to (x,y) space
134 | double Y0 = j - t;
135 | double x0 = xin - X0; // The x,y distances from the cell origin
136 | double y0 = yin - Y0;
137 | // For the 2D case, the simplex shape is an equilateral triangle.
138 | // Determine which simplex we are in.
139 | int i1, j1; // Offsets for second (middle) corner of simplex in (i,j)
140 | // coords
141 | if (x0 > y0) {
142 | i1 = 1;
143 | j1 = 0;
144 | } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
145 | else {
146 | i1 = 0;
147 | j1 = 1;
148 | } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
149 | // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
150 | // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
151 | // c = (3-sqrt(3))/6
152 | double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed
153 | // coords
154 | double y1 = y0 - j1 + G2;
155 | double x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y)
156 | // unskewed coords
157 | double y2 = y0 - 1.0 + 2.0 * G2;
158 | // Work out the hashed gradient indices of the three simplex corners
159 | int ii = i & 255;
160 | int jj = j & 255;
161 | int gi0 = permMod12[ii + perm[jj]];
162 | int gi1 = permMod12[ii + i1 + perm[jj + j1]];
163 | int gi2 = permMod12[ii + 1 + perm[jj + 1]];
164 | // Calculate the contribution from the three corners
165 | double t0 = 0.5 - x0 * x0 - y0 * y0;
166 | if (t0 < 0) {
167 | n0 = 0.0;
168 | } else {
169 | t0 *= t0;
170 | n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for
171 | // 2D gradient
172 | }
173 | double t1 = 0.5 - x1 * x1 - y1 * y1;
174 | if (t1 < 0) {
175 | n1 = 0.0;
176 | } else {
177 | t1 *= t1;
178 | n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
179 | }
180 | double t2 = 0.5 - x2 * x2 - y2 * y2;
181 | if (t2 < 0) {
182 | n2 = 0.0;
183 | } else {
184 | t2 *= t2;
185 | n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
186 | }
187 | // Add contributions from each corner to get the final noise value.
188 | // The result is scaled to return values in the interval [-1,1].
189 | return 70.0 * (n0 + n1 + n2);
190 | }
191 |
192 | // 3D simplex noise
193 | public double noise(double xin, double yin, double zin) {
194 | double n0, n1, n2, n3; // Noise contributions from the four corners
195 | // Skew the input space to determine which simplex cell we're in
196 | double s = (xin + yin + zin) * F3; // Very nice and simple skew factor
197 | // for 3D
198 | int i = fastfloor(xin + s);
199 | int j = fastfloor(yin + s);
200 | int k = fastfloor(zin + s);
201 | double t = (i + j + k) * G3;
202 | double X0 = i - t; // Unskew the cell origin back to (x,y,z) space
203 | double Y0 = j - t;
204 | double Z0 = k - t;
205 | double x0 = xin - X0; // The x,y,z distances from the cell origin
206 | double y0 = yin - Y0;
207 | double z0 = zin - Z0;
208 | // For the 3D case, the simplex shape is a slightly irregular
209 | // tetrahedron.
210 | // Determine which simplex we are in.
211 | int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k)
212 | // coords
213 | int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
214 | if (x0 >= y0) {
215 | if (y0 >= z0) {
216 | i1 = 1;
217 | j1 = 0;
218 | k1 = 0;
219 | i2 = 1;
220 | j2 = 1;
221 | k2 = 0;
222 | } // X Y Z order
223 | else if (x0 >= z0) {
224 | i1 = 1;
225 | j1 = 0;
226 | k1 = 0;
227 | i2 = 1;
228 | j2 = 0;
229 | k2 = 1;
230 | } // X Z Y order
231 | else {
232 | i1 = 0;
233 | j1 = 0;
234 | k1 = 1;
235 | i2 = 1;
236 | j2 = 0;
237 | k2 = 1;
238 | } // Z X Y order
239 | } else { // x0 y0) {
356 | rankx++;
357 | } else {
358 | ranky++;
359 | }
360 | if (x0 > z0) {
361 | rankx++;
362 | } else {
363 | rankz++;
364 | }
365 | if (x0 > w0) {
366 | rankx++;
367 | } else {
368 | rankw++;
369 | }
370 | if (y0 > z0) {
371 | ranky++;
372 | } else {
373 | rankz++;
374 | }
375 | if (y0 > w0) {
376 | ranky++;
377 | } else {
378 | rankw++;
379 | }
380 | if (z0 > w0) {
381 | rankz++;
382 | } else {
383 | rankw++;
384 | }
385 | int i1, j1, k1, l1; // The integer offsets for the second simplex corner
386 | int i2, j2, k2, l2; // The integer offsets for the third simplex corner
387 | int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
388 | // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some
389 | // order.
390 | // Many values of c will never occur, since e.g. x>y>z>w makes x= 3 ? 1 : 0;
398 | j1 = ranky >= 3 ? 1 : 0;
399 | k1 = rankz >= 3 ? 1 : 0;
400 | l1 = rankw >= 3 ? 1 : 0;
401 | // Rank 2 denotes the second largest coordinate.
402 | i2 = rankx >= 2 ? 1 : 0;
403 | j2 = ranky >= 2 ? 1 : 0;
404 | k2 = rankz >= 2 ? 1 : 0;
405 | l2 = rankw >= 2 ? 1 : 0;
406 | // Rank 1 denotes the second smallest coordinate.
407 | i3 = rankx >= 1 ? 1 : 0;
408 | j3 = ranky >= 1 ? 1 : 0;
409 | k3 = rankz >= 1 ? 1 : 0;
410 | l3 = rankw >= 1 ? 1 : 0;
411 | // The fifth corner has all coordinate offsets = 1, so no need to
412 | // compute that.
413 | double x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w)
414 | // coords
415 | double y1 = y0 - j1 + G4;
416 | double z1 = z0 - k1 + G4;
417 | double w1 = w0 - l1 + G4;
418 | double x2 = x0 - i2 + 2.0 * G4; // Offsets for third corner in (x,y,z,w)
419 | // coords
420 | double y2 = y0 - j2 + 2.0 * G4;
421 | double z2 = z0 - k2 + 2.0 * G4;
422 | double w2 = w0 - l2 + 2.0 * G4;
423 | double x3 = x0 - i3 + 3.0 * G4; // Offsets for fourth corner in
424 | // (x,y,z,w) coords
425 | double y3 = y0 - j3 + 3.0 * G4;
426 | double z3 = z0 - k3 + 3.0 * G4;
427 | double w3 = w0 - l3 + 3.0 * G4;
428 | double x4 = x0 - 1.0 + 4.0 * G4; // Offsets for last corner in (x,y,z,w)
429 | // coords
430 | double y4 = y0 - 1.0 + 4.0 * G4;
431 | double z4 = z0 - 1.0 + 4.0 * G4;
432 | double w4 = w0 - 1.0 + 4.0 * G4;
433 | // Work out the hashed gradient indices of the five simplex corners
434 | int ii = i & 255;
435 | int jj = j & 255;
436 | int kk = k & 255;
437 | int ll = l & 255;
438 | int gi0 = perm[ii + perm[jj + perm[kk + perm[ll]]]] % 32;
439 | int gi1 = perm[ii + i1 + perm[jj + j1 + perm[kk + k1 + perm[ll + l1]]]] % 32;
440 | int gi2 = perm[ii + i2 + perm[jj + j2 + perm[kk + k2 + perm[ll + l2]]]] % 32;
441 | int gi3 = perm[ii + i3 + perm[jj + j3 + perm[kk + k3 + perm[ll + l3]]]] % 32;
442 | int gi4 = perm[ii + 1 + perm[jj + 1 + perm[kk + 1 + perm[ll + 1]]]] % 32;
443 | // Calculate the contribution from the five corners
444 | double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
445 | if (t0 < 0) {
446 | n0 = 0.0;
447 | } else {
448 | t0 *= t0;
449 | n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
450 | }
451 | double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
452 | if (t1 < 0) {
453 | n1 = 0.0;
454 | } else {
455 | t1 *= t1;
456 | n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
457 | }
458 | double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
459 | if (t2 < 0) {
460 | n2 = 0.0;
461 | } else {
462 | t2 *= t2;
463 | n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
464 | }
465 | double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
466 | if (t3 < 0) {
467 | n3 = 0.0;
468 | } else {
469 | t3 *= t3;
470 | n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
471 | }
472 | double t4 = 0.6 - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
473 | if (t4 < 0) {
474 | n4 = 0.0;
475 | } else {
476 | t4 *= t4;
477 | n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
478 | }
479 | // Sum up and scale the result to cover the range [-1,1]
480 | return 27.0 * (n0 + n1 + n2 + n3 + n4);
481 | }
482 |
483 | // Inner class to speed upp gradient computations
484 | // (array access is a lot slower than member access)
485 | private static class Grad {
486 | double x, y, z, w;
487 |
488 | Grad(double x, double y, double z) {
489 | this.x = x;
490 | this.y = y;
491 | this.z = z;
492 | }
493 |
494 | Grad(double x, double y, double z, double w) {
495 | this.x = x;
496 | this.y = y;
497 | this.z = z;
498 | this.w = w;
499 | }
500 | }
501 |
502 | }
--------------------------------------------------------------------------------
/src/cm/cubestruct/world/SuperFlatGen.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.world;
2 |
3 | import cm.cubestruct.block.Block;
4 | import cm.cubestruct.block.DirtBlock;
5 | import cm.cubestruct.block.EmptyBlock;
6 | import cm.cubestruct.block.GrassBlock;
7 |
8 | public class SuperFlatGen implements Generator {
9 |
10 | @Override
11 | public Block genBlock(int wX, int wY,int wZ, int seed) {
12 | Block b=wY>6?null:(wY>5?new GrassBlock(wX, wY,wZ):new DirtBlock(wX,wY,wZ));//new EmptyBlock(wX, wY,wZ):(wY>5?new GrassBlock(wX, wY,wZ):new DirtBlock(wX,wY,wZ));
13 | return b;
14 | }
15 |
16 | @Override
17 | public int maxHeight(int wX, int wZ, int seed) {
18 | // TODO Auto-generated method stub
19 | return 7;
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/cm/cubestruct/world/World.java:
--------------------------------------------------------------------------------
1 | package cm.cubestruct.world;
2 |
3 | import java.util.ArrayList;
4 |
5 | import cm.cubestruct.engine.GameItem;
6 | import cm.cubestruct.engine.render.IRenderObject;
7 |
8 | public class World {
9 | public static final int WORLD_WIDTH=16;
10 | public Chunk[][] chunks=new Chunk[WORLD_WIDTH][WORLD_WIDTH];
11 | public Generator gen=new SuperFlatGen();
12 | public static final int WORLD_HEIGHT=64;
13 | public World(Generator gen2){
14 | gen=gen2;
15 | genChunk(WORLD_WIDTH/2,WORLD_WIDTH/2);
16 | genChunk(WORLD_WIDTH/2-1,WORLD_WIDTH/2-1);
17 | genChunk(WORLD_WIDTH/2,WORLD_WIDTH/2-1);
18 | genChunk(WORLD_WIDTH/2-1,WORLD_WIDTH/2);
19 | }
20 | public Chunk getChunk(int cX,int cZ){
21 | return chunks[cX][cZ];
22 | }
23 | private void genChunk(int cX,int cZ){
24 | if(cX-1 && cZ>-1){
25 | if(chunks[cX][cZ]==null){
26 | chunks[cX][cZ]=new Chunk(cX-WORLD_WIDTH/2,cZ-WORLD_WIDTH/2,gen);
27 | }
28 | }
29 | }
30 | public void render(){
31 | for(int x=0;x renderItems(){
40 | ArrayList c=new ArrayList();
41 | for(int x=0;xThis can only be invoked when the image has no alpha channel.
182 | * Calling this method causes {@link #hasAlpha()} to return true.
183 | *
184 | * @param r the red component of the color to make transparent
185 | * @param g the green component of the color to make transparent
186 | * @param b the blue component of the color to make transparent
187 | * @throws UnsupportedOperationException if the tRNS chunk data can't be set
188 | * @see #hasAlphaChannel()
189 | */
190 | public void overwriteTRNS(byte r, byte g, byte b) {
191 | if(hasAlphaChannel()) {
192 | throw new UnsupportedOperationException("image has an alpha channel");
193 | }
194 | byte[] pal = this.palette;
195 | if(pal == null) {
196 | transPixel = new byte[] { 0, r, 0, g, 0, b };
197 | } else {
198 | paletteA = new byte[pal.length/3];
199 | for(int i=0,j=0 ; i> 1)] & 255;
519 | switch(n-i) {
520 | default: dst[i+1] = (byte)(val & 15);
521 | case 1: dst[i ] = (byte)(val >> 4);
522 | }
523 | }
524 | }
525 |
526 | private void expand2(byte[] src, byte[] dst) {
527 | for(int i=1,n=dst.length ; i> 2)] & 255;
529 | switch(n-i) {
530 | default: dst[i+3] = (byte)((val ) & 3);
531 | case 3: dst[i+2] = (byte)((val >> 2) & 3);
532 | case 2: dst[i+1] = (byte)((val >> 4) & 3);
533 | case 1: dst[i ] = (byte)((val >> 6) );
534 | }
535 | }
536 | }
537 |
538 | private void expand1(byte[] src, byte[] dst) {
539 | for(int i=1,n=dst.length ; i> 3)] & 255;
541 | switch(n-i) {
542 | default: dst[i+7] = (byte)((val ) & 1);
543 | case 7: dst[i+6] = (byte)((val >> 1) & 1);
544 | case 6: dst[i+5] = (byte)((val >> 2) & 1);
545 | case 5: dst[i+4] = (byte)((val >> 3) & 1);
546 | case 4: dst[i+3] = (byte)((val >> 4) & 1);
547 | case 3: dst[i+2] = (byte)((val >> 5) & 1);
548 | case 2: dst[i+1] = (byte)((val >> 6) & 1);
549 | case 1: dst[i ] = (byte)((val >> 7) );
550 | }
551 | }
552 | }
553 |
554 | private void unfilter(byte[] curLine, byte[] prevLine) throws IOException {
555 | switch (curLine[0]) {
556 | case 0: // none
557 | break;
558 | case 1:
559 | unfilterSub(curLine);
560 | break;
561 | case 2:
562 | unfilterUp(curLine, prevLine);
563 | break;
564 | case 3:
565 | unfilterAverage(curLine, prevLine);
566 | break;
567 | case 4:
568 | unfilterPaeth(curLine, prevLine);
569 | break;
570 | default:
571 | throw new IOException("invalide filter type in scanline: " + curLine[0]);
572 | }
573 | }
574 |
575 | private void unfilterSub(byte[] curLine) {
576 | final int bpp = this.bytesPerPixel;
577 | for(int i=bpp+1,n=curLine.length ; i>> 1);
595 | }
596 | for(int n=curLine.length ; i>> 1);
598 | }
599 | }
600 |
601 | private void unfilterPaeth(byte[] curLine, byte[] prevLine) {
602 | final int bpp = this.bytesPerPixel;
603 |
604 | int i;
605 | for(i=1 ; i<=bpp ; ++i) {
606 | curLine[i] += prevLine[i];
607 | }
608 | for(int n=curLine.length ; i 256 || (chunkLength % 3) != 0) {
687 | throw new IOException("PLTE chunk has wrong length");
688 | }
689 | palette = new byte[paletteEntries*3];
690 | readChunk(palette, 0, palette.length);
691 | }
692 |
693 | private void readtRNS() throws IOException {
694 | switch (colorType) {
695 | case COLOR_GREYSCALE:
696 | checkChunkLength(2);
697 | transPixel = new byte[2];
698 | readChunk(transPixel, 0, 2);
699 | break;
700 | case COLOR_TRUECOLOR:
701 | checkChunkLength(6);
702 | transPixel = new byte[6];
703 | readChunk(transPixel, 0, 6);
704 | break;
705 | case COLOR_INDEXED:
706 | if(palette == null) {
707 | throw new IOException("tRNS chunk without PLTE chunk");
708 | }
709 | paletteA = new byte[palette.length/3];
710 | Arrays.fill(paletteA, (byte)0xFF);
711 | readChunk(paletteA, 0, paletteA.length);
712 | break;
713 | default:
714 | // just ignore it
715 | }
716 | }
717 |
718 | private void closeChunk() throws IOException {
719 | if(chunkRemaining > 0) {
720 | // just skip the rest and the CRC
721 | skip(chunkRemaining + 4);
722 | } else {
723 | readFully(buffer, 0, 4);
724 | int expectedCrc = readInt(buffer, 0);
725 | int computedCrc = (int)crc.getValue();
726 | if(computedCrc != expectedCrc) {
727 | throw new IOException("Invalid CRC");
728 | }
729 | }
730 | chunkRemaining = 0;
731 | chunkLength = 0;
732 | chunkType = 0;
733 | }
734 |
735 | private void openChunk() throws IOException {
736 | readFully(buffer, 0, 8);
737 | chunkLength = readInt(buffer, 0);
738 | chunkType = readInt(buffer, 4);
739 | chunkRemaining = chunkLength;
740 | crc.reset();
741 | crc.update(buffer, 4, 4); // only chunkType
742 | }
743 |
744 | private void openChunk(int expected) throws IOException {
745 | openChunk();
746 | if(chunkType != expected) {
747 | throw new IOException("Expected chunk: " + Integer.toHexString(expected));
748 | }
749 | }
750 |
751 | private void checkChunkLength(int expected) throws IOException {
752 | if(chunkLength != expected) {
753 | throw new IOException("Chunk has wrong size");
754 | }
755 | }
756 |
757 | private int readChunk(byte[] buffer, int offset, int length) throws IOException {
758 | if(length > chunkRemaining) {
759 | length = chunkRemaining;
760 | }
761 | readFully(buffer, offset, length);
762 | crc.update(buffer, offset, length);
763 | chunkRemaining -= length;
764 | return length;
765 | }
766 |
767 | private void refillInflater(Inflater inflater) throws IOException {
768 | while(chunkRemaining == 0) {
769 | closeChunk();
770 | openChunk(IDAT);
771 | }
772 | int read = readChunk(buffer, 0, buffer.length);
773 | inflater.setInput(buffer, 0, read);
774 | }
775 |
776 | private void readChunkUnzip(Inflater inflater, byte[] buffer, int offset, int length) throws IOException {
777 | assert(buffer != this.buffer);
778 | try {
779 | do {
780 | int read = inflater.inflate(buffer, offset, length);
781 | if(read <= 0) {
782 | if(inflater.finished()) {
783 | throw new EOFException();
784 | }
785 | if(inflater.needsInput()) {
786 | refillInflater(inflater);
787 | } else {
788 | throw new IOException("Can't inflate " + length + " bytes");
789 | }
790 | } else {
791 | offset += read;
792 | length -= read;
793 | }
794 | } while(length > 0);
795 | } catch (DataFormatException ex) {
796 | throw (IOException)(new IOException("inflate error").initCause(ex));
797 | }
798 | }
799 |
800 | private void readFully(byte[] buffer, int offset, int length) throws IOException {
801 | do {
802 | int read = input.read(buffer, offset, length);
803 | if(read < 0) {
804 | throw new EOFException();
805 | }
806 | offset += read;
807 | length -= read;
808 | } while(length > 0);
809 | }
810 |
811 | private int readInt(byte[] buffer, int offset) {
812 | return
813 | ((buffer[offset ] ) << 24) |
814 | ((buffer[offset+1] & 255) << 16) |
815 | ((buffer[offset+2] & 255) << 8) |
816 | ((buffer[offset+3] & 255) );
817 | }
818 |
819 | private void skip(long amount) throws IOException {
820 | while(amount > 0) {
821 | long skipped = input.skip(amount);
822 | if(skipped < 0) {
823 | throw new EOFException();
824 | }
825 | amount -= skipped;
826 | }
827 | }
828 |
829 | private static boolean checkSignature(byte[] buffer) {
830 | for(int i=0 ; i gameItems;
39 | public World world;
40 |
41 | public CubeStruct() {
42 | camera.position.add(0, 1.5f+20f, 0);
43 | camera.rotation.add(-80f,0.0f, 0);
44 | renderer = new Renderer();
45 | }
46 |
47 | @Override
48 | public void init(Window window) throws Exception {
49 | this.window=window;
50 | window.gl=this;
51 | renderer.init(window);
52 |
53 | gameItems = new ArrayList();
54 | world=new World(new BasicGen(1));
55 | System.out.println("WORLD GEND");
56 |
57 | if(world==null){
58 | for(float y=-5.0f;y<=0.0f;y++){
59 | for(float x=-7.0f;x<=8.0f;x++){
60 | for(float z=-7.0f;z<=8.0f;z++){
61 | GameItem gameItem = new Cube(new Vector3f(1.0f,1.0f,1.0f),y<0.0?"/textures/dirt.png":"/textures/grass.png");
62 | gameItem.setPosition(x, y, z);
63 |
64 | gameItems.add(gameItem);
65 | }
66 | }
67 | }
68 | }else{
69 | GameItem gameItem = new Cube(new Vector3f(1.0f,1.0f,1.0f),"/textures/dirt.png");
70 | gameItem.setPosition(0,5,0);
71 |
72 | gameItems.add(gameItem);
73 | }
74 |
75 | glfwSetInputMode(window.windowHandle, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
76 |
77 | }
78 |
79 | @Override
80 | public void input() {
81 | displyInc = 0;
82 | displxInc = 0;
83 | displzInc = 0;
84 | scaleInc = 0;
85 | ByteBuffer xpos = ByteBuffer.allocate(8);
86 | ByteBuffer ypos = ByteBuffer.allocate(8);
87 | DoubleBuffer x = BufferUtils.createDoubleBuffer(1);
88 | DoubleBuffer y = BufferUtils.createDoubleBuffer(1);
89 |
90 | glfwGetCursorPos(window.windowHandle, x, y);
91 |
92 | x.rewind();
93 | y.rewind();
94 |
95 | double newX = x.get();
96 | double newY = y.get();
97 | //System.out.println(newY);
98 |
99 | camera.rotation.x=(float) Math.max(Math.min(camera.rotation.x-(newY-window.getHeight()/2.0f)/4.0f,90.0f),-90.0f);
100 | camera.rotation.y=(float) (camera.rotation.y-(newX-window.getWidth()/2.0f)/4.0f);
101 | /* if (window.isKeyPressed(GLFW_KEY_UP)) {
102 | camera.rotation.x=Math.min(camera.rotation.x+0.1f,90.0f);
103 | }
104 | if (window.isKeyPressed(GLFW_KEY_DOWN)) {
105 | camera.rotation.x=Math.max(camera.rotation.x-0.1f,-90.0f);
106 | }
107 | if (window.isKeyPressed(GLFW_KEY_LEFT)) {
108 | camera.rotation.y=camera.rotation.y+0.1f;
109 | }
110 | if (window.isKeyPressed(GLFW_KEY_RIGHT)) {
111 | camera.rotation.y=camera.rotation.y-0.1f;
112 | }*/
113 | GLFW.glfwSetCursorPos(window.windowHandle, window.getWidth()/2.0,window.getHeight()/2.0);
114 | }
115 |
116 | @Override
117 | public void update(float interval) {
118 | if(window.isKeyPressed(GLFW_KEY_W)){
119 | camera.position=camera.getPosition().add(new Vector3f(0f,0,-interval*3.0f).rotate(
120 | new Quaternionf().setEulerAnglesXYZ(0.0f,
121 | -(float) Math.toRadians(camera.getRotation().y),0.0f)));
122 | }
123 | if(window.isKeyPressed(GLFW_KEY_S)){
124 | camera.position=camera.getPosition().add(new Vector3f(0f,0,interval*3.0f).rotate(
125 | new Quaternionf().setEulerAnglesXYZ(0.0f,
126 | -(float) Math.toRadians(camera.getRotation().y),0.0f)));
127 | }
128 | if(window.isKeyPressed(GLFW_KEY_A)){
129 | camera.position=camera.getPosition().add(new Vector3f(interval*3.0f,0,0f).rotate(
130 | new Quaternionf().setEulerAnglesXYZ(0.0f,
131 | -(float) Math.toRadians(camera.getRotation().y),0.0f)));
132 | }
133 | if(window.isKeyPressed(GLFW_KEY_D)){
134 | camera.position=camera.getPosition().add(new Vector3f(-interval*3.0f,0,0f).rotate(
135 | new Quaternionf().setEulerAnglesXYZ(0.0f,
136 | -(float) Math.toRadians(camera.getRotation().y),0.0f)));
137 | }
138 | if(window.isKeyPressed(GLFW_KEY_SPACE)){
139 | camera.position=camera.getPosition().add(new Vector3f(0.0f,interval*3.0f,0f));
140 | }
141 | if(window.isKeyPressed(GLFW.GLFW_KEY_LEFT_SHIFT) || window.isKeyPressed(GLFW.GLFW_KEY_RIGHT_SHIFT)){
142 | camera.position=camera.getPosition().add(new Vector3f(0.0f,-interval*3.0f,0f));
143 | }
144 |
145 | }
146 |
147 | @Override
148 | public void render() {
149 | world.gen();
150 | renderer.render(window,gameItems,camera,world);
151 | }
152 |
153 | @Override
154 | public void cleanup() {
155 | renderer.cleanup();
156 | world.cleanup();
157 | for (GameItem gameItem : gameItems) {
158 | gameItem.getMesh().cleanUp();
159 | }
160 | }
161 |
162 | @Override
163 | public void keyPressed(int key) {
164 | //System.out.println(key);
165 |
166 | // TODO Auto-generated method stub
167 |
168 | }
169 |
170 | }
171 |
--------------------------------------------------------------------------------
/src/org/cubestruct/game/Main.java:
--------------------------------------------------------------------------------
1 | package org.cubestruct.game;
2 |
3 | import cm.cubestruct.engine.GameEngine;
4 | import cm.cubestruct.engine.IGameLogic;
5 |
6 | public class Main {
7 | public static final String GAME_VERSION = "0.1 beta #1";
8 | public static final String GAME_NAME = "CubeStruct";
9 | public static void main(String[] args) {
10 | try {
11 | boolean vSync = true;
12 | IGameLogic gameLogic = new CubeStruct();
13 | GameEngine gameEng = new GameEngine(GAME_NAME, 600, 480, vSync, gameLogic);
14 | gameEng.start();
15 | } catch (Exception excp) {
16 | excp.printStackTrace();
17 | System.exit(-1);
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/src/org/cubestruct/game/Renderer.java:
--------------------------------------------------------------------------------
1 | package org.cubestruct.game;
2 |
3 | import org.joml.Matrix4f;
4 | import org.lwjgl.opengl.GL11;
5 |
6 | import cm.cubestruct.engine.GameItem;
7 | import cm.cubestruct.engine.Utils;
8 | import cm.cubestruct.engine.Window;
9 | import cm.cubestruct.engine.render.Camera;
10 | import cm.cubestruct.engine.render.ShaderProgram;
11 | import cm.cubestruct.engine.render.Transformation;
12 | import cm.cubestruct.world.World;
13 |
14 | import static org.lwjgl.opengl.GL11.*;
15 |
16 | import java.nio.FloatBuffer;
17 | import java.util.ArrayList;
18 |
19 | public class Renderer {
20 |
21 | /**
22 | * Field of View in Radians
23 | */
24 | private static final float FOV = (float) Math.toRadians(60.0f);
25 |
26 | private static final float Z_NEAR = 0.01f;
27 |
28 | private static final float Z_FAR = 100.f;
29 |
30 | private final Transformation transformation;
31 |
32 | private ShaderProgram shaderProgram;
33 |
34 | public Renderer() {
35 | transformation = new Transformation();
36 | }
37 |
38 | public void init(Window window) throws Exception {
39 | // Create shader
40 | shaderProgram = new ShaderProgram();
41 | shaderProgram.createVertexShader(Utils.loadResource("/shaders/vertex.vs"));
42 | shaderProgram.createFragmentShader(Utils.loadResource("/shaders/fragment.fs"));
43 | shaderProgram.link();
44 |
45 | // Create uniforms for world and projection matrices and texture
46 | shaderProgram.createUniform("projectionMatrix");
47 | shaderProgram.createUniform("worldMatrix");
48 | shaderProgram.createUniform("texture_sampler");
49 | }
50 |
51 | public void clear() {
52 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
53 | }
54 |
55 | public void render(Window window, ArrayList gameItems,Camera camera,World world) {
56 | clear();
57 | ArrayList allGameItems=new ArrayList();
58 | allGameItems.addAll(gameItems);
59 | if(world!=null){
60 | allGameItems.addAll(world.renderItems());
61 | }
62 | if ( window.isResized() ) {
63 | glViewport(0, 0, window.getWidth(), window.getHeight());
64 | window.setResized(false);
65 | }
66 |
67 | shaderProgram.bind();
68 |
69 | //GL11.glLoadMatrixf(new Matrix4f().identity().get(FloatBuffer.allocate(16)));
70 | // GL11.glTranslatef(camera.getPosition().x, camera.getPosition().y, camera.getPosition().z);
71 | // Update projection Matrix
72 | Matrix4f projectionMatrix = transformation.getProjectionMatrix(FOV, window.getWidth(), window.getHeight(), Z_NEAR, Z_FAR);
73 |
74 | shaderProgram.setUniform("projectionMatrix", projectionMatrix);
75 |
76 | shaderProgram.setUniform("texture_sampler", 0);
77 | // Render each gameItem
78 | for(GameItem gameItem : allGameItems) {
79 | // Set world matrix for this item
80 | Matrix4f worldMatrix = transformation.getWorldMatrix(
81 | gameItem.getPosition(),
82 | gameItem.getRotation(),
83 | gameItem.getScale(),camera);
84 | shaderProgram.setUniform("worldMatrix", worldMatrix);
85 |
86 | // Render the mes for this game item
87 | gameItem.render();
88 | }
89 |
90 | shaderProgram.unbind();
91 | }
92 |
93 | public void cleanup() {
94 | if (shaderProgram != null) {
95 | shaderProgram.cleanup();
96 | }
97 | }
98 | }
99 |
--------------------------------------------------------------------------------