如何使用java在opengl中使用(for循环)更改颜色

o4tp2gmn  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(272)

我正在用java在opengl中构建一个交通信号。我尝试使用for循环(充当计时器)进行彩色解析。我的代码目前只返回矩阵,没有颜色。我已经附上我已经尝试导入颜色作为一个java库,但没有工作以及。
有解决办法吗???

public class CS2150Coursework extends GraphicsLab
   {
            private Colour clrr;

    private Colour timer() {
        for (int i = 0; i < 13; i ++ ) {
        if (i <= 3) {
            clrr = Colour.RED;
//              System.out.print(clrr + "loop" + i);
        }

        else if (i >=4 && i <= 8) {
            clrr = Colour.ORANGE;
//              System.out.print(clrr + "loop" + i);
        }

        else if (i >=9 && i <= 13) {
            clrr = Colour.GREEN;
//              System.out.print(clrr + "loop" + i);
        }

    }

                return clrr;
               }

protected void renderScene()
    {
                 //draw trffic light 
         GL11.glPushMatrix();

         {
                GL11.glDisable(GL11.GL_CULL_FACE);
            GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
            GL11.glTranslatef(0.0f, -1.0f, -6.0f);
//          GL11.glRotatef(10, 40, 0, 0);

            drawTrafficLight(clrr);

         }
         GL11.glPopMatrix();

         // position and draw the second cube
         GL11.glPushMatrix();
         {
                GL11.glDisable(GL11.GL_CULL_FACE);
            GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
            GL11.glTranslatef(0.0f, 0.0f, -6.0f);
           drawTrafficLight(clrr);
         }
         GL11.glPopMatrix();

         // position and draw the third cube
         GL11.glPushMatrix();
         {
            GL11.glDisable(GL11.GL_CULL_FACE);
            GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
            GL11.glTranslatef(0.0f, 1.0f, -6.0f);
           drawTrafficLight(clrr);
         }
         GL11.glPopMatrix();

}

 private void drawTrafficLight(Colour clr)
    {

        clrr = clr;
        System.out.println(clr);
        // the vertices for the cube (note that all sides have a length of 1)
        Vertex v1 = new Vertex(-0.5f, -0.5f,  0.5f);
        Vertex v2 = new Vertex(-0.5f,  0.5f,  0.5f);
        Vertex v3 = new Vertex( 0.5f,  0.5f,  0.5f);
        Vertex v4 = new Vertex( 0.5f, -0.5f,  0.5f);
        Vertex v5 = new Vertex(-0.5f, -0.5f, -0.5f);
        Vertex v6 = new Vertex(-0.5f,  0.5f, -0.5f);
        Vertex v7 = new Vertex( 0.5f,  0.5f, -0.5f);
        Vertex v8 = new Vertex( 0.5f, -0.5f, -0.5f);

        // draw the near face:
        GL11.glBegin(GL11.GL_POLYGON);
        {
           clr.submit();

            v3.submit();
            v2.submit();
            v1.submit();
            v4.submit();
        }
        GL11.glEnd();

        // draw the left face:
        GL11.glBegin(GL11.GL_POLYGON);
        {
           clr.submit();

            v2.submit();
            v6.submit();
            v5.submit();
            v1.submit();
        }
        GL11.glEnd();

        // draw the right face:
        GL11.glBegin(GL11.GL_POLYGON);
        {
            clr.submit();

            v7.submit();
            v3.submit();
            v4.submit();
            v8.submit();
        }
        GL11.glEnd();

        // draw the top face:
        GL11.glBegin(GL11.GL_POLYGON);
        {
           clr.submit();

            v7.submit();
            v6.submit();
            v2.submit();
            v3.submit();
        }
        GL11.glEnd();

        // draw the bottom face:
        GL11.glBegin(GL11.GL_POLYGON);
        {
           clr.submit();

            v4.submit();
            v1.submit();
            v5.submit();
            v8.submit();
        }
        GL11.glEnd();

        // draw the far face:
        GL11.glBegin(GL11.GL_POLYGON);
        {
            clr.submit();

            v6.submit();
            v7.submit();
            v8.submit();
            v5.submit();
        }
        GL11.glEnd();
    }

}

下面是我的色彩课

package GraphicsLab;
import org.lwjgl.opengl.GL11;

/**
 * Encapsulates the concept of a colour consisting of red, green and blue components
 *
 * @author Anthony Jones and Dan Cornford
 */
public class Colour
{
    // some commonly used colours
    public static final Colour RED   = new Colour(1.0f,0.0f,0.0f);
    public static final Colour GREEN = new Colour(0.0f,1.0f,0.0f);
    public static final Colour PITCHGREEN = new Colour(0.20f,0.60f,0.20f);
    public static final Colour BLUE  = new Colour(0.0f,0.0f,1.0f);

    public static final Colour YELLOW  = new Colour(1.0f,1.0f,0.0f);
    public static final Colour PINK    = new Colour(1.0f,0.0f,1.0f);
    public static final Colour CYAN    = new Colour(0.0f,1.0f,1.0f);

    public static final Colour BLACK = new Colour(0.0f,0.0f,0.0f);
    public static final Colour WHITE = new Colour(1.0f,1.0f,1.0f);

    public static final Colour ORANGE = new Colour(238,154,0);

    /**
     * Constructs a Colour object from its RGB components with a float in the range 0-1.
     * @param red The Colour's red component 
     * @param green The Colour's green component
     * @param blue The Colour's blue component
     */
    public Colour(float red, float green, float blue)
    {
        this.red = red;
        this.green = green;
        this.blue = blue;
    }
    /**
     * Constructs a Colour object from its RGB components as integers
     * @param red The Colour's red component (int 0-255)
     * @param green The Colour's green component (int 0-255)
     * @param blue The Colour's blue component (int 0-255)
     */
    public Colour(int red, int green, int blue)
    {
        this.red = ((float) red)/255.0f;
        this.green = ((float) green)/255.0f;
        this.blue = ((float) blue)/255.0f;
    }

    /**
     * Submits this Colour to OpenGL using an immediate mode call
     */
    public final void submit()
    {   GL11.glColor3f(red, green, blue);
    }

    /**the red component of this colour */
    private float red;
    /**the green component of this colour */
    private float green;
    /**the blue component of this colour */
    private float blue;
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题