简体   繁体   中英

Analog clock in OpenGL 2.0 using Java

Can someone help to make a static analogue clock in OpenGL 2.0 using Java? I used Bresenham Circle to draw a circle for the clock but is there any other way to draw a circle here? Then I drew an hour, minute, and second hand but I got an error in the render part while drawing the numbers. Can someone help me here with how to fix this? The code which I tried is following:

public class MyClock implements GLEventListener {
    private int windowWidth = 1000;
    private int windowHeight = 900;

    public void display(GLAutoDrawable drawable) {
        render(drawable);
    }

    //render
    private void render(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glPointSize(5.0f);

        //drawing circle    
        bresenhamCircle(new Point(100,100), 200, new Color(1, 0, 1), gl);
        double x, y, d, delta_SE, delta_E; 
        x = 0;
        double r = 200;
        y = r;
        d = 5-4*r;

        setPixel(x ,y, gl);
        setPixel(-x,y, gl);
        setPixel(x,-y, gl);
        setPixel(-x,-y, gl);
        setPixel(y,x, gl);
        setPixel(-y,x, gl);
        setPixel(y,-x, gl);
        setPixel(-y,-x, gl);

        while (y>x) {
            if (d >= 0) {
                delta_SE = 4*(2*(x-y)+5);
                d+=delta_SE; x++; y--;
            }
            else {
                delta_E = 4*(2*x+3);
                d+=delta_E; x++;
            } 

            setPixel(x,y, gl);
            setPixel(-x,y, gl);
            setPixel(x,-y, gl);
            setPixel(-x,-y, gl);
            setPixel(y,x, gl);
            setPixel(-y,x, gl);
            setPixel(y,-x, gl);
            setPixel(-y,-x, gl);
        }

        //hour hand
        gl.glColor3d(0, 0, 1);
        gl.glBegin(GL2.GL_LINES);
        gl.glVertex2d(0, 00);
        gl.glVertex2d(70, 70);
        gl.glEnd();

        //minute hand
        gl.glColor3d(0, 0, 1);
        gl.glBegin(GL2.GL_LINES);
        gl.glVertex2d(0, 00);
        gl.glVertex2d(150, 20);
        gl.glEnd();

        //seconds hand
        gl.glColor3d(1, 0, 0);
        gl.glBegin(GL2.GL_LINES);
        gl.glVertex2d(0, 00);
        gl.glVertex2d(120, -120);
        gl.glEnd();

        //drawing numbers
        int AngleX, AngleY;
        int radius;
        double line;
        for (int i=1;i<=12;i++) {
            line = i/12*Math.PI*2;
            radius=170;

            AngleX=(int)((0)+(Math.sin(line)*radius));
            AngleY=(int)((0)+(Math.cos(line)*radius));
            gl.glColor3d(1, 1, 1);
            String a= Integer.toString(i);
            g.drawString(a,AngleX,AngleY);  
        }
    }

    //Bresenham Circle method
    public void bresenhamCircle(Point center, double radius, Color color, GL2 gl) {
        gl.glColor3d(0, 0, 1);
        gl.glBegin(GL2.GL_POINTS);
        gl.glVertex2d(00,200);  
        gl.glEnd();
    }

    private void setPixel(double x, double y,GL2 gl) {
        gl.glColor3d(0.0, 1.0, 0.0);
        gl.glBegin(GL2.GL_POINTS);
        gl.glVertex2d(0,0); 
        gl.glVertex2d( x, y);
        gl.glEnd();
    }

    public void dispose(GLAutoDrawable drawable) {

    }

    public void init(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glViewport(0, 0, windowWidth, windowHeight);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(-windowWidth / 2, windowWidth / 2, -windowHeight / 2, windowHeight / 2, 0, 1);
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
        gl.glEnable(GL2.GL_LINE_SMOOTH);
    }


    public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
        GL2 gl = drawable.getGL().getGL2();
        windowWidth = w;
        windowHeight = h;
        gl.glViewport(0, 0, w, h);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(-w / 2, w / 2, -h / 2, h / 2, 0, 1);
    }
}

As far as I know, there isn't a simple method built into OpenGL ES 2.0 that draws text. That means you have to either find an open-source library that you can use, or you can create your own way of rendering text. This post outlines nicely the different ways that you can render text with OpenGL.

The best way I have found based on my research and which is also stated in the above link, is to render text through images. This tutorial shows how to create a basic text engine and is what I used to get an idea of how to render custom text. The idea is to take a texture atlas ( a texture atlas is one image that contains all the letters, numbers, characters that you want to be able to render ) and based on what string you want to draw, the engine will crop out the necessary letters, numbers, or characters from the atlas needed for your string and combine them into a square polygon that you can then render to the screen. The tutorial that I linked is a basic text engine, but once you understand how it works, you can then modify and improve it to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM