我是OpenGL和JOGL的新手,但是我遇到了这个问题,我似乎无法找到解决方案。每当我有雾或某些光线时,它总是以平行于x轴的直线绘制。该类是问题发生的地方:
public class JoglCanvas扩展GLCanvas实现GLEventListener { private static final long serialVersionUID = 1173053770896367688L;
public FPSAnimator animator;
private GLU glu;
public JoglCanvas(int width, int height, GLCapabilities capabilities){
super(capabilities);
addGLEventListener(this);
setSize(width, height);
}
public void display(GLAutoDrawable drawable) {
GL2 gl = (GL2)drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
if(Settings.WireFrame){
gl.glPolygonMode( GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
}else{
gl.glPolygonMode( GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);
}
//Draw player hb
//drawCube(new Cube(Settings.Player1.get_actual_location(), Settings.Player1.Size), gl);
//drawSphere(new Vector(Camera.Location.x, Camera.Location.y, Camera.Location.z), Settings.TEXTURE_SKY, Camera.ViewDistance, gl);
//Preparing lights
float SHINE_ALL_DIRECTIONS = 0;
float[] lightPos = {0, 0, 0, SHINE_ALL_DIRECTIONS};
float[] lightColorAmbient = {0.2f, 0.2f, 0.2f, 0f};
float[] lightColorSpecular = {0.8f, 0.8f, 0.8f, 0f};
//Set Light parameters
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, lightPos, 0);
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT, lightColorAmbient, 0);
gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPECULAR, lightColorSpecular, 0);
//Enable lighting
gl.glEnable(GL2.GL_LIGHT1);
gl.glEnable(GL2.GL_LIGHTING);
//Set material
float[] rgba = {0.4f, 0.5f, 0.7f};
gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
gl.glMaterialf(GL2.GL_FRONT, GL2.GL_SHININESS, 0.5f);
//Enable fog
gl.glPushMatrix();
float Fog_distance = 90f;
float[] Fog_colour = {0,0,1f,0};
gl.glEnable(GL2.GL_FOG);
gl.glHint(GL2.GL_FOG_HINT, GL2.GL_NICEST);
//gl.glFogi(GL2.GL_FOG_MODE, GL2.GL_EXP);
gl.glFogi(GL2.GL_FOG_MODE, GL2.GL_EXP2);
//gl.glFogi(GL2.GL_FOG_MODE, GL2.GL_LINEAR);
gl.glFogf(GL2.GL_FOG_DENSITY, 0.005f);
gl.glFogfv(GL2.GL_FOG_COLOR, Fog_colour, 0);
gl.glFogf(GL2.GL_FOG_START, Fog_distance - 30);
gl.glFogf(GL2.GL_FOG_END, Fog_distance);
gl.glPopMatrix();
for(int x = 0; x<99; x++){
for(int z = 0; z<99; z++){
float[] Current_Height = {Height[x][z], Height[x+1][z], Height[x][z+1], Height[x+1][z+1],};
new Tile(new Vector(x*Tile.Size/2,0,z*Tile.Size/2), Current_Height).drawTile(gl);
}
}
setCamera(gl);
}
float[][] Height = new float[100][100];
private GL2 drawCube(Cube c, GL2 gl){
//Set material
float[] rgba = {0.8f, 0.8f, 0.8f, 1f};
gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT, rgba, 1);
gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
gl.glMaterialf(GL2.GL_FRONT, GL2.GL_SHININESS, 0.5f);
gl.glDisable(GL2.GL_TEXTURE_2D);
try{
Settings.Textures[c.GraphicsID].enable(gl);
Settings.Textures[c.GraphicsID].bind(gl);
}catch(NullPointerException e){
}
//drawSphere(c.Location, c.GraphicsID, 0.2f, gl);
final int x = 0, y = 1, z = 2;
final float[] TL = {0, 0, 0};
final float[] BR = {c.Size.x, c.Size.y, c.Size.z};
final float[] FrontTL = {TL[x], TL[y] ,TL[z]};
final float[] FrontTR = {BR[x], TL[y], TL[z]};
final float[] FrontBL = {TL[x], BR[y], TL[z]};
final float[] FrontBR = {BR[x], BR[y], TL[z]};
final float[] BackTL = {TL[x], TL[y], BR[z]};
final float[] BackTR = {BR[x], TL[y], BR[z]};
final float[] BackBL = {TL[x], BR[y], BR[z]};
final float[] BackBR = {BR[x], BR[y], BR[z]};
gl.glPushMatrix();
gl.glTranslatef(c.Location.x, c.Location.y, c.Location.z);
gl.glRotatef(c.Rotation.x, c.Rotation.y, c.Rotation.z, 0);
//Draw Cube
// Front Face.
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(0.0f, 0.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(FrontTL, 0);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3fv(FrontTR, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(FrontBR, 0);
gl.glEnd();
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(0.0f, 0.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(FrontTL, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(FrontBR, 0);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3fv(FrontBL, 0);
gl.glEnd();
//Left Face.
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(-1.0f, 0.0f, 0.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(FrontTL, 0);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3fv(BackTL, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(BackBL, 0);
gl.glEnd();
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(-1.0f, 0.0f, 0.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(FrontTL, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(BackBL, 0);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3fv(FrontBL, 0);
gl.glEnd();
//Right Face.
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(1.0f, 0.0f, 0.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(FrontTR, 0);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3fv(BackTR, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(BackBR, 0);
gl.glEnd();
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(1.0f, 0.0f, 0.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(FrontTR, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(BackBR, 0);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3fv(FrontBR, 0);
gl.glEnd();
//Bottom Face.
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(0.0f, -1.0f, 0.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(FrontBL, 0);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3fv(BackBL, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(BackBR, 0);
gl.glEnd();
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(0.0f, -1.0f, 0.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(FrontBL, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(BackBR, 0);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3fv(FrontBR, 0);
gl.glEnd();
//Top Face.
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(-1.0f, 0.0f, 0.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(FrontTL, 0);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3fv(BackTL, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(BackTR, 0);
gl.glEnd();
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(-1.0f, 0.0f, 0.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(FrontTL, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(BackTR, 0);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3fv(FrontTR, 0);
gl.glEnd();
// Back Face.
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(0.0f, 0.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(BackTL, 0);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3fv(BackTR, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(BackBR, 0);
gl.glEnd();
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glNormal3f(0.0f, 0.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3fv(BackTL, 0);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3fv(BackBR, 0);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3fv(BackBL, 0);
gl.glEnd();
// Restore old state.
gl.glPopMatrix();
return gl;
}
private GL2 drawSphere(Vector Pos, int t, float r, GL2 gl){
gl.glPushMatrix();
gl.glTranslatef(Pos.x, Pos.y, Pos.z);
GLUquadric Sphere = glu.gluNewQuadric();
glu.gluQuadricTexture(Sphere, false);
try{
Settings.Textures[t].enable(gl);
Settings.Textures[t].bind(gl);
glu.gluQuadricTexture(Sphere, true);
}catch(Exception e){
//System.err.println("Texture application " + e.getMessage());
}
glu.gluQuadricDrawStyle(Sphere, GLU.GLU_FILL);
glu.gluQuadricNormals(Sphere, GLU.GLU_FLAT);
glu.gluQuadricOrientation(Sphere, GLU.GLU_INSIDE);
glu.gluSphere(Sphere, r, Settings.Slices, Settings.Stacks);
glu.gluDeleteQuadric(Sphere);
gl.glPopMatrix();
return gl;
}
private void setCamera(GL2 gl){
Camera.Location = Settings.Player1.get_eye_pos();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
float widthHeightRatio = (float) getWidth()/ (float) getHeight();
glu.gluPerspective(Camera.FOV, widthHeightRatio, 1, Camera.ViewDistance);
glu.gluLookAt(Camera.Location.x, Camera.Location.y, Camera.Location.z,
Camera.Location.x+Camera.ViewPort.x, Camera.Location.y+Camera.ViewPort.y, Camera.Location.z+Camera.ViewPort.z,
0, 1, 0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void dispose(GLAutoDrawable arg0) {
}
public void init(GLAutoDrawable drawable) {
Height[0][0] = 0;
for(int x = 0; x<100; x++){
for(int z = 0; z<100; z++){
Height[x][z] = Maths.RandFloat(-Maths.RandInt(-10, 10)/2, Maths.RandInt(-10, 10)/2);
}
}
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
Settings.KeyBoard[e.getKeyCode()] = true;
}
public void keyReleased(KeyEvent e){
Settings.KeyBoard[e.getKeyCode()] = false;
}
});
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
if(Settings.LockMouse){
Settings.LockMouse = false;
}else{
Settings.LockMouse = true;
}
}
});
//LoadTexture
Settings.Textures[Settings.TEXTURE_STONE] = importTexture("Stone");
Settings.Textures[Settings.TEXTURE_SKY] = importTexture("Sky");
GL2 gl = (GL2)drawable.getGL();
drawable.setGL(new DebugGL2(gl));
//Global settings.
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
gl.glClearColor(0f, 0f, 0f, 0f);
glu = new GLU();
//Animator
animator = new FPSAnimator(this, 60);
animator.start();
}
public Texture importTexture(String Loc){
Texture t = null;
try{
FileInputStream stream = new FileInputStream(new File("Resources/" + Loc + ".png"));
TextureData data = TextureIO.newTextureData(getGLProfile(), stream, true, "png");
t = TextureIO.newTexture(data);
}catch(IOException e){
System.err.println(e.getMessage() + "\n");
}
return t;
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
gl.glViewport(0, 0, width, height);
}
}
是否有任何可能导致这一系列雾/光照而不是它在一个圆圈内的东西?
编辑: 我现在已经离开并用极坐标创建了我自己的系统,但是,我仍然有兴趣知道错误地做了什么。
编辑2:
解决方案:
private void setCamera(GL2 gl){
Camera.Location = Settings.Player1.get_eye_pos();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
float widthHeightRatio = (float) getWidth()/ (float) getHeight();
glu.gluPerspective(Camera.FOV, widthHeightRatio, 1, 10000);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(Camera.Location.x, Camera.Location.y, Camera.Location.z,
Camera.Location.x+Camera.ViewPort.x, Camera.Location.y+Camera.ViewPort.y, Camera.Location.z+Camera.ViewPort.z,
0, 1, 0);
//Enable fog
float Fog_distance = Camera.ViewDistance;
float[] Fog_colour = {0,0,1f,0};
gl.glEnable(GL2.GL_FOG);
gl.glHint(GL2.GL_FOG_HINT, GL2.GL_NICEST);
//gl.glFogi(GL2.GL_FOG_MODE, GL2.GL_EXP);
gl.glFogi(GL2.GL_FOG_MODE, GL2.GL_EXP2);
//gl.glFogi(GL2.GL_FOG_MODE, GL2.GL_LINEAR);
gl.glFogf(GL2.GL_FOG_DENSITY, 0.005f);
gl.glFogfv(GL2.GL_FOG_COLOR, Fog_colour, 0);
gl.glFogf(GL2.GL_FOG_START, Fog_distance - 30);
gl.glFogf(GL2.GL_FOG_END, Fog_distance);
}
答案 0 :(得分:1)
固定函数GL与视图矩阵定义的特定眼睛空间一起工作,并期望模型和视图矩阵的组合为GL_MODELVIEW
矩阵(因此名称)将顶点从对象空间转换为眼睛空间。照明和雾计算在眼睛空间进行。
对于眼睛空间,使用相机位于原点的对流,观察朝上的-z和y。而GLu的lookAt()
函数将完全生成该函数。
然而,这段代码
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
float widthHeightRatio = (float) getWidth()/ (float) getHeight();
glu.gluPerspective(Camera.FOV, widthHeightRatio, 1, Camera.ViewDistance);
glu.gluLookAt(Camera.Location.x, Camera.Location.y, Camera.Location.z,
Camera.Location.x+Camera.ViewPort.x, Camera.Location.y+Camera.ViewPort.y, Camera.Location.z+Camera.ViewPort.z,
0, 1, 0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
将投影和视图矩阵合并到GL_PROJECTION
矩阵中。这样做会破坏光线和雾气,因为现在GL认为眼睛空间是现实世界的空间。这意味着,照明和雾化甚至不会取决于观看位置。