Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

parsing - How to change colour with a (for loop) in OPENGL using JAVA

I'm building a traffic signal in OpenGL using java. I'm trying to parse in colour using a for loop(acts as a timer). My code presently returns just the matrix and no colour. I have attached i have tried importing color as a java library but that doesn't work as well.

Is there a workaround to this???

 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();
    }
    
}

Below is my color class

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...