OpenGL对象照明搞砸了

时间:2015-03-01 20:04:39

标签: opengl lighting

我创建了一组动画对象并在屏幕上移动。动画看起来很好,但我遇到了材质设置问题。球体上的照明看起来非常奇怪。框架:

frame

那么球体上的那些颜色,我不知道它们来自哪里。它本来是铜/黄铜,现在不记得了,但肯定不是......我会发布整个代码,因为我不知道问题可能在哪里。我为它有多乱而道歉。

package a3;

import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.gl2.GLUT;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.swing.*;
import java.nio.FloatBuffer;

public class Animation3 extends JFrame implements GLEventListener {
private float[] amb = new float[]{0.4f, 0.4f, 0.4f, 0f};
private float[] dif = new float[]{1f, 1f, 1f, 0f};
private float outerTorusDepth = 0.0f;
private float torusRotatingAngle = 0.01f;
private float delta = 0.01f;
private float s = 0f;
private float angle1 = 0f;
private float radius = 0.2f;
private float height = 0.3f;
private float deltaR = 0.001f;
private float deltaH = 0.001f;

private float alpha = 0f;

private static final float R = 1.0f; // radius of rotation around z axis
private static final float R2 = 2f; // radius of rotation around z axis

private GLU glu;
private GLUT glut;
private GLCanvas canvas;

public Animation3() {
    super("Animation");

    GLCapabilities caps = new GLCapabilities(GLProfile.getDefault());
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);

    getContentPane().add(canvas);
}

public void run() {
    setSize(1000, 1000);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setVisible(true);
    canvas.requestFocusInWindow();

    Animator animator = new Animator();
    animator.add(canvas);
    animator.start();
}

public static void main(String[] args) {
    new Animation3().run();
}

public void init(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    glu = new GLU();
    glut = new GLUT();

    drawable.getGL().setSwapInterval(1);

    initLighting(drawable);

    gl.glEnable(GL2.GL_DEPTH_TEST);
    gl.glEnable(GL2.GL_CULL_FACE);

    gl.glMatrixMode(GL2.GL_PROJECTION);
    glu.gluPerspective(40f, 1f, 1f, 20f);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    glu.gluLookAt(0, 0, 5,
            0, 0, 0,
            0, 1, 0);
}

public void display(GLAutoDrawable drawable) {
    update();

    GL2 gl = drawable.getGL().getGL2();

    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
    gl.glEnable(GL2.GL_LIGHT1);
    gl.glDisable(GL2.GL_LIGHT2);
    amb[3] = dif[3] = (float) (0.5 - Math.cos(s * .95) / 2);
    gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT, FloatBuffer.wrap(amb));
    gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_DIFFUSE, FloatBuffer.wrap(dif));

    alpha += 0.001;
    float tx = (float) (R * Math.cos(alpha));
    float ty = (float) (R * Math.sin(alpha));
    float tx2 = (float) (R2 * Math.cos(alpha));
    float ty2 = (float) (R2 * Math.sin(alpha));



    gl.glPushMatrix();

    gl.glTranslatef(tx, ty, outerTorusDepth);
    gl.glRotatef(torusRotatingAngle,-torusRotatingAngle,torusRotatingAngle*2, 5f);
    gl.glScalef(0.1f, 0.1f, 0.1f);

    float[] mat_ambient3 ={ 0.0f,0.0f,0.0f,1.0f };
    float[] mat_diffuse3 ={ 0.1f,0.35f,0.1f,1.0f};
    float[] mat_specular3 ={0.45f,0.55f,0.45f,1.0f };
    float shine = 32.0f ;
    gl.glMaterialfv(gl.GL_FRONT, gl.GL_SPECULAR, FloatBuffer.wrap(mat_ambient3));
    gl.glMaterialfv(gl.GL_FRONT, gl.GL_DIFFUSE, FloatBuffer.wrap(mat_diffuse3));
    gl.glMaterialfv(gl.GL_FRONT, gl.GL_AMBIENT, FloatBuffer.wrap(mat_specular3));
    gl.glMaterialf(gl.GL_FRONT, gl.GL_SHININESS, shine);

    glut.glutSolidDodecahedron();

    gl.glPopMatrix();
    gl.glPushMatrix();

    gl.glTranslatef(tx, ty, outerTorusDepth);
    gl.glRotatef(torusRotatingAngle,1f,2f, 5f);
    gl.glScalef(0.1f, 0.1f, 0.1f);
    gl.glTranslatef(tx2, ty2, outerTorusDepth);

    float[] mat_ambient ={ 0.2295f, 0.08825f, 0.0275f, 1.0f };
    float[] mat_diffuse ={0.5508f, 0.2118f, 0.066f, 1.0f };
    float[] mat_specular ={0.580594f, 0.223257f, 0.0695701f, 1.0f };
    shine =51.2f ;
    gl.glMaterialfv(gl.GL_FRONT, gl.GL_SPECULAR, FloatBuffer.wrap(mat_ambient));
    gl.glMaterialfv(gl.GL_FRONT, gl.GL_DIFFUSE, FloatBuffer.wrap(mat_diffuse));
    gl.glMaterialfv(gl.GL_FRONT, gl.GL_AMBIENT, FloatBuffer.wrap(mat_specular));
    gl.glMaterialf(gl.GL_FRONT, gl.GL_SHININESS, shine);

    glut.glutSolidSphere(0.4f, 100, 100);

    gl.glPopMatrix();




    gl.glPushMatrix();

    gl.glTranslatef(tx, ty, outerTorusDepth);
    gl.glRotatef(torusRotatingAngle, 5f, -torusRotatingAngle, 5f);

    float[] mat_ambient2 ={ 0.1f, 0.18725f, 0.1745f, 0.8f };
    float[] mat_diffuse2 ={0.396f, 0.74151f, 0.69102f, 0.8f };
    float[] mat_specular2 ={0.297254f, 0.30829f, 0.306678f, 0.8f };
    shine = 12.8f;
    gl.glMaterialfv(gl.GL_FRONT, gl.GL_SPECULAR, FloatBuffer.wrap(mat_ambient2));
    gl.glMaterialfv(gl.GL_FRONT, gl.GL_DIFFUSE, FloatBuffer.wrap(mat_diffuse2));
    gl.glMaterialfv(gl.GL_FRONT, gl.GL_AMBIENT, FloatBuffer.wrap(mat_specular2));
    gl.glMaterialf(gl.GL_FRONT, gl.GL_SHININESS, shine);

    glut.glutSolidTorus(0.1f, 0.5f, 100, 100);

    gl.glPopMatrix();


    gl.glPushAttrib(GL2.GL_ENABLE_BIT);
    gl.glDisable(GL2.GL_DEPTH_TEST);
    gl.glDisable(GL2.GL_LIGHTING);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glPushMatrix();
    gl.glLoadIdentity();
    glu.gluOrtho2D(0, 1500, 0, 1500);
    gl.glMatrixMode(GL2.GL_MODELVIEW);

    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glPopMatrix();
    gl.glPopAttrib();
    gl.glMatrixMode(GL2.GL_MODELVIEW);
}

private void update() {
    angle1 = (angle1 + 1.1f) % 360f;
    s += 0.05;
    if (outerTorusDepth >= 2.0f || outerTorusDepth <= -2.0f) 
        delta = -delta;
    outerTorusDepth += delta;
    torusRotatingAngle = (torusRotatingAngle + 2.5f) % 360f;


    if (radius > 0.3f || radius < 0.1f)
        deltaR = -deltaR;
    if (height > 0.35f || height < 0.15f)
        deltaH = -deltaH;
    radius += deltaR;
    height += deltaH;
}

private static final float[] LIGHT0_AMBIENT = new float[]{0.8f, 0.8f, 0.8f, 1f};
private static final float[] LIGHT_0_DIFFUSE = new float[]{0f, 0f, 0f, 1f};

private static final float[] LIGHT1_DIFFUSE = new float[]{1f, 0f, 0f, 1f};
private static final float[] LIGHT1_POSITION = new float[]{5f, 1f, 1f, 0f};

private static final float[] LIGHT2_DIFFUSE = new float[]{0f, 0f, 1f, 1f};
private static final float[] LIGHT2_POSITION = new float[]{-10f, 1f, 1f, 0f};

private static void initLighting(GLAutoDrawable glAutoDrawable) {
    GL2 gl2 = glAutoDrawable.getGL().getGL2();
    gl2.glEnable(GL2.GL_LIGHTING);
    gl2.glEnable(GL2.GL_LIGHT0);

    gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_AMBIENT, FloatBuffer.wrap(LIGHT0_AMBIENT));
    gl2.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, FloatBuffer.wrap(LIGHT_0_DIFFUSE));
    //
            gl2.glLightfv(GL2.GL_LIGHT1, GL2.GL_DIFFUSE, FloatBuffer.wrap(LIGHT1_DIFFUSE));
            gl2.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, FloatBuffer.wrap(LIGHT1_POSITION));
    //
            gl2.glLightfv(GL2.GL_LIGHT2, GL2.GL_DIFFUSE, FloatBuffer.wrap(LIGHT2_DIFFUSE));
            gl2.glLightfv(GL2.GL_LIGHT2, GL2.GL_POSITION, FloatBuffer.wrap(LIGHT2_POSITION));
}

@Override
public void dispose(GLAutoDrawable drawable) {
}

public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
}
}

0 个答案:

没有答案