Revision 1315 of "कंप्यूटर ग्राफिक्स/2013-14/जीओजीएल-टेम्पलेट" on hiwikiversity

{{कंप्यूटर ग्राफिक्स -- 2013-2014 -- info.uvt.ro/पेज हैडर}}
== पूर्ण उदाहरण ==
दो उदाहरण टेम्पलेट्स यहां दिए गए हैं। एक ऑर्थोग्राफिक प्रोजेक्शन का उपयोग दृश्यों के लिए और एक पर्सपेक्टिव प्रोजेक्शन का उपयोग दृश्यों के लिए। उदाहरणों को भविष्य के जीओजीएल(JOGL) आधारित एप्लीकेशन के लिए टेम्पलेट्स के रूप में इस्तेमाल किया जा सकता हैं।

=== ऑर्थोग्राफिक प्रोजेक्शन उदाहरण ===

<source lang="java">
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.fixedfunc.GLMatrixFunc;

import javax.swing.JFrame;

import com.jogamp.opengl.util.Animator;

public class MainFrame
		extends JFrame
		implements GLEventListener
{

	private GLCanvas canvas;
	private Animator animator;

	// क्लिपिंग प्लेन की स्थिति को निर्दिष्ट करने के लिए (दूरी में वृद्धि/कमी करें) इस चर को संशोधित करें
 	//यह glOrtho method द्वारा उपयोग किया जाता है
	private double v_size = 1.0;

	// एप्लीकेशन मैन प्रवेश बिंदु
	public static void main(String args[]) 
	{
		new MainFrame();
	}

	// डिफ़ॉल्ट कन्स्ट्रक्टर
	public MainFrame()
	{
		super("Java OpenGL");
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		
		this.setSize(800, 600);
		
		this.initializeJogl();
		
		this.setVisible(true);
	}
	
	private void initializeJogl()
	{
		// एक नया जीएल प्रोफाइल बनाना
		GLProfile glprofile = GLProfile.getDefault();
		//ओपनजीएल पैरामीटर में हेरफेर करने के लिए ऑब्जेक्ट बनाना
		GLCapabilities capabilities = new GLCapabilities(glprofile);
		
		// कुछ ओपनजीएल पैरामीटर सेट करना
		capabilities.setHardwareAccelerated(true);
		capabilities.setDoubleBuffered(true);

		// 2x एन्टी अलियासिंग सक्षम करने का प्रयास करें यह अधिकतर हार्डवेयर पर समर्थित होना चाहिए।
	 capabilities.setNumSamples(2);
        	capabilities.setSampleBuffers(true);
		
		// एक ओपन जीएल डिस्प्ले विजेट बनाना - कैनवास
		this.canvas = new GLCanvas(capabilities);
		
		// फ्रेम के केंद्र में कैनवास जोड़ना
		this.getContentPane().add(this.canvas);
		
		// कैनवास के लिए एक ओपन जीएल इवेंट श्रोता को जोड़ना
		this.canvas.addGLEventListener(this);
		
		// एक एनीमेटर बनाना जिससे स्क्रीन प्रति सेकंड 40 बार प्रतिलिपि होगा।
		this.animator = new Animator(this.canvas);
			
		// एनीमेटर शुरू करना
		this.animator.start();
	}
	
	public void init(GLAutoDrawable canvas)
	{
		// कैनवास से संबंधित जीएल उदाहरण प्राप्त करना
		GL2 gl = canvas.getGL().getGL2();
		
		// स्पष्ट रंग सेट करना - रंग जिसका उपयोग कैनवास को मिटाने के लिए किया जाएगा।
		gl.glClearColor(0, 0, 0, 0);
		
		//मॉडलव्यु मैट्रिक्स का चयन करना
		gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);

	}
	
	public void display(GLAutoDrawable canvas)
	{
		GL2 gl = canvas.getGL().getGL2();
		
		// कैनवास मिटाकर - यह स्पष्ट रंग से भरना
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);

		// Add your scene code here
		
		// Forcing the scene to be rendered.
		gl.glFlush();
	}
	
	public void reshape(GLAutoDrawable canvas, int left, int top, int width, int height)
	{
		GL2 gl = canvas.getGL().getGL2();
		
		// Selecting the viewport -- the display area -- to be the entire widget.
		gl.glViewport(0, 0, width, height);
		
		// Determining the width to height ratio of the widget.
		double ratio = (double) width / (double) height;
		
		// Selecting the projection matrix.
		gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
		
		gl.glLoadIdentity();
		
		// Selecting the view volume to be x from 0 to 1, y from 0 to 1, z from -1 to 1.
		// But we are careful to keep the aspect ratio and enlarging the width or the height.
		if (ratio < 1)
			gl.glOrtho(-v_size, v_size, -v_size, v_size / ratio, -1, 1);
		else
			gl.glOrtho(-v_size, v_size * ratio, -v_size, v_size, -1, 1);

		// Selecting the modelview matrix.
		gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);		
	}
	
	public void displayChanged(GLAutoDrawable canvas, boolean modeChanged, boolean deviceChanged)
	{
		return;
	}

	@Override
	public void dispose(GLAutoDrawable arg0) {
		// TODO Auto-generated method stub
	}
}
</source>

=== पर्सपेक्टिव प्रोजेक्शन उदाहरण ===

<source lang="java">
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;

import javax.swing.JFrame;
 
import com.jogamp.opengl.util.Animator;

public class MainFrame
		extends JFrame
		implements GLEventListener
{

	private GLCanvas canvas;
	private Animator animator;

	private GLU glu;

	// Application main entry point
	public static void main(String args[])
	{
		new MainFrame();
	}

	// Default constructor;
	public MainFrame()
	{
		super("Java OpenGL");
		
		// Registering a window event listener to handle the closing event.
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		
		this.setSize(800, 600);
		
		this.initializeJogl();
		
		this.setVisible(true);
	}
	
	private void initializeJogl()
	{
		// Creating a new GL profile.
		GLProfile glprofile = GLProfile.getDefault();
		// Creating an object to manipulate OpenGL parameters.
		GLCapabilities capabilities = new GLCapabilities(glprofile);
		
		// Setting some OpenGL parameters.
		capabilities.setHardwareAccelerated(true);
		capabilities.setDoubleBuffered(true);

		// Try to enable 2x anti aliasing. It should be supported on most hardware.
	 capabilities.setNumSamples(2);
        	capabilities.setSampleBuffers(true);
		
		// Creating an OpenGL display widget -- canvas.
		this.canvas = new GLCanvas(capabilities);
		
		// Adding the canvas in the center of the frame.
		this.getContentPane().add(this.canvas);
		
		// Adding an OpenGL event listener to the canvas.
		this.canvas.addGLEventListener(this);
		
		// Creating an animator that will redraw the scene 40 times per second.
		this.animator = new Animator(this.canvas);
		
		// Registering the canvas to the animator.
		this.animator.add(this.canvas);
		
		// Starting the animator.
		this.animator.start();
	}
	
	public void init(GLAutoDrawable canvas)
	{
		// Obtaining the GL instance associated with the canvas.
		GL2 gl = canvas.getGL().getGL2();
		
		// Initialize GLU. We'll need it for perspective and camera setup.
		this.glu = GLU.createGLU();

		// Setting the clear color -- the color which will be used to erase the canvas.
		gl.glClearColor(0, 0, 0, 0);
		
		// मॉडलव्यु मैट्रिक्स का चयन करना
		gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);

	}
	
	public void display(GLAutoDrawable canvas)
	{
		GL2 gl = canvas.getGL().getGL2();
		
		//कैनवास मिटाकर - यह स्पष्ट रंग से भरना
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);

		gl.glLoadIdentity();

		// अपना स्क्रीन कोड यहां जोड़ें
		
		// रेन्डरिग करने के लिए स्क्रीन को मजबूर करना
		gl.glFlush();
	}
	
	public void reshape(GLAutoDrawable canvas, int left, int top, int width, int height)
	{
		GL2 gl = canvas.getGL().getGL2();
		
		// व्यूपोर्ट का चयन - डीसप्ल क्षेत्र - पूरे विजेट होने के लिए
		gl.glViewport(0, 0, width, height);
		
		// विजेट का चौड़ाई से ऊँचाई अनुपात निर्धारित करना
		double ratio = (double) width / (double) height;
		
		// प्रोजेक्शन मैट्रिक्स का चयन करना
		gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
		
		gl.glLoadIdentity();
		
		glu.gluPerspective (38, ratio, 0.1, 100);

		// मॉडलव्यु मैट्रिक्स का चयन करना
		gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);		
	}
	
	public void displayChanged(GLAutoDrawable canvas, boolean modeChanged, boolean deviceChanged)
	{
		return;
	}

	@Override
	public void dispose(GLAutoDrawable arg0) {
	
		
	}
}
</source>

[[Category:HI]]
[[Category:कंप्यूटर ग्राफिक्स]]
[[Category:प्रोग्रामिंग उदाहरण]]