Skip to content

Commit

Permalink
makes 2d text draw available publicly
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-pernollet committed Nov 6, 2020
1 parent adf1f70 commit 0f7a423
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
47 changes: 13 additions & 34 deletions src/main/java/jgl/GL.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,34 +112,15 @@ private void debugWriteImageTo(String file, RenderedImage image) {

/* ******************** PROVIDE IMAGE ********************/

/** void glXSwapBuffers (Display *dpy, GLXDrawable drawable) */
// public void glXSwapBuffers (Graphics g, Applet o) {
/**
* Draws the image buffer that was built by {@link GL#glFlush()} with the caller {@link Graphics} context
*
* OpenGL equivalent:
*
* <code>void glXSwapBuffers (Display *dpy, GLXDrawable drawable)</code>
*/
public void glXSwapBuffers(Graphics g, ImageObserver o) {
// if (JavaImage != null) g.drawImage (JavaImage, StartX, StartY, o);

// DEBUG CHART
/*
* if(JavaImage==null)
* System.err.println("WARNING : GL.glXSwap has null image");
*
*/

g.drawImage(JavaImage, StartX, StartY, o);

// if (Context.RenderMode != GL_RENDER) { return; }
// int i, j, k = 0;
// for (i = StartY; i < Context.Viewport.Height; i++) {
// for (j = StartX; j < Context.Viewport.Width; j++) {
// g.setColor(new Color (Context.ColorBuffer.Buffer[k++]));
// g.drawLine(j, i, j, i);
// }
// }

// add for Java2D
// Graphics2D g2 = (Graphics2D) g;
// g2.drawImage (JavaImage, 0, 0, o);

// System.out.println ("call draw image");
}

public void glXSwapBuffers(Graphics g, Applet o) {
Expand Down Expand Up @@ -184,7 +165,7 @@ public void glFlush() {

// Generates an image from the toolkit to use this producer
Image colorBuffer = JavaComponent.createImage(producer);

// ------------------------------------------
// Write GL content in a temporary image
// and then to the image returned to Canvas
Expand Down Expand Up @@ -217,7 +198,7 @@ public void glFlush() {
}
}

private void hackClearColorWithG2DfillRect(Graphics2D g2d) {
protected void hackClearColorWithG2DfillRect(Graphics2D g2d) {
Color backgroundColor = getClearColorAWT();
g2d.setColor(backgroundColor);
g2d.fillRect(0, 0, Context.Viewport.Width, Context.Viewport.Height);
Expand All @@ -227,10 +208,8 @@ public static Color glIntToColor(int color) {
float r = gl_util.ItoR(color);
float g = gl_util.ItoG(color);
float b = gl_util.ItoB(color);
float a = gl_util.ItoA(color);

// return new Color((int)r,(int)g,(int)b);//,(int)a);
return new Color(r / 255, g / 255, b / 255);// ,(int)a);
//float a = gl_util.ItoA(color);
return new Color(r / 255, g / 255, b / 255);
}

public Color getClearColorAWT() {
Expand Down Expand Up @@ -266,7 +245,7 @@ protected void drawText(Graphics2D g2d) {
* To be called by {@link GLUT#glutBitmapString(Font, String, float, float)} to
* append text to a list of text to render at {@link GL#glFlush()} step.
*/
protected void appendText(Font font, String string, int x, int y) {
public void appendTextToDraw(Font font, String string, int x, int y) {
synchronized (textsToDraw) {
textsToDraw.add(new TextToDraw(font, string, x, y));
}
Expand All @@ -276,7 +255,7 @@ protected void appendText(Font font, String string, int x, int y) {
* To be called by {@link GLUT#glutBitmapString(Font, String, float, float)} to
* append text to a list of text to render at {@link GL#glFlush()} step.
*/
protected void appendText(Font font, String string, int x, int y, float r, float g, float b) {
public void appendTextToDraw(Font font, String string, int x, int y, float r, float g, float b) {
synchronized (textsToDraw) {
textsToDraw.add(new TextToDraw(font, string, x, y, r, g, b));
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/jgl/GLCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@ public void update(Graphics g) {
}

public void paint(Graphics g) {
System.out.println("want to draw : " + kPaint);
myGL.glXSwapBuffers(g, this);
g.drawString("GLCanvas.paint(Graphics g) : " + kPaint, 10, 12*6);

//System.out.println("SWAPPED" );
// firePostPaintEvents(g);

kPaint++;
}
int kPaint = 0;

// ************ RETRIEVE RENDERING CONTEXT ************ //

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/jgl/GLUT.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public GLUT(GL myGL) {
* @see {@link #glutBitmapString(Font, String, float, float, float, float, float, float) to avoid doing the model-to-screen projection.
*/
public void glutBitmapString(Font font, String string, float x, float y) {
JavaGL.appendText(font, string, (int)x, (int)y);
JavaGL.appendTextToDraw(font, string, (int)x, (int)y);
}

/**
Expand All @@ -125,7 +125,7 @@ public void glutBitmapString(Font font, String string, float x, float y) {
* </code></pre>
*
* Behind the scene it makes the model-to-screen conversion and then
* provide all data to {@link GL#appendText(Font, String, int, int, float, float, float)}
* provide all data to {@link GL#appendTextToDraw(Font, String, int, int, float, float, float)}
* that will handle the text rendering in {@link GL#glFlush()}
*/
public void glutBitmapString(Font font, String string, float x, float y, float z, float r, float g, float b) {
Expand All @@ -135,7 +135,7 @@ public void glutBitmapString(Font font, String string, float x, float y, float z
double winY = JavaGL.Context.Viewport.Height - win[1];


JavaGL.appendText(font, string, (int)winX, (int)winY, r, g, b);
JavaGL.appendTextToDraw(font, string, (int)winX, (int)winY, r, g, b);
}

protected double[] modelToScreen(float x, float y, float z) {
Expand Down Expand Up @@ -876,8 +876,7 @@ else if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
*/
public void processMouseMotionEvent(MouseEvent e) {
if(motionMethod==null) {
System.err.println("Motion method is undefined, please call glut.glutMotionFunc(\"doMotion\")");

//System.err.println("Motion method is undefined, please call glut.glutMotionFunc(\"doMotion\")");
return;
}

Expand Down

0 comments on commit 0f7a423

Please sign in to comment.