gl.glColor3f in a loop seems to almost never work?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

gl.glColor3f in a loop seems to almost never work?

sirus20x6
hey i'm drawing some  lines that are a cross section of the grand canyon in my research project. user can select anywhere from 1 cross section to 24 evenly spaced across time (models erosion).

I found this handy formula for getting evenly spaced colors (not taking into account the biases of our eyes or brain)


//(x is the iteration we are on, so this goes from 0.0f to 1.0f

        float hue = (float) x / (XSectionManager.getXSection(0).getMaxNIterates() + 1 ); //hue
        float saturation = 1.0f; //saturation
        float brightness = 1.0f; //brightness
        Color myRGBColor = Color.getHSBColor(hue, saturation, brightness);
        gl.glColor3f(myRGBColor.getRed(), myRGBColor.getGreen(), myRGBColor.getBlue());

the problem is the first line draws yellow just fine then I get a few lines cyan and then a few magenta. I even stepped through it in intellij and it has a color swatch that shows you what color myRGBColor is and it changes correctly in there.
I'm probably not doing the draw calls correctly?

Thanks for your help.

<code>
    private void drawScaleBar() {
        gl.glClearColor(.75f, .75f, .75f, 1.0f);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);


        float scaleHeight = canvas.getHeight();
        if (scaleHeight > 300.0f) scaleHeight = 300.0f;


        float scaleBorder = 0.03f; // Fraction of screen height
        scaleHeight = (1.0f - 2 * scaleBorder) * scaleHeight; // Give a little border
        float scaleWidth = 0.05f * scaleHeight; // Seems like a good ratio
        float scalePosX;
        float scalePosY;
        // scalePosX = canvas.getHeight() * scaleBorder;
        // scalePosY = canvas.getHeight() * (1.0f - scaleBorder) - scaleHeight;
        scalePosX = 10.0f;
        scalePosY = 320.0f - scaleHeight;

        float textPosX, textPosY;
        textPosX = scalePosX + scaleWidth + 10.0f;


        // Calculate divisions and division locations
        // Hardwired for now

        float drawScaleX = 0.01911016f; // defaults
        float drawScaleY = 0.33095807f;
        float saturation = 1.0f; //saturation
        float brightness = 1.0f; //brightness


//for (int x = XSectionManager.getXSection(0).getNIterates(); x < XSectionManager.getXSection(0).getMaxNIterates(); x++) {




        if (XSectionManager.getXSection(0) != null && XSectionManager.getXSection(0).getNIterates() > 0) {
            drawScaleX = .8f * (canvas.getWidth() / (XSectionManager.getXSection(0).crossSectionMaxX - XSectionManager.getXSection(0).crossSectionMinX));
            drawScaleY = 15.0f * (canvas.getHeight() / (XSectionManager.getXSection(0).crossSectionMaxY - XSectionManager.getXSection(0).crossSectionMinY));

            for (int x = 1; x <= XSectionManager.getXSection(0).getNIterates(); x++){

                float hue = (float) x / (XSectionManager.getXSection(0).getMaxNIterates() + 1 ); //hue
                Color myRGBColor = Color.getHSBColor(hue, saturation, brightness);
                System.out.println("x is " + x + " getMaxNIterates is " + XSectionManager.getXSection(0).getMaxNIterates());
                gl.glColor3f(myRGBColor.getRed(), myRGBColor.getGreen(), myRGBColor.getBlue());
                gl.glBegin(GL2.GL_LINE_STRIP);
                gl.glLineWidth(3.5f);


                for (int i = 1; i < XSectionManager.getXSection(0).dMaxValues; i++) {

//                    System.out.println("x is " + x);
                    gl.glVertex3f(XSectionManager.getXSection(0).values[0][i] * drawScaleX,
                            XSectionManager.getXSection(0).values[x][i] * drawScaleY,
                            -1.55f + (x * .001f));
//                    System.out.print("X = " + XSectionManager.getXSection(0).values[0][i] + " ");
//                    System.out.print("Y = " + XSectionManager.getXSection(0).values[x][i] + " \n");
                }
                gl.glEnd();
        }
//            System.out.println("NIterates is " + XSectionManager.getXSection(0).getNIterates());
//            System.out.println("getMaxNIterates is " + XSectionManager.getXSection(0).getMaxNIterates());
        }

//}

        //System.out.println(canvas.getHeight());

        gl.glEnd();
        gl.glColor3f(0.0f, 0.0f, 0.0f);
        gl.glBegin(GL2.GL_LINES);
        gl.glVertex3f(scalePosX * 5, scalePosY + 30.0f, -1.5f);
        gl.glVertex3f(50 + (40000 * drawScaleX), scalePosY + 30.0f, -1.5f);
        gl.glEnd();


        // Draw label tics
        gl.glColor3f(0.0f, 0.0f, 0.0f);
        gl.glBegin(GL2.GL_LINES);
        gl.glVertex3f(50, scalePosY + 30.0f, -1.5f);
        gl.glVertex3f(50, scalePosY + 35.0f, -1.5f);
        //System.out.println("Line 50 * drawScaleX " + (50 * drawScaleX));
        gl.glVertex3f(50 + (10000 * drawScaleX), scalePosY + 30.0f, -1.5f);
        gl.glVertex3f(50 + (10000 * drawScaleX), scalePosY + 35.0f, -1.5f);
        //System.out.println("Line 10050 * drawScaleX " + (10050 * drawScaleX));
        gl.glVertex3f(50 + (20000 * drawScaleX), scalePosY + 30.0f, -1.5f);
        gl.glVertex3f(50 + (20000 * drawScaleX), scalePosY + 35.0f, -1.5f);
        //System.out.println("Line 20050 * drawScaleX " + (20050 * drawScaleX));
        gl.glVertex3f(50 + (30000 * drawScaleX), scalePosY + 30.0f, -1.5f);
        gl.glVertex3f(50 + (30000 * drawScaleX), scalePosY + 35.0f, -1.5f);
        //System.out.println("Line 30050 * drawScaleX " + (30050 * drawScaleX));
        gl.glVertex3f(50 + (40000 * drawScaleX), scalePosY + 30.0f, -1.5f);
        gl.glVertex3f(50 + (40000 * drawScaleX), scalePosY + 35.0f, -1.5f);
        //System.out.println("Line 40050 * drawScaleX " + (40050 * drawScaleX));


        gl.glEnd();

        // Draw labels


        vertScaleTextEngine.beginRendering(canvas.getWidth(), canvas.getHeight());
        gl.glMatrixMode(5888);
        gl.glPushMatrix();

        gl.glRotatef(270, 0, 0, 1);
        vertScaleTextEngine.setColor(0.0f, 0.0f, 0.0f, 1.0f);


        int total = 0;
        //61200

        for (int i = 0; i < 5; i++) {
            textPosY = (float) (10000 * i * drawScaleX);

            String Label = Float.toString(total);
            total = total + 10000;
            vertScaleTextEngine.draw(Label, (int) textPosX - 90, (int) textPosY + 50);

            //System.out.println("TextPosY " + ((int) textPosY -50));
        }

        vertScaleTextEngine.endRendering();

    }

</code>



Reply | Threaded
Open this post in threaded view
|

Re: gl.glColor3f in a loop seems to almost never work?

gouessej
Administrator
Hi

glColor* should be used between glBegin/glEnd even though in theory it should be possible to modify the current color elsewhere.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: gl.glColor3f in a loop seems to almost never work?

sirus20x6
Thanks for the tip! I've moved the color between begin and end (right after begin), but I still get the same behavior.
Reply | Threaded
Open this post in threaded view
|

Re: gl.glColor3f in a loop seems to almost never work?

gouessej
Administrator
Please read the documentation:
http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#getBlue()
https://www.opengl.org/sdk/docs/man2/xhtml/glColor.xml

AWT returns a color component in the range [0; 255] whereas OpenGL expects a color component in the range [0;1].
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: gl.glColor3f in a loop seems to almost never work?

sirus20x6
thanks i forgot the get color component wasnt normalized.
Reply | Threaded
Open this post in threaded view
|

Re: gl.glColor3f in a loop seems to almost never work?

gouessej
Administrator
You're welcome.
Julien Gouesse | Personal blog | Website