Wednesday, September 28, 2016

Play a video or video capture of webcam - texture on cube #OpenGL #OpenCV

Just visit this awesome site where there is this awesome code which needed a few modifications to play back any video or capture video from the webcam. Now you won't believe this but it was as simple as that. With OpenCV I feel I just turned a pro.

Snapshot:


The mouse can be used as a trackball and you can rotate the scene which consists of a cube on both sides of which texture of video frames is mapped. That snapshot was not still image, it is me sitting in front of the computer poring over the monitor.

Monday, September 26, 2016

Add an image opened using #OpenCV as a texture in OpenGL

Image result for what is OpenCVI was toying with OpenCV since a long time. Here is a tutorial on how to download and configure eclipse for OpenCV. OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel's research center in Nizhny Novgorod (Russia), it was later supported by Willow Garage and is now maintained by Itseez.

In this C++ project in Eclipse an image is opened using imread method of OpenCV tool and the opened image is used as a texture to map to a polygon drawn across the screen. Actually this image in the window is itself a snapshot of another program which does texture mapping inside a 3D house scene program. I could have chosen any other image.

Snapshot:

Here is the source.

Sunday, September 18, 2016

#JOGL Various Textures to Polygons

Some textures of size 10 x 10 have been created from scratch using formulas such as

//Use this formula to create texture in the program- Tex1
int red=(0xff)*(x%2);
int green=(0xff)*(y%2);
int blue=(0xff);//*(x^y);

Resulting Snapshot:

//Use this formula to create texture in the program- Tex2
int red=(int)((0xff)*Math.sin(10*x*Math.PI/180));
int green=(0xff);
int blue=(0xff);
Resulting Snapshot:                       
Going any further I feel would kill your drive to do something yourself and get exhilarating textures. So I hope I have shown the way to create beautiful patterns with sin,cos,modulus and other mathematical operations.

If you are knowledgeable about MipMaps and filtering in jogl then you may note that I have used following Magnification filter.

gl2.glTexParameteri(GL2.GL_TEXTURE_2D,GL2.GL_TEXTURE_MAG_FILTER,GL2.GL_NEAREST);

Here is the source which you can modify and tinker.

Next goal is to  use procedural methods to generate textures as mentioned in the site here.

A textile manufacture in my area Apsara silks is featured here in this article on https://www.indiantextilemagazine.in/apsara-silks-exploring-new-frontiers-to-stay-ahead/
I infact had an appointment with Mr goenka that I didn't keep up with. It was supposed to be a website that this manufacturer wanted to setup. All talk of industry academia tieup is hocus pocus as academia is a lot poorer and industry indifferent. So if you want more stupidity than skipping appointments you could always conjure up websites that let you design a pattern and get the digital print on fabric of your choice within days if not weeks.

#JOGL Lighting - An Animation

In this project the light source is moving in a circular path around the eight spheres. The position of GL_LIGHT0 is changed by a call to the method glLightfv().

Snapshot(GIF):

Here is the source.

#JOGL Setting Material Properties

In this program LIGHTING has been enabled and the material properties have been set.
A glutSolidSphere() has been drawn eight times each time the SHININESS component has been increased by 16.
Snapshot:

Here is the source.

Friday, September 9, 2016

#JOGL Cubes with Vertex Arrays

Here is a simple program to draw a cube with vertex arrays that is shared as a demo program by the author David J Eck in his site of Hogwarts and William Smith college.

I am sharing this program as is and all copyrights rest with the original author.

Snapshot:


Here is the source.

#JOGL Program to create and apply a one dimensional texture on a cube

In this program I am applying a one dimensional texture on a cube.

Snapshot:


Here is  the source.

#JOGL Applying texture to polygon created from a byte array

Here I am applying a texture to the polygon after creating the texture from a byte buffer. A variety of textures can be created this way.

Snapshot:


Here is the source. A few modifications to this program allows you to create different textures by just changing the formula. Here is the same program with variety of textures mentioned as formulas.

#JOGL A simple program to apply a texture to cube faces and rotate the cube

This program not only texture is applied to a cube but the cube is made to rotate on pressing the arrow keys.

Snapshot:


Here is the code.

#JOGL A simple program to apply texture(image) to a polygon

A simple program in Java bindings for OpenGL where I apply a texture to a square(polygon). The texture is imported from an image file. The image is of a cartoon character Jerry whom i like very much. I like other characters too like tom and the dog.

Snapshot:


Here is the code.

#JOGL A simple Paddle Wheel

In this program, which the author David J Eck describes as a simple 3D program(chapter: Geometry) in his graphics book, a paddle wheel is being drawn. We can define a polygon in the x-y plane above the x axis and rotate it wrt the x axis each time with varying angles. Then to get a 3D look we tilt the paddle wheel by rotating about y axis.

Snapshot:


Here is the source.

#JOGL Simple program to draw a triangle using a display list

In this simple program a triangle is being drawn using a display list. The display list is identified by an integer. The integer is generated by call to glGenLists(). Then calls to glNewList() and glEndList() defines the list. Finally a call of glCallList() is made to draw whatever has been defined in the display list.
Snapshot:
Here is the source.

#JOGL Simple Program to draw a Triangle and rotate it about z-axis

Although the use of JPanel and calling repaint method on it is a good way to draw in Java Bindings for OpenGL I have followed the other method of creating a GLCanvas object and adding it to the JFrame object. In this program I have created a basic frame which sets the background color to red and clears the canvas and then draws a triangle. Each time the display is called by the FPSAnimator object the triangle is drawn at a new position after rotation by an angle. Angle is incremented in the display function itself.

Snapshot:

Here is the source in case you are interested to know.

Friday, September 2, 2016

Translate a Circle by pressing Swing Class JButton in #JOGL

This program translates the circle on pressing the translate button.


Here is the source:(online Viewing)


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;

import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.awt.GLJPanel;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class CircleTranslate extends GLJPanel implements GLEventListener,ActionListener {

/**
 * Interface to the GLU library.
 */
private GLU glu;
int x,y;
static JButton okButton;
/**
 * Take care of initialization here.
 */


CircleTranslate()
{
// super(g);
super( new GLCapabilities(null) ); // Makes a panel with default OpenGL "capabilities".
GLJPanel drawable = new GLJPanel();               // new GLJPanel inside GLJPanel
drawable.setPreferredSize(new Dimension(200,100));
setLayout(new BorderLayout());
add(drawable, BorderLayout.CENTER);
drawable.addGLEventListener(this); // Set up events for OpenGL drawing!
// drawable.addMouseListener(this);
//drawable.addMouseMotionListener(this);
//drawable.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {
// System.exit(0);
x++;
repaint();
}
public void init(GLAutoDrawable gld) {
    GL2 gl = gld.getGL().getGL2();
    glu = new GLU();

    gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//    gl.glViewport(-250, -150, 250, 150);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluOrtho2D(-250.0, 250.0, -150.0, 150.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    x=10;y=30;
    repaint();
}

/**
 * Take care of drawing here.
 */
public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
    /*
     * put your code here
     */
   
    drawCircle(gl, x, y, 50);
}

public void reshape(GLAutoDrawable drawable, int x, int y, int width,
        int height) {
}


private void drawCircle(GL2 gl, int x1, int y1, int r) {
    gl.glPointSize(1.0f);
    gl.glBegin(GL2.GL_POINTS);
   
    int x=0,y=r,p=1-r;
    while(x<y)
    {
   
    if(p<0)
    p+=2*x+3;
    else {
    p+=2*(x-y)+5;
    y--;
    }
    x++;
    circleSymmetry(gl,x1,y1,x,y);

    }
    gl.glEnd();//end drawing of points

}
public void dispose(GLAutoDrawable arg0)
{
}
public void circleSymmetry(GL2 gl,int xc,int yc,int x,int y)
{
gl.glVertex2i(xc+x , yc+y );
gl.glVertex2i(xc-x , yc+y );
gl.glVertex2i(xc+x , yc-y );
gl.glVertex2i(xc-x , yc-y );
gl.glVertex2i(xc+y , yc+x );
gl.glVertex2i(xc+y , yc-x );
gl.glVertex2i(xc-y , yc+x );
gl.glVertex2i(xc-y , yc-x );
}
public static void main(String args[])
{
JFrame window = new JFrame("Circle Translate");
    // The canvas
    CircleTranslate panel = new CircleTranslate();
    panel.setPreferredSize(new Dimension(1000,1000));      
    okButton = new JButton("Translate");
    JPanel content = new JPanel();

content.setLayout(new BorderLayout());
content.add(panel, BorderLayout.CENTER);
content.add(okButton, BorderLayout.SOUTH);
    okButton.addActionListener(panel);
   
    window.setContentPane(content);
    window.pack();
    window.setLocation(0,0);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
    panel.requestFocusInWindow();
}
}
Thanks!!!