[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

OpenGL question

Posted on 2008-02-11
10
Medium Priority
?
761 Views
Last Modified: 2013-12-14
hi everyone,
I am learning OpenGL and trying to do an question in the book which is to draw a point running along a circle. I was able to draw the point in the circle already but when it comes to moving it along the circle I have big trouble. I did use some kind of loops to loops the drawing points along the circle but when I am running the program, it create a line in the circle instead of showing me point by point moving along the circle.

How can I fix that, I think it is the display function that only show the finish result or something like that

please help


 
import java.awt.*;
import javax.swing.*;
import javax.media.opengl.*;
 
public class ductranhw1 extends J1_0_Point {
 
    double centerX = WIDTH / 2, centerY = HEIGHT / 2, radius = WIDTH / 3, theta = 0;
    double delta = 4 / (Math.PI * radius);
 
    public ductranhw1() {
        capabilities.setDoubleBuffered(true);
    }
 
    public void display(GLAutoDrawable drawable) {
 
        GL gl = drawable.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
 
        // draw circle
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        gl.glPointSize(4);
        drawCircle(centerX, centerY, radius);
 
        for (int i = 0; i < 50; i++) {
            //draw red point
            theta = theta + delta;
            double x = radius * Math.cos(theta) + centerX;
            double y = radius * Math.sin(theta) + centerY;
            gl.glPointSize(10);
            gl.glColor3f(1.0f, 0.0f, 0.0f);
 
            try {
                drawPoint(x, y);
                Thread.sleep(10);
 
            } catch (Exception ignore) {
            }
        }
    }
 
    public static void main(String[] args) {
        ductranhw1 frame = new ductranhw1();
        frame.setTitle("Duc Tran Minh Homework1");
        frame.setSize(WIDTH, HEIGHT);
        frame.setVisible(true);
    }
 
    private void drawCircle(double centerX, double centerY, double radius) {
        double th = 0;
        while (th <= 2 * Math.PI) {
            th = th + delta;
            double x = radius * Math.cos(th) + centerX;
            double y = radius * Math.sin(th) + centerY;
            drawPoint(x, y);
        }
    }
 
    private void drawPoint(double x, double y) {
        gl.glBegin(GL.GL_POINTS);
        gl.glVertex2d(x, y);
        gl.glEnd();
    }
}

Open in new window

0
Comment
Question by:dminh01
  • 5
  • 5
10 Comments
 
LVL 20

Accepted Solution

by:
ikework earned 1500 total points
ID: 20867338
do you mean rendered with the "display"-memberfunc orr with "drawCircle"

where did you define WIDTH and HEIGHT?

you use a pointsize of 10 in display

gl.glPointSize(10);

try to set it to 1 after the loop


ike
0
 

Author Comment

by:dminh01
ID: 20867519
Basically I wanted to render with display() function. The main idea is to draw that red circle to MOVE ALONG the circle.
However I was only able to draw that circle but NOT MOVING. As the book say, isnt the dispay is suppose to automatically loop?

The WIDTH AND HEIGHT is defined in a given class. Here is it



/***********************************************************
 * Created on 2004-2-20
 * @author Jim X. Chen: draw a point
 * Modified 2007-9-8 for the new JOGL system
 */
import java.awt.*;
import java.awt.event.*;
 
import javax.media.opengl.*;
import com.sun.opengl.util.*;
import javax.media.opengl.glu.GLU;
 
public class J1_0_Point extends Frame implements
        GLEventListener {
 
    static int HEIGHT = 600,  WIDTH = 600;
    static GL gl; //interface to OpenGL
    static GLCanvas canvas; // drawable in a frame
    static GLCapabilities capabilities;
 
    public J1_0_Point() {
 
        //1. specify a drawable: canvas
        capabilities = new GLCapabilities();
        canvas = new GLCanvas();
 
 
        //2. listen to the events related to canvas: reshape
        canvas.addGLEventListener(this);
 
        //3. add the canvas to fill the Frame container
        add(canvas, BorderLayout.CENTER);
 
        //4. interface to OpenGL functions
        gl = canvas.getGL();
    }
 
    public static void main(String[] args) {
        J1_0_Point frame = new J1_0_Point();
 
        //5. set the size of the frame and make it visible
        frame.setSize(WIDTH, HEIGHT);
        frame.setVisible(true);
    }
 
 
    // called once for OpenGL initialization
    public void init(GLAutoDrawable drawable) {
 
        //6. specify a drawing color: red
        gl.glColor3f(1.0f, 0.0f, 0.0f);
    }
 
 
    // called for handling reshaped drawing area
    public void reshape(
            GLAutoDrawable drawable,
            int x,
            int y,
            int width,
            int height) {
 
        WIDTH = width; // new width and height saved
        HEIGHT = height;
 
        //7. specify the drawing area (frame) coordinates
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(0, width, 0, height, -1.0, 1.0);
    }
 
 
    // called for OpenGL rendering every reshape
    public void display(GLAutoDrawable drawable) {
 
        //8. specify to draw a point
        //gl.glPointSize(10);
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glBegin(GL.GL_POINTS);
        gl.glVertex2i(WIDTH / 2, HEIGHT / 2);
        gl.glEnd();
    }
 
 
    // called if display mode or device are changed
    public void displayChanged(
            GLAutoDrawable drawable,
            boolean modeChanged,
            boolean deviceChanged) {
    }
}

Open in new window

0
 
LVL 20

Expert Comment

by:ikework
ID: 20867772
>> However I was only able to draw that circle but NOT MOVING

can you explain that? so simply rendering the circle with "drawCircle" works?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:dminh01
ID: 20867842
Yes. I was trying to move the POINT ALONG THE CIRCLE.
So the circle is dead while the point must move point to another point in the circle

Thanks

I already success draw the circle and one point in it. However to make the POINT MOVE ALONG THE CIRCLE is the problem

Thanks
0
 
LVL 20

Expert Comment

by:ikework
ID: 20868058
>>  However to make the POINT MOVE ALONG THE CIRCLE is the problem

in your renderloop only render one point per frame, increase theta in that frame and in next frame the next point is rendered. you need a new member-variable in your class, i called it "theta":

    // new member in your class
    double theta = 0;

    public void display(GLAutoDrawable drawable) {
 
        GL gl = drawable.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
 
        // draw circle
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        gl.glPointSize(4);

        double x = radius * Math.cos(theta) + centerX;
        double y = radius * Math.sin(theta) + centerY;
        drawPoint(x, y);

        theta = theta + delta;
        if( theta > 2 * Math.PI ) theta = 0.0f;
    }

0
 

Author Comment

by:dminh01
ID: 20868163
I tried your solution but it does not work. the point does not move along the circle. It still stay at one point in the circle

By the way I did have THETA=0 at declaration

Thanks

Any other advice
/*
 * Name: Duc Tran Minh G00450548
 * CS 451 Homework 1
 */
 
import java.awt.*;
import javax.swing.*;
import javax.media.opengl.*;
 
public class ductranhw1 extends J1_0_Point {
 
    double centerX = WIDTH / 2, centerY = HEIGHT / 2, radius = WIDTH / 3, theta = 0;
    double delta = 4 / (Math.PI * radius);
 
    public ductranhw1() {
        capabilities.setDoubleBuffered(true);
 
    }
 
    public void display(GLAutoDrawable drawable) {
 
 
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
 
        // draw circle
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        gl.glPointSize(4);
        drawCircle(centerX, centerY, radius);
 
 
        //draw red point
 
        double x = radius * Math.cos(theta) + centerX;
        double y = radius * Math.sin(theta) + centerY;
        gl.glPointSize(10);
        gl.glColor3f(1.0f, 0.0f, 0.0f);
        drawPoint(x, y);
        
        theta = theta + delta;
        if (theta > 2 * Math.PI) {
            theta = 0.0f;
        }
        
        
        try {
            Thread.sleep(100);
        } catch (Exception ignore) {
 
        }
    }
 
    public static void main(String[] args) {
        ductranhw1 frame = new ductranhw1();
        frame.setTitle("Duc Tran Minh Homework1");
        frame.setSize(WIDTH, HEIGHT);
        frame.setVisible(true);
    }
 
    private void drawCircle(double centerX, double centerY, double radius) {
        double th = 0;
        while (th <= 2 * Math.PI) {
            th = th + delta;
            double x = radius * Math.cos(th) + centerX;
            double y = radius * Math.sin(th) + centerY;
            drawPoint(x, y);
        }
    }
 
    private void drawPoint(double x, double y) {
        gl.glBegin(GL.GL_POINTS);
        gl.glVertex2d(x, y);
        gl.glEnd();
    }
}
 
  

Open in new window

0
 
LVL 20

Expert Comment

by:ikework
ID: 20868815
ah sorry, i forgot, you have to set a timer, which calls the display-function every x milliseconds ..
0
 

Author Comment

by:dminh01
ID: 20869376
that sound right because it seems like the display function only draw pics when it finish drawing. Is there anyway to show the process of display function

thanks
0
 

Author Comment

by:dminh01
ID: 20869391
by the way how do you set that timer

thanks i am so new to openGL
0
 
LVL 20

Expert Comment

by:ikework
ID: 20870028
>> only draw pics when it finish drawing
right, unless you call swap-buffers yourself

>> Is there anyway to show the process of display function
not sure, what you mean ..

>> by the way how do you set that timer
that's not about opengl, you have to set a java-timer, here are some examples:

http://www.javapractices.com/topic/TopicAction.do?Id=54
http://kickjava.com/1830.htm

0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question