LWJGL GLTranslation add to previos 翻译
LWJGL GLTranslation add to previos Translation
我这里的编码有问题。
当我渲染 2 个或更多块时,新块的位置与旧块相加,因此这些块之间的距离不同。我使用了 FPCameraController,所以我无法添加 GL11.glloadIdentify() 因为我的块消失了。
渲染Class:
while(!Display.isCloseRequested) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
GL11.glCullFace(GL11.GL_BACK);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_BLEND);
GL11.glPopMatrix(); //From 2D
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45f, (float) Display.getWidth() / Display.getHeight(), 0.1f, 1000);
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
GL11.glMatrixMode(GL11.GL_MODELVIEW);
//distance in mouse movement from the last getDX() call.
dx = Mouse.getDX();
//distance in mouse movement from the last getDY() call.
dy = Mouse.getDY();
//controll camera yaw from x movement fromt the mouse
camera.yaw(dx * mouseSensitivity);
//controll camera pitch from y movement fromt the mouse
camera.pitch(dy * mouseSensitivity);
//when passing in the distance to move
//we times the movementSpeed with dt this is a time scale
//so if its a slow frame u move more then a fast frame
//so on a slow computer you move just as fast as on a fast computer
if (Keyboard.isKeyDown(Keyboard.KEY_W))//move forward
{
camera.walkForward(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S))//move backwards
{
camera.walkBackwards(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A))//strafe left
{
camera.strafeLeft(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D))//strafe right
{
camera.strafeRight(movementSpeed*dt);
}
CubeRenderer.renderTexturedCube(TextureManager.cube_unknown.getTexture(), 0,0,0);
CubeRenderer.renderTexturedCube(TextureManager.cube_unknown.getTexture(), 0,1,0);
CubeRenderer.renderTexturedCube(TextureManager.cube_unknown.getTexture(), 0,2,0);
CubeRenderer.renderTexturedCube(TextureManager.cube_unknown.getTexture(), 1,2,0);
CubeRenderer.renderTexturedCube(TextureManager.cube_unknown.getTexture(), -1,2,0);
GL11.glLoadIdentity();
camera.lookThrough();
Display.sync(60);
Display.update();
}
Display.destroy();
使用这些变量渲染 class:
纹理特克斯;
浮动 x,y,z;
GL11.glTranslatef(x,y,z);
GL11.glRotatef(0,0.0f,0.0f,0.0f);
GL11.glColor3f(0.5f,0.5f,1.0f);
tex.bind();
GL11.glPolygonMode(GL11.GL_FRONT, GL11.GL_FILL);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(-1, -1, 1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(1, -1, 1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(1, 1, 1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(-1, 1, 1);
GL11.glEnd();
//left
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(-1, -1, -1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(-1, -1, 1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(-1, 1, 1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(-1, 1, -1);
GL11.glEnd();
//right
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(1, -1, -1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(1, 1, -1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(1, 1, 1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(1, -1, 1);
GL11.glEnd();
//front
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(-1, -1, -1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(1, -1, -1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(1, -1, 1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(-1, -1, 1);
GL11.glEnd();
//back
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(1, 1, -1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(-1, 1, -1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(-1, 1, 1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(1, 1, 1);
GL11.glEnd();
//bottom
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(1, -1, -1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(-1, -1, -1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(-1, 1, -1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(1, 1, -1);
GL11.glEnd();
使用GL11.glPushMatrix
保存旧矩阵,使用GL11.glPopMatrix
恢复。在 renderTexturedCube
(或任何你想要的地方):
GL11.glPushMatrix(); // saves the current matrix
GL11.glTranslatef(x, y, z);
GL11.glRotatef(0, 0.0f, 0.0f, 0.0f); // not sure why you have this; it does nothing
// rest of cube rendering code goes here
GL11.glPopMatrix(); // restore the matrix that was saved at the start of the method
我这里的编码有问题。 当我渲染 2 个或更多块时,新块的位置与旧块相加,因此这些块之间的距离不同。我使用了 FPCameraController,所以我无法添加 GL11.glloadIdentify() 因为我的块消失了。
渲染Class:
while(!Display.isCloseRequested) { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
GL11.glCullFace(GL11.GL_BACK);
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_BLEND);
GL11.glPopMatrix(); //From 2D
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45f, (float) Display.getWidth() / Display.getHeight(), 0.1f, 1000);
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
GL11.glMatrixMode(GL11.GL_MODELVIEW);
//distance in mouse movement from the last getDX() call.
dx = Mouse.getDX();
//distance in mouse movement from the last getDY() call.
dy = Mouse.getDY();
//controll camera yaw from x movement fromt the mouse
camera.yaw(dx * mouseSensitivity);
//controll camera pitch from y movement fromt the mouse
camera.pitch(dy * mouseSensitivity);
//when passing in the distance to move
//we times the movementSpeed with dt this is a time scale
//so if its a slow frame u move more then a fast frame
//so on a slow computer you move just as fast as on a fast computer
if (Keyboard.isKeyDown(Keyboard.KEY_W))//move forward
{
camera.walkForward(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S))//move backwards
{
camera.walkBackwards(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A))//strafe left
{
camera.strafeLeft(movementSpeed*dt);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D))//strafe right
{
camera.strafeRight(movementSpeed*dt);
}
CubeRenderer.renderTexturedCube(TextureManager.cube_unknown.getTexture(), 0,0,0);
CubeRenderer.renderTexturedCube(TextureManager.cube_unknown.getTexture(), 0,1,0);
CubeRenderer.renderTexturedCube(TextureManager.cube_unknown.getTexture(), 0,2,0);
CubeRenderer.renderTexturedCube(TextureManager.cube_unknown.getTexture(), 1,2,0);
CubeRenderer.renderTexturedCube(TextureManager.cube_unknown.getTexture(), -1,2,0);
GL11.glLoadIdentity();
camera.lookThrough();
Display.sync(60);
Display.update();
}
Display.destroy();
使用这些变量渲染 class: 纹理特克斯; 浮动 x,y,z;
GL11.glTranslatef(x,y,z);
GL11.glRotatef(0,0.0f,0.0f,0.0f);
GL11.glColor3f(0.5f,0.5f,1.0f);
tex.bind();
GL11.glPolygonMode(GL11.GL_FRONT, GL11.GL_FILL);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(-1, -1, 1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(1, -1, 1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(1, 1, 1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(-1, 1, 1);
GL11.glEnd();
//left
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(-1, -1, -1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(-1, -1, 1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(-1, 1, 1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(-1, 1, -1);
GL11.glEnd();
//right
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(1, -1, -1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(1, 1, -1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(1, 1, 1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(1, -1, 1);
GL11.glEnd();
//front
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(-1, -1, -1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(1, -1, -1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(1, -1, 1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(-1, -1, 1);
GL11.glEnd();
//back
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(1, 1, -1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(-1, 1, -1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(-1, 1, 1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(1, 1, 1);
GL11.glEnd();
//bottom
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0); GL11.glVertex3f(1, -1, -1);
GL11.glTexCoord2f(0, 1); GL11.glVertex3f(-1, -1, -1);
GL11.glTexCoord2f(1, 1); GL11.glVertex3f(-1, 1, -1);
GL11.glTexCoord2f(1, 0); GL11.glVertex3f(1, 1, -1);
GL11.glEnd();
使用GL11.glPushMatrix
保存旧矩阵,使用GL11.glPopMatrix
恢复。在 renderTexturedCube
(或任何你想要的地方):
GL11.glPushMatrix(); // saves the current matrix
GL11.glTranslatef(x, y, z);
GL11.glRotatef(0, 0.0f, 0.0f, 0.0f); // not sure why you have this; it does nothing
// rest of cube rendering code goes here
GL11.glPopMatrix(); // restore the matrix that was saved at the start of the method