# OpenGL question

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

``````

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

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

} 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();
}
}
``````
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
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

//3. add the canvas to fill the Frame container

//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.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) {
}
}
``````
0
Commented:
>> 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
Author Commented:
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
Commented:
>>  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 Commented:
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

``````/*
* 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);

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

``````
0
Commented:
ah sorry, i forgot, you have to set a timer, which calls the display-function every x milliseconds ..
0
Author Commented:
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 Commented:
by the way how do you set that timer

thanks i am so new to openGL
0
Commented:
>> 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
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.