Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL Rotating the Camera in 3D
    text
    copied!<p>In OpenGL I have been trying to rotate the camera so I can see the whole 3D Space. I researched rotating the space at the translation point but All I get is the Camera rotating around the center. </p> <p>Here is the code I am using to rotate the view (in Java):</p> <p>Game Loop</p> <pre><code>public Game(){ try { Display.setDisplayMode(new DisplayMode(1280, 720)); Display.setTitle("Survive the Planets"); Display.create(); } catch (LWJGLException e) { e.printStackTrace(); } //Initialize OpenGL glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50f, 1280f / 720f, 1.0f, 10000); glMatrixMode(GL_MODELVIEW); glEnable(GL_TEXTURE_2D); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); Planet planet = Planet.generateCreative(); planet.viewPlanet(); while(!Display.isCloseRequested()){ //render glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); planet.render(); //update if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){ Display.destroy(); System.exit(0); return; } Camera.rotate(1, 1, 1); Display.update(); Display.sync(240); } Display.destroy(); } </code></pre> <p>Camera Class</p> <pre><code>public class Camera { private static float x, y, z, rX, rY, rZ; public static void translate(float x, float y, float z){ Camera.x += x; Camera.y += y; Camera.z += z; glTranslatef(x, y, z); } public static void rotate(float x, float y, float z){ glRotatef(x, 1, 0, 0); glRotatef(y, 0, 1, 0); glRotatef(z, 0, 0, 1); Camera.rX += x; Camera.rY += y; Camera.rZ += z; } public static void setLocation(float x, float y, float z){ float dx = x - Camera.x; float dy = y - Camera.y; float dz = z - Camera.z; glTranslatef(dx, dy, dz); Camera.x = x; Camera.y = y; Camera.z = z; } public static void setRotation(float x, float y, float z){ float dx = x - Camera.rX; float dy = y - Camera.rY; float dz = z - Camera.rZ; glRotatef(dx, 1, 0, 0); glRotatef(dy, 0, 1, 0); glRotatef(dz, 0, 0, 1); Camera.rX = x; Camera.rY = y; Camera.rZ = z; } } </code></pre> <p>Planet Class:</p> <pre><code>public class Planet { private List&lt;Block&gt; blocks; private PlanetReference ref; private Planet(){ } public void render() { for(Block block : blocks){ block.render(); } } public static Planet generate(PlanetReference ref){ Planet p = new Planet(); p.ref = ref; List&lt;Block&gt; blocks = new CopyOnWriteArrayList&lt;Block&gt;(); System.out.println("Genrating planet " + ref.getName() + "... "); //world generation int r = ref.getRadius() * 32; int i = 0; for(int x = -r; x != r; x += 32){ for(int y = -r; y != r; y += 32){ for(int z = -r; z != r; z += 32){ i += 1; Cube cube = new Cube(x, y, z, 32); if(Game.getDistance(cube.getCenter()) &lt; r){ Block block = new Block(p, Material.DIRT, cube.getLocation()); blocks.add(block); } } } } p.blocks = blocks; System.out.println(blocks.size() + " blocks where generated. " + i); return p; } public void viewPlanet(){ Camera.setLocation(0, 0, -(ref.getRadius() * 96)); } public static Planet generateCreative() { return Planet.generate(new PlanetReference("Creative", 16, 9, 64)); } } </code></pre> <p>Any suggestions on what I should do? If you need anymore code just leave a comment.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload