Reputation: 93
I'm fairly new to OpenGL and JOGL, but I have encountered this problem that where I can't seem to figure out a solution. Whenever I have fog or some lighting, it is always drawn in a straight line parallel to the x-axis. The class is where the problem occurs:
public class JoglCanvas extends GLCanvas implements 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);
}
}
Is there anything at all that could cause this line of fog/lighting instead of it being in a circle?
EDIT: I have now gone and created my own system with polar co-ordinates, however, I would still be interested in knowing what was done incorrectly.
EDIT 2:
The solution:
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);
}
Upvotes: 1
Views: 399
Reputation: 45342
Fixed function GL works with an specific eye space defined by the view matrix and expects the composition of the model and the view matrix as GL_MODELVIEW
matrix (hence the name) to transform the vertices from object space into eye space. Lighting and fog calculations are carried out in eye space.
For eye space, the convetion that the camera is located at origin, looking at -z and y pointing upwards is used. And GLu's lookAt()
function will exactly generate that.
However, this code
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();
combines projection and view matrix into the GL_PROJECTION
matrix. Doing so will break lighting and fog, because now what GL thinks of as eye space is in reality world space. That means, the lighting and fog will not even depend on the viewing position.
Upvotes: 1