diff options
Diffstat (limited to 'src/redbook')
119 files changed, 19836 insertions, 0 deletions
diff --git a/src/redbook/.gitignore b/src/redbook/.gitignore new file mode 100644 index 0000000..32ac8f1 --- /dev/null +++ b/src/redbook/.gitignore @@ -0,0 +1,6 @@ +build +dist +jogl-dist +lib/nblibraries-private.properties +**~ +nbproject/private diff --git a/src/redbook/applet.policy b/src/redbook/applet.policy new file mode 100644 index 0000000..53a9d40 --- /dev/null +++ b/src/redbook/applet.policy @@ -0,0 +1,3 @@ +grant { +permission java.security.AllPermission; +}; diff --git a/src/redbook/build.xml b/src/redbook/build.xml new file mode 100644 index 0000000..af316d5 --- /dev/null +++ b/src/redbook/build.xml @@ -0,0 +1,125 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<!-- You may freely edit this file. See commented blocks below for -->
+<!-- some examples of how to customize the build. -->
+<project name="redbook" default="default" basedir=".">
+ <description>Builds, tests, and runs the project redbook.</description>
+ <import file="nbproject/build-impl.xml"/>
+ <import file="nbproject/build-jogl-template-impl.xml"/>
+ <!--
+
+ There exist several targets which are by default empty and which can be
+ used for execution of your tasks. These targets are usually executed
+ before and after some main targets. They are:
+
+ -pre-init: called before initialization of project properties
+ -post-init: called after initialization of project properties
+ -pre-compile: called before javac compilation
+ -post-compile: called after javac compilation
+ -pre-compile-single: called before javac compilation of single file
+ -post-compile-single: called after javac compilation of single file
+ -pre-compile-test: called before javac compilation of JUnit tests
+ -post-compile-test: called after javac compilation of JUnit tests
+ -pre-compile-test-single: called before javac compilation of single JUnit test
+ -post-compile-test-single: called after javac compilation of single JUunit test
+ -pre-jar: called before JAR building
+ -post-jar: called after JAR building
+ -post-clean: called after cleaning build products
+
+ (Targets beginning with '-' are not intended to be called on their own.)
+
+ Example of inserting an obfuscator after compilation could look like this:
+
+ <target name="-post-compile">
+ <obfuscate>
+ <fileset dir="${build.classes.dir}"/>
+ </obfuscate>
+ </target>
+
+ For list of available properties check the imported
+ nbproject/build-impl.xml file.
+
+
+ Another way to customize the build is by overriding existing main targets.
+ The targets of interest are:
+
+ -init-macrodef-javac: defines macro for javac compilation
+ -init-macrodef-junit: defines macro for junit execution
+ -init-macrodef-debug: defines macro for class debugging
+ -init-macrodef-java: defines macro for class execution
+ -do-jar-with-manifest: JAR building (if you are using a manifest)
+ -do-jar-without-manifest: JAR building (if you are not using a manifest)
+ run: execution of project
+ -javadoc-build: Javadoc generation
+ test-report: JUnit report generation
+
+ An example of overriding the target for project execution could look like this:
+
+ <target name="run" depends="JavaApplication3-impl.jar">
+ <exec dir="bin" executable="launcher.exe">
+ <arg file="${dist.jar}"/>
+ </exec>
+ </target>
+
+ Notice that the overridden target depends on the jar target and not only on
+ the compile target as the regular run target does. Again, for a list of available
+ properties which you can use, check the target you are overriding in the
+ nbproject/build-impl.xml file.
+
+ -->
+
+ <!--
+
+ This is a predefined -post-jar target to trigger the jogl-template specific
+ part of the build build.
+
+ Feel free to customize this targets as long as you preserve the dependency
+ on -jogl-template-post-jar.
+
+ If you need to do some work _before_ the -jogl-template-post-jar target is
+ called, use
+ -->
+ <target name="-post-init" depends="-check-if-jogl-downloaded,-download-jogl-dist" >
+ </target>
+
+ <target name="-download-jogl-dist" unless="jogl.dist.present">
+
+ <fail unless="natives.platform" message="please set the 'natives.platform' property in nbproject/project.properties or in nbproject/private/private.properties" />
+
+ <echo message="lay back and relax, I am downloading jogl..."/>
+ <copy todir="jogl-dist" flatten="true" verbose="true">
+ <resources>
+ <url url="${jogl.webres}/jogl-2.0-webstart.zip"/>
+ <url url="${jogl.webres}/nativewindow-2.0-webstart.zip"/>
+ <url url="${jogl.webres}/newt-2.0-webstart.zip"/>
+ <url url="${jogl.webres}/gluegen-rt-2.0-webstart.zip"/>
+ <url url="${jogl.webres}/jogl-2.0-docs.zip"/>
+ </resources>
+ </copy>
+ <echo message="done"/>
+
+ <antcall target="jogl-template-deploy-jogl" inheritall="true"/>
+ </target>
+
+ <target name="-check-if-jogl-downloaded">
+ <mkdir dir="jogl-dist"/>
+ <available file="jogl-dist/jogl-2.0-webstart.zip" property="jogl.dist.present"/>
+ </target>
+
+ <target name="-post-jar" depends="-jogl-template-post-jar">
+ </target>
+
+ <!--
+ This is a predefined jnlp target to trigger the jogl-template specific
+ patches to the file generated by netbeans webstart support.
+ -->
+ <target name="jnlp" depends="-jogl-template-jnlp">
+ </target>
+
+ <!--
+ This is a predefined -post-clean target to trigger the jogl-template specific
+ cleanup of the projects build.
+ -->
+ <target name="-post-clean" depends="-jogl-template-post-clean">
+ </target>
+
+</project>
diff --git a/src/redbook/etc/Util.java b/src/redbook/etc/Util.java new file mode 100644 index 0000000..77e3e7f --- /dev/null +++ b/src/redbook/etc/Util.java @@ -0,0 +1,32 @@ +package etc; + +import java.util.Scanner; +import java.util.TreeSet; + +/** + * + * @author mbien + */ +public class Util { + + + public static void main(String[] args) { + Scanner scanner = new Scanner(Util.class.getResourceAsStream("redbook.txt")); + + TreeSet<String> set = new TreeSet<String>(); + while(scanner.hasNext()) + set.add(scanner.next()); + +// for (String item : set) { +// System.out.println("<applet width='${JNLP.APPLET.WIDTH}' height='${JNLP.APPLET.HEIGHT}'>"); +// System.out.println(" <param name='jnlp_href' value='${JNLP.FILE}'/>"); +// System.out.println(" <param name='demo' value='"+item+"'/>"); +// System.out.println("</applet>"); +// } + for (String item : set) { + System.out.println("<option value='"+item+"' selected>"+item); + } + + + } +} diff --git a/src/redbook/etc/redbook.txt b/src/redbook/etc/redbook.txt new file mode 100644 index 0000000..3671a5b --- /dev/null +++ b/src/redbook/etc/redbook.txt @@ -0,0 +1,92 @@ +glredbook10.accanti +glredbook10.accnot +glredbook10.accpersp +glredbook10.accum +glredbook10.aim +glredbook10.alpha +glredbook10.alpha3D +glredbook10.anti +glredbook10.antipoint +glredbook10.antipoly +glredbook10.bezcurve +glredbook10.bezmesh +glredbook10.bezsurf +glredbook12x.blendeqn +glredbook10.checker +glredbook10.checker2 +glredbook10.chess +glredbook10.clip +glredbook10.colormat +glredbook12x.colormatrix +glredbook12x.colortable +glredbook1314.combiner +glredbook10.cone +glredbook12x.convolution +glredbook10.cube +glredbook1314.cubemap +glredbook10.depthcue +glredbook10.disk +glredbook10.dof +glredbook10.dofnot +glredbook10.doublebuf +glredbook10.drawf +glredbook10.feedback +glredbook10.fog +glredbook1314.fogcoord +glredbook10.font +glredbook11.aargb +glredbook11.hello +glredbook12x.histogram +glredbook11.image +glredbook10.jfont +glredbook10.light +glredbook10.linelist +glredbook10.lines +glredbook10.list +glredbook10.list2 +glredbook10.maplight +glredbook10.material +glredbook12x.minmax +glredbook10.mipmap +glredbook10.model +glredbook10.movelight +glredbook1314.multisamp +glredbook12x.multitex +glredbook1314.mvarray +glredbook10.pickdepth +glredbook10.pickline +glredbook10.picksquare +glredbook10.plane +glredbook10.planet +glredbook10.planetup +glredbook1314.pointp +glredbook11.polyoff +glredbook10.polys +glredbook12x.quadric +glredbook10.robot +glredbook10.sccolorlight +glredbook10.scene +glredbook10.scenebamb +glredbook10.sceneflat +glredbook10.select +glredbook1314.shadowmap +glredbook10.simple +glredbook10.smooth +glredbook10.sphere +glredbook10.stencil +glredbook10.stroke +glredbook10.tea +glredbook10.teaambient +glredbook10.teapots +glredbook11.tess +glredbook11.tesswind +glredbook11.texbind +glredbook11.texgen +glredbook11.texprox +glredbook11.texsub +glredbook12x.texture3d +glredbook10.texturesurf +glredbook11.torus +glredbook11.unproject +glredbook11.varray +glredbook11.wrap diff --git a/src/redbook/lib/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar b/src/redbook/lib/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar Binary files differnew file mode 100644 index 0000000..1bb1def --- /dev/null +++ b/src/redbook/lib/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar diff --git a/src/redbook/lib/JWSAntTasks/org-netbeans-modules-javawebstart-anttasks.jar b/src/redbook/lib/JWSAntTasks/org-netbeans-modules-javawebstart-anttasks.jar Binary files differnew file mode 100644 index 0000000..9e9f8d8 --- /dev/null +++ b/src/redbook/lib/JWSAntTasks/org-netbeans-modules-javawebstart-anttasks.jar diff --git a/src/redbook/lib/nblibraries.properties b/src/redbook/lib/nblibraries.properties new file mode 100644 index 0000000..ee0711c --- /dev/null +++ b/src/redbook/lib/nblibraries.properties @@ -0,0 +1,4 @@ +libs.CopyLibs.classpath=\ + ${base}/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar +libs.JWSAntTasks.classpath=\ + ${base}/JWSAntTasks/org-netbeans-modules-javawebstart-anttasks.jar diff --git a/src/redbook/manifest.mf b/src/redbook/manifest.mf new file mode 100644 index 0000000..1574df4 --- /dev/null +++ b/src/redbook/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0
+X-COMMENT: Main-Class will be added automatically by build
+
diff --git a/src/redbook/master-applet.jnlp b/src/redbook/master-applet.jnlp new file mode 100644 index 0000000..3e49332 --- /dev/null +++ b/src/redbook/master-applet.jnlp @@ -0,0 +1,23 @@ +<jnlp spec="1.0+" codebase="${jnlp.codebase}" href="launch.jnlp"> + <information> + <title>${APPLICATION.TITLE}</title> + <vendor>${APPLICATION.VENDOR}</vendor> + <homepage href="${APPLICATION.HOMEPAGE}"/> + <description>${APPLICATION.DESC}</description> + <description kind="short">${APPLICATION.DESC.SHORT}</description> +<!--${JNLP.ICONS}--> +<!--${JNLP.OFFLINE.ALLOWED}--> + </information> +<!--${JNLP.SECURITY}--> + <resources> +<!--${JNLP.RESOURCES.RUNTIME}--> +<!--${JNLP.RESOURCES.MAIN.JAR}--> +<!--${JNLP.RESOURCES.JARS}--> +<!--${JNLP.RESOURCES.EXTENSIONS}--> + <extension name="jogl-all-awt" href="http://download.java.net/media/jogl/jsr-231-2.x-webstart/jogl-all-awt.jnlp" /> + </resources> + <applet-desc main-class="${jnlp.main.class}" name="${APPLICATION.TITLE}" + width="${jnlp.applet.width}" height="${jnlp.applet.height}"> +<!--${JNLP.APPLET.PARAMS}--> + </applet-desc> +</jnlp> diff --git a/src/redbook/nbproject/build-impl.xml b/src/redbook/nbproject/build-impl.xml new file mode 100644 index 0000000..1f8d07b --- /dev/null +++ b/src/redbook/nbproject/build-impl.xml @@ -0,0 +1,722 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +*** GENERATED FROM project.xml - DO NOT EDIT *** +*** EDIT ../build.xml INSTEAD *** + +For the purpose of easier reading the script +is divided into following sections: + + - initialization + - compilation + - jar + - execution + - debugging + - javadoc + - junit compilation + - junit execution + - junit debugging + - applet + - cleanup + + --> +<project xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1" xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3" xmlns:jaxrpc="http://www.netbeans.org/ns/j2se-project/jax-rpc" basedir=".." default="default" name="RedBook_Samples-impl"> + <import file="jnlp-impl.xml"/> + <fail message="Please build using Ant 1.7.1 or higher."> + <condition> + <not> + <antversion atleast="1.7.1"/> + </not> + </condition> + </fail> + <target depends="test,jar,javadoc" description="Build and test whole project." name="default"/> + <!-- + ====================== + INITIALIZATION SECTION + ====================== + --> + <target name="-pre-init"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target depends="-pre-init" name="-init-private"> + <property file="nbproject/private/config.properties"/> + <property file="nbproject/private/configs/${config}.properties"/> + <property file="nbproject/private/private.properties"/> + </target> + <target depends="-pre-init,-init-private" name="-init-libraries"> + <property location="./lib/nblibraries.properties" name="libraries.1.path"/> + <dirname file="${libraries.1.path}" property="libraries.1.dir.nativedirsep"/> + <pathconvert dirsep="/" property="libraries.1.dir"> + <path path="${libraries.1.dir.nativedirsep}"/> + </pathconvert> + <basename file="${libraries.1.path}" property="libraries.1.basename" suffix=".properties"/> + <touch file="${libraries.1.dir}/${libraries.1.basename}-private.properties"/> + <loadproperties encoding="ISO-8859-1" srcfile="${libraries.1.dir}/${libraries.1.basename}-private.properties"> + <filterchain> + <replacestring from="$${base}" to="${libraries.1.dir}"/> + <escapeunicode/> + </filterchain> + </loadproperties> + <loadproperties encoding="ISO-8859-1" srcfile="${libraries.1.path}"> + <filterchain> + <replacestring from="$${base}" to="${libraries.1.dir}"/> + <escapeunicode/> + </filterchain> + </loadproperties> + </target> + <target depends="-pre-init,-init-private,-init-libraries" name="-init-user"> + <property file="${user.properties.file}"/> + <!-- The two properties below are usually overridden --> + <!-- by the active platform. Just a fallback. --> + <property name="default.javac.source" value="1.4"/> + <property name="default.javac.target" value="1.4"/> + </target> + <target depends="-pre-init,-init-private,-init-libraries,-init-user" name="-init-project"> + <property file="nbproject/configs/${config}.properties"/> + <property file="nbproject/project.properties"/> + </target> + <target depends="-pre-init,-init-private,-init-libraries,-init-user,-init-project,-init-macrodef-property" name="-do-init"> + <available file="${manifest.file}" property="manifest.available"/> + <condition property="manifest.available+main.class"> + <and> + <isset property="manifest.available"/> + <isset property="main.class"/> + <not> + <equals arg1="${main.class}" arg2="" trim="true"/> + </not> + </and> + </condition> + <condition property="manifest.available+main.class+mkdist.available"> + <and> + <istrue value="${manifest.available+main.class}"/> + <isset property="libs.CopyLibs.classpath"/> + </and> + </condition> + <condition property="have.tests"> + <or> + <available file="${test.src.dir}"/> + </or> + </condition> + <condition property="have.sources"> + <or> + <available file="${src.dir}"/> + </or> + </condition> + <condition property="netbeans.home+have.tests"> + <and> + <isset property="netbeans.home"/> + <isset property="have.tests"/> + </and> + </condition> + <condition property="no.javadoc.preview"> + <and> + <isset property="javadoc.preview"/> + <isfalse value="${javadoc.preview}"/> + </and> + </condition> + <property name="run.jvmargs" value=""/> + <property name="javac.compilerargs" value=""/> + <property name="work.dir" value="${basedir}"/> + <condition property="no.deps"> + <and> + <istrue value="${no.dependencies}"/> + </and> + </condition> + <property name="javac.debug" value="true"/> + <property name="javadoc.preview" value="true"/> + <property name="application.args" value=""/> + <property name="source.encoding" value="${file.encoding}"/> + <condition property="javadoc.encoding.used" value="${javadoc.encoding}"> + <and> + <isset property="javadoc.encoding"/> + <not> + <equals arg1="${javadoc.encoding}" arg2=""/> + </not> + </and> + </condition> + <property name="javadoc.encoding.used" value="${source.encoding}"/> + <property name="includes" value="**"/> + <property name="excludes" value=""/> + <property name="do.depend" value="false"/> + <condition property="do.depend.true"> + <istrue value="${do.depend}"/> + </condition> + <condition else="" property="javac.compilerargs.jaxws" value="-Djava.endorsed.dirs='${jaxws.endorsed.dir}'"> + <and> + <isset property="jaxws.endorsed.dir"/> + <available file="nbproject/jaxws-build.xml"/> + </and> + </condition> + </target> + <target name="-post-init"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target depends="-pre-init,-init-private,-init-libraries,-init-user,-init-project,-do-init" name="-init-check"> + <fail unless="src.dir">Must set src.dir</fail> + <fail unless="test.src.dir">Must set test.src.dir</fail> + <fail unless="build.dir">Must set build.dir</fail> + <fail unless="dist.dir">Must set dist.dir</fail> + <fail unless="build.classes.dir">Must set build.classes.dir</fail> + <fail unless="dist.javadoc.dir">Must set dist.javadoc.dir</fail> + <fail unless="build.test.classes.dir">Must set build.test.classes.dir</fail> + <fail unless="build.test.results.dir">Must set build.test.results.dir</fail> + <fail unless="build.classes.excludes">Must set build.classes.excludes</fail> + <fail unless="dist.jar">Must set dist.jar</fail> + </target> + <target name="-init-macrodef-property"> + <macrodef name="property" uri="http://www.netbeans.org/ns/j2se-project/1"> + <attribute name="name"/> + <attribute name="value"/> + <sequential> + <property name="@{name}" value="${@{value}}"/> + </sequential> + </macrodef> + </target> + <target name="-init-macrodef-javac"> + <macrodef name="javac" uri="http://www.netbeans.org/ns/j2se-project/3"> + <attribute default="${src.dir}" name="srcdir"/> + <attribute default="${build.classes.dir}" name="destdir"/> + <attribute default="${javac.classpath}" name="classpath"/> + <attribute default="${includes}" name="includes"/> + <attribute default="${excludes}" name="excludes"/> + <attribute default="${javac.debug}" name="debug"/> + <attribute default="${empty.dir}" name="sourcepath"/> + <attribute default="${empty.dir}" name="gensrcdir"/> + <element name="customize" optional="true"/> + <sequential> + <property location="${build.dir}/empty" name="empty.dir"/> + <mkdir dir="${empty.dir}"/> + <javac debug="@{debug}" deprecation="${javac.deprecation}" destdir="@{destdir}" encoding="${source.encoding}" excludes="@{excludes}" includeantruntime="false" includes="@{includes}" source="${javac.source}" sourcepath="@{sourcepath}" srcdir="@{srcdir}" target="${javac.target}"> + <src> + <dirset dir="@{gensrcdir}" erroronmissingdir="false"> + <include name="*"/> + </dirset> + </src> + <classpath> + <path path="@{classpath}"/> + </classpath> + <compilerarg line="${javac.compilerargs} ${javac.compilerargs.jaxws}"/> + <customize/> + </javac> + </sequential> + </macrodef> + <macrodef name="depend" uri="http://www.netbeans.org/ns/j2se-project/3"> + <attribute default="${src.dir}" name="srcdir"/> + <attribute default="${build.classes.dir}" name="destdir"/> + <attribute default="${javac.classpath}" name="classpath"/> + <sequential> + <depend cache="${build.dir}/depcache" destdir="@{destdir}" excludes="${excludes}" includes="${includes}" srcdir="@{srcdir}"> + <classpath> + <path path="@{classpath}"/> + </classpath> + </depend> + </sequential> + </macrodef> + <macrodef name="force-recompile" uri="http://www.netbeans.org/ns/j2se-project/3"> + <attribute default="${build.classes.dir}" name="destdir"/> + <sequential> + <fail unless="javac.includes">Must set javac.includes</fail> + <pathconvert pathsep="," property="javac.includes.binary"> + <path> + <filelist dir="@{destdir}" files="${javac.includes}"/> + </path> + <globmapper from="*.java" to="*.class"/> + </pathconvert> + <delete> + <files includes="${javac.includes.binary}"/> + </delete> + </sequential> + </macrodef> + </target> + <target name="-init-macrodef-junit"> + <macrodef name="junit" uri="http://www.netbeans.org/ns/j2se-project/3"> + <attribute default="${includes}" name="includes"/> + <attribute default="${excludes}" name="excludes"/> + <attribute default="**" name="testincludes"/> + <sequential> + <junit dir="${work.dir}" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" showoutput="true"> + <batchtest todir="${build.test.results.dir}"> + <fileset dir="${test.src.dir}" excludes="@{excludes},${excludes}" includes="@{includes}"> + <filename name="@{testincludes}"/> + </fileset> + </batchtest> + <classpath> + <path path="${run.test.classpath}"/> + </classpath> + <syspropertyset> + <propertyref prefix="test-sys-prop."/> + <mapper from="test-sys-prop.*" to="*" type="glob"/> + </syspropertyset> + <formatter type="brief" usefile="false"/> + <formatter type="xml"/> + <jvmarg line="${run.jvmargs}"/> + </junit> + </sequential> + </macrodef> + </target> + <target depends="-init-debug-args" name="-init-macrodef-nbjpda"> + <macrodef name="nbjpdastart" uri="http://www.netbeans.org/ns/j2se-project/1"> + <attribute default="${main.class}" name="name"/> + <attribute default="${debug.classpath}" name="classpath"/> + <attribute default="" name="stopclassname"/> + <sequential> + <nbjpdastart addressproperty="jpda.address" name="@{name}" stopclassname="@{stopclassname}" transport="${debug-transport}"> + <classpath> + <path path="@{classpath}"/> + </classpath> + </nbjpdastart> + </sequential> + </macrodef> + <macrodef name="nbjpdareload" uri="http://www.netbeans.org/ns/j2se-project/1"> + <attribute default="${build.classes.dir}" name="dir"/> + <sequential> + <nbjpdareload> + <fileset dir="@{dir}" includes="${fix.classes}"> + <include name="${fix.includes}*.class"/> + </fileset> + </nbjpdareload> + </sequential> + </macrodef> + </target> + <target name="-init-debug-args"> + <property name="version-output" value="java version "${ant.java.version}"/> + <condition property="have-jdk-older-than-1.4"> + <or> + <contains string="${version-output}" substring="java version "1.0"/> + <contains string="${version-output}" substring="java version "1.1"/> + <contains string="${version-output}" substring="java version "1.2"/> + <contains string="${version-output}" substring="java version "1.3"/> + </or> + </condition> + <condition else="-Xdebug" property="debug-args-line" value="-Xdebug -Xnoagent -Djava.compiler=none"> + <istrue value="${have-jdk-older-than-1.4}"/> + </condition> + <condition else="dt_socket" property="debug-transport-by-os" value="dt_shmem"> + <os family="windows"/> + </condition> + <condition else="${debug-transport-by-os}" property="debug-transport" value="${debug.transport}"> + <isset property="debug.transport"/> + </condition> + </target> + <target depends="-init-debug-args" name="-init-macrodef-debug"> + <macrodef name="debug" uri="http://www.netbeans.org/ns/j2se-project/3"> + <attribute default="${main.class}" name="classname"/> + <attribute default="${debug.classpath}" name="classpath"/> + <element name="customize" optional="true"/> + <sequential> + <java classname="@{classname}" dir="${work.dir}" fork="true"> + <jvmarg line="${debug-args-line}"/> + <jvmarg value="-Xrunjdwp:transport=${debug-transport},address=${jpda.address}"/> + <jvmarg value="-Dfile.encoding=${source.encoding}"/> + <redirector errorencoding="${source.encoding}" inputencoding="${source.encoding}" outputencoding="${source.encoding}"/> + <jvmarg line="${run.jvmargs}"/> + <classpath> + <path path="@{classpath}"/> + </classpath> + <syspropertyset> + <propertyref prefix="run-sys-prop."/> + <mapper from="run-sys-prop.*" to="*" type="glob"/> + </syspropertyset> + <customize/> + </java> + </sequential> + </macrodef> + </target> + <target name="-init-macrodef-java"> + <macrodef name="java" uri="http://www.netbeans.org/ns/j2se-project/1"> + <attribute default="${main.class}" name="classname"/> + <attribute default="${run.classpath}" name="classpath"/> + <element name="customize" optional="true"/> + <sequential> + <java classname="@{classname}" dir="${work.dir}" fork="true"> + <jvmarg value="-Dfile.encoding=${source.encoding}"/> + <redirector errorencoding="${source.encoding}" inputencoding="${source.encoding}" outputencoding="${source.encoding}"/> + <jvmarg line="${run.jvmargs}"/> + <classpath> + <path path="@{classpath}"/> + </classpath> + <syspropertyset> + <propertyref prefix="run-sys-prop."/> + <mapper from="run-sys-prop.*" to="*" type="glob"/> + </syspropertyset> + <customize/> + </java> + </sequential> + </macrodef> + </target> + <target name="-init-presetdef-jar"> + <presetdef name="jar" uri="http://www.netbeans.org/ns/j2se-project/1"> + <jar compress="${jar.compress}" jarfile="${dist.jar}"> + <j2seproject1:fileset dir="${build.classes.dir}"/> + </jar> + </presetdef> + </target> + <target depends="-pre-init,-init-private,-init-libraries,-init-user,-init-project,-do-init,-post-init,-init-check,-init-macrodef-property,-init-macrodef-javac,-init-macrodef-junit,-init-macrodef-nbjpda,-init-macrodef-debug,-init-macrodef-java,-init-presetdef-jar" name="init"/> + <!-- + =================== + COMPILATION SECTION + =================== + --> + <target depends="init" name="deps-jar" unless="no.deps"/> + <target depends="init,-check-automatic-build,-clean-after-automatic-build" name="-verify-automatic-build"/> + <target depends="init" name="-check-automatic-build"> + <available file="${build.classes.dir}/.netbeans_automatic_build" property="netbeans.automatic.build"/> + </target> + <target depends="init" if="netbeans.automatic.build" name="-clean-after-automatic-build"> + <antcall target="clean"/> + </target> + <target depends="init,deps-jar" name="-pre-pre-compile"> + <mkdir dir="${build.classes.dir}"/> + </target> + <target name="-pre-compile"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target if="do.depend.true" name="-compile-depend"> + <pathconvert property="build.generated.subdirs"> + <dirset dir="${build.generated.sources.dir}" erroronmissingdir="false"> + <include name="*"/> + </dirset> + </pathconvert> + <j2seproject3:depend srcdir="${src.dir}:${build.generated.subdirs}"/> + </target> + <target depends="init,deps-jar,-pre-pre-compile,-pre-compile,-compile-depend" if="have.sources" name="-do-compile"> + <j2seproject3:javac gensrcdir="${build.generated.sources.dir}"/> + <copy todir="${build.classes.dir}"> + <fileset dir="${src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/> + </copy> + </target> + <target name="-post-compile"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target depends="init,deps-jar,-verify-automatic-build,-pre-pre-compile,-pre-compile,-do-compile,-post-compile" description="Compile project." name="compile"/> + <target name="-pre-compile-single"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target depends="init,deps-jar,-pre-pre-compile" name="-do-compile-single"> + <fail unless="javac.includes">Must select some files in the IDE or set javac.includes</fail> + <j2seproject3:force-recompile/> + <j2seproject3:javac excludes="" gensrcdir="${build.generated.sources.dir}" includes="${javac.includes}" sourcepath="${src.dir}"/> + </target> + <target name="-post-compile-single"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target depends="init,deps-jar,-verify-automatic-build,-pre-pre-compile,-pre-compile-single,-do-compile-single,-post-compile-single" name="compile-single"/> + <!-- + ==================== + JAR BUILDING SECTION + ==================== + --> + <target depends="init" name="-pre-pre-jar"> + <dirname file="${dist.jar}" property="dist.jar.dir"/> + <mkdir dir="${dist.jar.dir}"/> + </target> + <target name="-pre-jar"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target depends="init,compile,-pre-pre-jar,-pre-jar" name="-do-jar-without-manifest" unless="manifest.available"> + <j2seproject1:jar/> + </target> + <target depends="init,compile,-pre-pre-jar,-pre-jar" if="manifest.available" name="-do-jar-with-manifest" unless="manifest.available+main.class"> + <j2seproject1:jar manifest="${manifest.file}"/> + </target> + <target depends="init,compile,-pre-pre-jar,-pre-jar" if="manifest.available+main.class" name="-do-jar-with-mainclass" unless="manifest.available+main.class+mkdist.available"> + <j2seproject1:jar manifest="${manifest.file}"> + <j2seproject1:manifest> + <j2seproject1:attribute name="Main-Class" value="${main.class}"/> + </j2seproject1:manifest> + </j2seproject1:jar> + <echo>To run this application from the command line without Ant, try:</echo> + <property location="${build.classes.dir}" name="build.classes.dir.resolved"/> + <property location="${dist.jar}" name="dist.jar.resolved"/> + <pathconvert property="run.classpath.with.dist.jar"> + <path path="${run.classpath}"/> + <map from="${build.classes.dir.resolved}" to="${dist.jar.resolved}"/> + </pathconvert> + <echo>java -cp "${run.classpath.with.dist.jar}" ${main.class}</echo> + </target> + <target depends="init,compile,-pre-pre-jar,-pre-jar" if="manifest.available+main.class+mkdist.available" name="-do-jar-with-libraries"> + <property location="${build.classes.dir}" name="build.classes.dir.resolved"/> + <pathconvert property="run.classpath.without.build.classes.dir"> + <path path="${run.classpath}"/> + <map from="${build.classes.dir.resolved}" to=""/> + </pathconvert> + <pathconvert pathsep=" " property="jar.classpath"> + <path path="${run.classpath.without.build.classes.dir}"/> + <chainedmapper> + <flattenmapper/> + <globmapper from="*" to="lib/*"/> + </chainedmapper> + </pathconvert> + <taskdef classname="org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs" classpath="${libs.CopyLibs.classpath}" name="copylibs"/> + <copylibs compress="${jar.compress}" jarfile="${dist.jar}" manifest="${manifest.file}" runtimeclasspath="${run.classpath.without.build.classes.dir}"> + <fileset dir="${build.classes.dir}"/> + <manifest> + <attribute name="Main-Class" value="${main.class}"/> + <attribute name="Class-Path" value="${jar.classpath}"/> + </manifest> + </copylibs> + <echo>To run this application from the command line without Ant, try:</echo> + <property location="${dist.jar}" name="dist.jar.resolved"/> + <echo>java -jar "${dist.jar.resolved}"</echo> + </target> + <target depends="init,compile,-pre-pre-jar,-pre-jar" if="libs.CopyLibs.classpath" name="-do-jar-with-libraries-without-manifest" unless="manifest.available+main.class"> + <property location="${build.classes.dir}" name="build.classes.dir.resolved"/> + <pathconvert property="run.classpath.without.build.classes.dir"> + <path path="${run.classpath}"/> + <map from="${build.classes.dir.resolved}" to=""/> + </pathconvert> + <pathconvert pathsep=" " property="jar.classpath"> + <path path="${run.classpath.without.build.classes.dir}"/> + <chainedmapper> + <flattenmapper/> + <globmapper from="*" to="lib/*"/> + </chainedmapper> + </pathconvert> + <taskdef classname="org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs" classpath="${libs.CopyLibs.classpath}" name="copylibs"/> + <copylibs compress="${jar.compress}" jarfile="${dist.jar}" runtimeclasspath="${run.classpath.without.build.classes.dir}"> + <fileset dir="${build.classes.dir}"/> + </copylibs> + </target> + <target name="-post-jar"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target depends="init,compile,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-do-jar-with-libraries-without-manifest,-post-jar,jnlp" description="Build JAR." name="jar"/> + <!-- + ================= + EXECUTION SECTION + ================= + --> + <target depends="init,compile" description="Run a main class." name="run"> + <j2seproject1:java> + <customize> + <arg line="${application.args}"/> + </customize> + </j2seproject1:java> + </target> + <target name="-do-not-recompile"> + <property name="javac.includes.binary" value=""/> + </target> + <target depends="init,-do-not-recompile,compile-single" name="run-single"> + <fail unless="run.class">Must select one file in the IDE or set run.class</fail> + <j2seproject1:java classname="${run.class}"/> + </target> + <target depends="init,-do-not-recompile,compile-test-single" name="run-test-with-main"> + <fail unless="run.class">Must select one file in the IDE or set run.class</fail> + <j2seproject1:java classname="${run.class}" classpath="${run.test.classpath}"/> + </target> + <!-- + ================= + DEBUGGING SECTION + ================= + --> + <target depends="init" if="netbeans.home" name="-debug-start-debugger"> + <j2seproject1:nbjpdastart name="${debug.class}"/> + </target> + <target depends="init" if="netbeans.home" name="-debug-start-debugger-main-test"> + <j2seproject1:nbjpdastart classpath="${debug.test.classpath}" name="${debug.class}"/> + </target> + <target depends="init,compile" name="-debug-start-debuggee"> + <j2seproject3:debug> + <customize> + <arg line="${application.args}"/> + </customize> + </j2seproject3:debug> + </target> + <target depends="init,compile,-debug-start-debugger,-debug-start-debuggee" description="Debug project in IDE." if="netbeans.home" name="debug"/> + <target depends="init" if="netbeans.home" name="-debug-start-debugger-stepinto"> + <j2seproject1:nbjpdastart stopclassname="${main.class}"/> + </target> + <target depends="init,compile,-debug-start-debugger-stepinto,-debug-start-debuggee" if="netbeans.home" name="debug-stepinto"/> + <target depends="init,compile-single" if="netbeans.home" name="-debug-start-debuggee-single"> + <fail unless="debug.class">Must select one file in the IDE or set debug.class</fail> + <j2seproject3:debug classname="${debug.class}"/> + </target> + <target depends="init,-do-not-recompile,compile-single,-debug-start-debugger,-debug-start-debuggee-single" if="netbeans.home" name="debug-single"/> + <target depends="init,compile-test-single" if="netbeans.home" name="-debug-start-debuggee-main-test"> + <fail unless="debug.class">Must select one file in the IDE or set debug.class</fail> + <j2seproject3:debug classname="${debug.class}" classpath="${debug.test.classpath}"/> + </target> + <target depends="init,-do-not-recompile,compile-test-single,-debug-start-debugger-main-test,-debug-start-debuggee-main-test" if="netbeans.home" name="debug-test-with-main"/> + <target depends="init" name="-pre-debug-fix"> + <fail unless="fix.includes">Must set fix.includes</fail> + <property name="javac.includes" value="${fix.includes}.java"/> + </target> + <target depends="init,-pre-debug-fix,compile-single" if="netbeans.home" name="-do-debug-fix"> + <j2seproject1:nbjpdareload/> + </target> + <target depends="init,-pre-debug-fix,-do-debug-fix" if="netbeans.home" name="debug-fix"/> + <!-- + =============== + JAVADOC SECTION + =============== + --> + <target depends="init" name="-javadoc-build"> + <mkdir dir="${dist.javadoc.dir}"/> + <javadoc additionalparam="${javadoc.additionalparam}" author="${javadoc.author}" charset="UTF-8" destdir="${dist.javadoc.dir}" docencoding="UTF-8" encoding="${javadoc.encoding.used}" failonerror="true" noindex="${javadoc.noindex}" nonavbar="${javadoc.nonavbar}" notree="${javadoc.notree}" private="${javadoc.private}" source="${javac.source}" splitindex="${javadoc.splitindex}" use="${javadoc.use}" useexternalfile="true" version="${javadoc.version}" windowtitle="${javadoc.windowtitle}"> + <classpath> + <path path="${javac.classpath}"/> + </classpath> + <fileset dir="${src.dir}" excludes="${excludes}" includes="${includes}"> + <filename name="**/*.java"/> + </fileset> + <fileset dir="${build.generated.sources.dir}" erroronmissingdir="false"> + <include name="**/*.java"/> + </fileset> + </javadoc> + </target> + <target depends="init,-javadoc-build" if="netbeans.home" name="-javadoc-browse" unless="no.javadoc.preview"> + <nbbrowse file="${dist.javadoc.dir}/index.html"/> + </target> + <target depends="init,-javadoc-build,-javadoc-browse" description="Build Javadoc." name="javadoc"/> + <!-- + ========================= + JUNIT COMPILATION SECTION + ========================= + --> + <target depends="init,compile" if="have.tests" name="-pre-pre-compile-test"> + <mkdir dir="${build.test.classes.dir}"/> + </target> + <target name="-pre-compile-test"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target if="do.depend.true" name="-compile-test-depend"> + <j2seproject3:depend classpath="${javac.test.classpath}" destdir="${build.test.classes.dir}" srcdir="${test.src.dir}"/> + </target> + <target depends="init,compile,-pre-pre-compile-test,-pre-compile-test,-compile-test-depend" if="have.tests" name="-do-compile-test"> + <j2seproject3:javac classpath="${javac.test.classpath}" debug="true" destdir="${build.test.classes.dir}" srcdir="${test.src.dir}"/> + <copy todir="${build.test.classes.dir}"> + <fileset dir="${test.src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/> + </copy> + </target> + <target name="-post-compile-test"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target depends="init,compile,-pre-pre-compile-test,-pre-compile-test,-do-compile-test,-post-compile-test" name="compile-test"/> + <target name="-pre-compile-test-single"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target depends="init,compile,-pre-pre-compile-test,-pre-compile-test-single" if="have.tests" name="-do-compile-test-single"> + <fail unless="javac.includes">Must select some files in the IDE or set javac.includes</fail> + <j2seproject3:force-recompile destdir="${build.test.classes.dir}"/> + <j2seproject3:javac classpath="${javac.test.classpath}" debug="true" destdir="${build.test.classes.dir}" excludes="" includes="${javac.includes}" sourcepath="${test.src.dir}" srcdir="${test.src.dir}"/> + <copy todir="${build.test.classes.dir}"> + <fileset dir="${test.src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/> + </copy> + </target> + <target name="-post-compile-test-single"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target depends="init,compile,-pre-pre-compile-test,-pre-compile-test-single,-do-compile-test-single,-post-compile-test-single" name="compile-test-single"/> + <!-- + ======================= + JUNIT EXECUTION SECTION + ======================= + --> + <target depends="init" if="have.tests" name="-pre-test-run"> + <mkdir dir="${build.test.results.dir}"/> + </target> + <target depends="init,compile-test,-pre-test-run" if="have.tests" name="-do-test-run"> + <j2seproject3:junit testincludes="**/*Test.java"/> + </target> + <target depends="init,compile-test,-pre-test-run,-do-test-run" if="have.tests" name="-post-test-run"> + <fail if="tests.failed" unless="ignore.failing.tests">Some tests failed; see details above.</fail> + </target> + <target depends="init" if="have.tests" name="test-report"/> + <target depends="init" if="netbeans.home+have.tests" name="-test-browse"/> + <target depends="init,compile-test,-pre-test-run,-do-test-run,test-report,-post-test-run,-test-browse" description="Run unit tests." name="test"/> + <target depends="init" if="have.tests" name="-pre-test-run-single"> + <mkdir dir="${build.test.results.dir}"/> + </target> + <target depends="init,compile-test-single,-pre-test-run-single" if="have.tests" name="-do-test-run-single"> + <fail unless="test.includes">Must select some files in the IDE or set test.includes</fail> + <j2seproject3:junit excludes="" includes="${test.includes}"/> + </target> + <target depends="init,compile-test-single,-pre-test-run-single,-do-test-run-single" if="have.tests" name="-post-test-run-single"> + <fail if="tests.failed" unless="ignore.failing.tests">Some tests failed; see details above.</fail> + </target> + <target depends="init,-do-not-recompile,compile-test-single,-pre-test-run-single,-do-test-run-single,-post-test-run-single" description="Run single unit test." name="test-single"/> + <!-- + ======================= + JUNIT DEBUGGING SECTION + ======================= + --> + <target depends="init,compile-test" if="have.tests" name="-debug-start-debuggee-test"> + <fail unless="test.class">Must select one file in the IDE or set test.class</fail> + <property location="${build.test.results.dir}/TEST-${test.class}.xml" name="test.report.file"/> + <delete file="${test.report.file}"/> + <mkdir dir="${build.test.results.dir}"/> + <j2seproject3:debug classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner" classpath="${ant.home}/lib/ant.jar:${ant.home}/lib/ant-junit.jar:${debug.test.classpath}"> + <customize> + <syspropertyset> + <propertyref prefix="test-sys-prop."/> + <mapper from="test-sys-prop.*" to="*" type="glob"/> + </syspropertyset> + <arg value="${test.class}"/> + <arg value="showoutput=true"/> + <arg value="formatter=org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter"/> + <arg value="formatter=org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter,${test.report.file}"/> + </customize> + </j2seproject3:debug> + </target> + <target depends="init,compile-test" if="netbeans.home+have.tests" name="-debug-start-debugger-test"> + <j2seproject1:nbjpdastart classpath="${debug.test.classpath}" name="${test.class}"/> + </target> + <target depends="init,-do-not-recompile,compile-test-single,-debug-start-debugger-test,-debug-start-debuggee-test" name="debug-test"/> + <target depends="init,-pre-debug-fix,compile-test-single" if="netbeans.home" name="-do-debug-fix-test"> + <j2seproject1:nbjpdareload dir="${build.test.classes.dir}"/> + </target> + <target depends="init,-pre-debug-fix,-do-debug-fix-test" if="netbeans.home" name="debug-fix-test"/> + <!-- + ========================= + APPLET EXECUTION SECTION + ========================= + --> + <target depends="init,compile-single" name="run-applet"> + <fail unless="applet.url">Must select one file in the IDE or set applet.url</fail> + <j2seproject1:java classname="sun.applet.AppletViewer"> + <customize> + <arg value="${applet.url}"/> + </customize> + </j2seproject1:java> + </target> + <!-- + ========================= + APPLET DEBUGGING SECTION + ========================= + --> + <target depends="init,compile-single" if="netbeans.home" name="-debug-start-debuggee-applet"> + <fail unless="applet.url">Must select one file in the IDE or set applet.url</fail> + <j2seproject3:debug classname="sun.applet.AppletViewer"> + <customize> + <arg value="${applet.url}"/> + </customize> + </j2seproject3:debug> + </target> + <target depends="init,compile-single,-debug-start-debugger,-debug-start-debuggee-applet" if="netbeans.home" name="debug-applet"/> + <!-- + =============== + CLEANUP SECTION + =============== + --> + <target depends="init" name="deps-clean" unless="no.deps"/> + <target depends="init" name="-do-clean"> + <delete dir="${build.dir}"/> + <delete dir="${dist.dir}"/> + </target> + <target name="-post-clean"> + <!-- Empty placeholder for easier customization. --> + <!-- You can override this target in the ../build.xml file. --> + </target> + <target depends="init,deps-clean,-do-clean,-post-clean" description="Clean build products." name="clean"/> +</project> diff --git a/src/redbook/nbproject/build-jogl-template-impl.xml b/src/redbook/nbproject/build-jogl-template-impl.xml new file mode 100644 index 0000000..1f69eb2 --- /dev/null +++ b/src/redbook/nbproject/build-jogl-template-impl.xml @@ -0,0 +1,204 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<!--
+*** GENERATED FROM jogl-projects module - DO NOT EDIT ***
+*** EDIT ../build.xml INSTEAD ***
+-->
+<project name="jogl-template-impl" default="default" basedir=".." >
+
+ <description>Ant targets for JOGL2 projects.</description>
+
+ <target name="-jogl-template-post-jar">
+ <echo message="Creating native distibutions"/>
+
+ <antcall target="-create-native-distribution" inheritall="true">
+ <param name="platform.name" value="linux-i586"/>
+ </antcall>
+ <antcall target="-create-native-distribution" inheritall="true">
+ <param name="platform.name" value="linux-amd64"/>
+ </antcall>
+ <antcall target="-create-native-distribution" inheritall="true">
+ <param name="platform.name" value="macosx-ppc"/>
+ </antcall>
+ <antcall target="-create-native-distribution" inheritall="true">
+ <param name="platform.name" value="macosx-universal"/>
+ </antcall>
+ <antcall target="-create-native-distribution" inheritall="true">
+ <param name="platform.name" value="solaris-sparc"/>
+ </antcall>
+ <antcall target="-create-native-distribution" inheritall="true">
+ <param name="platform.name" value="solaris-sparcv9"/>
+ </antcall>
+ <antcall target="-create-native-distribution" inheritall="true">
+ <param name="platform.name" value="solaris-i586"/>
+ </antcall>
+ <antcall target="-create-native-distribution" inheritall="true">
+ <param name="platform.name" value="windows-i586"/>
+ </antcall>
+ <antcall target="-create-native-distribution" inheritall="true">
+ <param name="platform.name" value="windows-amd64"/>
+ </antcall>
+
+ </target>
+
+ <target name="-create-native-distribution">
+ <unzip dest="${dist.dir}/lib" overwrite="true">
+ <patternset>
+ <include name="**-natives-${platform.name}.jar"/>
+ </patternset>
+ <fileset dir="jogl-dist">
+ <include name="jogl-2.0-webstart.zip"/>
+ <include name="nativewindow-2.0-webstart.zip"/>
+ <include name="newt-2.0-webstart.zip"/>
+ <include name="gluegen-rt-2.0-webstart.zip"/>
+ </fileset>
+ </unzip>
+ <unzip dest="${dist.dir}/lib/natives-${platform.name}" overwrite="true">
+ <patternset>
+ <exclude name="META-INF/**"/>
+ </patternset>
+ <fileset dir="${dist.dir}/lib">
+ <include name="**-natives-${platform.name}.jar"/>
+ </fileset>
+ </unzip>
+ <delete>
+ <fileset dir="${dist.dir}/lib">
+ <include name="**-natives-${platform.name}.jar"/>
+ </fileset>
+ </delete>
+ </target>
+
+ <target name="-jogl-template-post-clean">
+ <echo message="Removing native distibutions"/>
+ <delete dir="${dist.dir}/lib/natives-linux-i586"/>
+ <delete dir="${dist.dir}/lib/natives-linux-amd64"/>
+ <delete dir="${dist.dir}/lib/natives-macosx-ppc"/>
+ <delete dir="${dist.dir}/lib/natives-macosx-universal"/>
+ <delete dir="${dist.dir}/lib/natives-solaris-sparc"/>
+ <delete dir="${dist.dir}/lib/natives-solaris-sparcv9"/>
+ <delete dir="${dist.dir}/lib/natives-solaris-i586"/>
+ <delete dir="${dist.dir}/lib/natives-windows-i586"/>
+ <delete dir="${dist.dir}/lib/natives-windows-amd64"/>
+ </target>
+
+ <!-- remove jogl libraries from jnlp and jars from distribution -->
+ <target name="-jogl-template-jnlp" depends="-jogl-template-test-jnlp-enabled,-jogl-template-patch-master-jnlp,jnlp-impl.jnlp" if="is.jnlp.enabled">
+
+ <!--remove jogl from dist/lib folder-->
+ <antcall target="-jogl-template-post-clean" inheritall="true"/>
+
+ <delete file="${dist.dir}/lib/jogl.all.jar"/>
+ <delete file="${dist.dir}/lib/nativewindow.all.jar"/>
+ <delete file="${dist.dir}/lib/newt.all.jar"/>
+ <delete file="${dist.dir}/lib/gluegen-rt.jar"/>
+
+ <!--remove jogl dependency from jnlp to add it later as jnlp extention-->
+ <replace file="${dist.dir}/launch.jnlp">
+ <replacetoken><![CDATA[<jar href="lib/jogl.all.jar"/>]]></replacetoken>
+ <replacevalue></replacevalue>
+ </replace>
+ <replace file="${dist.dir}/launch.jnlp">
+ <replacetoken><![CDATA[<jar href="lib/nativewindow.all.jar"/>]]></replacetoken>
+ <replacevalue></replacevalue>
+ </replace>
+ <replace file="${dist.dir}/launch.jnlp">
+ <replacetoken><![CDATA[<jar href="lib/newt.all.jar"/>]]></replacetoken>
+ <replacevalue></replacevalue>
+ </replace>
+ <replace file="${dist.dir}/launch.jnlp">
+ <replacetoken><![CDATA[<jar href="lib/gluegen-rt.jar"/>]]></replacetoken>
+ <replacevalue></replacevalue>
+ </replace>
+ <replace file="${dist.dir}/launch.jnlp">
+ <replacetoken><![CDATA[<jar href="lib/jogl.all.jar" download="eager"/>]]></replacetoken>
+ <replacevalue></replacevalue>
+ </replace>
+ <replace file="${dist.dir}/launch.jnlp">
+ <replacetoken><![CDATA[<jar href="lib/nativewindow.all.jar" download="eager"/>]]></replacetoken>
+ <replacevalue></replacevalue>
+ </replace>
+ <replace file="${dist.dir}/launch.jnlp">
+ <replacetoken><![CDATA[<jar href="lib/newt.all.jar" download="eager"/>]]></replacetoken>
+ <replacevalue></replacevalue>
+ </replace>
+ <replace file="${dist.dir}/launch.jnlp">
+ <replacetoken><![CDATA[<jar href="lib/gluegen-rt.jar" download="eager"/>]]></replacetoken>
+ <replacevalue></replacevalue>
+ </replace>
+ <replace file="${dist.dir}/launch.jnlp">
+ <replacetoken><![CDATA[-Djava.library.path="lib"]]></replacetoken>
+ <replacevalue></replacevalue>
+ </replace>
+
+ </target>
+
+ <!--noop in case jnlp-impl.xml is not available-->
+ <target name="jnlp-impl.jnlp" ></target>
+
+ <!--inserts jogl jnlp extensions into the jnlp master-->
+ <target name="-jogl-template-patch-master-jnlp" depends="-jogl-template-check-master-jnlp" if="is.jnlp.unpatched">
+ <replace file="master-${jnlp.descriptor}.jnlp">
+ <replacetoken><![CDATA[</resources>]]></replacetoken>
+ <replacevalue><![CDATA[ <extension name="jogl-all-awt" href="http://download.java.net/media/jogl/jsr-231-2.x-webstart/jogl-all-awt.jnlp" />
+ </resources>]]></replacevalue>
+ </replace>
+ </target>
+
+ <target name="-jogl-template-check-master-jnlp" if="is.jnlp.enabled">
+ <loadfile property="master.jnlp.content" srcfile="master-${jnlp.descriptor}.jnlp"/>
+ <condition property="is.jnlp.unpatched">
+ <not>
+ <contains string="${master.jnlp.content}" substring='extension name="jogl-all-awt"'/>
+ </not>
+ </condition>
+ </target>
+
+ <target name="-jogl-template-test-jnlp-enabled">
+ <condition property="is.jnlp.enabled">
+ <equals arg1="${jnlp.enabled}" arg2="true" casesensitive="false" trim="true"/>
+ </condition>
+ </target>
+
+ <!-- deploys JOGL and its dependencies to the projects lib folder,
+ platform dependent natives are deployed as well -->
+ <target name="jogl-template-deploy-jogl">
+
+ <echo message="Deploying JOGL"/>
+
+ <unzip src="jogl-dist/jogl-2.0-webstart.zip" dest="lib" overwrite="true">
+ <patternset>
+ <include name="jogl.all.jar"/>
+ <include name="jogl-natives-${natives.platform}.jar"/>
+ </patternset>
+ </unzip>
+ <unzip src="lib/jogl-natives-${natives.platform}.jar" dest="lib" overwrite="true"/>
+ <delete file="lib/jogl-natives-${natives.platform}.jar"/>
+
+ <unzip src="jogl-dist/nativewindow-2.0-webstart.zip" dest="lib" overwrite="true">
+ <patternset>
+ <include name="nativewindow.all.jar"/>
+ <include name="nativewindow-natives-${natives.platform}.jar"/>
+ </patternset>
+ </unzip>
+ <unzip src="lib/nativewindow-natives-${natives.platform}.jar" dest="lib" overwrite="true"/>
+ <delete file="lib/nativewindow-natives-${natives.platform}.jar"/>
+
+ <unzip src="jogl-dist/newt-2.0-webstart.zip" dest="lib" overwrite="true">
+ <patternset>
+ <include name="newt.all.jar"/>
+ <include name="newt-natives-${natives.platform}.jar"/>
+ </patternset>
+ </unzip>
+ <unzip src="lib/newt-natives-${natives.platform}.jar" dest="lib" overwrite="true"/>
+ <delete file="lib/newt-natives-${natives.platform}.jar"/>
+
+ <unzip src="jogl-dist/gluegen-rt-2.0-webstart.zip" dest="lib" overwrite="true">
+ <patternset>
+ <include name="gluegen-rt.jar"/>
+ <include name="gluegen-rt-natives-${natives.platform}.jar"/>
+ </patternset>
+ </unzip>
+ <unzip src="lib/gluegen-rt-natives-${natives.platform}.jar" dest="lib" overwrite="true"/>
+ <delete file="lib/gluegen-rt-natives-${natives.platform}.jar"/>
+ </target>
+
+</project>
diff --git a/src/redbook/nbproject/configs/JWS_generated.properties b/src/redbook/nbproject/configs/JWS_generated.properties new file mode 100644 index 0000000..ec91cd6 --- /dev/null +++ b/src/redbook/nbproject/configs/JWS_generated.properties @@ -0,0 +1,7 @@ +#Sat Sep 05 02:53:57 CEST 2009 +$label=Web Start +$target.debug=jws-debug +compile.on.save.unsupported.javawebstart=true +$target.run=jws-run +main.class=glredbood.JOGLApplet +run.jvmargs= diff --git a/src/redbook/nbproject/genfiles.properties b/src/redbook/nbproject/genfiles.properties new file mode 100644 index 0000000..e3256a4 --- /dev/null +++ b/src/redbook/nbproject/genfiles.properties @@ -0,0 +1,5 @@ +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=f7c0fcba +nbproject/build-impl.xml.script.CRC32=138943b7 +nbproject/[email protected] diff --git a/src/redbook/nbproject/jnlp-impl.xml b/src/redbook/nbproject/jnlp-impl.xml new file mode 100644 index 0000000..497e1f5 --- /dev/null +++ b/src/redbook/nbproject/jnlp-impl.xml @@ -0,0 +1,293 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + +Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + + +The contents of this file are subject to the terms of either the GNU +General Public License Version 2 only ("GPL") or the Common +Development and Distribution License("CDDL") (collectively, the +"License"). You may not use this file except in compliance with the +License. You can obtain a copy of the License at +http://www.netbeans.org/cddl-gplv2.html +or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the +specific language governing permissions and limitations under the +License. When distributing the software, include this License Header +Notice in each file and include the License file at +nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this +particular file as subject to the "Classpath" exception as provided +by Sun in the GPL Version 2 section of the License file that +accompanied this code. If applicable, add the following below the +License Header, with the fields enclosed by brackets [] replaced by +your own identifying information: +"Portions Copyrighted [year] [name of copyright owner]" + +Contributor(s): + +The Original Software is NetBeans. The Initial Developer of the Original +Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun +Microsystems, Inc. All Rights Reserved. + +If you wish your version of this file to be governed by only the CDDL +or only the GPL Version 2, indicate your decision by adding +"[Contributor] elects to include this software in this distribution +under the [CDDL or GPL Version 2] license." If you do not indicate a +single choice of license, a recipient has the option to distribute +your version of this file under either the CDDL, the GPL Version 2 or +to extend the choice of license to its licensees as provided above. +However, if you add GPL Version 2 code and therefore, elected the GPL +Version 2 license, then the option applies only if the new code is +made subject to such option by the copyright holder. +--> + +<project name="jnlp-impl" default="default" basedir=".."> + + <property name="jnlp.file.name.default" value="launch.jnlp"/> + <property name="jnlp.dest.dir" location="dist"/> + + <!-- helper file to create list of arguments, etc. --> + <property name="helper.file" location="helper.txt"/> + + <target name="default"> + <echo message="Default target is not set, you must specify which target you want to run."/> + </target> + + <!-- Main target --> + + <target name="jnlp" depends="-init-filename,-test-jnlp-enabled" if="is.jnlp.enabled"> + <delete file="dist/lib/javaws.jar" failonerror="false"/> + <antcall target="generate-jnlp"/> + <antcall target="generate-html-preview"/> + </target> + + <!-- Init jnlp filename --> + + <target name="-init-filename" depends="-check-filename-prop,-set-jnlp-filename-custom,-set-jnlp-filename-default"/> + + <target name="-set-jnlp-filename-custom" if="jnlp.file.name.set"> + <property name="jnlp.file" value="${jnlp.file.name}"/> + </target> + + <target name="-set-jnlp-filename-default" unless="jnlp.file.name.set"> + <property name="jnlp.file" value="${jnlp.file.name.default}"/> + </target> + + <target name="-check-filename-prop"> + <condition property="jnlp.file.name.set"> + <and> + <isset property="jnlp.file.name"/> + <not> + <equals arg1="jnlp.file.name" arg2="" trim="true"/> + </not> + </and> + </condition> + </target> + + <!-- Test JNLP enabled--> + + <target name="-test-jnlp-enabled"> + <condition property="is.jnlp.enabled"> + <istrue value="${jnlp.enabled}"/> + </condition> + </target> + + <!-- Generating JNLP file --> + + <target name="generate-jnlp" depends="sign-jars,-test-generate-task-available,-unavailable-generate-task" if="generate.task.available"> + <taskdef name="generate-jnlp" classname="org.netbeans.modules.javawebstart.anttasks.GenerateJnlpFileTask" + classpath="${libs.JWSAntTasks.classpath}"/> + <generate-jnlp destfile="${jnlp.dest.dir}/${jnlp.file}_" template="master-${jnlp.descriptor}.jnlp" destdir="dist"/> + <antcall target="-strip-empty-lines"/> + </target> + + <target name="-strip-empty-lines"> + <copy file="${jnlp.dest.dir}/${jnlp.file}_" tofile="${jnlp.dest.dir}/${jnlp.file}" overwrite="true" encoding="UTF-8"> + <filterchain> + <linecontainsregexp> + <regexp pattern=".+"/> + </linecontainsregexp> + </filterchain> + </copy> + <delete file="${jnlp.dest.dir}/${jnlp.file}_" failonerror="false"/> + </target> + + <target name="-test-generate-task-available"> + <available property="generate.task.available" classname="org.netbeans.modules.javawebstart.anttasks.GenerateJnlpFileTask" + classpath="${libs.JWSAntTasks.classpath}"/> + </target> + + <target name="-unavailable-generate-task" unless="generate.task.available"> + <echo message="Task required to generate JNLP file is missing, probably the library 'JWS Ant Tasks' is missing either from shared folder or from IDE installation."/> + <fail/> + </target> + + <!-- Codebase processing --> + + <target name="-codebase-props-check"> + <condition property="local.codebase"> + <or> + <not> + <isset property="jnlp.codebase.type"/> + </not> + <equals arg1="${jnlp.codebase.type}" arg2="local" trim="true"/> + </or> + </condition> + <condition property="non.user.codebase"> + <or> + <not> + <isset property="jnlp.codebase.type"/> + </not> + <equals arg1="${jnlp.codebase.type}" arg2="local" trim="true"/> + <equals arg1="${jnlp.codebase.type}" arg2="web" trim="true"/> + </or> + </condition> + <condition property="user.codebase"> + <equals arg1="${jnlp.codebase.type}" arg2="user" trim="true"/> + </condition> + </target> + + <target name="-init-non-user-codebase" if="non.user.codebase"> + <property name="jnlp.codebase.value" value="${jnlp.codebase.url}"/> + </target> + + <target name="-init-user-codebase" if="user.codebase"> + <property name="jnlp.codebase.value" value="${jnlp.codebase.user}"/> + </target> + + <!-- Security --> + + <target name="-security-props-check"> + <condition property="jnlp.signed.true"> + <istrue value="${jnlp.signed}"/> + </condition> + </target> + + <target name="-jnlp-init-keystore" depends="-jnlp-init-keystore-props,-check-keystore-exists" unless="jnlp.signjar.keystore.exists"> + <echo message="${application.vendor}" file="${helper.file}"/> + <loadfile property="application.vendor.filtered" srcfile="${helper.file}"> + <filterchain> + <deletecharacters chars=","/> + </filterchain> + </loadfile> + <delete file="${helper.file}"/> + <property name="jnlp.signjar.vendor" value="CN=${application.vendor.filtered}"/> + <echo message="Going to create default keystore in ${jnlp.signjar.keystore}"/> + <genkey dname="${jnlp.signjar.vendor}" alias="${jnlp.signjar.alias}" keystore="${jnlp.signjar.keystore}" + storepass="${jnlp.signjar.storepass}" keypass="${jnlp.signjar.keypass}"/> + </target> + + <target name="-check-keystore-exists"> + <available property="jnlp.signjar.keystore.exists" file="${jnlp.signjar.keystore}"/> + </target> + + <target name="-jnlp-init-keystore-props"> + <property name="jnlp.signjar.keystore" value="${basedir}/build/nb-jws.ks"/> + <property name="jnlp.signjar.storepass" value="storepass"/> + <property name="jnlp.signjar.keypass" value="keypass"/> + <property name="jnlp.signjar.alias" value="nb-jws"/> + </target> + + <!-- Signing --> + + <target name="-test-signjars-task-available"> + <available property="signjars.task.available" + classname="org.netbeans.modules.javawebstart.anttasks.SignJarsTask" + classpath="${libs.JWSAntTasks.classpath}"/> + </target> + + <target name="-check-signing-possible" depends="-security-props-check,-test-signjars-task-available,-unavailable-signjars-task"> + <condition property="jnlp.signed.true+signjars.task.available"> + <and> + <isset property="jnlp.signed.true"/> + <isset property="signjars.task.available"/> + </and> + </condition> + </target> + + <target name="-unavailable-signjars-task" depends="-test-signjars-task-available" unless="signjars.task.available"> + <echo message="Task required to sign JAR file is missing, probably the library 'JWS Ant Tasks' is missing either from shared folder or from IDE installation. JAR files will not be signed."/> + </target> + + <target name="sign-jars" depends="-jnlp-init-keystore,-check-signing-possible" if="jnlp.signed.true+signjars.task.available"> + <taskdef name="sign-jars" classname="org.netbeans.modules.javawebstart.anttasks.SignJarsTask" + classpath="${libs.JWSAntTasks.classpath}"/> + <sign-jars keystore="${jnlp.signjar.keystore}" storepass="${jnlp.signjar.storepass}" + keypass="${jnlp.signjar.keypass}" alias="${jnlp.signjar.alias}" mainjar="${dist.jar}" destdir="dist" + codebase="${jnlp.codebase.value}" signedjarsprop="jnlp.signed.jars" + componentsprop="jnlp.components"> + <fileset dir="dist/lib"> + <include name="*.jar"/> + </fileset> + </sign-jars> + </target> + + <!-- Running/Debugging --> + + <target name="jws-run" depends="jar,-verify-jnlp-enabled,-verify-codebase" description="Start javaws execution"> + <exec executable="${java.home}/bin/javaws"> + <arg file="${jnlp.dest.dir}/${jnlp.file}"/> + </exec> + </target> + + <target name="jws-debug" if="netbeans.home" depends="jar,-verify-jnlp-enabled,-verify-codebase,-debug-start-debugger,-debug-javaws-debuggee" + description="Debug javaws project in IDE"/> + + <target name="-init-debug-args"> + <property name="version-output" value="java version "${ant.java.version}"/> + <condition property="have-jdk-older-than-1.4"> + <or> + <contains string="${version-output}" substring="java version "1.0"/> + <contains string="${version-output}" substring="java version "1.1"/> + <contains string="${version-output}" substring="java version "1.2"/> + <contains string="${version-output}" substring="java version "1.3"/> + </or> + </condition> + <condition else="-Xdebug" property="debug-args-line" value="-Xdebug -Xnoagent -Djava.compiler=none"> + <istrue value="${have-jdk-older-than-1.4}"/> + </condition> + <condition else="dt_socket" property="debug-transport-by-os" value="dt_shmem"> + <os family="windows"/> + </condition> + <condition else="${debug-transport-by-os}" property="debug-transport" value="${debug.transport}"> + <isset property="debug.transport"/> + </condition> + </target> + + <target name="-debug-javaws-debuggee" depends="-init-debug-args"> + <exec executable="${java.home}/bin/javaws"> + <env key="JAVAWS_VM_ARGS" value="${debug-args-line} -Xrunjdwp:transport=${debug-transport},address=${jpda.address}"/> + <arg file="${jnlp.dest.dir}/${jnlp.file}"/> + </exec> + </target> + + <target name="-verify-codebase" depends="-codebase-props-check" unless="local.codebase"> + <fail message="Project cannot be run with non-local codebase. Open project properties dialog and set Web Start Codebase to Local Execution."/> + </target> + + <target name="-verify-jnlp-enabled" depends="-test-jnlp-enabled" unless="is.jnlp.enabled"> + <fail message="Project cannot be run with selected Run Configuration when Java Web Start is disabled."/> + </target> + + <!-- Generate simple HTML preview page --> + + <target name="-check-html-preview"> + <condition property="generate.html.preview"> + <or> + <equals arg1="${jnlp.descriptor}" arg2="application"/> + <equals arg1="${jnlp.descriptor}" arg2="applet"/> + </or> + </condition> + </target> + + <target name="generate-html-preview" depends="-check-html-preview" if="generate.html.preview"> + <copy file="preview-${jnlp.descriptor}.html" tofile="${jnlp.dest.dir}/launch.html" overwrite="true" encoding="UTF-8"> + <filterchain> + <replacestring from="${JNLP.FILE}" to="${jnlp.file}"/> + <replacestring from="${JNLP.APPLET.WIDTH}" to="${jnlp.applet.width}"/> + <replacestring from="${JNLP.APPLET.HEIGHT}" to="${jnlp.applet.height}"/> + </filterchain> + </copy> + </target> + +</project> diff --git a/src/redbook/nbproject/project.properties b/src/redbook/nbproject/project.properties new file mode 100644 index 0000000..173b026 --- /dev/null +++ b/src/redbook/nbproject/project.properties @@ -0,0 +1,106 @@ +#platform to run this project on; supported values: +# linux-i586 +# linux-amd64 +# macosx-ppc +# macosx-universal +# solaris-sparc +# solaris-sparcv9 +# solaris-i586 +# windows-i586 +# windows-amd64 +#natives.platform=linux-amd64 + +# Space-separated list of JVM arguments used when running the project +# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value +# or test-sys-prop.name=value to set system properties for unit tests): +run.jvmargs=-Djava.library.path="lib" -Djava.security.policy=applet.policy + +jogl.webres=http://download.java.net/media/jogl/builds/nightly + +source.reference.jogl.all.jar=jogl-dist/jogl-2.0-src.zip +source.reference.gluegen-rt.jar=jogl-dist/gluegen-rt-2.0-src.zip +source.reference.nativewindow.all.jar=jogl-dist/nativewindow-2.0-src.zip +source.reference.newt.all.jar=jogl-dist/newt-2.0-src.zip + +javadoc.reference.jogl.all.jar=jogl-dist/jogl-2.0-doc.zip!/jogl-2.0-docs/ +javadoc.reference.gluegen-rt.jar=jogl-dist/jogl-2.0-doc.zip!/jogl-2.0-docs/ +javadoc.reference.nativewindow.all.jar=jogl-dist/jogl-2.0-doc.zip!/jogl-2.0-docs/ +javadoc.reference.newt.all.jar=jogl-dist/jogl-2.0-doc.zip!/jogl-2.0-docs/ + +file.reference.gluegen-rt.jar=lib/gluegen-rt.jar +file.reference.jogl.all.jar=lib/jogl.all.jar +file.reference.nativewindow.all.jar=lib/nativewindow.all.jar +file.reference.newt.all.jar=lib/newt.all.jar + +jnlp.applet.class=glredbook.JOGLApplet +jnlp.applet.height=400 +jnlp.applet.width=400 +jnlp.codebase.type=local +jnlp.codebase.url=file:/home/mbien/NetBeansProjects/redbook/dist/ +jnlp.descriptor=applet +jnlp.enabled=true +jnlp.offline-allowed=false +jnlp.signed=false + +ant.customtasks.libs=JWSAntTasks +application.args= +application.title=redbook +application.vendor=mbien +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/RedBook_Samples.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= + +includes=** +jar.compress=false +javac.classpath=\ + ${file.reference.jogl.all.jar}:\ + ${file.reference.gluegen-rt.jar}:\ + ${file.reference.nativewindow.all.jar}:\ + ${file.reference.newt.all.jar} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.source=1.5 +javac.target=1.5 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding= +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=org.yourorghere.Redbook +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +src.dir=src +test.src.dir=test diff --git a/src/redbook/nbproject/project.xml b/src/redbook/nbproject/project.xml new file mode 100644 index 0000000..3ba8d37 --- /dev/null +++ b/src/redbook/nbproject/project.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://www.netbeans.org/ns/project/1"> + <type>org.netbeans.modules.java.j2seproject</type> + <configuration> + <buildExtensions xmlns="http://www.netbeans.org/ns/ant-build-extender/1"> + <extension file="jnlp-impl.xml" id="jws"> + <dependency dependsOn="jnlp" target="jar"/> + </extension> + </buildExtensions> + <data xmlns="http://www.netbeans.org/ns/j2se-project/3"> + <name>RedBook Samples</name> + <minimum-ant-version>1.6.5</minimum-ant-version> + <source-roots> + <root id="src.dir"/> + </source-roots> + <test-roots> + <root id="test.src.dir"/> + </test-roots> + </data> + <libraries xmlns="http://www.netbeans.org/ns/ant-project-libraries/1"> + <definitions>./lib/nblibraries.properties</definitions> + </libraries> + </configuration> +</project> diff --git a/src/redbook/preview-applet.html b/src/redbook/preview-applet.html new file mode 100644 index 0000000..d2c6c4b --- /dev/null +++ b/src/redbook/preview-applet.html @@ -0,0 +1,185 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<!-- ########################## IMPORTANT NOTE ############################ --> +<!-- This preview HTML page will work only with JDK 6 update 10 and higher! --> +<!-- ###################################################################### --> +<html> + <head> + <title>Test page for launching the applet via JNLP</title> + <script type="text/javascript"> + current = 0; + function next() { + if (document.slideform.slide[current+1]) { + document.slideform.applet.loadDemo(document.slideform.slide[current+1].value); + document.slideform.slide.selectedIndex = ++current; + } + else first(); + } + function previous() { + if (current-1 >= 0) { + document.slideform.applet.loadDemo(document.slideform.slide[current-1].value); + document.slideform.slide.selectedIndex = --current; + } + else last(); + } + function first() { + current = 0; + document.slideform.applet.loadDemo(document.slideform.slide[0].value); + document.slideform.slide.selectedIndex = 0; + } + function last() { + current = document.slideform.slide.length-1; + document.slideform.applet.loadDemo(document.slideform.slide[current].value); + document.slideform.slide.selectedIndex = current; + } + function change() { + current = document.slideform.slide.selectedIndex; + document.slideform.applet.loadDemo(document.slideform.slide[current].value); + } + // End --> + </script> + </head> + <body> + <h3>Test page for launching the applet via JNLP</h3> + <form name=slideform action=""> + <table cellspacing=1 cellpadding=4 bgcolor="#000000"> + <tr> + <td align=center bgcolor="white"> + <b>Applet Slideshow</b> + </td> + </tr> + <tr> + <td align=center bgcolor="white" width=200 height=150> + <applet name="applet" width='${JNLP.APPLET.WIDTH}' height='${JNLP.APPLET.HEIGHT}'> + <param name='jnlp_href' value='${JNLP.FILE}'/> + <param name='demo' value='glredbook10.accanti'/> + </applet> + </td> + </tr> + <tr> + <td align=center bgcolor="#C0C0C0"> + <select name="slide" onChange="change();"> + <option value='glredbook10.accanti' selected>glredbook10.accanti + <option value='glredbook10.accnot' selected>glredbook10.accnot + <option value='glredbook10.accpersp' selected>glredbook10.accpersp + <option value='glredbook10.accum' selected>glredbook10.accum + <option value='glredbook10.aim' selected>glredbook10.aim + <option value='glredbook10.alpha' selected>glredbook10.alpha + <option value='glredbook10.alpha3D' selected>glredbook10.alpha3D + <option value='glredbook10.anti' selected>glredbook10.anti + <option value='glredbook10.antipoint' selected>glredbook10.antipoint + <option value='glredbook10.antipoly' selected>glredbook10.antipoly + <option value='glredbook10.bezcurve' selected>glredbook10.bezcurve + <option value='glredbook10.bezmesh' selected>glredbook10.bezmesh + <option value='glredbook10.bezsurf' selected>glredbook10.bezsurf + <option value='glredbook10.checker' selected>glredbook10.checker + <option value='glredbook10.checker2' selected>glredbook10.checker2 + <option value='glredbook10.chess' selected>glredbook10.chess + <option value='glredbook10.clip' selected>glredbook10.clip + <option value='glredbook10.colormat' selected>glredbook10.colormat + <option value='glredbook10.cone' selected>glredbook10.cone + <option value='glredbook10.cube' selected>glredbook10.cube + <option value='glredbook10.depthcue' selected>glredbook10.depthcue + <option value='glredbook10.disk' selected>glredbook10.disk + <option value='glredbook10.dof' selected>glredbook10.dof + <option value='glredbook10.dofnot' selected>glredbook10.dofnot + <option value='glredbook10.doublebuf' selected>glredbook10.doublebuf + <option value='glredbook10.drawf' selected>glredbook10.drawf + <option value='glredbook10.feedback' selected>glredbook10.feedback + <option value='glredbook10.fog' selected>glredbook10.fog + <option value='glredbook10.font' selected>glredbook10.font + <option value='glredbook10.jfont' selected>glredbook10.jfont + <option value='glredbook10.light' selected>glredbook10.light + <option value='glredbook10.linelist' selected>glredbook10.linelist + <option value='glredbook10.lines' selected>glredbook10.lines + <option value='glredbook10.list' selected>glredbook10.list + <option value='glredbook10.list2' selected>glredbook10.list2 + <option value='glredbook10.maplight' selected>glredbook10.maplight + <option value='glredbook10.material' selected>glredbook10.material + <option value='glredbook10.mipmap' selected>glredbook10.mipmap + <option value='glredbook10.model' selected>glredbook10.model + <option value='glredbook10.movelight' selected>glredbook10.movelight + <option value='glredbook10.pickdepth' selected>glredbook10.pickdepth + <option value='glredbook10.pickline' selected>glredbook10.pickline + <option value='glredbook10.picksquare' selected>glredbook10.picksquare + <option value='glredbook10.plane' selected>glredbook10.plane + <option value='glredbook10.planet' selected>glredbook10.planet + <option value='glredbook10.planetup' selected>glredbook10.planetup + <option value='glredbook10.polys' selected>glredbook10.polys + <option value='glredbook10.robot' selected>glredbook10.robot + <option value='glredbook10.sccolorlight' selected>glredbook10.sccolorlight + <option value='glredbook10.scene' selected>glredbook10.scene + <option value='glredbook10.scenebamb' selected>glredbook10.scenebamb + <option value='glredbook10.sceneflat' selected>glredbook10.sceneflat + <option value='glredbook10.select' selected>glredbook10.select + <option value='glredbook10.simple' selected>glredbook10.simple + <option value='glredbook10.smooth' selected>glredbook10.smooth + <option value='glredbook10.sphere' selected>glredbook10.sphere + <option value='glredbook10.stencil' selected>glredbook10.stencil + <option value='glredbook10.stroke' selected>glredbook10.stroke + <option value='glredbook10.tea' selected>glredbook10.tea + <option value='glredbook10.teaambient' selected>glredbook10.teaambient + <option value='glredbook10.teapots' selected>glredbook10.teapots + <option value='glredbook10.texturesurf' selected>glredbook10.texturesurf + <option value='glredbook11.aargb' selected>glredbook11.aargb + <option value='glredbook11.hello' selected>glredbook11.hello + <option value='glredbook11.image' selected>glredbook11.image + <option value='glredbook11.polyoff' selected>glredbook11.polyoff + <option value='glredbook11.tess' selected>glredbook11.tess + <option value='glredbook11.tesswind' selected>glredbook11.tesswind + <option value='glredbook11.texbind' selected>glredbook11.texbind + <option value='glredbook11.texgen' selected>glredbook11.texgen + <option value='glredbook11.texprox' selected>glredbook11.texprox + <option value='glredbook11.texsub' selected>glredbook11.texsub + <option value='glredbook11.torus' selected>glredbook11.torus + <option value='glredbook11.unproject' selected>glredbook11.unproject + <option value='glredbook11.varray' selected>glredbook11.varray + <option value='glredbook11.wrap' selected>glredbook11.wrap + <option value='glredbook12x.blendeqn' selected>glredbook12x.blendeqn + <option value='glredbook12x.colormatrix' selected>glredbook12x.colormatrix + <option value='glredbook12x.colortable' selected>glredbook12x.colortable + <option value='glredbook12x.convolution' selected>glredbook12x.convolution + <option value='glredbook12x.histogram' selected>glredbook12x.histogram + <option value='glredbook12x.minmax' selected>glredbook12x.minmax + <option value='glredbook12x.multitex' selected>glredbook12x.multitex + <option value='glredbook12x.quadric' selected>glredbook12x.quadric + <option value='glredbook12x.texture3d' selected>glredbook12x.texture3d + <option value='glredbook1314.combiner' selected>glredbook1314.combiner + <option value='glredbook1314.cubemap' selected>glredbook1314.cubemap + <option value='glredbook1314.fogcoord' selected>glredbook1314.fogcoord + <option value='glredbook1314.multisamp' selected>glredbook1314.multisamp + <option value='glredbook1314.mvarray' selected>glredbook1314.mvarray + <option value='glredbook1314.pointp' selected>glredbook1314.pointp + <option value='glredbook1314.shadowmap' selected>glredbook1314.shadowmap + </select> + </td> + </tr> + <tr> + <td align=center bgcolor="#C0C0C0"> + <input type=button onClick="first();" value="|<<" title="Beginning"> + <input type=button onClick="previous();" value="<<" title="Previous"> + <input type=button onClick="next();" value=">>" title="Next"> + <input type=button onClick="last();" value=">>|" title="End"> + </td> + </tr> + </table> + </form> + + <!-- Or use the following script element to launch with the Deployment Toolkit --> + <!-- Open the deployJava.js script to view its documentation --> + <!-- + <script src="http://java.com/js/deployJava.js"></script> + <script> + var attributes = { + codebase: [applet codebase], + code: [class to launch], + archive: [JAR file with the applet], + width: [applet width], + height: [applet height] + }; + var parameters = { [applet parameters] }; + var version = [JDK version]; + deployJava.runApplet(attributes, parameters, version); + </script> + --> + </body> +</html> diff --git a/src/redbook/src/Data/leeds.bin b/src/redbook/src/Data/leeds.bin Binary files differnew file mode 100644 index 0000000..5dc105e --- /dev/null +++ b/src/redbook/src/Data/leeds.bin diff --git a/src/redbook/src/glredbook/JOGLApplet.java b/src/redbook/src/glredbook/JOGLApplet.java new file mode 100644 index 0000000..d11deac --- /dev/null +++ b/src/redbook/src/glredbook/JOGLApplet.java @@ -0,0 +1,64 @@ +package glredbook; + +import glredbook10.GLSkeleton; +import java.awt.Component; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JApplet; + +/** + * + * @author michael-bien.com + */ +public class JOGLApplet extends JApplet { + + private GLSkeleton<?> skeleton; + + @Override + public void start() { + String className = getParameter("demo"); + loadDemo(className); + } + + @Override + public void stop() { + if (skeleton != null) { + skeleton.runExit(); + } + } + + private Logger log() { + return Logger.getLogger(JOGLApplet.class.getName()); + } + + public void loadDemo(String className) { + + if (skeleton != null) { + skeleton.runExit(); + remove((Component) skeleton.drawable); + } + + log().info("i'll try to instantiate: " + className); + + try { + + final Class<?> clazz = Class.forName(className); + + try { + skeleton = (GLSkeleton<?>) clazz.newInstance(); + System.out.println(skeleton); + add((Component) skeleton.drawable); + System.out.println("added"); + validate(); + } catch (InstantiationException ex) { + log().log(Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + log().log(Level.SEVERE, null, ex); + } + + + } catch (ClassNotFoundException ex) { + log().log(Level.SEVERE, "can't find main class", ex); + } + } +} diff --git a/src/redbook/src/glredbook10/GLSkeleton.java b/src/redbook/src/glredbook10/GLSkeleton.java new file mode 100644 index 0000000..3a4c439 --- /dev/null +++ b/src/redbook/src/glredbook10/GLSkeleton.java @@ -0,0 +1,47 @@ +package glredbook10;
+
+
+import com.sun.opengl.util.FPSAnimator;
+import javax.media.opengl.*;
+
+public abstract class GLSkeleton<D extends GLAutoDrawable> {
+
+ public final D drawable;
+
+ protected FPSAnimator animator;
+ protected int FramePerSecond = 24;
+
+
+ public GLSkeleton() {
+ drawable = createDrawable();
+ }
+
+ protected abstract D createDrawable();
+
+ /**
+ * Call the reference canvas's display methods. Should be called after
+ * handling of input events.
+ */
+ public final void refresh() {
+ if (drawable == null)
+ throw new RuntimeException("GLDrawable is not set.");
+ drawable.display();
+ }//
+
+
+ public final void setAnimator(FPSAnimator animator) {
+ this.animator = animator;
+ }//
+
+ public final void runExit() {
+ new Thread(new Runnable() {
+ public void run() {
+ if(animator!=null)
+ animator.stop();
+ drawable.destroy();
+ }
+ }).start();
+
+ }//
+
+}//
diff --git a/src/redbook/src/glredbook10/README.txt b/src/redbook/src/glredbook10/README.txt new file mode 100644 index 0000000..d3c409e --- /dev/null +++ b/src/redbook/src/glredbook10/README.txt @@ -0,0 +1,73 @@ +This ReadMe belongs to the glredbook10 package of glredbook project.
+
+The Java code example in this directory are ports of C examples that
+accompany the examples printed in the _OpenGL Programming Guide_,
+published by Addison-Wesley; ISBN 0-201-63274-8.
+(Japanese language edition: ISBN: 4-7952-9645-6)
+
+The source code examples here need to be compiled with jogl.jar
+and the system dependent jogl-natives-*.jar
+(where * is either linux, win32, solsparc, solx86, and/or macosx).
+
+- No ant build file is available yet.
+- All programs require Java Runtime 1.4.2.
+- All examples that includes jitter.h
+ has an internal copy.
+- All examples extends from glskeleton.java
+
+Note: Color Index examples are not support in javax.media.opengl (jogl),
+so they are not ported. NURBS are also not ported for the same reason.
+LIST:
+- antiindex.c
+- antipindex.c
+- fogindex.c
+- nurbs.c
+- surface.c
+- trim.c
+
+jfont.java is my replacement for xfont.c. jfont uses Java's GlyphVector
+structure a string's geometry while xfont uses X Window specific code
+to do the same.
+
+Kiet Le (Java port)
+ak.kiet.le '@' gmail '.' com
+ak.kiet.le.googlepages.com/theredbookinjava.html
+These ports are (c) Copyright 2006, Kiet Le.
+
+/*
+ * For the software in this directory
+ * (c) Copyright 1993, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED
+ * Permission to use, copy, modify, and distribute this software for
+ * any purpose and without fee is hereby granted, provided that the above
+ * copyright notice appear in all copies and that both the copyright notice
+ * and this permission notice appear in supporting documentation, and that
+ * the name of Silicon Graphics, Inc. not be used in advertising
+ * or publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
+ * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
+ * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
+ * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
+ * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
+ * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
+ * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
+ * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights
+ * Use, duplication, or disclosure by the Government is subject to
+ * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
+ * (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor
+ * clauses in the FAR or the DOD or NASA FAR Supplement.
+ * Unpublished-- rights reserved under the copyright laws of the
+ * United States. Contractor/manufacturer is Silicon Graphics,
+ * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
diff --git a/src/redbook/src/glredbook10/accanti.java b/src/redbook/src/glredbook10/accanti.java new file mode 100644 index 0000000..12cd9cf --- /dev/null +++ b/src/redbook/src/glredbook10/accanti.java @@ -0,0 +1,366 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * Use the accumulation buffer to do full-scene antialiasing on a scene with
+ * orthographic parallel projection.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class accanti
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+ private static final int ACSIZE = 8;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setAccumBlueBits(16);
+ caps.setAccumGreenBits(16);
+ caps.setAccumRedBits(16);
+ //
+ GLJPanel canvas = new GLJPanel(caps);
+ canvas.addGLEventListener(this);
+ canvas.addKeyListener(this);
+ return canvas;
+ }
+
+ public static void main(String[] args) {
+
+ accanti demo = new accanti();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("accanti");
+ frame.getContentPane().add(demo.drawable);
+
+ frame.setSize(250, 250);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize lighting and other values.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float mat_ambient[] = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
+ float mat_specular[] = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_position[] = new float[] { 0.0f, 0.0f, 10.0f, 1.0f };
+ float lm_ambient[] = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 50.0f);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lm_ambient, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glShadeModel(GL2.GL_FLAT);
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glClearAccum(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ int viewport[] = new int[4];
+ int jitter;
+
+ gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
+
+ gl.glClear(GL2.GL_ACCUM_BUFFER_BIT);
+ for (jitter = 0; jitter < ACSIZE; jitter++) {
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glPushMatrix();
+ /*
+ * Note that 4.5 is the distance in world space between left and
+ * right and bottom and top. This formula converts fractional pixel
+ * movement to world coordinates.
+ */
+ gl.glTranslatef(j8[jitter].x * 4.5f / viewport[2], //
+ j8[jitter].y * 4.5f / viewport[3], 0.0f);
+ displayObjects(gl);
+ gl.glPopMatrix();
+ gl.glAccum(GL2.GL_ACCUM, 1.0f / ACSIZE);
+ }
+ gl.glAccum(GL2.GL_RETURN, 1.0f);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-2.25, 2.25, -2.25 * h / w, 2.25 * h / w, -10.0, 10.0);
+ else
+ gl.glOrtho(-2.25 * w / h, 2.25 * w / h, -2.25, 2.25, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+ public void displayObjects(GL2 gl) {
+ float torus_diffuse[] = new float[] { 0.7f, 0.7f, 0.0f, 1.0f };
+ float cube_diffuse[] = new float[] { 0.0f, 0.7f, 0.7f, 1.0f };
+ float sphere_diffuse[] = new float[] { 0.7f, 0.0f, 0.7f, 1.0f };
+ float octa_diffuse[] = new float[] { 0.7f, 0.4f, 0.4f, 1.0f };
+
+ gl.glPushMatrix();
+ gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.80f, 0.35f, 0.0f);
+ gl.glRotatef(100.0f, 1.0f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, torus_diffuse, 0);
+ glut.glutSolidTorus(0.275, 0.85, 16, 16);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.75f, -0.50f, 0.0f);
+ gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
+ gl.glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, cube_diffuse, 0);
+ glut.glutSolidCube(1.5f);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.75f, 0.60f, 0.0f);
+ gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, sphere_diffuse, 0);
+ glut.glutSolidSphere(1.0, 16, 16);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.70f, -0.90f, 0.25f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, octa_diffuse, 0);
+ glut.glutSolidOctahedron();
+ gl.glPopMatrix();
+
+ gl.glPopMatrix();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ // TODO Auto-generated method stub
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ // TODO Auto-generated method stub
+ }
+
+ /* 2 jitter points */
+ jitter_point j2[] = { new jitter_point(0.246490f, 0.249999f),
+ new jitter_point(-0.246490f, -0.249999f) };
+
+ /* 3 jitter points */
+ jitter_point j3[] = { new jitter_point(-0.373411, -0.250550),//
+ new jitter_point(0.256263, 0.368119), //
+ new jitter_point(0.117148, -0.117570) };
+
+ /* 4 jitter points */
+ jitter_point j4[] = { new jitter_point(-0.208147, 0.353730),
+ new jitter_point(0.203849, -0.353780),
+ new jitter_point(-0.292626, -0.149945),
+ new jitter_point(0.296924, 0.149994) };
+
+ /* 8 jitter points */
+ jitter_point j8[] = { new jitter_point(-0.334818, 0.435331),
+ new jitter_point(0.286438, -0.393495),
+ new jitter_point(0.459462, 0.141540),
+ new jitter_point(-0.414498, -0.192829),
+ new jitter_point(-0.183790, 0.082102),
+ new jitter_point(-0.079263, -0.317383),
+ new jitter_point(0.102254, 0.299133),
+ new jitter_point(0.164216, -0.054399) };
+
+ /* 15 jitter points */
+ jitter_point j15[] = { new jitter_point(0.285561, 0.188437),
+ new jitter_point(0.360176, -0.065688),
+ new jitter_point(-0.111751, 0.275019),
+ new jitter_point(-0.055918, -0.215197),
+ new jitter_point(-0.080231, -0.470965),
+ new jitter_point(0.138721, 0.409168),
+ new jitter_point(0.384120, 0.458500),
+ new jitter_point(-0.454968, 0.134088),
+ new jitter_point(0.179271, -0.331196),
+ new jitter_point(-0.307049, -0.364927),
+ new jitter_point(0.105354, -0.010099),
+ new jitter_point(-0.154180, 0.021794),
+ new jitter_point(-0.370135, -0.116425),
+ new jitter_point(0.451636, -0.300013),
+ new jitter_point(-0.370610, 0.387504) };
+
+ /* 24 jitter points */
+ jitter_point j24[] = { new jitter_point(0.030245, 0.136384),
+ new jitter_point(0.018865, -0.348867),
+ new jitter_point(-0.350114, -0.472309),
+ new jitter_point(0.222181, 0.149524),
+ new jitter_point(-0.393670, -0.266873),
+ new jitter_point(0.404568, 0.230436),
+ new jitter_point(0.098381, 0.465337),
+ new jitter_point(0.462671, 0.442116),
+ new jitter_point(0.400373, -0.212720),
+ new jitter_point(-0.409988, 0.263345),
+ new jitter_point(-0.115878, -0.001981),
+ new jitter_point(0.348425, -0.009237),
+ new jitter_point(-0.464016, 0.066467),
+ new jitter_point(-0.138674, -0.468006),
+ new jitter_point(0.144932, -0.022780),
+ new jitter_point(-0.250195, 0.150161),
+ new jitter_point(-0.181400, -0.264219),
+ new jitter_point(0.196097, -0.234139),
+ new jitter_point(-0.311082, -0.078815),
+ new jitter_point(0.268379, 0.366778),
+ new jitter_point(-0.040601, 0.327109),
+ new jitter_point(-0.234392, 0.354659),
+ new jitter_point(-0.003102, -0.154402),
+ new jitter_point(0.297997, -0.417965) };
+
+ /* 66 jitter points */
+ jitter_point j66[] = { new jitter_point(0.266377, -0.218171),
+ new jitter_point(-0.170919, -0.429368),
+ new jitter_point(0.047356, -0.387135),
+ new jitter_point(-0.430063, 0.363413),
+ new jitter_point(-0.221638, -0.313768),
+ new jitter_point(0.124758, -0.197109),
+ new jitter_point(-0.400021, 0.482195),
+ new jitter_point(0.247882, 0.152010),
+ new jitter_point(-0.286709, -0.470214),
+ new jitter_point(-0.426790, 0.004977),
+ new jitter_point(-0.361249, -0.104549),
+ new jitter_point(-0.040643, 0.123453),
+ new jitter_point(-0.189296, 0.438963),
+ new jitter_point(-0.453521, -0.299889),
+ new jitter_point(0.408216, -0.457699),
+ new jitter_point(0.328973, -0.101914),
+ new jitter_point(-0.055540, -0.477952),
+ new jitter_point(0.194421, 0.453510), //
+ new jitter_point(0.404051, 0.224974), //
+ new jitter_point(0.310136, 0.419700),
+ new jitter_point(-0.021743, 0.403898),
+ new jitter_point(-0.466210, 0.248839),
+ new jitter_point(0.341369, 0.081490),
+ new jitter_point(0.124156, -0.016859),
+ new jitter_point(-0.461321, -0.176661),
+ new jitter_point(0.013210, 0.234401),
+ new jitter_point(0.174258, -0.311854),
+ new jitter_point(0.294061, 0.263364),
+ new jitter_point(-0.114836, 0.328189),
+ new jitter_point(0.041206, -0.106205),
+ new jitter_point(0.079227, 0.345021),
+ new jitter_point(-0.109319, -0.242380),
+ new jitter_point(0.425005, -0.332397),
+ new jitter_point(0.009146, 0.015098),
+ new jitter_point(-0.339084, -0.355707),
+ new jitter_point(-0.224596, -0.189548),
+ new jitter_point(0.083475, 0.117028),
+ new jitter_point(0.295962, -0.334699),
+ new jitter_point(0.452998, 0.025397),
+ new jitter_point(0.206511, -0.104668),
+ new jitter_point(0.447544, -0.096004),
+ new jitter_point(-0.108006, -0.002471),
+ new jitter_point(-0.380810, 0.130036),
+ new jitter_point(-0.242440, 0.186934),
+ new jitter_point(-0.200363, 0.070863),
+ new jitter_point(-0.344844, -0.230814),
+ new jitter_point(0.408660, 0.345826),
+ new jitter_point(-0.233016, 0.305203),
+ new jitter_point(0.158475, -0.430762),
+ new jitter_point(0.486972, 0.139163),
+ new jitter_point(-0.301610, 0.009319),
+ new jitter_point(0.282245, -0.458671),
+ new jitter_point(0.482046, 0.443890),
+ new jitter_point(-0.121527, 0.210223),
+ new jitter_point(-0.477606, -0.424878),
+ new jitter_point(-0.083941, -0.121440),
+ new jitter_point(-0.345773, 0.253779),
+ new jitter_point(0.234646, 0.034549),
+ new jitter_point(0.394102, -0.210901),
+ new jitter_point(-0.312571, 0.397656),
+ new jitter_point(0.200906, 0.333293),
+ new jitter_point(0.018703, -0.261792),
+ new jitter_point(-0.209349, -0.065383),
+ new jitter_point(0.076248, 0.478538),
+ new jitter_point(-0.073036, -0.355064),
+ new jitter_point(0.145087, 0.221726) };
+
+
+ class jitter_point {
+ private static final int MAX_SAMPLES = 66;
+ float x, y;
+
+ public jitter_point(double x, double y) {
+ this.x = (float) x;
+ this.y = (float) y;
+ }
+ }
+
+ public void setIdle(boolean isidle) {
+ }
+}
+/*
+ * For the software in this directory (c) Copyright 1993, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED Permission to use, copy, modify, and distribute this
+ * software for any purpose and without fee is hereby granted, provided that the
+ * above copyright notice appear in all copies and that both the copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of Silicon Graphics, Inc. not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND WITHOUT
+ * WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT
+ * LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
+ * PURPOSE. IN NO EVENT SHALL SILICON GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE
+ * ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES
+ * OF ANY KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF
+ * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES,
+ * WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF THE POSSIBILITY OF
+ * SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR
+ * IN CONNECTION WITH THE POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights Use, duplication, or disclosure by the
+ * Government is subject to restrictions set forth in FAR 52.227.19(c)(2) or
+ * subparagraph (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor clauses in the
+ * FAR or the DOD or NASA FAR Supplement. Unpublished-- rights reserved under
+ * the copyright laws of the United States. Contractor/manufacturer is Silicon
+ * Graphics, Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
\ No newline at end of file diff --git a/src/redbook/src/glredbook10/accnot.java b/src/redbook/src/glredbook10/accnot.java new file mode 100644 index 0000000..cd5bcca --- /dev/null +++ b/src/redbook/src/glredbook10/accnot.java @@ -0,0 +1,190 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import com.sun.opengl.util.gl2.GLUT;
+import javax.media.opengl.*; import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class accnot//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setAccumBlueBits(16);
+ caps.setAccumGreenBits(16);
+ caps.setAccumRedBits(16);
+ //
+ GLJPanel canvas = new GLJPanel(caps);
+ canvas.addGLEventListener(this);
+ canvas.addKeyListener(this);
+ return canvas;
+ }
+
+ public static void main(String[] args) {
+ accnot demo = new accnot();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("accnot");
+ frame.setSize(250, 250);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ }
+
+ /*
+ * Initialize lighting and other values.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float mat_ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_position[] = { 0.0f, 0.0f, 10.0f, 1.0f };
+ float lm_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 50.0f);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lm_ambient, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float torus_diffuse[] = { 0.7f, 0.7f, 0.0f, 1.0f };
+ float cube_diffuse[] = { 0.0f, 0.7f, 0.7f, 1.0f };
+ float sphere_diffuse[] = { 0.7f, 0.0f, 0.7f, 1.0f };
+ float octa_diffuse[] = { 0.7f, 0.4f, 0.4f, 1.0f };
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glPushMatrix();
+ gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.80f, 0.35f, 0.0f);
+ gl.glRotatef(100.0f, 1.0f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, torus_diffuse, 0);
+ glut.glutSolidTorus(0.275f, 0.85f, 10, 10);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.75f, -0.50f, 0.0f);
+ gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
+ gl.glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, cube_diffuse, 0);
+ glut.glutSolidCube(1.5f);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.75f, 0.60f, 0.0f);
+ gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, sphere_diffuse, 0);
+ glut.glutSolidSphere(1.0f, 10, 10);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.70f, -0.90f, 0.25f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, octa_diffuse, 0);
+ glut.glutSolidOctahedron();
+ gl.glPopMatrix();
+
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-2.25, 2.25, -2.25 * h / w, 2.25 * h / w, -10.0, 10.0);
+ else
+ gl.glOrtho(-2.25 * w / h, 2.25 * w / h, -2.25, 2.25, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}//
+/*
+ * For the software in this directory
+ * (c) Copyright 1993, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED
+ * Permission to use, copy, modify, and distribute this software for
+ * any purpose and without fee is hereby granted, provided that the above
+ * copyright notice appear in all copies and that both the copyright notice
+ * and this permission notice appear in supporting documentation, and that
+ * the name of Silicon Graphics, Inc. not be used in advertising
+ * or publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
+ * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
+ * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
+ * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
+ * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
+ * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
+ * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
+ * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights
+ * Use, duplication, or disclosure by the Government is subject to
+ * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
+ * (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor
+ * clauses in the FAR or the DOD or NASA FAR Supplement.
+ * Unpublished-- rights reserved under the copyright laws of the
+ * United States. Contractor/manufacturer is Silicon Graphics,
+ * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
\ No newline at end of file diff --git a/src/redbook/src/glredbook10/accpersp.java b/src/redbook/src/glredbook10/accpersp.java new file mode 100644 index 0000000..8bd4ef1 --- /dev/null +++ b/src/redbook/src/glredbook10/accpersp.java @@ -0,0 +1,410 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * Use the accumulation buffer to do full-scene antialiasing on a scene with
+ * perspective projection.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class accpersp//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+ //
+ private static final int ACSIZE = 8;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setAccumBlueBits(16);
+ caps.setAccumGreenBits(16);
+ caps.setAccumRedBits(16);
+ //
+ GLJPanel canvas = new GLJPanel(caps);
+ canvas.addGLEventListener(this);
+ canvas.addKeyListener(this);
+ return canvas;
+ }
+
+ public static void main(String[] args) {
+
+ accpersp demo = new accpersp();
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("accpersp");
+ frame.setSize(250, 250);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float mat_ambient[] = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
+ float mat_specular[] = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_position[] = new float[] { 0.0f, 0.0f, 10.0f, 1.0f };
+ float lm_ambient[] = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 50.0f);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lm_ambient, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glShadeModel(GL2.GL_FLAT);
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glClearAccum(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ int viewport[] = new int[4];
+ int jitter;
+
+ gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
+
+ gl.glClear(GL2.GL_ACCUM_BUFFER_BIT);
+ for (jitter = 0; jitter < ACSIZE; jitter++) {
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ accPerspective(gl, 50.0, (double) viewport[2]
+ / (double) viewport[3], 1.0, 15.0, j8[jitter].x,
+ j8[jitter].y, 0.0, 0.0, 1.0);
+ displayObjects(gl);
+ gl.glAccum(GL2.GL_ACCUM, 1.0f / ACSIZE);
+ }
+ gl.glAccum(GL2.GL_RETURN, 1.0f);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(x, y, w, h);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+
+ }
+
+ /*
+ * accFrustum() The first 6 arguments are identical to the glFrustum() call.
+ * pixdx and pixdy are anti-alias jitter in pixels. Set both equal to 0.0
+ * for no anti-alias jitter. eyedx and eyedy are depth-of field jitter in
+ * pixels. Set both equal to 0.0 for no depth of field effects. focus is
+ * distance from eye to plane in focus. focus must be greater than, but not
+ * equal to 0.0. Note that accFrustum() calls glTranslatef(). You will
+ * probably want to insure that your ModelView matrix has been initialized
+ * to identity before calling accFrustum().
+ */
+ private void accFrustum(GL2 gl, double left, double right, double bottom,
+ double top, double near, double far, double pixdx, double pixdy,
+ double eyedx, double eyedy, double focus) {
+ double xwsize, ywsize;
+ double dx, dy;
+ int viewport[] = new int[4];
+
+ gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
+
+ xwsize = right - left;
+ ywsize = top - bottom;
+
+ dx = -(pixdx * xwsize / (double) viewport[2] + eyedx * near / focus);
+ dy = -(pixdy * ywsize / (double) viewport[3] + eyedy * near / focus);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glFrustum(left + dx, right + dx, bottom + dy, top + dy, near, far);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslatef((float) -eyedx, (float) -eyedy, 0.0f);
+ }
+
+ /*
+ * accPerspective() The first 4 arguments are identical to the
+ * gluPerspective() call. pixdx and pixdy are anti-alias jitter in pixels.
+ * Set both equal to 0.0 for no anti-alias jitter. eyedx and eyedy are
+ * depth-of field jitter in pixels. Set both equal to 0.0 for no depth of
+ * field effects. focus is distance from eye to plane in focus. focus must
+ * be greater than, but not equal to 0.0. Note that accPerspective() calls
+ * accFrustum().
+ */
+ void accPerspective(GL2 gl, double fovy, double aspect, double near,
+ double far, double pixdx, double pixdy, double eyedx, double eyedy,
+ double focus) {
+ double fov2, left, right, bottom, top;
+
+ fov2 = ((fovy * Math.PI) / 180.0) / 2.0;
+
+ top = near / (Math.cos(fov2) / Math.sin(fov2));
+ bottom = -top;
+
+ right = top * aspect;
+ left = -right;
+
+ accFrustum(gl, left, right, bottom, top, near, far, pixdx, pixdy,
+ eyedx, eyedy, focus);
+ }
+
+ private void displayObjects(GL2 gl) {
+ float torus_diffuse[] = new float[] { 0.7f, 0.7f, 0.0f, 1.0f };
+ float cube_diffuse[] = new float[] { 0.0f, 0.7f, 0.7f, 1.0f };
+ float sphere_diffuse[] = new float[] { 0.7f, 0.0f, 0.7f, 1.0f };
+ float octa_diffuse[] = new float[] { 0.7f, 0.4f, 0.4f, 1.0f };
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, 0.0f, -5.0f);
+ gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.80f, 0.35f, 0.0f);
+ gl.glRotatef(100.0f, 1.0f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, torus_diffuse, 0);
+ glut.glutSolidTorus(0.275, 0.85, 16, 16);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.75f, -0.50f, 0.0f);
+ gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
+ gl.glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, cube_diffuse, 0);
+ glut.glutSolidCube(1.5f);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.75f, 0.60f, 0.0f);
+ gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, sphere_diffuse, 0);
+ glut.glutSolidSphere(1.0, 16, 16);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.70f, -0.90f, 0.25f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, octa_diffuse, 0);
+ glut.glutSolidOctahedron();
+ gl.glPopMatrix();
+
+ gl.glPopMatrix();
+ }
+
+ public void keyTyped(KeyEvent key) {
+
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+ class jitter_point {
+ private static final int MAX_SAMPLES = 66;
+ float x, y;
+
+ public jitter_point(double x, double y) {
+ this.x = (float) x;
+ this.y = (float) y;
+ }
+ }
+ /* 2 jitter points */
+ jitter_point j2[] = { new jitter_point(0.246490f, 0.249999f),
+ new jitter_point(-0.246490f, -0.249999f) };
+
+ /* 3 jitter points */
+ jitter_point j3[] = { new jitter_point(-0.373411, -0.250550),//
+ new jitter_point(0.256263, 0.368119), //
+ new jitter_point(0.117148, -0.117570) };
+
+ /* 4 jitter points */
+ jitter_point j4[] = { new jitter_point(-0.208147, 0.353730),
+ new jitter_point(0.203849, -0.353780),
+ new jitter_point(-0.292626, -0.149945),
+ new jitter_point(0.296924, 0.149994) };
+
+ /* 8 jitter points */
+ jitter_point j8[] = { new jitter_point(-0.334818, 0.435331),
+ new jitter_point(0.286438, -0.393495),
+ new jitter_point(0.459462, 0.141540),
+ new jitter_point(-0.414498, -0.192829),
+ new jitter_point(-0.183790, 0.082102),
+ new jitter_point(-0.079263, -0.317383),
+ new jitter_point(0.102254, 0.299133),
+ new jitter_point(0.164216, -0.054399) };
+
+ /* 15 jitter points */
+ jitter_point j15[] = { new jitter_point(0.285561, 0.188437),
+ new jitter_point(0.360176, -0.065688),
+ new jitter_point(-0.111751, 0.275019),
+ new jitter_point(-0.055918, -0.215197),
+ new jitter_point(-0.080231, -0.470965),
+ new jitter_point(0.138721, 0.409168),
+ new jitter_point(0.384120, 0.458500),
+ new jitter_point(-0.454968, 0.134088),
+ new jitter_point(0.179271, -0.331196),
+ new jitter_point(-0.307049, -0.364927),
+ new jitter_point(0.105354, -0.010099),
+ new jitter_point(-0.154180, 0.021794),
+ new jitter_point(-0.370135, -0.116425),
+ new jitter_point(0.451636, -0.300013),
+ new jitter_point(-0.370610, 0.387504) };
+
+ /* 24 jitter points */
+ jitter_point j24[] = { new jitter_point(0.030245, 0.136384),
+ new jitter_point(0.018865, -0.348867),
+ new jitter_point(-0.350114, -0.472309),
+ new jitter_point(0.222181, 0.149524),
+ new jitter_point(-0.393670, -0.266873),
+ new jitter_point(0.404568, 0.230436),
+ new jitter_point(0.098381, 0.465337),
+ new jitter_point(0.462671, 0.442116),
+ new jitter_point(0.400373, -0.212720),
+ new jitter_point(-0.409988, 0.263345),
+ new jitter_point(-0.115878, -0.001981),
+ new jitter_point(0.348425, -0.009237),
+ new jitter_point(-0.464016, 0.066467),
+ new jitter_point(-0.138674, -0.468006),
+ new jitter_point(0.144932, -0.022780),
+ new jitter_point(-0.250195, 0.150161),
+ new jitter_point(-0.181400, -0.264219),
+ new jitter_point(0.196097, -0.234139),
+ new jitter_point(-0.311082, -0.078815),
+ new jitter_point(0.268379, 0.366778),
+ new jitter_point(-0.040601, 0.327109),
+ new jitter_point(-0.234392, 0.354659),
+ new jitter_point(-0.003102, -0.154402),
+ new jitter_point(0.297997, -0.417965) };
+
+ /* 66 jitter points */
+ jitter_point j66[] = { new jitter_point(0.266377, -0.218171),
+ new jitter_point(-0.170919, -0.429368),
+ new jitter_point(0.047356, -0.387135),
+ new jitter_point(-0.430063, 0.363413),
+ new jitter_point(-0.221638, -0.313768),
+ new jitter_point(0.124758, -0.197109),
+ new jitter_point(-0.400021, 0.482195),
+ new jitter_point(0.247882, 0.152010),
+ new jitter_point(-0.286709, -0.470214),
+ new jitter_point(-0.426790, 0.004977),
+ new jitter_point(-0.361249, -0.104549),
+ new jitter_point(-0.040643, 0.123453),
+ new jitter_point(-0.189296, 0.438963),
+ new jitter_point(-0.453521, -0.299889),
+ new jitter_point(0.408216, -0.457699),
+ new jitter_point(0.328973, -0.101914),
+ new jitter_point(-0.055540, -0.477952),
+ new jitter_point(0.194421, 0.453510), //
+ new jitter_point(0.404051, 0.224974), //
+ new jitter_point(0.310136, 0.419700),
+ new jitter_point(-0.021743, 0.403898),
+ new jitter_point(-0.466210, 0.248839),
+ new jitter_point(0.341369, 0.081490),
+ new jitter_point(0.124156, -0.016859),
+ new jitter_point(-0.461321, -0.176661),
+ new jitter_point(0.013210, 0.234401),
+ new jitter_point(0.174258, -0.311854),
+ new jitter_point(0.294061, 0.263364),
+ new jitter_point(-0.114836, 0.328189),
+ new jitter_point(0.041206, -0.106205),
+ new jitter_point(0.079227, 0.345021),
+ new jitter_point(-0.109319, -0.242380),
+ new jitter_point(0.425005, -0.332397),
+ new jitter_point(0.009146, 0.015098),
+ new jitter_point(-0.339084, -0.355707),
+ new jitter_point(-0.224596, -0.189548),
+ new jitter_point(0.083475, 0.117028),
+ new jitter_point(0.295962, -0.334699),
+ new jitter_point(0.452998, 0.025397),
+ new jitter_point(0.206511, -0.104668),
+ new jitter_point(0.447544, -0.096004),
+ new jitter_point(-0.108006, -0.002471),
+ new jitter_point(-0.380810, 0.130036),
+ new jitter_point(-0.242440, 0.186934),
+ new jitter_point(-0.200363, 0.070863),
+ new jitter_point(-0.344844, -0.230814),
+ new jitter_point(0.408660, 0.345826),
+ new jitter_point(-0.233016, 0.305203),
+ new jitter_point(0.158475, -0.430762),
+ new jitter_point(0.486972, 0.139163),
+ new jitter_point(-0.301610, 0.009319),
+ new jitter_point(0.282245, -0.458671),
+ new jitter_point(0.482046, 0.443890),
+ new jitter_point(-0.121527, 0.210223),
+ new jitter_point(-0.477606, -0.424878),
+ new jitter_point(-0.083941, -0.121440),
+ new jitter_point(-0.345773, 0.253779),
+ new jitter_point(0.234646, 0.034549),
+ new jitter_point(0.394102, -0.210901),
+ new jitter_point(-0.312571, 0.397656),
+ new jitter_point(0.200906, 0.333293),
+ new jitter_point(0.018703, -0.261792),
+ new jitter_point(-0.209349, -0.065383),
+ new jitter_point(0.076248, 0.478538),
+ new jitter_point(-0.073036, -0.355064),
+ new jitter_point(0.145087, 0.221726) };
+}
+/*
+ * For the software in this directory
+ * (c) Copyright 1993, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED
+ * Permission to use, copy, modify, and distribute this software for
+ * any purpose and without fee is hereby granted, provided that the above
+ * copyright notice appear in all copies and that both the copyright notice
+ * and this permission notice appear in supporting documentation, and that
+ * the name of Silicon Graphics, Inc. not be used in advertising
+ * or publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
+ * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
+ * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
+ * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
+ * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
+ * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
+ * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
+ * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights
+ * Use, duplication, or disclosure by the Government is subject to
+ * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
+ * (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor
+ * clauses in the FAR or the DOD or NASA FAR Supplement.
+ * Unpublished-- rights reserved under the copyright laws of the
+ * United States. Contractor/manufacturer is Silicon Graphics,
+ * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
\ No newline at end of file diff --git a/src/redbook/src/glredbook10/accum.java b/src/redbook/src/glredbook10/accum.java new file mode 100644 index 0000000..fa335b6 --- /dev/null +++ b/src/redbook/src/glredbook10/accum.java @@ -0,0 +1,277 @@ +package glredbook10;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.gl2.GLUT;
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * Do a sixteen pass
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class accum//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ private GLUT glut;
+ private static final int ACSIZE = 16;
+ private int width, height;
+ private float dx[] = new float[ACSIZE];
+ private float dy[] = new float[ACSIZE];
+ private float jitter3[][] = new float[][] { { 0.5f, 0.5f },
+ { 1.35899e-05f, 0.230369f }, { 0.000189185f, 0.766878f }, };
+ private float jitter16[][] = new float[][] { { 0.4375f, 0.4375f },
+ { 0.1875f, 0.5625f }, { 0.9375f, 1.1875f },
+ { 0.4375f, 0.9375f - 1 }, { 0.6875f, 0.5625f },
+ { 0.1875f, 0.0625f }, { 0.6875f, 0.3125f }, { 0.1875f, 0.3125f },
+ { 0.4375f, 0.1875f }, { 0.9375f - 1, 0.4375f },
+ { 0.6875f, 0.8125f }, { 0.4375f, 0.6875f }, { 0.6875f, 0.0625f },
+ { 0.9375f, 0.9375f }, { 1.1875f, 0.8125f }, { 0.9375f, 0.6875f }, };
+
+ private float jitter29[][] = new float[][] { { 0.5f, 0.5f },
+ { 0.498126f, 0.141363f }, { 0.217276f, 0.651732f },
+ { 0.439503f, 0.954859f }, { 0.734171f, 0.836294f },
+ { 0.912454f, 0.79952f }, { 0.406153f, 0.671156f },
+ { 0.0163892f, 0.631994f }, { 0.298064f, 0.843476f },
+ { 0.312025f, 0.0990405f }, { 0.98135f, 0.965697f },
+ { 0.841999f, 0.272378f }, { 0.559348f, 0.32727f },
+ { 0.809331f, 0.638901f }, { 0.632583f, 0.994471f },
+ { 0.00588314f, 0.146344f }, { 0.713365f, 0.437896f },
+ { 0.185173f, 0.246584f }, { 0.901735f, 0.474544f },
+ { 0.366423f, 0.296698f }, { 0.687032f, 0.188184f },
+ { 0.313256f, 0.472999f }, { 0.543195f, 0.800044f },
+ { 0.629329f, 0.631599f }, { 0.818263f, 0.0439354f },
+ { 0.163978f, 0.00621497f }, { 0.109533f, 0.812811f },
+ { 0.131325f, 0.471624f }, { 0.0196755f, 0.331813f }, };
+
+ private float jitter90[][] = new float[][] { { 0.5f, 0.5f },
+ { 0.784289f, 0.417355f }, { 0.608691f, 0.678948f },
+ { 0.546538f, 0.976002f }, { 0.972245f, 0.270498f },
+ { 0.765121f, 0.189392f }, { 0.513193f, 0.743827f },
+ { 0.123709f, 0.874866f }, { 0.991334f, 0.745136f },
+ { 0.56342f, 0.0925047f }, { 0.662226f, 0.143317f },
+ { 0.444563f, 0.928535f }, { 0.248017f, 0.981655f },
+ { 0.100115f, 0.771923f }, { 0.593937f, 0.559383f },
+ { 0.392095f, 0.225932f }, { 0.428776f, 0.812094f },
+ { 0.510615f, 0.633584f }, { 0.836431f, 0.00343328f },
+ { 0.494037f, 0.391771f }, { 0.617448f, 0.792324f },
+ { 0.688599f, 0.48914f }, { 0.530421f, 0.859206f },
+ { 0.0742278f, 0.665344f }, { 0.979388f, 0.626835f },
+ { 0.183806f, 0.479216f }, { 0.151222f, 0.0803998f },
+ { 0.476489f, 0.157863f }, { 0.792675f, 0.653531f },
+ { 0.0990416f, 0.267284f }, { 0.776667f, 0.303894f },
+ { 0.312904f, 0.296018f }, { 0.288777f, 0.691008f },
+ { 0.460097f, 0.0436075f }, { 0.594323f, 0.440751f },
+ { 0.876296f, 0.472043f }, { 0.0442623f, 0.0693901f },
+ { 0.355476f, 0.00442787f }, { 0.391763f, 0.361327f },
+ { 0.406994f, 0.696053f }, { 0.708393f, 0.724992f },
+ { 0.925807f, 0.933103f }, { 0.850618f, 0.11774f },
+ { 0.867486f, 0.233677f }, { 0.208805f, 0.285484f },
+ { 0.572129f, 0.211505f }, { 0.172931f, 0.180455f },
+ { 0.327574f, 0.598031f }, { 0.685187f, 0.372379f },
+ { 0.23375f, 0.878555f }, { 0.960657f, 0.409561f },
+ { 0.371005f, 0.113866f }, { 0.29471f, 0.496941f },
+ { 0.748611f, 0.0735321f }, { 0.878643f, 0.34504f },
+ { 0.210987f, 0.778228f }, { 0.692961f, 0.606194f },
+ { 0.82152f, 0.8893f }, { 0.0982095f, 0.563104f },
+ { 0.214514f, 0.581197f }, { 0.734262f, 0.956545f },
+ { 0.881377f, 0.583548f }, { 0.0560485f, 0.174277f },
+ { 0.0729515f, 0.458003f }, { 0.719604f, 0.840564f },
+ { 0.325388f, 0.7883f }, { 0.26136f, 0.0848927f },
+ { 0.393754f, 0.467505f }, { 0.425361f, 0.577672f },
+ { 0.648594f, 0.0248658f }, { 0.983843f, 0.521048f },
+ { 0.272936f, 0.395127f }, { 0.177695f, 0.675733f },
+ { 0.89175f, 0.700901f }, { 0.632301f, 0.908259f },
+ { 0.782859f, 0.53611f }, { 0.0141421f, 0.855548f },
+ { 0.0437116f, 0.351866f }, { 0.939604f, 0.0450863f },
+ { 0.0320883f, 0.962943f }, { 0.341155f, 0.895317f },
+ { 0.952087f, 0.158387f }, { 0.908415f, 0.820054f },
+ { 0.481435f, 0.281195f }, { 0.675525f, 0.25699f },
+ { 0.585273f, 0.324454f }, { 0.156488f, 0.376783f },
+ { 0.140434f, 0.977416f }, { 0.808155f, 0.77305f },
+ { 0.282973f, 0.188937f }, };
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ // we absolutely need accumulation buffer for this
+ caps.setAccumBlueBits(16);
+ caps.setAccumGreenBits(16);
+ caps.setAccumRedBits(16);
+ caps.setAccumAlphaBits(16);
+ //
+ GLJPanel canvas = new GLJPanel(caps);
+ canvas.addGLEventListener(this);
+ canvas.addKeyListener(this);
+ return canvas;
+ }
+
+ public static void main(String[] args) {
+ accum demo = new accum();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("accum");
+ frame.setSize(512, 512);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float ambient[] = { 0.4f, 0.4f, 0.4f, 1.0f };
+ float diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float position[] = { 0.0f, 2.0f, 2.0f, 0.0f };
+ float mat_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+ float mat_diffuse[] = { 0.7f, 0.7f, 0.7f, 1.0f };
+ float mat_specular[] = { 0.9f, 0.9f, 0.9f, 1.0f };
+ float mat_shininess[] = { 50.0f };
+ float lmodel_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, specular, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);
+
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+
+ gl.glEnable(GL.GL_CULL_FACE);
+ gl.glCullFace(GL.GL_FRONT);
+
+ gl.glClearAccum(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL2.GL_ACCUM_BUFFER_BIT);
+ loaddxdy();
+ for (int i = 0; i < (ACSIZE); i++) {
+ System.out.println("Pass " + i);
+ gl.glPushMatrix();
+ gl.glTranslatef(dx[i], dy[i], 0.0f);
+ gl.glRotatef(45.0f, 1.0f, 1.0f, 1.0f);
+ gl.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT);
+ glut.glutSolidTeapot(1.0f);
+ gl.glPopMatrix();
+ gl.glAccum(GL2.GL_ACCUM, 1.0f / (ACSIZE));
+ }
+ System.out.println("final job");
+ gl.glAccum(GL2.GL_RETURN, 1.0f);
+ System.out.println("done");
+ gl.glFlush();
+ drawable.swapBuffers();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ width = w;
+ height = h;
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-3.0, 3.0, -3.0 * (float) h / (float) w, 3.0 * (float) h
+ / (float) w, -15.0, 15.0);
+ else
+ gl.glOrtho(-3.0 * (float) w / (float) h, 3.0 * (float) w
+ / (float) h, -3.0, 3.0, -15.0, 15.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void loaddxdy() {
+ int i;
+ for (i = 0; i < ACSIZE; i++) {
+ dx[i] = jitter16[i][0] * 10 / width;
+ dy[i] = jitter16[i][1] * 10 / height;
+ }
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
+/*
+ * For the software in this directory
+ * (c) Copyright 1993, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED
+ * Permission to use, copy, modify, and distribute this software for
+ * any purpose and without fee is hereby granted, provided that the above
+ * copyright notice appear in all copies and that both the copyright notice
+ * and this permission notice appear in supporting documentation, and that
+ * the name of Silicon Graphics, Inc. not be used in advertising
+ * or publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
+ * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
+ * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
+ * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
+ * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
+ * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
+ * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
+ * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights
+ * Use, duplication, or disclosure by the Government is subject to
+ * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
+ * (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor
+ * clauses in the FAR or the DOD or NASA FAR Supplement.
+ * Unpublished-- rights reserved under the copyright laws of the
+ * United States. Contractor/manufacturer is Silicon Graphics,
+ * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
\ No newline at end of file diff --git a/src/redbook/src/glredbook10/aim.java b/src/redbook/src/glredbook10/aim.java new file mode 100644 index 0000000..9bed4bc --- /dev/null +++ b/src/redbook/src/glredbook10/aim.java @@ -0,0 +1,167 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program calculates the fovy (field of view angle in the y direction), by
+ * using trigonometry, given the size of an object and its size.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class aim//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ GLJPanel canvas = new GLJPanel(caps);
+ canvas.addGLEventListener(this);
+ canvas.addKeyListener(this);
+ return canvas;
+ }
+
+ public static void main(String[] args) {
+ aim demo = new aim();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("aim");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ /*
+ * Clear the screen. Set the current color to white. Draw the wire frame
+ * cube and sphere.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+
+ gl.glLoadIdentity();
+ /* glTranslatef() as viewing transformation */
+ gl.glTranslatef(0.0f, 0.0f, -5.0f);
+ glut.glutWireCube(2.0f);
+ glut.glutWireSphere(1.0f, 10, 10);
+ gl.glFlush();
+ }
+
+ /*
+ * Called when the window is first opened and whenever the window is
+ * reconfigured (moved or resized).
+ */
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ double theta;
+
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ theta = calculateAngle(2.0, 5.0);
+ glu.gluPerspective(theta, (float) w / (float) h, 1.0, 20.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * atan2 () is a system math routine which calculates the arctangent of an
+ * angle, given length of the opposite and adjacent sides of a right
+ * triangle. atan2 () is not an OpenGL routine.
+ */
+ private double calculateAngle(double size, double distance) {
+ double radtheta, degtheta;
+
+ radtheta = 2.0 * Math.atan2(size / 2.0, distance);
+ degtheta = (180.0 * radtheta) / Math.PI;
+ System.out.println("degtheta is " + degtheta);
+ return ((double) degtheta);
+ }
+
+ public void keyTyped(KeyEvent key) {
+ // TODO Auto-generated method stub
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
+/*
+ * For the software in this directory
+ * (c) Copyright 1993, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED
+ * Permission to use, copy, modify, and distribute this software for
+ * any purpose and without fee is hereby granted, provided that the above
+ * copyright notice appear in all copies and that both the copyright notice
+ * and this permission notice appear in supporting documentation, and that
+ * the name of Silicon Graphics, Inc. not be used in advertising
+ * or publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
+ * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
+ * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
+ * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
+ * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
+ * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
+ * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
+ * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights
+ * Use, duplication, or disclosure by the Government is subject to
+ * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
+ * (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor
+ * clauses in the FAR or the DOD or NASA FAR Supplement.
+ * Unpublished-- rights reserved under the copyright laws of the
+ * United States. Contractor/manufacturer is Silicon Graphics,
+ * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
\ No newline at end of file diff --git a/src/redbook/src/glredbook10/alpha.java b/src/redbook/src/glredbook10/alpha.java new file mode 100644 index 0000000..a207bdc --- /dev/null +++ b/src/redbook/src/glredbook10/alpha.java @@ -0,0 +1,155 @@ +package glredbook10;
+
+import javax.swing.*;
+
+import java.awt.event.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLCanvas;
+import javax.media.opengl.glu.GLU;
+
+
+/**
+ * This program draws several overlapping filled polygons to demonstrate the
+ * effect order has on alpha blending results. Use the 't' key to toggle the
+ * order of drawing polygons.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class alpha//
+ extends GLSkeleton<GLCanvas>
+ implements GLEventListener, KeyListener {
+
+ @Override
+ protected GLCanvas createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLCanvas canvas = new GLCanvas(caps);
+ canvas.addGLEventListener(this);
+ canvas.addKeyListener(this);
+ return canvas;
+ }
+
+ /*
+ * Main Loop Open window with initial window size, title bar, RGBA display
+ * mode, and handle input events.
+ */
+ public static void main(String[] args) {
+ alpha demo = new alpha();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("alpha");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize alpha blending function.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glEnable(GL.GL_BLEND);
+ gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glClearColor(0, 0, 0, 0);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor4f(1.0f, 1.0f, 0.0f, 0.75f);
+ gl.glRectf(0.0f, 0.0f, 0.5f, 1.0f);
+
+ gl.glColor4f(0.0f, 1.0f, 1.0f, 0.75f);
+ gl.glRectf(0.0f, 0.0f, 1.f, 0.5f);
+ /* draw colored polygons in reverse order in upper right */
+ gl.glColor4f(0.f, 1.0f, 1.0f, 0.75f);
+ gl.glRectf(0.5f, 0.5f, 1.0f, 1.0f);
+
+ gl.glColor4f(1.0f, 1.0f, 0.0f, 0.75f);
+ gl.glRectf(0.5f, 0.5f, 1.0f, 1.0f);
+
+ gl.glEnd();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ GLU glu = new GLU();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ glu.gluOrtho2D(0.0, 1.0, 0.0, 1.0 * h / w);
+ else
+ glu.gluOrtho2D(0.0, 1.0 * w / h, 0.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}//
+/*
+ * For the software in this directory
+ * (c) Copyright 1993, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED
+ * Permission to use, copy, modify, and distribute this software for
+ * any purpose and without fee is hereby granted, provided that the above
+ * copyright notice appear in all copies and that both the copyright notice
+ * and this permission notice appear in supporting documentation, and that
+ * the name of Silicon Graphics, Inc. not be used in advertising
+ * or publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
+ * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
+ * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
+ * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
+ * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
+ * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
+ * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
+ * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights
+ * Use, duplication, or disclosure by the Government is subject to
+ * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
+ * (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor
+ * clauses in the FAR or the DOD or NASA FAR Supplement.
+ * Unpublished-- rights reserved under the copyright laws of the
+ * United States. Contractor/manufacturer is Silicon Graphics,
+ * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
\ No newline at end of file diff --git a/src/redbook/src/glredbook10/alpha3D.java b/src/redbook/src/glredbook10/alpha3D.java new file mode 100644 index 0000000..9225e23 --- /dev/null +++ b/src/redbook/src/glredbook10/alpha3D.java @@ -0,0 +1,199 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+
+import javax.swing.*;
+
+import javax.media.opengl.*; import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+
+/**
+ * This program demonstrates how to intermix opaque and alpha blended polygons
+ * in the same scene, by using glDepthMask. Pressing the left mouse button
+ * toggles the eye position.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class alpha3D//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ private GLUT glut;
+
+ private boolean eyePosition = false;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel canvas = new GLJPanel(caps);
+ canvas.addGLEventListener(this);
+ canvas.addKeyListener(this);
+ canvas.addMouseListener(this);
+ return canvas;
+ }
+
+ public static void main(String[] args) {
+
+ alpha3D demo = new alpha3D();
+
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("alpha3D");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ float mat_ambient[] = { 0.0f, 0.0f, 0.0f, 0.15f };
+ float mat_specular[] = { 1.0f, 1.0f, 1.0f, 0.15f };
+ float mat_shininess[] = { 15.0f };
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float position[] = { 0.0f, 0.0f, 1.0f, 1.0f };
+ float mat_torus[] = { 0.75f, 0.75f, 0.0f, 1.0f };
+ float mat_cylinder[] = { 0.0f, 0.75f, 0.75f, 0.15f };
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
+ gl.glPushMatrix();
+ if (eyePosition)
+ glu.gluLookAt(0.0, 0.0, 9.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
+ else
+ glu.gluLookAt(0.0, 0.0, -9.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, 0.0f, 1.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_torus, 0);
+ glut.glutSolidTorus(0.275, 0.85, 10, 10);
+ gl.glPopMatrix();
+
+ gl.glEnable(GL.GL_BLEND);
+ gl.glDepthMask(false);
+ gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL.GL_ONE);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_cylinder, 0);
+ gl.glTranslatef(0.0f, 0.0f, -1.0f);
+ glut.glutSolidCube(2.0f);// (1.0, 2.0);
+ gl.glDepthMask(true);
+ gl.glDisable(GL.GL_BLEND);
+ gl.glPopMatrix();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(30.0, (float) w / (float) h, 1.0, 20.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent mouse) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ if (mouse.getButton() == MouseEvent.BUTTON1) //
+ eyePosition = !eyePosition;
+
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
+/*
+ * For the software in this directory
+ * (c) Copyright 1993, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED
+ * Permission to use, copy, modify, and distribute this software for
+ * any purpose and without fee is hereby granted, provided that the above
+ * copyright notice appear in all copies and that both the copyright notice
+ * and this permission notice appear in supporting documentation, and that
+ * the name of Silicon Graphics, Inc. not be used in advertising
+ * or publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
+ * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
+ * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
+ * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
+ * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
+ * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
+ * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
+ * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights
+ * Use, duplication, or disclosure by the Government is subject to
+ * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
+ * (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor
+ * clauses in the FAR or the DOD or NASA FAR Supplement.
+ * Unpublished-- rights reserved under the copyright laws of the
+ * United States. Contractor/manufacturer is Silicon Graphics,
+ * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
\ No newline at end of file diff --git a/src/redbook/src/glredbook10/anti.java b/src/redbook/src/glredbook10/anti.java new file mode 100644 index 0000000..dda87e5 --- /dev/null +++ b/src/redbook/src/glredbook10/anti.java @@ -0,0 +1,165 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program draws antialiased lines in RGBA mode.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class anti//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ anti demo = new anti();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("anti");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize antialiasing for RGBA mode, including alpha blending, hint,
+ * and line width. Print out implementation specific info on line width
+ * granularity and width.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ float values[] = new float[2];
+ gl.glGetFloatv(GL2.GL_LINE_WIDTH_GRANULARITY, values, 0);
+ System.out
+ .println("GL.GL_LINE_WIDTH_GRANULARITY value is " + values[0]);
+
+ gl.glGetFloatv(GL2.GL_LINE_WIDTH_RANGE, values, 0);
+ System.out.println("GL.GL_LINE_WIDTH_RANGE values are " //
+ + values[0] + ", " + values[1]);
+
+ gl.glEnable(GL2.GL_LINE_SMOOTH);
+ gl.glEnable(GL.GL_BLEND);
+ gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
+ gl.glHint(GL2.GL_LINE_SMOOTH_HINT, GL.GL_DONT_CARE);
+ gl.glLineWidth(1.5f);
+
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ }
+
+ /*
+ * display() draws an icosahedron with a large alpha value, 1.0.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
+ glut.glutWireIcosahedron();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(45.0, (float) w / (float) h, 3.0, 5.0);
+
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslatef(0.0f, 0.0f, -4.0f); /* move object into view */
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
+/*
+ * For the software in this directory
+ * (c) Copyright 1993, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED
+ * Permission to use, copy, modify, and distribute this software for
+ * any purpose and without fee is hereby granted, provided that the above
+ * copyright notice appear in all copies and that both the copyright notice
+ * and this permission notice appear in supporting documentation, and that
+ * the name of Silicon Graphics, Inc. not be used in advertising
+ * or publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
+ * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
+ * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
+ * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
+ * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
+ * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
+ * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
+ * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights
+ * Use, duplication, or disclosure by the Government is subject to
+ * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
+ * (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor
+ * clauses in the FAR or the DOD or NASA FAR Supplement.
+ * Unpublished-- rights reserved under the copyright laws of the
+ * United States. Contractor/manufacturer is Silicon Graphics,
+ * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
\ No newline at end of file diff --git a/src/redbook/src/glredbook10/antipoint.java b/src/redbook/src/glredbook10/antipoint.java new file mode 100644 index 0000000..b53b608 --- /dev/null +++ b/src/redbook/src/glredbook10/antipoint.java @@ -0,0 +1,150 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * The program draws antialiased points, in RGBA mode.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class antipoint//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ antipoint demo = new antipoint();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("antipoint");
+ frame.setSize(100, 100);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize point anti-aliasing for RGBA mode, including alpha blending,
+ * hint, and point size. These points are 3.0 pixels big.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glEnable(GL2.GL_POINT_SMOOTH);
+ gl.glEnable(GL.GL_BLEND);
+ gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
+ gl.glHint(GL2.GL_POINT_SMOOTH_HINT, GL.GL_DONT_CARE);
+ gl.glPointSize(3.0f);
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ /*
+ * display() draws several points.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ int i;
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
+ gl.glBegin(GL.GL_POINTS);
+ for (i = 1; i < 10; i++) {
+ gl.glVertex2f((float) i * 10.0f, (float) i * 10.0f);
+ }
+ gl.glEnd();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w < h)
+ gl.glOrtho(0.0, 100.0, 0.0, 100.0 * (float) h / (float) w, -1.0,
+ 1.0);
+ else
+ gl.glOrtho(0.0, 100.0 * (float) w / (float) h, 0.0, 100.0, -1.0,
+ 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
+/*
+ * For the software in this directory (c) Copyright 1993, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED Permission to use, copy, modify, and distribute this
+ * software for any purpose and without fee is hereby granted, provided that the
+ * above copyright notice appear in all copies and that both the copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of Silicon Graphics, Inc. not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND WITHOUT
+ * WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT
+ * LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
+ * PURPOSE. IN NO EVENT SHALL SILICON GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE
+ * ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES
+ * OF ANY KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF
+ * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES,
+ * WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF THE POSSIBILITY OF
+ * SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR
+ * IN CONNECTION WITH THE POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights Use, duplication, or disclosure by the
+ * Government is subject to restrictions set forth in FAR 52.227.19(c)(2) or
+ * subparagraph (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor clauses in the
+ * FAR or the DOD or NASA FAR Supplement. Unpublished-- rights reserved under
+ * the copyright laws of the United States. Contractor/manufacturer is Silicon
+ * Graphics, Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
+ */
\ No newline at end of file diff --git a/src/redbook/src/glredbook10/antipoly.java b/src/redbook/src/glredbook10/antipoly.java new file mode 100644 index 0000000..d015a94 --- /dev/null +++ b/src/redbook/src/glredbook10/antipoly.java @@ -0,0 +1,187 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program draws filled polygons with antialiased edges. The special
+ * GL_SRC_ALPHA_SATURATE blending function is used. Pressing the left mouse
+ * button turns the antialiasing on and off.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class antipoly //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ private GLUT glut;
+ private boolean polySmooth;
+
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ antipoly demo = new antipoly();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("antipoly");
+ frame.setSize(200, 200);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ float mat_ambient[] = { 0.0f, 0.0f, 0.0f, 1.00f };
+ float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.00f };
+ float mat_shininess[] = { 15.0f };
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL.GL_BLEND);
+ gl.glCullFace(GL.GL_BACK);
+ gl.glEnable(GL.GL_CULL_FACE);
+ gl.glEnable(GL2.GL_POLYGON_SMOOTH);
+ polySmooth = true;
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ /*
+ * Note: polygons must be drawn from back to front for proper blending.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float position[] = { 0.0f, 0.0f, 1.0f, 0.0f };
+ float mat_cube1[] = { 0.75f, 0.75f, 0.0f, 1.0f };
+ float mat_cube2[] = { 0.0f, 0.75f, 0.75f, 1.0f };
+
+ if (polySmooth)
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ else
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ toggleSmooth(gl);
+
+ gl.glPushMatrix();
+
+ gl.glTranslatef(0.0f, 0.0f, -8.0f);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
+
+ gl.glBlendFunc(GL2.GL_SRC_ALPHA_SATURATE, GL.GL_ONE);
+
+ gl.glPushMatrix();
+ gl.glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
+ gl.glRotatef(60.0f, 0.0f, 1.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_cube1, 0);
+ glut.glutSolidCube(1.0f);
+ gl.glPopMatrix();
+
+ gl.glTranslatef(0.0f, 0.0f, -2.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_cube2, 0);
+ gl.glRotatef(30.0f, 0.0f, 1.0f, 0.0f);
+ gl.glRotatef(60.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutSolidCube(1.0f);
+ gl.glPopMatrix();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(30.0, (float) w / (float) h, 1.0, 20.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void toggleSmooth(GL2 gl) {
+ if (polySmooth) {
+ gl.glDisable(GL.GL_BLEND);
+ gl.glDisable(GL2.GL_POLYGON_SMOOTH);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ } else {
+ gl.glEnable(GL.GL_BLEND);
+ gl.glEnable(GL2.GL_POLYGON_SMOOTH);
+ gl.glDisable(GL.GL_DEPTH_TEST);
+ }
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent mouse) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ if (mouse.getButton() == MouseEvent.BUTTON1) {
+ polySmooth = !polySmooth;
+ System.out.println(polySmooth);
+ }
+
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/bezcurve.java b/src/redbook/src/glredbook10/bezcurve.java new file mode 100644 index 0000000..bde4503 --- /dev/null +++ b/src/redbook/src/glredbook10/bezcurve.java @@ -0,0 +1,135 @@ +package glredbook10;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.FloatBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program uses evaluators to draw a Bezier curve.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class bezcurve//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private float ctrlpoints[][] = new float[][] { { -4.0f, -4.0f, 0.0f },
+ { -2.0f, 4.0f, 0.0f }, { 2.0f, -4.0f, 0.0f }, { 4.0f, 4.0f, 0.0f } };
+ private FloatBuffer ctrlpointBuf;
+
+ // = BufferUtil.newFloatBuffer(ctrlpoints[0].length * ctrlpoints.length);
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setNumSamples(2);
+ caps.setSampleBuffers(true);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ bezcurve demo = new bezcurve();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("bezcurve");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ // need to convert 2d array to buffer type
+ ctrlpointBuf = BufferUtil.newFloatBuffer(ctrlpoints[0].length
+ * ctrlpoints.length);
+ for (int i = 0; i < ctrlpoints.length; i++) {
+ ctrlpointBuf.put(ctrlpoints[i]);
+ }
+ ctrlpointBuf.rewind();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glMap1f(GL2.GL_MAP1_VERTEX_3, 0.0f, 1.0f, 3, 4, ctrlpointBuf);
+ gl.glEnable(GL2.GL_MAP1_VERTEX_3);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glBegin(GL.GL_LINE_STRIP);
+ for (int i = 0; i <= 30; i++) {
+ gl.glEvalCoord1f((float) i / (float) 30.0);
+ }
+ gl.glEnd();
+ /* The following code displays the control points as dots. */
+ gl.glPointSize(5.0f);
+ gl.glColor3f(1.0f, 1.0f, 0.0f);
+ gl.glBegin(GL.GL_POINTS);
+ for (int i = 0; i < 4; i++) {
+ gl.glVertex3fv(ctrlpointBuf);
+ ctrlpointBuf.position(i * 3);
+ }
+ gl.glEnd();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h) //
+ gl.glOrtho(-5.0, 5.0, -5.0 * (float) h / (float) w, //
+ 5.0 * (float) h / (float) w, -5.0, 5.0);
+ else
+ gl.glOrtho(-5.0 * (float) w / (float) h, //
+ 5.0 * (float) w / (float) h,//
+ -5.0, 5.0, -5.0, 5.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/bezmesh.java b/src/redbook/src/glredbook10/bezmesh.java new file mode 100644 index 0000000..01029bb --- /dev/null +++ b/src/redbook/src/glredbook10/bezmesh.java @@ -0,0 +1,149 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import java.nio.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program renders a wireframe (mesh) Bezier surface, using two-dimensional
+ * evaluators.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class bezmesh//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ // as from C version of file
+ private static final float ctrlpoints[][][] = new float[][][] {
+ { { -1.5f, -1.5f, 4.0f }, { -0.5f, -1.5f, 2.0f },
+ { 0.5f, -1.5f, -1.0f }, { 1.5f, -1.5f, 2.0f } },
+ { { -1.5f, -0.5f, 1.0f }, { -0.5f, -0.5f, 3.0f },
+ { 0.5f, -0.5f, 0.0f }, { 1.5f, -0.5f, -1.0f } },
+ { { -1.5f, 0.5f, 4.0f }, { -0.5f, 0.5f, 0.0f },
+ { 0.5f, 0.5f, 3.0f }, { 1.5f, 0.5f, 4.0f } },
+ { { -1.5f, 1.5f, -2.0f }, { -0.5f, 1.5f, -2.0f },
+ { 0.5f, 1.5f, 0.0f }, { 1.5f, 1.5f, -1.0f } } };
+ // need float buffer instead of n-dimensional array above
+ private FloatBuffer ctrlpointsBuf = BufferUtil
+ .newFloatBuffer(ctrlpoints.length * ctrlpoints[0].length
+ * ctrlpoints[0][0].length);
+ {// SO copy 4x4x3 array above to float buffer
+ for (int i = 0; i < ctrlpoints.length; i++) {
+ // System.out.print(ctrlpoints.length+ " ");
+ for (int j = 0; j < ctrlpoints[0].length; j++) {
+ // System.out.println(ctrlpoints[0][0].length+" ");
+ for (int k = 0; k < ctrlpoints[0][0].length; k++) {
+ ctrlpointsBuf.put(ctrlpoints[i][j][k]);
+ System.out.print(ctrlpoints[i][j][k] + " ");
+ }
+ System.out.println();
+ }
+ }
+ // THEN rewind it before use
+ ctrlpointsBuf.rewind();
+ }
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ bezmesh demo = new bezmesh();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("bezmesh");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+ gl.glMap2f(GL2.GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, ctrlpointsBuf);
+ gl.glEnable(GL2.GL_MAP2_VERTEX_3);
+ gl.glMapGrid2f(20, 0.0f, 1.0f, 20, 0.0f, 1.0f);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glPushMatrix();
+ gl.glRotatef(85.0f, 1.0f, 1.0f, 1.0f);
+ for (int j = 0; j <= 8; j++) {
+ gl.glBegin(GL.GL_LINE_STRIP);
+ for (int i = 0; i <= 30; i++)
+ gl.glEvalCoord2f((float) i / 30.0f, (float) j / 8.0f);
+ gl.glEnd();
+ gl.glBegin(GL.GL_LINE_STRIP);
+ for (int i = 0; i <= 30; i++)
+ gl.glEvalCoord2f((float) j / 8.0f, (float) i / 30.0f);
+ gl.glEnd();
+ }
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-4.0, 4.0, -4.0 * (float) h / (float) w, 4.0 * (float) h
+ / (float) w, -4.0, 4.0);
+ else
+ gl.glOrtho(-4.0 * (float) w / (float) h, 4.0 * (float) w
+ / (float) h, -4.0, 4.0, -4.0, 4.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/bezsurf.java b/src/redbook/src/glredbook10/bezsurf.java new file mode 100644 index 0000000..a223ef8 --- /dev/null +++ b/src/redbook/src/glredbook10/bezsurf.java @@ -0,0 +1,160 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import java.nio.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program renders a wireframe (mesh) Bezier surface, using two-dimensional
+ * evaluators.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class bezsurf//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ // as from C version of file
+ private static final float ctrlpoints[][][] = new float[][][] {
+ { { -1.5f, -1.5f, 4.0f }, { -0.5f, -1.5f, 2.0f },
+ { 0.5f, -1.5f, -1.0f }, { 1.5f, -1.5f, 2.0f } },
+ { { -1.5f, -0.5f, 1.0f }, { -0.5f, -0.5f, 3.0f },
+ { 0.5f, -0.5f, 0.0f }, { 1.5f, -0.5f, -1.0f } },
+ { { -1.5f, 0.5f, 4.0f }, { -0.5f, 0.5f, 0.0f },
+ { 0.5f, 0.5f, 3.0f }, { 1.5f, 0.5f, 4.0f } },
+ { { -1.5f, 1.5f, -2.0f }, { -0.5f, 1.5f, -2.0f },
+ { 0.5f, 1.5f, 0.0f }, { 1.5f, 1.5f, -1.0f } } };
+ // need float buffer instead of n-dimensional array above
+ private FloatBuffer ctrlpointsBuf = BufferUtil
+ .newFloatBuffer(ctrlpoints.length * ctrlpoints[0].length
+ * ctrlpoints[0][0].length);
+ {// SO copy 4x4x3 array above to float buffer
+ for (int i = 0; i < ctrlpoints.length; i++) {
+ // System.out.print(ctrlpoints.length+ " ");
+ for (int j = 0; j < ctrlpoints[0].length; j++) {
+ // System.out.println(ctrlpoints[0][0].length+" ");
+ for (int k = 0; k < ctrlpoints[0][0].length; k++) {
+ ctrlpointsBuf.put(ctrlpoints[i][j][k]);
+ System.out.print(ctrlpoints[i][j][k] + " ");
+ }
+ System.out.println();
+ }
+ }
+ // THEN rewind it before use
+ ctrlpointsBuf.rewind();
+ }
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+ public static void main(String[] args) {
+
+ bezsurf demo = new bezsurf();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("bezsurf");
+ frame.setSize(512, 256);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ gl.glEnable(GL2.GL_MAP2_VERTEX_3);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+ gl.glMap2f(GL2.GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, ctrlpointsBuf);
+ gl.glMapGrid2f(20, 0.0f, 1.0f, 20, 0.0f, 1.0f);
+
+ initlights(gl);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glPushMatrix();
+ gl.glRotatef(85.0f, 1.0f, 1.0f, 1.0f);
+ gl.glEvalMesh2(GL2.GL_FILL, 0, 20, 0, 20);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-4.0, 4.0, -4.0 * (float) h / (float) w, 4.0 * (float) h
+ / (float) w, -4.0, 4.0);
+ else
+ gl.glOrtho(-4.0 * (float) w / (float) h, 4.0 * (float) w
+ / (float) h, -4.0, 4.0, -4.0, 4.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void initlights(GL2 gl) {
+ float ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+ float position[] = { 0.0f, 0.0f, 2.0f, 1.0f };
+ float mat_diffuse[] = { 0.6f, 0.6f, 0.6f, 1.0f };
+ float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float mat_shininess[] = { 50.0f };
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/checker.java b/src/redbook/src/glredbook10/checker.java new file mode 100644 index 0000000..fcd2e23 --- /dev/null +++ b/src/redbook/src/glredbook10/checker.java @@ -0,0 +1,173 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import java.nio.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program texture maps a checkerboard image onto two rectangles. This
+ * program clamps the texture, if the texture coordinates fall outside 0.0 and
+ * 1.0.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class checker //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ /* Create checkerboard texture */
+ private static final int checkImageWidth = 64;
+ private static final int checkImageHeight = 64;
+ private static final int color = 3;
+ // private byte checkImage[][][] = new
+ // byte[checkImageWidth][checkImageHeight][color];
+ private ByteBuffer checkImageBuf = //
+ BufferUtil.newByteBuffer(checkImageHeight * checkImageWidth * color);
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ checker demo = new checker();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("checker");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+
+ makeCheckImage();
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, color, checkImageWidth,
+ checkImageHeight, 0, GL2.GL_RGB, GL.GL_UNSIGNED_BYTE,
+ checkImageBuf);// checkImage[0][0][0]);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2f(0.0f, 0.0f);
+ gl.glVertex3f(-2.0f, -1.0f, 0.0f);
+ gl.glTexCoord2f(0.0f, 1.0f);
+ gl.glVertex3f(-2.0f, 1.0f, 0.0f);
+ gl.glTexCoord2f(1.0f, 1.0f);
+ gl.glVertex3f(0.0f, 1.0f, 0.0f);
+ gl.glTexCoord2f(1.0f, 0.0f);
+ gl.glVertex3f(0.0f, -1.0f, 0.0f);
+
+ gl.glTexCoord2f(0.0f, 0.0f);
+ gl.glVertex3f(1.0f, -1.0f, 0.0f);
+ gl.glTexCoord2f(0.0f, 1.0f);
+ gl.glVertex3f(1.0f, 1.0f, 0.0f);
+ gl.glTexCoord2f(1.0f, 1.0f);
+ gl.glVertex3f(2.41421f, 1.0f, -1.41421f);
+ gl.glTexCoord2f(1.0f, 0.0f);
+ gl.glVertex3f(2.41421f, -1.0f, -1.41421f);
+ gl.glEnd();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+
+ glu.gluPerspective(60.0, 1.0 * (float) w / (float) h, 1.0, 30.0);
+
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+
+ gl.glTranslatef(0.0f, 0.0f, -3.6f);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * 3D array won't be used. I left it here for you to see.
+ */
+ private void makeCheckImage() {
+ byte c = (byte) 0xFF;
+
+ for (int i = 0; i < checkImageWidth; i++) {
+ for (int j = 0; j < checkImageHeight; j++) {
+ // c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;
+ c = (byte) ((((byte) ((i & 0x8) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x8) == 0 ? 0x00 : 0xff))));
+
+ // checkImage[i][j][0] = (byte) c;
+ // checkImage[i][j][1] = (byte) c;
+ // checkImage[i][j][2] = (byte) c;
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ }
+ }
+ checkImageBuf.rewind();
+ }//
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/checker2.java b/src/redbook/src/glredbook10/checker2.java new file mode 100644 index 0000000..861f633 --- /dev/null +++ b/src/redbook/src/glredbook10/checker2.java @@ -0,0 +1,175 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import java.nio.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program texture maps a checkerboard image onto two rectangles. This
+ * program clamps the texture, if the texture coordinates fall outside 0.0 and
+ * 1.0.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class checker2//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ /* Create checkerboard texture */
+ private static final int checkImageWidth = 64;
+ private static final int checkImageHeight = 64;
+ private static final int color = 3;
+ private byte checkImage[][][] = new byte[checkImageWidth][checkImageHeight][color];
+ private ByteBuffer checkImageBuf = //
+ BufferUtil.newByteBuffer(checkImageHeight * checkImageWidth * color);
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ checker2 demo = new checker2();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("checker2");
+
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+
+ makeCheckImage();
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, color, checkImageWidth,
+ checkImageHeight, 0, GL2.GL_RGB, GL.GL_UNSIGNED_BYTE,
+ checkImageBuf);// checkImage[0][0][0]);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2f(0.0f, 0.0f);
+ gl.glVertex3f(-2.0f, -1.0f, 0.0f);
+ gl.glTexCoord2f(0.0f, 1.0f);
+ gl.glVertex3f(-2.0f, 1.0f, 0.0f);
+ gl.glTexCoord2f(1.0f, 1.0f);
+ gl.glVertex3f(0.0f, 1.0f, 0.0f);
+ gl.glTexCoord2f(1.0f, 0.0f);
+ gl.glVertex3f(0.0f, -1.0f, 0.0f);
+
+ gl.glTexCoord2f(0.0f, 0.0f);
+ gl.glVertex3f(1.0f, -1.0f, 0.0f);
+ gl.glTexCoord2f(0.0f, 1.0f);
+ gl.glVertex3f(1.0f, 1.0f, 0.0f);
+ gl.glTexCoord2f(1.0f, 1.0f);
+ gl.glVertex3f(2.41421f, 1.0f, -1.41421f);
+ gl.glTexCoord2f(1.0f, 0.0f);
+ gl.glVertex3f(2.41421f, -1.0f, -1.41421f);
+ gl.glEnd();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(60.0, 1.0 * (float) w / (float) h, 1.0, 30.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslatef(0.0f, 0.0f, -3.6f);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * 3D array won't be used. I left it here for you to see.
+ */
+ private void makeCheckImage() {
+ byte c = (byte) 0xFF;
+
+ for (int i = 0; i < checkImageWidth; i++) {
+ for (int j = 0; j < checkImageHeight; j++) {
+ // c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;
+ c = (byte) ((((byte) ((i & 0x8) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x8) == 0 ? 0x00 : 0xff))));
+ // if (i % 2 == 0) if (j % 2 == 1) c = (byte) 0xff;
+ // else c = (byte) 0x00;
+ // else if (j % 2 == 1) c = (byte) 0x00;
+ // else c = (byte) 0xff;
+ checkImage[i][j][0] = (byte) c;
+ checkImage[i][j][1] = (byte) c;
+ checkImage[i][j][2] = (byte) c;
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ }
+ }
+ checkImageBuf.rewind();
+ }//
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/chess.java b/src/redbook/src/glredbook10/chess.java new file mode 100644 index 0000000..354f4bc --- /dev/null +++ b/src/redbook/src/glredbook10/chess.java @@ -0,0 +1,167 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import java.nio.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program texture maps a checkerboard image onto two rectangles. The
+ * texture coordinates for the rectangles are 0.0 to 3.0.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class chess//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ /* Create checkerboard texture */
+ private static final int checkImageWidth = 64;
+ private static final int checkImageHeight = 64;
+ private static final int color = 3;
+// private byte checkImage[][][] = new byte[checkImageWidth][checkImageHeight][color];
+ private ByteBuffer checkImageBuf = //
+ BufferUtil.newByteBuffer(checkImageHeight * checkImageWidth * color);
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ chess demo = new chess();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("chess");
+ frame.setSize(512, 256);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+
+ makeCheckImage();
+
+ // gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, color, checkImageWidth,
+ checkImageHeight, 0, GL2.GL_RGB, GL.GL_UNSIGNED_BYTE,
+ checkImageBuf);// checkImage[0][0][0]);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2f(0.0f, 0.0f);
+ gl.glVertex3f(-2.0f, -1.0f, 0.0f);
+ gl.glTexCoord2f(0.0f, 3.0f);
+ gl.glVertex3f(-2.0f, 1.0f, 0.0f);
+ gl.glTexCoord2f(3.0f, 3.0f);
+ gl.glVertex3f(0.0f, 1.0f, 0.0f);
+ gl.glTexCoord2f(3.0f, 0.0f);
+ gl.glVertex3f(0.0f, -1.0f, 0.0f);
+
+ gl.glTexCoord2f(0.0f, 0.0f);
+ gl.glVertex3f(1.0f, -1.0f, 0.0f);
+ gl.glTexCoord2f(0.0f, 3.0f);
+ gl.glVertex3f(1.0f, 1.0f, 0.0f);
+ gl.glTexCoord2f(3.0f, 3.0f);
+ gl.glVertex3f(2.41421f, 1.0f, -1.41421f);
+ gl.glTexCoord2f(3.0f, 0.0f);
+ gl.glVertex3f(2.41421f, -1.0f, -1.41421f);
+ gl.glEnd();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(60.0, 1.0 * (float) w / (float) h, 1.0, 30.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslatef(0.0f, 0.0f, -3.6f);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * 3D array won't be used. I left it here for you to see.
+ */
+ private void makeCheckImage() {
+ byte c = (byte) 0xFF;
+
+ for (int i = 0; i < checkImageWidth; i++) {
+ for (int j = 0; j < checkImageHeight; j++) {
+ // c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;
+ c = (byte) ((((byte) ((i & 0x8) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x8) == 0 ? 0x00 : 0xff))));
+ // checkImage[i][j][0] = (byte) c;
+ // checkImage[i][j][1] = (byte) c;
+ // checkImage[i][j][2] = (byte) c;
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ }
+ }
+ checkImageBuf.rewind();
+ }//
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/clip.java b/src/redbook/src/glredbook10/clip.java new file mode 100644 index 0000000..0166d9f --- /dev/null +++ b/src/redbook/src/glredbook10/clip.java @@ -0,0 +1,118 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates arbitrary clipping planes.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class clip//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ clip demo = new clip();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("clip");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ gl.glClearColor(0, 0, 0, 0);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ double eqn[] = { 0.0, 1.0, 0.0, 0.0 };
+ double eqn2[] = { 1.0, 0.0, 0.0, 0.0 };
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, 0.0f, -5.0f);
+
+ /* clip lower half -- y < 0 */
+ gl.glClipPlane(GL2.GL_CLIP_PLANE0, eqn, 0);
+ gl.glEnable(GL2.GL_CLIP_PLANE0);
+ /* clip left half -- x < 0 */
+ gl.glClipPlane(GL2.GL_CLIP_PLANE1, eqn2, 0);
+ gl.glEnable(GL2.GL_CLIP_PLANE1);
+
+ gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutWireSphere(1.0, 20, 16);
+ gl.glPopMatrix();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 20.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ // TODO Auto-generated method stub
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook10/colormat.java b/src/redbook/src/glredbook10/colormat.java new file mode 100644 index 0000000..924db22 --- /dev/null +++ b/src/redbook/src/glredbook10/colormat.java @@ -0,0 +1,187 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+
+import javax.swing.*;
+
+import javax.media.opengl.*;
+
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * After initialization, the program will be in ColorMaterial mode. Pressing the
+ * mouse buttons will change the color of the diffuse reflection.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class colormat//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLUT glut;
+ private float diffuseMaterial[] = { 0.5f, 0.5f, 0.5f, 1.0f };
+ private boolean diffuseColorChanged = false;
+
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ colormat demo = new colormat();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("colormat");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }//
+ /*
+ * Initialize values for material property, light source, lighting model,
+ * and depth buffer.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, diffuseMaterial, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 25.0f);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ gl.glColorMaterial(GL.GL_FRONT, GL2.GL_DIFFUSE);
+ gl.glEnable(GL2.GL_COLOR_MATERIAL);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ if (diffuseColorChanged) {
+ gl.glColor4fv(diffuseMaterial, 0);
+ diffuseColorChanged = !diffuseColorChanged;
+ }
+
+ glut.glutSolidSphere(1.0f, 10, 10);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+
+ if (w <= h)
+ gl.glOrtho(-1.5, 1.5, -1.5 * (float) h / (float) w, 1.5 * (float) h
+ / (float) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-1.5 * (float) w / (float) h, 1.5 * (float) w
+ / (float) h, -1.5, 1.5, -10.0, 10.0);
+
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void changeRedDiffuse() {
+ diffuseMaterial[0] += 0.1;
+ if (diffuseMaterial[0] > 1.0)
+ diffuseMaterial[0] = 0.0f;
+ // gl.glColor4fv(diffuseMaterial, 0);
+ }
+
+ private void changeGreenDiffuse() {
+ diffuseMaterial[1] += 0.1;
+ if (diffuseMaterial[1] > 1.0)
+ diffuseMaterial[1] = 0.0f;
+ // gl.glColor4fv(diffuseMaterial, 0);
+ }
+
+ private void changeBlueDiffuse() {
+ diffuseMaterial[2] += 0.1;
+ if (diffuseMaterial[2] > 1.0)
+ diffuseMaterial[2] = 0.0f;
+ // gl.glColor4fv(diffuseMaterial, 0);
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent mouse) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ switch (mouse.getButton()) {
+ case MouseEvent.BUTTON1:
+ changeRedDiffuse();
+ diffuseColorChanged = true;
+ break;
+ case MouseEvent.BUTTON2:
+ changeGreenDiffuse();
+ diffuseColorChanged = true;
+ break;
+ case MouseEvent.BUTTON3:
+ changeBlueDiffuse();
+ diffuseColorChanged = true;
+ break;
+ default:
+ break;
+ }
+
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/cone.java b/src/redbook/src/glredbook10/cone.java new file mode 100644 index 0000000..bc7a003 --- /dev/null +++ b/src/redbook/src/glredbook10/cone.java @@ -0,0 +1,139 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates the use of the GL lighting model. A sphere is drawn
+ * using a grey material characteristic. A single light source illuminates the
+ * object.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class cone//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ cone demo = new cone();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("cone");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }//
+
+ /*
+ * Initialize material property, light source, and lighting model.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+
+ glut = new GLUT();
+ //
+ float mat_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+ float mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
+ /* mat_specular and mat_shininess are NOT default values */
+ float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float mat_shininess[] = { 50.0f };
+
+ float light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ /* light_position is NOT default value */
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+
+ float lmodel_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glTranslatef(0.0f, -1.0f, 0.0f);
+ gl.glRotatef(250.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutSolidCone(1.0, 2.0, 20, 20);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-1.5, 1.5, -1.5 * (float) h / (float) w, 1.5 * (float) h
+ / (float) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-1.5 * (float) w / (float) h, 1.5 * (float) w
+ / (float) h, -1.5, 1.5, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/cube.java b/src/redbook/src/glredbook10/cube.java new file mode 100644 index 0000000..a767252 --- /dev/null +++ b/src/redbook/src/glredbook10/cube.java @@ -0,0 +1,114 @@ +package glredbook10;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.gl2.GLUT;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates a single modeling transformation, glScalef() and a
+ * single viewing transformation, gluLookAt(). A wireframe cube is rendered.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class cube //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ cube demo = new cube();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("cube");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ /*
+ * Clear the screen. Set the current color to white. Draw the wire frame
+ * cube.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glLoadIdentity(); /* clear the matrix */
+ /* viewing transformation */
+ glu.gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
+ gl.glScalef(1.0f, 2.0f, 1.0f); /* modeling transformation */
+ glut.glutWireCube(1.0f);
+ gl.glFlush();
+
+ }
+
+ /*
+ * Called when the window is first opened and whenever the window is
+ * reconfigured (moved or resized).
+ */
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glMatrixMode(GL2.GL_PROJECTION); /* prepare for and then */
+ gl.glLoadIdentity(); /* define the projection */
+ gl.glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); /* transformation */
+ gl.glMatrixMode(GL2.GL_MODELVIEW); /* back to modelview matrix */
+ gl.glViewport(0, 0, w, h); /* define the viewport */
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent arg0) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent arg0) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/depthcue.java b/src/redbook/src/glredbook10/depthcue.java new file mode 100644 index 0000000..e8a3be7 --- /dev/null +++ b/src/redbook/src/glredbook10/depthcue.java @@ -0,0 +1,120 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+/**
+ * This program draws a wireframe model, which uses intensity (brightness) to
+ * give clues to distance. Fog is used to achieve this effect.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class depthcue//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ depthcue demo = new depthcue();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("depthcue");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize linear fog for depth cueing.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ float fogColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+
+ gl.glEnable(GL2.GL_FOG);
+ gl.glFogi(GL2.GL_FOG_MODE, GL.GL_LINEAR);
+ gl.glHint(GL2.GL_FOG_HINT, GL.GL_NICEST); /* per pixel */
+ gl.glFogf(GL2.GL_FOG_START, 3.0f);
+ gl.glFogf(GL2.GL_FOG_END, 5.0f);
+ gl.glFogfv(GL2.GL_FOG_COLOR, fogColor, 0);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ /*
+ * display() draws an icosahedron.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ glut.glutWireIcosahedron();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(45.0, (float) w / (float) h, 3.0, 5.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslatef(0.0f, 0.0f, -4.0f); /* move object into view */
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/disk.java b/src/redbook/src/glredbook10/disk.java new file mode 100644 index 0000000..f20264d --- /dev/null +++ b/src/redbook/src/glredbook10/disk.java @@ -0,0 +1,138 @@ +package glredbook10;
+
+import java.awt.event.*;
+
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+import javax.media.opengl.glu.GLUquadric;
+
+/**
+ * This program demonstrates the use of the quadrics Utility Library routines to
+ * draw circles and arcs.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class disk//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUquadric quadObj;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+ public static void main(String[] args) {
+
+ disk demo = new disk();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("disk");
+ frame.setSize(512, 512);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+
+ quadObj = glu.gluNewQuadric();
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ /*
+ * Clear the screen. For each triangle, set the current color and modify the
+ * modelview matrix.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ gl.glPushMatrix();
+ glu.gluQuadricDrawStyle(quadObj, GLU.GLU_FILL);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glTranslatef(10.0f, 10.0f, 0.0f);
+ glu.gluDisk(quadObj, 0.0, 5.0, 10, 2);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glColor3f(1.0f, 1.0f, 0.0f);
+ gl.glTranslatef(20.0f, 20.0f, 0.0f);
+ glu.gluPartialDisk(quadObj, 0.0, 5.0, 10, 3, 30.0, 120.0);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ glu.gluQuadricDrawStyle(quadObj, GLU.GLU_SILHOUETTE);
+ gl.glColor3f(0.0f, 1.0f, 1.0f);
+ gl.glTranslatef(30.0f, 30.0f, 0.0f);
+ glu.gluPartialDisk(quadObj, 0.0, 5.0, 10, 3, 135.0, 270.0);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ glu.gluQuadricDrawStyle(quadObj, GLU.GLU_LINE);
+ gl.glColor3f(1.0f, 0.0f, 1.0f);
+ gl.glTranslatef(40.0f, 40.0f, 0.0f);
+ glu.gluDisk(quadObj, 2.0, 5.0, 10, 10);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(0.0, 50.0, 0.0, 50.0 * (float) h / (float) w, -1.0, 1.0);
+ else
+ gl.glOrtho(0.0, 50.0 * (float) w / (float) h, 0.0, 50.0, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void run() {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/dof.java b/src/redbook/src/glredbook10/dof.java new file mode 100644 index 0000000..b73b2de --- /dev/null +++ b/src/redbook/src/glredbook10/dof.java @@ -0,0 +1,396 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates use of the accumulation buffer to create an
+ * out-of-focus depth-of-field effect. The teapots are drawn several times into
+ * the accumulation buffer. The viewing volume is jittered, except at the focal
+ * point, where the viewing volume is at the same position, each time. In this
+ * case, the gold teapot remains in focus.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class dof//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+ //
+ private int teapotList;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ // Be certain you request an accumulation buffer.
+ caps.setAccumBlueBits(16);
+ caps.setAccumGreenBits(16);
+ caps.setAccumRedBits(16);
+ caps.setAccumAlphaBits(16);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ dof demo = new dof();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("dof");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float ambient[] = new float[] { 0.0f, 0.0f, 0.0f, 1.0f };
+ float diffuse[] = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
+ float specular[] = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
+ float position[] = new float[] { 0.0f, 3.0f, 3.0f, 0.0f };
+
+ float lmodel_ambient[] = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };
+ float local_view[] = new float[] { 0.0f };
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
+
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER, local_view, 0);
+
+ gl.glFrontFace(GL.GL_CW);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glClearAccum(0.0f, 0.0f, 0.0f, 0.0f);
+ /* make teapot display list */
+ teapotList = gl.glGenLists(1);
+ gl.glNewList(teapotList, GL2.GL_COMPILE);
+ glut.glutSolidTeapot(0.5f);
+ gl.glEndList();
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ int jitter;
+ int viewport[] = new int[4];
+
+ gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
+ gl.glClear(GL2.GL_ACCUM_BUFFER_BIT);
+
+ for (jitter = 0; jitter < 8; jitter++) {
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ accPerspective(gl, 45.0, (double) viewport[2]
+ / (double) viewport[3], 1.0, 15.0, 0.0, 0.0,
+ 0.33 * j8[jitter].x, 0.33 * j8[jitter].y, 5.0);
+
+ /* ruby, gold, silver, emerald, and cyan teapots */
+ renderTeapot(gl, -1.1f, -0.5f, -4.5f, 0.1745f, 0.01175f, 0.01175f,
+ 0.61424f, 0.04136f, 0.04136f, 0.727811f, 0.626959f,
+ 0.626959f, 0.6f);
+ renderTeapot(gl, -0.5f, -0.5f, -5.0f, 0.24725f, 0.1995f, 0.0745f,
+ 0.75164f, 0.60648f, 0.22648f, 0.628281f, 0.555802f,
+ 0.366065f, 0.4f);
+ renderTeapot(gl, 0.2f, -0.5f, -5.5f, 0.19225f, 0.19225f, 0.19225f,
+ 0.50754f, 0.50754f, 0.50754f, 0.508273f, 0.508273f,
+ 0.508273f, 0.4f);
+ renderTeapot(gl, 1.0f, -0.5f, -6.0f, 0.0215f, 0.1745f, 0.0215f,
+ 0.07568f, 0.61424f, 0.07568f, 0.633f, 0.727811f, 0.633f,
+ 0.6f);
+ renderTeapot(gl, 1.8f, -0.5f, -6.5f, 0.0f, 0.1f, 0.06f, 0.0f,
+ 0.50980392f, 0.50980392f, 0.50196078f, 0.50196078f,
+ 0.50196078f, .25f);
+ gl.glAccum(GL2.GL_ACCUM, 0.125f);
+ }
+ gl.glAccum(GL2.GL_RETURN, 1.0f);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * accFrustum() The first 6 arguments are identical to the glFrustum() call.
+ * pixdx and pixdy are anti-alias jitter in pixels. Set both equal to 0.0
+ * for no anti-alias jitter. eyedx and eyedy are depth-of field jitter in
+ * pixels. Set both equal to 0.0 for no depth of field effects. focus is
+ * distance from eye to plane in focus. focus must be greater than, but not
+ * equal to 0.0. Note that accFrustum() calls glTranslatef(). You will
+ * probably want to insure that your ModelView matrix has been initialized
+ * to identity before calling accFrustum().
+ */
+ private void accFrustum(GL2 gl, double left, double right, double bottom,
+ double top, double near, double far, double pixdx, double pixdy,
+ double eyedx, double eyedy, double focus) {
+ double xwsize, ywsize;
+ double dx, dy;
+ int viewport[] = new int[4];
+
+ gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
+
+ xwsize = right - left;
+ ywsize = top - bottom;
+
+ dx = -(pixdx * xwsize / (double) viewport[2] + eyedx * near / focus);
+ dy = -(pixdy * ywsize / (double) viewport[3] + eyedy * near / focus);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glFrustum(left + dx, right + dx, bottom + dy, top + dy, near, far);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslatef((float) -eyedx, (float) -eyedy, 0.0f);
+ }
+
+ /*
+ * accPerspective() The first 4 arguments are identical to the
+ * gluPerspective() call. pixdx and pixdy are anti-alias jitter in pixels.
+ * Set both equal to 0.0 for no anti-alias jitter. eyedx and eyedy are
+ * depth-of field jitter in pixels. Set both equal to 0.0 for no depth of
+ * field effects. focus is distance from eye to plane in focus. focus must
+ * be greater than, but not equal to 0.0. Note that accPerspective() calls
+ * accFrustum().
+ */
+ private void accPerspective(GL2 gl, double fovy, double aspect, double near,
+ double far, double pixdx, double pixdy, double eyedx, double eyedy,
+ double focus) {
+ double fov2, left, right, bottom, top;
+
+ fov2 = ((fovy * Math.PI) / 180.0) / 2.0;
+
+ top = near / (Math.cos(fov2) / Math.sin(fov2));
+ bottom = -top;
+
+ right = top * aspect;
+ left = -right;
+
+ accFrustum(gl, left, right, bottom, top, near, far, pixdx, pixdy,
+ eyedx, eyedy, focus);
+ }
+
+ private void renderTeapot(GL2 gl, float x, float y, float z, float ambr,
+ float ambg, float ambb, float difr, float difg, float difb,
+ float specr, float specg, float specb, float shine) {
+ float mat[] = new float[4];
+
+ gl.glPushMatrix();
+ gl.glTranslatef(x, y, z);
+ mat[0] = ambr;
+ mat[1] = ambg;
+ mat[2] = ambb;
+ mat[3] = 1.0f;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0);
+ mat[0] = difr;
+ mat[1] = difg;
+ mat[2] = difb;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat, 0);
+ mat[0] = specr;
+ mat[1] = specg;
+ mat[2] = specb;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, shine * 128.0f);
+ gl.glCallList(teapotList);
+ gl.glPopMatrix();
+ }
+
+ /*
+ * display() draws 5 teapots into the accumulation buffer several times;
+ * each time with a jittered perspective. The focal point is at z = 5.0, so
+ * the gold teapot will stay in focus. The amount of jitter is adjusted by
+ * the magnitude of the accPerspective() jitter; in this example, 0.33. In
+ * this example, the teapots are drawn 8 times. See jitter.h
+ */
+ public void keyTyped(KeyEvent key) {
+
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+
+ }
+
+ /* 2 jitter points */
+ jitter_point j2[] = { new jitter_point(0.246490f, 0.249999f),
+ new jitter_point(-0.246490f, -0.249999f) };
+
+ /* 3 jitter points */
+ jitter_point j3[] = { new jitter_point(-0.373411, -0.250550),//
+ new jitter_point(0.256263, 0.368119), //
+ new jitter_point(0.117148, -0.117570) };
+
+ /* 4 jitter points */
+ jitter_point j4[] = { new jitter_point(-0.208147, 0.353730),
+ new jitter_point(0.203849, -0.353780),
+ new jitter_point(-0.292626, -0.149945),
+ new jitter_point(0.296924, 0.149994) };
+
+ /* 8 jitter points */
+ jitter_point j8[] = { new jitter_point(-0.334818, 0.435331),
+ new jitter_point(0.286438, -0.393495),
+ new jitter_point(0.459462, 0.141540),
+ new jitter_point(-0.414498, -0.192829),
+ new jitter_point(-0.183790, 0.082102),
+ new jitter_point(-0.079263, -0.317383),
+ new jitter_point(0.102254, 0.299133),
+ new jitter_point(0.164216, -0.054399) };
+
+ /* 15 jitter points */
+ jitter_point j15[] = { new jitter_point(0.285561, 0.188437),
+ new jitter_point(0.360176, -0.065688),
+ new jitter_point(-0.111751, 0.275019),
+ new jitter_point(-0.055918, -0.215197),
+ new jitter_point(-0.080231, -0.470965),
+ new jitter_point(0.138721, 0.409168),
+ new jitter_point(0.384120, 0.458500),
+ new jitter_point(-0.454968, 0.134088),
+ new jitter_point(0.179271, -0.331196),
+ new jitter_point(-0.307049, -0.364927),
+ new jitter_point(0.105354, -0.010099),
+ new jitter_point(-0.154180, 0.021794),
+ new jitter_point(-0.370135, -0.116425),
+ new jitter_point(0.451636, -0.300013),
+ new jitter_point(-0.370610, 0.387504) };
+
+ /* 24 jitter points */
+ jitter_point j24[] = { new jitter_point(0.030245, 0.136384),
+ new jitter_point(0.018865, -0.348867),
+ new jitter_point(-0.350114, -0.472309),
+ new jitter_point(0.222181, 0.149524),
+ new jitter_point(-0.393670, -0.266873),
+ new jitter_point(0.404568, 0.230436),
+ new jitter_point(0.098381, 0.465337),
+ new jitter_point(0.462671, 0.442116),
+ new jitter_point(0.400373, -0.212720),
+ new jitter_point(-0.409988, 0.263345),
+ new jitter_point(-0.115878, -0.001981),
+ new jitter_point(0.348425, -0.009237),
+ new jitter_point(-0.464016, 0.066467),
+ new jitter_point(-0.138674, -0.468006),
+ new jitter_point(0.144932, -0.022780),
+ new jitter_point(-0.250195, 0.150161),
+ new jitter_point(-0.181400, -0.264219),
+ new jitter_point(0.196097, -0.234139),
+ new jitter_point(-0.311082, -0.078815),
+ new jitter_point(0.268379, 0.366778),
+ new jitter_point(-0.040601, 0.327109),
+ new jitter_point(-0.234392, 0.354659),
+ new jitter_point(-0.003102, -0.154402),
+ new jitter_point(0.297997, -0.417965) };
+
+ /* 66 jitter points */
+ jitter_point j66[] = { new jitter_point(0.266377, -0.218171),
+ new jitter_point(-0.170919, -0.429368),
+ new jitter_point(0.047356, -0.387135),
+ new jitter_point(-0.430063, 0.363413),
+ new jitter_point(-0.221638, -0.313768),
+ new jitter_point(0.124758, -0.197109),
+ new jitter_point(-0.400021, 0.482195),
+ new jitter_point(0.247882, 0.152010),
+ new jitter_point(-0.286709, -0.470214),
+ new jitter_point(-0.426790, 0.004977),
+ new jitter_point(-0.361249, -0.104549),
+ new jitter_point(-0.040643, 0.123453),
+ new jitter_point(-0.189296, 0.438963),
+ new jitter_point(-0.453521, -0.299889),
+ new jitter_point(0.408216, -0.457699),
+ new jitter_point(0.328973, -0.101914),
+ new jitter_point(-0.055540, -0.477952),
+ new jitter_point(0.194421, 0.453510), //
+ new jitter_point(0.404051, 0.224974), //
+ new jitter_point(0.310136, 0.419700),
+ new jitter_point(-0.021743, 0.403898),
+ new jitter_point(-0.466210, 0.248839),
+ new jitter_point(0.341369, 0.081490),
+ new jitter_point(0.124156, -0.016859),
+ new jitter_point(-0.461321, -0.176661),
+ new jitter_point(0.013210, 0.234401),
+ new jitter_point(0.174258, -0.311854),
+ new jitter_point(0.294061, 0.263364),
+ new jitter_point(-0.114836, 0.328189),
+ new jitter_point(0.041206, -0.106205),
+ new jitter_point(0.079227, 0.345021),
+ new jitter_point(-0.109319, -0.242380),
+ new jitter_point(0.425005, -0.332397),
+ new jitter_point(0.009146, 0.015098),
+ new jitter_point(-0.339084, -0.355707),
+ new jitter_point(-0.224596, -0.189548),
+ new jitter_point(0.083475, 0.117028),
+ new jitter_point(0.295962, -0.334699),
+ new jitter_point(0.452998, 0.025397),
+ new jitter_point(0.206511, -0.104668),
+ new jitter_point(0.447544, -0.096004),
+ new jitter_point(-0.108006, -0.002471),
+ new jitter_point(-0.380810, 0.130036),
+ new jitter_point(-0.242440, 0.186934),
+ new jitter_point(-0.200363, 0.070863),
+ new jitter_point(-0.344844, -0.230814),
+ new jitter_point(0.408660, 0.345826),
+ new jitter_point(-0.233016, 0.305203),
+ new jitter_point(0.158475, -0.430762),
+ new jitter_point(0.486972, 0.139163),
+ new jitter_point(-0.301610, 0.009319),
+ new jitter_point(0.282245, -0.458671),
+ new jitter_point(0.482046, 0.443890),
+ new jitter_point(-0.121527, 0.210223),
+ new jitter_point(-0.477606, -0.424878),
+ new jitter_point(-0.083941, -0.121440),
+ new jitter_point(-0.345773, 0.253779),
+ new jitter_point(0.234646, 0.034549),
+ new jitter_point(0.394102, -0.210901),
+ new jitter_point(-0.312571, 0.397656),
+ new jitter_point(0.200906, 0.333293),
+ new jitter_point(0.018703, -0.261792),
+ new jitter_point(-0.209349, -0.065383),
+ new jitter_point(0.076248, 0.478538),
+ new jitter_point(-0.073036, -0.355064),
+ new jitter_point(0.145087, 0.221726) };
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+ class jitter_point {
+ private static final int MAX_SAMPLES = 66;
+ float x, y;
+
+ public jitter_point(double x, double y) {
+ this.x = (float) x;
+ this.y = (float) y;
+ }
+ }
+}
diff --git a/src/redbook/src/glredbook10/dofnot.java b/src/redbook/src/glredbook10/dofnot.java new file mode 100644 index 0000000..5d1a365 --- /dev/null +++ b/src/redbook/src/glredbook10/dofnot.java @@ -0,0 +1,173 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates the same scene as dof.c, but without use of the
+ * accumulation buffer, so everything is in focus.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class dofnot//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ dofnot demo = new dofnot();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("dofnot");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ float ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float position[] = { 0.0f, 3.0f, 3.0f, 0.0f };
+
+ float lmodel_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+ float local_view[] = { 0.0f };
+
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
+
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER, local_view, 0);
+
+ gl.glFrontFace(GL.GL_CW);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ /*
+ * display() draws 5 teapots into the accumulation buffer several times;
+ * each time with a jittered perspective. The focal point is at z = 5.0, so
+ * the gold teapot will stay in focus. The amount of jitter is adjusted by
+ * the magnitude of the accPerspective() jitter; in this example, 0.33. In
+ * this example, the teapots are drawn 8 times. See jitter.h
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glPushMatrix();
+ /* ruby, gold, silver, emerald, and cyan teapots */
+ renderTeapot(gl, -1.1f, -0.5f, -4.5f, 0.1745f, 0.01175f, 0.01175f,
+ 0.61424f, 0.04136f, 0.04136f, 0.727811f, 0.626959f, 0.626959f,
+ 0.6f);
+ renderTeapot(gl, -0.5f, -0.5f, -5.0f, 0.24725f, 0.1995f, 0.0745f,
+ 0.75164f, 0.60648f, 0.22648f, 0.628281f, 0.555802f, 0.366065f,
+ 0.4f);
+ renderTeapot(gl, 0.2f, -0.5f, -5.5f, 0.19225f, 0.19225f, 0.19225f,
+ 0.50754f, 0.50754f, 0.50754f, 0.508273f, 0.508273f, 0.508273f,
+ 0.4f);
+ renderTeapot(gl, 1.0f, -0.5f, -6.0f, 0.0215f, 0.1745f, 0.0215f,
+ 0.07568f, 0.61424f, 0.07568f, 0.633f, 0.727811f, 0.633f, 0.6f);
+ renderTeapot(gl, 1.8f, -0.5f, -6.5f, 0.0f, 0.1f, 0.06f, 0.0f,
+ 0.5098039f, 0.50980392f, 0.50196078f, 0.50196078f, 0.50196078f,
+ .25f);
+
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(45.0, (float) w / (float) h, 1.0, 15.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void renderTeapot(GL2 gl, float x, float y, float z, float ambr,
+ float ambg, float ambb, float difr, float difg, float difb,
+ float specr, float specg, float specb, float shine) {
+ float mat[] = new float[4];
+
+ gl.glPushMatrix();
+ gl.glTranslatef(x, y, z);
+ mat[0] = ambr;
+ mat[1] = ambg;
+ mat[2] = ambb;
+ mat[3] = 1.0f;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0);
+ mat[0] = difr;
+ mat[1] = difg;
+ mat[2] = difb;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat, 0);
+ mat[0] = specr;
+ mat[1] = specg;
+ mat[2] = specb;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, shine * 128.0f);
+ glut.glutSolidTeapot(0.5);
+ gl.glPopMatrix();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/doublebuf.java b/src/redbook/src/glredbook10/doublebuf.java new file mode 100644 index 0000000..9c96e8f --- /dev/null +++ b/src/redbook/src/glredbook10/doublebuf.java @@ -0,0 +1,150 @@ +package glredbook10;
+
+import javax.swing.*;
+import java.awt.event.*;
+
+import com.sun.opengl.util.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This is a simple double buffered program. Pressing the left mouse button
+ * rotates the rectangle. Pressing the middle mouse button stops the rotation.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class doublebuf//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private float spin = 0f, spinDelta = 0f;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.setDoubleBuffered(true);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ doublebuf demo = new doublebuf();
+
+ FPSAnimator animator = new FPSAnimator(demo.drawable, 60);
+ demo.setAnimator(animator);
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("doublebuf");
+ frame.setSize(512, 256);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ animator.start();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public synchronized void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glPushMatrix();
+ gl.glRotatef(spin, 0.0f, 0.0f, 1.0f);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glRectf(-25.0f, -25.0f, 25.0f, 25.0f);
+ gl.glPopMatrix();
+
+ gl.glFlush();
+
+ spinDisplay();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ float aspect = 0;
+ if (w <= h) {
+ aspect = (float) h / (float) w;
+ gl.glOrtho(-50.0, 50.0, -50.0 * aspect, 50.0 * aspect, //
+ -1.0, 1.0);
+ } else {
+ aspect = (float) w / (float) h;
+ gl.glOrtho(-50.0 * aspect, 50.0 * aspect, -50.0, 50.0, //
+ -1.0, 1.0);
+ }
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void spinDisplay() {
+ spin = spin + spinDelta;
+ if (spin > 360f)
+ spin = spin - 360;
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ super.runExit();
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent key) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ switch (mouse.getButton()) {
+ case MouseEvent.BUTTON1:
+ spinDelta = 2f;
+ break;
+ case MouseEvent.BUTTON2:
+ case MouseEvent.BUTTON3:
+ spinDelta = 0f;
+ break;
+ }//
+
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}//
diff --git a/src/redbook/src/glredbook10/drawf.java b/src/redbook/src/glredbook10/drawf.java new file mode 100644 index 0000000..ee953c0 --- /dev/null +++ b/src/redbook/src/glredbook10/drawf.java @@ -0,0 +1,106 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * Draws the bitmapped letter F on the screen (several times). This demonstrates
+ * use of the glBitmap() call.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class drawf//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private byte rasters[] = new byte[] { (byte) 0xc0, (byte) 0x00,
+ (byte) 0xc0, (byte) 0x00, (byte) 0xc0, (byte) 0x00, (byte) 0xc0,
+ (byte) 0x00, (byte) 0xc0, (byte) 0x00, (byte) 0xff, (byte) 0x00,
+ (byte) 0xff, (byte) 0x00, (byte) 0xc0, (byte) 0x00, (byte) 0xc0,
+ (byte) 0x00, (byte) 0xc0, (byte) 0x00, (byte) 0xff, (byte) 0xc0,
+ (byte) 0xff, (byte) 0xc0 };
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ drawf demo = new drawf();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("drawf");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glRasterPos2f(20.5f, 20.5f);
+ gl.glBitmap(10, 12, 0.0f, 0.0f, 12.0f, 0.0f, rasters, 0);
+ gl.glBitmap(10, 12, 0.0f, 0.0f, 12.0f, 0.0f, rasters, 0);
+ gl.glBitmap(10, 12, 0.0f, 0.0f, 12.0f, 0.0f, rasters, 0);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0, w, 0, h, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ // TODO Auto-generated method stub
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ // TODO Auto-generated method stub
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/feedback.java b/src/redbook/src/glredbook10/feedback.java new file mode 100644 index 0000000..81ced6e --- /dev/null +++ b/src/redbook/src/glredbook10/feedback.java @@ -0,0 +1,171 @@ +package glredbook10;
+
+import javax.swing.*;
+import java.awt.event.*;
+
+import java.nio.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates use of OpenGL feedback. First, a lighting
+ * environment is set up and a few lines are drawn. Then feedback mode is
+ * entered, and the same lines are drawn. The results in the feedback buffer are
+ * printed.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class feedback//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ feedback demo = new feedback();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("feedback");
+ frame.setSize(200, 200);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float feedBuffer[] = new float[1024];
+ FloatBuffer feedBuf = BufferUtil.newFloatBuffer(1024);
+ int size;
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0.0, 100.0, 0.0, 100.0, 0.0, 1.0);
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ drawGeometry(gl, GL2.GL_RENDER);
+
+ gl.glFeedbackBuffer(1024, GL2.GL_3D_COLOR, feedBuf);
+ gl.glRenderMode(GL2.GL_FEEDBACK);
+ drawGeometry(gl, GL2.GL_FEEDBACK);
+
+ size = gl.glRenderMode(GL2.GL_RENDER);
+ feedBuf.get(feedBuffer);
+ printBuffer(gl, size, feedBuffer);
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width,
+ int height) {
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * Draw a few lines and two points, one of which will be clipped. If in
+ * feedback mode, a passthrough token is issued between the each primitive.
+ */
+ void drawGeometry(GL2 gl, int mode) {
+ gl.glBegin(GL.GL_LINE_STRIP);
+ gl.glNormal3f(0.0f, 0.0f, 1.0f);
+ gl.glVertex3f(30.0f, 30.0f, 0.0f);
+ gl.glVertex3f(50.0f, 60.0f, 0.0f);
+ gl.glVertex3f(70.0f, 40.0f, 0.0f);
+ gl.glEnd();
+ if (mode == GL2.GL_FEEDBACK)
+ gl.glPassThrough(1.0f);
+ gl.glBegin(GL.GL_POINTS);
+ gl.glVertex3f(-100.0f, -100.0f, -100.0f); /* will be clipped */
+ gl.glEnd();
+ if (mode == GL2.GL_FEEDBACK)
+ gl.glPassThrough(2.0f);
+ gl.glBegin(GL.GL_POINTS);
+ gl.glNormal3f(0.0f, 0.0f, 1.0f);
+ gl.glVertex3f(50.0f, 50.0f, 0.0f);
+ gl.glEnd();
+ }
+
+ /* Write contents of one vertex to stdout. */
+ void print3DcolorVertex(int size, int count, float[] buffer) {
+ int i;
+
+ System.out.println(" ");
+ for (i = 0; i < 7; i++) {
+ System.out.println(" " + buffer[size - count]);
+ count = count - 1;
+ }
+ System.out.println();
+ }
+
+ /* Write contents of entire buffer. (Parse tokens!) */
+ private void printBuffer(GL2 gl, int size, float[] buffer) {
+ int count;
+ float token;
+
+ count = size;
+ while (count > 0) {
+ token = buffer[size - count];
+ count--;
+ if (token == GL2.GL_PASS_THROUGH_TOKEN) {
+ System.out.println("GL.GL_PASS_THROUGH_TOKEN");
+ System.out.println("\t " + buffer[size - count]);
+ count--;
+ } else if (token == GL2.GL_POINT_TOKEN) {
+ System.out.println("GL.GL_POINT_TOKEN");
+ print3DcolorVertex(size, count, buffer);
+ } else if (token == GL2.GL_LINE_TOKEN) {
+ System.out.println("GL.GL_LINE_TOKEN ");
+ print3DcolorVertex(size, count, buffer);
+ print3DcolorVertex(size, count, buffer);
+ } else if (token == GL2.GL_LINE_RESET_TOKEN) {
+ System.out.println("GL.GL_LINE_RESET_TOKEN ");
+ print3DcolorVertex(size, count, buffer);
+ print3DcolorVertex(size, count, buffer);
+ }
+ }
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/fog.java b/src/redbook/src/glredbook10/fog.java new file mode 100644 index 0000000..c74917e --- /dev/null +++ b/src/redbook/src/glredbook10/fog.java @@ -0,0 +1,202 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program draws 5 red teapots, each at a different z distance from the
+ * eye, in different types of fog. Pressing the left mouse button chooses
+ * between 3 types of fog: exponential, exponential squared, and linear. In this
+ * program, there is a fixed density value, as well as fixed start and end
+ * values for the linear fog.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class fog
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener{
+ private GLUT glut;
+ private int fogMode;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ fog demo = new fog();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("fog");
+ frame.setSize(450, 150);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize z-buffer, projection matrix, light source, and lighting model.
+ * Do not specify a material property here.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float position[] = { 0.0f, 3.0f, 3.0f, 0.0f };
+ float local_view[] = { 0.0f };
+
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER, local_view, 0);
+
+ gl.glFrontFace(GL.GL_CW);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+ gl.glEnable(GL2.GL_FOG);
+ {
+ float fogColor[] = { 0.5f, 0.5f, 0.5f, 1.0f };
+
+ fogMode = GL2.GL_EXP;
+ gl.glFogi(GL2.GL_FOG_MODE, fogMode);
+ gl.glFogfv(GL2.GL_FOG_COLOR, fogColor, 0);
+ gl.glFogf(GL2.GL_FOG_DENSITY, 0.35f);
+ gl.glHint(GL2.GL_FOG_HINT, GL.GL_DONT_CARE);
+
+ gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
+ }
+ }
+
+ /*
+ * display() draws 5 teapots at different z positions.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ //
+ if (fogMode == GL2.GL_EXP2) {
+ gl.glFogf(GL2.GL_FOG_START, 1.0f);
+ gl.glFogf(GL2.GL_FOG_END, 5.0f);
+ }
+ gl.glFogi(GL2.GL_FOG_MODE, fogMode);
+
+ renderRedTeapot(gl, -4.0f, -0.5f, -1.0f);
+ renderRedTeapot(gl, -2.0f, -0.5f, -2.0f);
+ renderRedTeapot(gl, 0.0f, -0.5f, -3.0f);
+ renderRedTeapot(gl, 2.0f, -0.5f, -4.0f);
+ renderRedTeapot(gl, 4.0f, -0.5f, -5.0f);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= (h * 3))
+ gl.glOrtho(-6.0, 6.0, -2.0 * ((float) h * 3) / (float) w, 2.0
+ * ((float) h * 3) / (float) w, 0.0, 10.0);
+ else
+ gl.glOrtho(-6.0 * (float) w / ((float) h * 3), //
+ 6.0 * (float) w / ((float) h * 3), -2.0, 2.0, 0.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void cycleFog() {
+ if (fogMode == GL2.GL_EXP) {
+ fogMode = GL2.GL_EXP2;
+ System.out.println("Fog mode is GL.GL_EXP2");
+ } else if (fogMode == GL2.GL_EXP2) {
+ fogMode = GL.GL_LINEAR;
+ System.out.println("Fog mode is GL.GL_LINEAR\n");
+ } else if (fogMode == GL.GL_LINEAR) {
+ fogMode = GL2.GL_EXP;
+ System.out.println("Fog mode is GL.GL_EXP\n");
+ }
+ }
+
+ private void renderRedTeapot(GL2 gl, float x, float y, float z) {
+ float mat[] = new float[4];
+
+ gl.glPushMatrix();
+ gl.glTranslatef(x, y, z);
+ mat[0] = 0.1745f;
+ mat[1] = 0.01175f;
+ mat[2] = 0.01175f;
+ mat[3] = 1.0f;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0);
+ mat[0] = 0.61424f;
+ mat[1] = 0.04136f;
+ mat[2] = 0.04136f;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat, 0);
+ mat[0] = 0.727811f;
+ mat[1] = 0.626959f;
+ mat[2] = 0.626959f;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 0.6f * 128.0f);
+ glut.glutSolidTeapot(1.0);
+ gl.glPopMatrix();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent mouse) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ cycleFog();
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/font.java b/src/redbook/src/glredbook10/font.java new file mode 100644 index 0000000..e4262f5 --- /dev/null +++ b/src/redbook/src/glredbook10/font.java @@ -0,0 +1,373 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import java.nio.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * Draws some text in a bitmapped font. Uses glBitmap() and other pixel
+ * routines. Also demonstrates use of display lists. <br>
+ * <br>
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class font //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private int fontOffset;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ font demo = new font();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("font");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glShadeModel(GL2.GL_FLAT);
+ makeRasterFont(gl);
+ }
+
+ /*
+ * Everything above this line could be in a library that defines a font. To
+ * make it work, you've got to call makeRasterFont() before you start making
+ * calls to printString().
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float white[] = { 1.0f, 1.0f, 1.0f };
+
+ // char teststring[] = new char[33];
+ String teststring = "";
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3fv(white, 0);
+ for (int i = 32; i < 127; i += 32) {
+ gl.glRasterPos2i(20, 200 - 18 * i / 32);
+ for (int j = 0; j < 32; j++)
+ teststring += (char) (i + j);
+ teststring += '\0';
+ printString(gl, teststring);
+ }
+ gl.glRasterPos2i(20, 100);
+ printString(gl, "The quick brown fox jumps");
+ gl.glRasterPos2i(20, 82);
+ printString(gl, "over a lazy dog.");
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0.0, w, 0.0, h, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeRasterFont(GL2 gl) {
+ int i;
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ fontOffset = gl.glGenLists(128);
+ for (i = 32; i < 127; i++) {
+ gl.glNewList(i + fontOffset, GL2.GL_COMPILE);
+ gl.glBitmap(8, 13, 0.0f, 2.0f, 10.0f, 0.0f, rasters[i - 32], 0);
+ gl.glEndList();
+ }
+ }
+
+ private void printString(GL2 gl, String s) {
+ ByteBuffer str = BufferUtil.newByteBuffer(s.length());
+ str.put(s.getBytes());
+ str.rewind();
+ //
+ gl.glPushAttrib(GL2.GL_LIST_BIT);
+ gl.glListBase(fontOffset);
+ gl.glCallLists(s.length(), GL.GL_UNSIGNED_BYTE, str);
+ gl.glPopAttrib();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ private byte rasters[][] = {
+ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00 },
+ { 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18,
+ 0x18, 0x18 },
+ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x36,
+ 0x36, 0x36 },
+ { 0x00, 0x00, 0x00, 0x66, 0x66, (byte) 0xff, 0x66, 0x66,
+ (byte) 0xff, 0x66, 0x66, 0x00, 0x00 },
+ { 0x00, 0x00, 0x18, 0x7e, (byte) 0xff, 0x1b, 0x1f, 0x7e,
+ (byte) 0xf8, (byte) 0xd8, (byte) 0xff, 0x7e, 0x18 },
+ { 0x00, 0x00, 0x0e, 0x1b, (byte) 0xdb, 0x6e, 0x30, 0x18, 0x0c,
+ 0x76, (byte) 0xdb, (byte) 0xd8, 0x70 },
+ { 0x00, 0x00, 0x7f, (byte) 0xc6, (byte) 0xcf, (byte) 0xd8, 0x70,
+ 0x70, (byte) 0xd8, (byte) 0xcc, (byte) 0xcc, 0x6c, 0x38 },
+ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x1c,
+ 0x0c, 0x0e },
+ { 0x00, 0x00, 0x0c, 0x18, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
+ 0x18, 0x0c },
+ { 0x00, 0x00, 0x30, 0x18, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
+ 0x18, 0x30 },
+ { 0x00, 0x00, 0x00, 0x00, (byte) 0x99, 0x5a, 0x3c, (byte) 0xff,
+ 0x3c, 0x5a, (byte) 0x99, 0x00, 0x00 },
+ { 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, (byte) 0xff, (byte) 0xff,
+ 0x18, 0x18, 0x18, 0x00, 0x00 },
+ { 0x00, 0x00, 0x30, 0x18, 0x1c, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00 },
+ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff,
+ 0x00, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, 0x00, 0x38, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00 },
+ { 0x00, 0x60, 0x60, 0x30, 0x30, 0x18, 0x18, 0x0c, 0x0c, 0x06, 0x06,
+ 0x03, 0x03 },
+ { 0x00, 0x00, 0x3c, 0x66, (byte) 0xc3, (byte) 0xe3, (byte) 0xf3,
+ (byte) 0xdb, (byte) 0xcf, (byte) 0xc7, (byte) 0xc3, 0x66,
+ 0x3c },
+ { 0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x78,
+ 0x38, 0x18 },
+ { 0x00, 0x00, (byte) 0xff, (byte) 0xc0, (byte) 0xc0, 0x60, 0x30,
+ 0x18, 0x0c, 0x06, 0x03, (byte) 0xe7, 0x7e },
+ { 0x00, 0x00, 0x7e, (byte) 0xe7, 0x03, 0x03, 0x07, 0x7e, 0x07,
+ 0x03, 0x03, (byte) 0xe7, 0x7e },
+ { 0x00, 0x00, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, (byte) 0xff,
+ (byte) 0xcc, 0x6c, 0x3c, 0x1c, 0x0c },
+ { 0x00, 0x00, 0x7e, (byte) 0xe7, 0x03, 0x03, 0x07, (byte) 0xfe,
+ (byte) 0xc0, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xff },
+ { 0x00, 0x00, 0x7e, (byte) 0xe7, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc7, (byte) 0xfe, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xe7, 0x7e },
+ { 0x00, 0x00, 0x30, 0x30, 0x30, 0x30, 0x18, 0x0c, 0x06, 0x03, 0x03,
+ 0x03, (byte) 0xff },
+ { 0x00, 0x00, 0x7e, (byte) 0xe7, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xe7, 0x7e, (byte) 0xe7, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xe7, 0x7e },
+ { 0x00, 0x00, 0x7e, (byte) 0xe7, 0x03, 0x03, 0x03, 0x7f,
+ (byte) 0xe7, (byte) 0xc3, (byte) 0xc3, (byte) 0xe7, 0x7e },
+ { 0x00, 0x00, 0x00, 0x38, 0x38, 0x00, 0x00, 0x38, 0x38, 0x00, 0x00,
+ 0x00, 0x00 },
+ { 0x00, 0x00, 0x30, 0x18, 0x1c, 0x1c, 0x00, 0x00, 0x1c, 0x1c, 0x00,
+ 0x00, 0x00 },
+ { 0x00, 0x00, 0x06, 0x0c, 0x18, 0x30, 0x60, (byte) 0xc0, 0x60,
+ 0x30, 0x18, 0x0c, 0x06 },
+ { 0x00, 0x00, 0x00, 0x00, (byte) 0xff, (byte) 0xff, 0x00,
+ (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x03, 0x06, 0x0c, 0x18,
+ 0x30, 0x60 },
+ { 0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x18, 0x0c, 0x06, 0x03,
+ (byte) 0xc3, (byte) 0xc3, 0x7e },
+ { 0x00, 0x00, 0x3f, 0x60, (byte) 0xcf, (byte) 0xdb, (byte) 0xd3,
+ (byte) 0xdd, (byte) 0xc3, 0x7e, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xff, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, 0x66,
+ 0x3c, 0x18 },
+ { 0x00, 0x00, (byte) 0xfe, (byte) 0xc7, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc7, (byte) 0xfe, (byte) 0xc7, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc7, (byte) 0xfe },
+ { 0x00, 0x00, 0x7e, (byte) 0xe7, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xe7, 0x7e },
+ { 0x00, 0x00, (byte) 0xfc, (byte) 0xce, (byte) 0xc7, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc7, (byte) 0xce, (byte) 0xfc },
+ { 0x00, 0x00, (byte) 0xff, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xfc, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xc0, (byte) 0xff },
+ { 0x00, 0x00, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xc0, (byte) 0xfc, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xc0, (byte) 0xff },
+ { 0x00, 0x00, 0x7e, (byte) 0xe7, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xcf, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xe7, 0x7e },
+ { 0x00, 0x00, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xff, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xc3 },
+ { 0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+ 0x18, 0x7e },
+ { 0x00, 0x00, 0x7c, (byte) 0xee, (byte) 0xc6, 0x06, 0x06, 0x06,
+ 0x06, 0x06, 0x06, 0x06, 0x06 },
+ { 0x00, 0x00, (byte) 0xc3, (byte) 0xc6, (byte) 0xcc, (byte) 0xd8,
+ (byte) 0xf0, (byte) 0xe0, (byte) 0xf0, (byte) 0xd8,
+ (byte) 0xcc, (byte) 0xc6, (byte) 0xc3 },
+ { 0x00, 0x00, (byte) 0xff, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xc0, (byte) 0xc0 },
+ { 0x00, 0x00, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xdb, (byte) 0xff,
+ (byte) 0xff, (byte) 0xe7, (byte) 0xc3 },
+ { 0x00, 0x00, (byte) 0xc7, (byte) 0xc7, (byte) 0xcf, (byte) 0xcf,
+ (byte) 0xdf, (byte) 0xdb, (byte) 0xfb, (byte) 0xf3,
+ (byte) 0xf3, (byte) 0xe3, (byte) 0xe3 },
+ { 0x00, 0x00, 0x7e, (byte) 0xe7, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xe7, 0x7e },
+ { 0x00, 0x00, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xfe, (byte) 0xc7, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc7, (byte) 0xfe },
+ { 0x00, 0x00, 0x3f, 0x6e, (byte) 0xdf, (byte) 0xdb, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, 0x66,
+ 0x3c },
+ { 0x00, 0x00, (byte) 0xc3, (byte) 0xc6, (byte) 0xcc, (byte) 0xd8,
+ (byte) 0xf0, (byte) 0xfe, (byte) 0xc7, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc7, (byte) 0xfe },
+ { 0x00, 0x00, 0x7e, (byte) 0xe7, 0x03, 0x03, 0x07, 0x7e,
+ (byte) 0xe0, (byte) 0xc0, (byte) 0xc0, (byte) 0xe7, 0x7e },
+ { 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+ 0x18, (byte) 0xff },
+ { 0x00, 0x00, 0x7e, (byte) 0xe7, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xc3 },
+ { 0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3 },
+ { 0x00, 0x00, (byte) 0xc3, (byte) 0xe7, (byte) 0xff, (byte) 0xff,
+ (byte) 0xdb, (byte) 0xdb, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xc3 },
+ { 0x00, 0x00, (byte) 0xc3, 0x66, 0x66, 0x3c, 0x3c, 0x18, 0x3c,
+ 0x3c, 0x66, 0x66, (byte) 0xc3 },
+ { 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x3c, 0x66,
+ 0x66, (byte) 0xc3 },
+ { 0x00, 0x00, (byte) 0xff, (byte) 0xc0, (byte) 0xc0, 0x60, 0x30,
+ 0x7e, 0x0c, 0x06, 0x03, 0x03, (byte) 0xff },
+ { 0x00, 0x00, 0x3c, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
+ 0x30, 0x3c },
+ { 0x00, 0x03, 0x03, 0x06, 0x06, 0x0c, 0x0c, 0x18, 0x18, 0x30, 0x30,
+ 0x60, 0x60 },
+ { 0x00, 0x00, 0x3c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
+ 0x0c, 0x3c },
+ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ (byte) 0xc3, 0x66, 0x3c, 0x18 },
+ { (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x38,
+ 0x30, 0x70 },
+ { 0x00, 0x00, 0x7f, (byte) 0xc3, (byte) 0xc3, 0x7f, 0x03,
+ (byte) 0xc3, 0x7e, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, (byte) 0xfe, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xfe, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xc0, (byte) 0xc0 },
+ { 0x00, 0x00, 0x7e, (byte) 0xc3, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xc3, 0x7e, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, 0x7f, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, 0x7f, 0x03, 0x03, 0x03, 0x03, 0x03 },
+ { 0x00, 0x00, 0x7f, (byte) 0xc0, (byte) 0xc0, (byte) 0xfe,
+ (byte) 0xc3, (byte) 0xc3, 0x7e, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, 0x30, 0x30, 0x30, 0x30, 0x30, (byte) 0xfc, 0x30,
+ 0x30, 0x30, 0x33, 0x1e },
+ { 0x7e, (byte) 0xc3, 0x03, 0x03, 0x7f, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, 0x7e, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xfe, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xc0, (byte) 0xc0 },
+ { 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00,
+ 0x18, 0x00 },
+ { 0x38, 0x6c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x00, 0x00,
+ 0x0c, 0x00 },
+ { 0x00, 0x00, (byte) 0xc6, (byte) 0xcc, (byte) 0xf8, (byte) 0xf0,
+ (byte) 0xd8, (byte) 0xcc, (byte) 0xc6, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xc0, (byte) 0xc0 },
+ { 0x00, 0x00, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+ 0x18, 0x78 },
+ { 0x00, 0x00, (byte) 0xdb, (byte) 0xdb, (byte) 0xdb, (byte) 0xdb,
+ (byte) 0xdb, (byte) 0xdb, (byte) 0xfe, 0x00, 0x00, 0x00,
+ 0x00 },
+ { 0x00, 0x00, (byte) 0xc6, (byte) 0xc6, (byte) 0xc6, (byte) 0xc6,
+ (byte) 0xc6, (byte) 0xc6, (byte) 0xfc, 0x00, 0x00, 0x00,
+ 0x00 },
+ { 0x00, 0x00, 0x7c, (byte) 0xc6, (byte) 0xc6, (byte) 0xc6,
+ (byte) 0xc6, (byte) 0xc6, 0x7c, 0x00, 0x00, 0x00, 0x00 },
+ { (byte) 0xc0, (byte) 0xc0, (byte) 0xc0, (byte) 0xfe, (byte) 0xc3,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, (byte) 0xfe, 0x00,
+ 0x00, 0x00, 0x00 },
+ { 0x03, 0x03, 0x03, 0x7f, (byte) 0xc3, (byte) 0xc3, (byte) 0xc3,
+ (byte) 0xc3, 0x7f, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0, (byte) 0xc0,
+ (byte) 0xc0, (byte) 0xe0, (byte) 0xfe, 0x00, 0x00, 0x00,
+ 0x00 },
+ { 0x00, 0x00, (byte) 0xfe, 0x03, 0x03, 0x7e, (byte) 0xc0,
+ (byte) 0xc0, 0x7f, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, 0x1c, 0x36, 0x30, 0x30, 0x30, 0x30, (byte) 0xfc,
+ 0x30, 0x30, 0x30, 0x00 },
+ { 0x00, 0x00, 0x7e, (byte) 0xc6, (byte) 0xc6, (byte) 0xc6,
+ (byte) 0xc6, (byte) 0xc6, (byte) 0xc6, 0x00, 0x00, 0x00,
+ 0x00 },
+ { 0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66, (byte) 0xc3,
+ (byte) 0xc3, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, (byte) 0xc3, (byte) 0xe7, (byte) 0xff, (byte) 0xdb,
+ (byte) 0xc3, (byte) 0xc3, (byte) 0xc3, 0x00, 0x00, 0x00,
+ 0x00 },
+ { 0x00, 0x00, (byte) 0xc3, 0x66, 0x3c, 0x18, 0x3c, 0x66,
+ (byte) 0xc3, 0x00, 0x00, 0x00, 0x00 },
+ { (byte) 0xc0, 0x60, 0x60, 0x30, 0x18, 0x3c, 0x66, 0x66,
+ (byte) 0xc3, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, (byte) 0xff, 0x60, 0x30, 0x18, 0x0c, 0x06,
+ (byte) 0xff, 0x00, 0x00, 0x00, 0x00 },
+ { 0x00, 0x00, 0x0f, 0x18, 0x18, 0x18, 0x38, (byte) 0xf0, 0x38,
+ 0x18, 0x18, 0x18, 0x0f },
+ { 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
+ 0x18, 0x18 },
+ { 0x00, 0x00, (byte) 0xf0, 0x18, 0x18, 0x18, 0x1c, 0x0f, 0x1c,
+ 0x18, 0x18, 0x18, (byte) 0xf0 },
+ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, (byte) 0x8f,
+ (byte) 0xf1, 0x60, 0x00, 0x00, 0x00 } };
+ private ByteBuffer rastersBuf = //
+ BufferUtil.newByteBuffer(rasters.length * rasters[0].length);
+ {
+ for (int i = 0; i < rasters.length; i++)
+ rastersBuf.put((byte[]) rasters[i]);// put each array into byte
+ // buffer
+ rastersBuf.rewind();
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook10/glredbook10.txt b/src/redbook/src/glredbook10/glredbook10.txt new file mode 100644 index 0000000..ae0df7d --- /dev/null +++ b/src/redbook/src/glredbook10/glredbook10.txt @@ -0,0 +1 @@ +Main-Class: glredbook10.simple
\ No newline at end of file diff --git a/src/redbook/src/glredbook10/jfont.java b/src/redbook/src/glredbook10/jfont.java new file mode 100644 index 0000000..f3b9b26 --- /dev/null +++ b/src/redbook/src/glredbook10/jfont.java @@ -0,0 +1,160 @@ +package glredbook10;
+
+/**
+ * Draws some text in using GlyphVector.<br>
+ * This example is my replacement for xfont.c.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+import java.awt.*;
+import java.awt.event.*;
+import java.awt.font.GlyphVector;
+import java.awt.geom.PathIterator;
+
+import javax.swing.*;
+
+
+import javax.media.opengl.*;
+
+import javax.media.opengl.awt.GLJPanel;
+
+public class jfont //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ private JFrame frame;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ jfont demo = new jfont();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("jfont");
+ frame.setSize(500, 500);
+ demo.setJFrame(frame);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void setJFrame(JFrame frame) {
+ this.frame = frame;
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glEnable(GL2.GL_POLYGON_SMOOTH);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glPushMatrix();
+ gl.glTranslatef(frame.getWidth() / 2 - frame.getWidth() / 4, //
+ frame.getHeight() / 2, 0);
+ // GL has lower left origin compare java's upper left
+ gl.glScalef(5, -5, 0);
+
+ gl.glColor3f(0, 0, 1);
+ drawString(gl, "OpenGL", true);
+
+ gl.glTranslatef(-20, -20, 0);
+ gl.glScalef(0.75f, 1, 0);
+ gl.glColor3f(1, 0, 0);
+ drawString(gl, "javax.media.opengl", false);
+
+ gl.glTranslatef(20, 40, 0);
+ drawString(gl, "OpenGL Everywhere", false);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0, (float) w, 0, (float) h, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void drawString(GL2 gl, String s, boolean drawBounds) {
+ Font font = new Font("Times", Font.PLAIN, 14);// getFont();
+ System.out.println(font.toString());
+ Graphics2D g2 = (Graphics2D) frame.getGraphics();
+
+ //FontMetrics fontInfo = g2.getFontMetrics(font);
+ GlyphVector gv = font.createGlyphVector(g2.getFontRenderContext(), s);
+ Shape shape = gv.getOutline();
+ // System.out.println(gv.toString());
+ PathIterator itor = shape.getPathIterator(null, 0.01f);// very fine
+ // grain
+ int it = 0;
+ float seg[] = new float[6];
+ if (drawBounds)
+ drawGlyphBounds(gl, shape.getBounds());
+ gl.glBegin(GL.GL_LINE_LOOP);
+ while (!itor.isDone()) {
+ System.out.println(++it + " " + seg[0] + " " + seg[1]);
+ itor.currentSegment(seg);
+ gl.glVertex2f(seg[0], seg[1]);
+ itor.next();
+ gl.glColor3d(Math.random(), Math.random(), Math.random());
+ }
+ gl.glEnd();
+ }
+
+ private void drawGlyphBounds(GL2 gl, Rectangle r) {
+ gl.glBegin(GL.GL_LINE_LOOP);
+ gl.glVertex2f(r.x, r.y);
+ gl.glVertex2f(r.x + r.width, r.y);
+ gl.glVertex2f(r.x + r.width, r.height + r.y);
+ gl.glVertex2f(r.x, r.y + r.height);
+ gl.glEnd();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/light.java b/src/redbook/src/glredbook10/light.java new file mode 100644 index 0000000..cbc5f09 --- /dev/null +++ b/src/redbook/src/glredbook10/light.java @@ -0,0 +1,124 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates the use of the OpenGL lighting model. A sphere
+ * is drawn using a grey material characteristic. A single light source
+ * illuminates the object.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class light//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ light demo = new light();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("light");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize material property, light source, lighting model, and depth
+ * buffer.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float mat_shininess[] = { 50.0f };
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_SMOOTH);
+ //
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+ //
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glColor4d(1, 0, 0, 0);
+ glut.glutSolidSphere(1.0, 20, 20);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h) //
+ gl.glOrtho(-1.5, 1.5,//
+ -1.5 * (float) h / (float) w,//
+ 1.5 * (float) h / (float) w,//
+ -10.0, 10.0);
+ else
+ gl.glOrtho(-1.5 * (float) w / (float) h, //
+ 1.5 * (float) w / (float) h, //
+ -1.5, 1.5, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/linelist.java b/src/redbook/src/glredbook10/linelist.java new file mode 100644 index 0000000..02f033e --- /dev/null +++ b/src/redbook/src/glredbook10/linelist.java @@ -0,0 +1,129 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates using display lists to call different line
+ * stipples.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class linelist
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private int offset;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ linelist demo = new linelist();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("linelist");
+ frame.setSize(400, 150);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ /* background to be cleared to black */
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+
+ offset = gl.glGenLists(3);
+ gl.glNewList(offset, GL2.GL_COMPILE);
+ gl.glDisable(GL2.GL_LINE_STIPPLE);
+ gl.glEndList();
+ gl.glNewList(offset + 1, GL2.GL_COMPILE);
+ gl.glEnable(GL2.GL_LINE_STIPPLE);
+ gl.glLineStipple(1, (short) 0x0F0F);
+ gl.glEndList();
+ gl.glNewList(offset + 2, GL2.GL_COMPILE);
+ gl.glEnable(GL2.GL_LINE_STIPPLE);
+ gl.glLineStipple(1, (short) 0x1111);
+ gl.glEndList();
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ /* draw all lines in white */
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+
+ gl.glCallList(offset);
+ drawOneLine(gl, 50.0f, 125.0f, 350.0f, 125.0f);
+ gl.glCallList(offset + 1);
+ drawOneLine(gl, 50.0f, 100.0f, 350.0f, 100.0f);
+ gl.glCallList(offset + 2);
+ drawOneLine(gl, 50.0f, 75.0f, 350.0f, 75.0f);
+ gl.glCallList(offset + 1);
+ drawOneLine(gl, 50.0f, 50.0f, 350.f, 50.0f);
+ gl.glCallList(offset);
+ drawOneLine(gl, 50.0f, 25.0f, 350.0f, 25.0f);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0, 400, 0, 200, -1, 1);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void drawOneLine(GL2 gl, float x1, float y1, float x2, float y2) {
+ gl.glBegin(GL.GL_LINES);
+ gl.glVertex2f(x1, y1);
+ gl.glVertex2f(x2, y2);
+ gl.glEnd();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/lines.java b/src/redbook/src/glredbook10/lines.java new file mode 100644 index 0000000..ecba228 --- /dev/null +++ b/src/redbook/src/glredbook10/lines.java @@ -0,0 +1,151 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates geometric primitives and their attributes.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class lines//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public void run() {
+ lines demo = new lines();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("lines");
+ frame.setSize(400, 150);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(drawable);
+ frame.setVisible(true);
+ drawable.requestFocusInWindow();
+ }
+
+ public static void main(String[] args) {
+ new lines().run();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ int i;
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ /* select white for all lines */
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+
+ /* in 1st row, 3 lines, each with a different stipple */
+ gl.glEnable(GL2.GL_LINE_STIPPLE);
+
+ gl.glLineStipple(1, (short) 0x0101); /* dotted */
+ drawOneLine(gl, 50.0f, 125.0f, 150.0f, 125.0f);
+ gl.glLineStipple(1, (short) 0x00FF); /* dashed */
+ drawOneLine(gl, 150.0f, 125.0f, 250.0f, 125.0f);
+ gl.glLineStipple(1, (short) 0x1C47); /* dash/dot/dash */
+ drawOneLine(gl, 250.0f, 125.0f, 350.0f, 125.0f);
+
+ /* in 2nd row, 3 wide lines, each with different stipple */
+ gl.glLineWidth(5.0f);
+ gl.glLineStipple(1, (short) 0x0101); /* dotted */
+ drawOneLine(gl, 50.0f, 100.0f, 150.0f, 100.f);
+ gl.glLineStipple(1, (short) 0x00FF); /* dashed */
+ drawOneLine(gl, 150.0f, 100.0f, 250.0f, 100.0f);
+ gl.glLineStipple(1, (short) 0x1C47); /* dash/dot/dash */
+ drawOneLine(gl, 250.0f, 100.0f, 350.0f, 100.0f);
+ gl.glLineWidth(1.0f);
+
+ /* in 3rd row, 6 lines, with dash/dot/dash stipple */
+ /* as part of a single connected line strip */
+ gl.glLineStipple(1, (short) 0x1C47); /* dash/dot/dash */
+ gl.glBegin(GL.GL_LINE_STRIP);
+ for (i = 0; i < 7; i++)
+ gl.glVertex2f(50.0f + ((float) i * 50.0f), 75.0f);
+ gl.glEnd();
+
+ /* in 4th row, 6 independent lines with same stipple */
+ for (i = 0; i < 6; i++) {
+ drawOneLine(gl, 50.0f + ((float) i * 50.0f), 50.0f,
+ 50.0f + ((float) (i + 1) * 50.0f), 50.0f);
+ }
+
+ /* in 5th row, 1 line, with dash/dot/dash stipple */
+ /* and a stipple repeat factor of 5 */
+ gl.glLineStipple(5, (short) 0x1C47); /* dash/dot/dash */
+ drawOneLine(gl, 50.0f, 25.0f, 350.0f, 25.0f);
+
+ gl.glDisable(GL2.GL_LINE_STIPPLE);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluOrtho2D(0.0, (double) w, 0.0, (double) h);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void drawOneLine(GL2 gl, float x1, float y1, float x2, float y2) {
+ gl.glBegin(GL.GL_LINES);
+
+ gl.glVertex2f((x1), (y1));
+ gl.glVertex2f((x2), (y2));
+ gl.glEnd();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/list.java b/src/redbook/src/glredbook10/list.java new file mode 100644 index 0000000..f91b8ec --- /dev/null +++ b/src/redbook/src/glredbook10/list.java @@ -0,0 +1,122 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+import javax.media.opengl.*; import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+
+/**
+ * This program demonstrates how to make and execute a display list. Note that
+ * attributes, such as current color and matrix, are changed.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class list//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private int listName;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ list demo = new list();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("list");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setSize(400, 50);
+ frame.setLocationRelativeTo(null);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ listName = gl.glGenLists(1);
+ gl.glNewList(listName, GL2.GL_COMPILE);
+ gl.glColor3f(1f, 0f, 0f);
+ gl.glBegin(GL2.GL_TRIANGLES);
+ gl.glVertex2f(0f, 0f);
+ gl.glVertex2f(1f, 0f);
+ gl.glVertex2f(0f, 1f);
+ gl.glEnd();
+ gl.glTranslatef(1.5f, 0f, 0f);
+ gl.glEndList();
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(0f, 1f, 0f);// has not affect
+ for (int i = 0; i < 10; i++)
+ gl.glCallList(listName);
+ drawLine(gl); // * is this line green? NO! where is the line drawn?
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ GLU glu = new GLU();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+
+ float aspect = 0f;
+ if (w <= h) {
+ aspect = (float) h / (float) w;
+ glu.gluOrtho2D(0.0, 2.0, -0.5 * aspect, 1.5 * aspect);
+ } else {
+ aspect = (float) w / (float) h;
+ glu.gluOrtho2D(0.0, 2.0 * aspect, -0.5, 1.5);
+ }
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void drawLine(GL2 gl) {
+ gl.glBegin(GL.GL_LINES);
+ gl.glVertex2f(0f, 0.5f);
+ gl.glVertex2f(15f, .05f);
+ gl.glEnd();
+ }
+
+ public void keyTyped(KeyEvent arg0) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent arg0) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook10/list2.java b/src/redbook/src/glredbook10/list2.java new file mode 100644 index 0000000..4203f95 --- /dev/null +++ b/src/redbook/src/glredbook10/list2.java @@ -0,0 +1,127 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*; import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+
+/**
+ * This program demonstrates glGenList() and glPushAttrib(). The matrix and
+ * color are restored, before the line is drawn.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class list2//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private int listName;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+ public static void main(String[] args) {
+ list2 demo = new list2();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("list2");
+ frame.setSize(400, 50);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ float color_vector[] = { 1.0f, 0.0f, 0.0f };
+
+ listName = gl.glGenLists(1);
+ gl.glNewList(listName, GL2.GL_COMPILE);
+ gl.glPushAttrib(GL2.GL_CURRENT_BIT);
+ gl.glColor3fv(color_vector, 0);
+ gl.glBegin(GL2.GL_TRIANGLES);
+ gl.glVertex2f(0.0f, 0.0f);
+ gl.glVertex2f(1.0f, 0.0f);
+ gl.glVertex2f(0.0f, 1.0f);
+ gl.glEnd();
+ gl.glTranslatef(1.5f, 0.0f, 0.0f);
+ gl.glPopAttrib();
+ gl.glEndList();
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float new_color[] = { 0.0f, 1.0f, 0.0f };
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3fv(new_color, 0);
+ gl.glPushMatrix();
+ for (int i = 0; i < 10; i++)
+ gl.glCallList(listName);
+ gl.glPopMatrix();
+ drawLine(gl);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ glu.gluOrtho2D(0.0, 2.0, -0.5 * (float) h / (float) w, 1.5
+ * (float) h / (float) w);
+ else
+ glu.gluOrtho2D(0.0, 2.0 * (float) w / (float) h, -0.5, 1.5);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void drawLine(GL2 gl) {
+ gl.glBegin(GL.GL_LINES);
+ gl.glVertex2f(0.0f, 0.5f);
+ gl.glVertex2f(15.0f, 0.5f);
+ gl.glEnd();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/maplight.java b/src/redbook/src/glredbook10/maplight.java new file mode 100644 index 0000000..2563420 --- /dev/null +++ b/src/redbook/src/glredbook10/maplight.java @@ -0,0 +1,123 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates the use of the GL lighting model. A sphere is drawn
+ * using a magenta diffuse reflective and white specular material property. A
+ * single light source illuminates the object. This program illustrates lighting
+ * in color map mode.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class maplight//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+ public static void main(String[] args) {
+ maplight demo = new maplight();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("maplight");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize material property, light source, and lighting model.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+ float mat_colormap[] = { 16.0f, 48.0f, 79.0f };
+ float mat_shininess[] = { 10.0f };
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_COLOR_INDEXES, mat_colormap, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ /*
+ * jogl does not implement color index mode index color code for (int i =
+ * 0; i < 32; i++) { // glut. auxSetOneColor (16 + i, 1.0 * (i/32.0),
+ * 0.0, 1.0 * (i/32.0)); // glut.auxSetOneColor (48 + i, 1.0, 1.0 *
+ * (i/32.0), 1.0); } gl.glClearIndex(0);
+ */
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-1.5, 1.5, -1.5 * (float) h / (float) w, 1.5 * (float) h
+ / (float) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-1.5 * (float) w / (float) h, 1.5 * (float) w
+ / (float) h, -1.5, 1.5, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/material.java b/src/redbook/src/glredbook10/material.java new file mode 100644 index 0000000..c333040 --- /dev/null +++ b/src/redbook/src/glredbook10/material.java @@ -0,0 +1,298 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates the use of the GL lighting model. Several objects
+ * are drawn using different material characteristics. A single light source
+ * illuminates the objects.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class material//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ material demo = new material();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("material");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setSize(512, 512);
+ frame.setLocationRelativeTo(null);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize z-buffer, projection matrix, light source, and lighting model.
+ * Do not specify a material property here.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float position[] = { 0.0f, 3.0f, 2.0f, 0.0f };
+ float lmodel_ambient[] = { 0.4f, 0.4f, 0.4f, 1.0f };
+ float local_view[] = { 0.0f };
+
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER, local_view, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+
+ gl.glClearColor(0.0f, 0.1f, 0.1f, 0.0f);
+ }
+
+ /*
+ * Draw twelve spheres in 3 rows with 4 columns. The spheres in the first
+ * row have materials with no ambient reflection. The second row has
+ * materials with significant ambient reflection. The third row has
+ * materials with colored ambient reflection. The first column has materials
+ * with blue, diffuse reflection only. The second column has blue diffuse
+ * reflection, as well as specular reflection with a low shininess exponent.
+ * The third column has blue diffuse reflection, as well as specular
+ * reflection with a high shininess exponent (a more concentrated
+ * highlight). The fourth column has materials which also include an
+ * emissive component. glTranslatef() is used to move spheres to their
+ * appropriate locations.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ GLUT glut = new GLUT();
+ //
+ float no_mat[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
+ float mat_ambient_color[] = { 0.8f, 0.8f, 0.2f, 1.0f };
+ float mat_diffuse[] = { 0.1f, 0.5f, 0.8f, 1.0f };
+ float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float no_shininess[] = { 0.0f };
+ float low_shininess[] = { 5.0f };
+ float high_shininess[] = { 100.0f };
+ float mat_emission[] = { 0.3f, 0.2f, 0.2f, 0.0f };
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ /*
+ * draw sphere in first row, first column diffuse reflection only; no
+ * ambient or specular
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(-3.75f, 3.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, no_mat, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, no_mat, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, no_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, no_mat, 0);
+ glut.glutSolidSphere(1.0, 20, 20);
+ gl.glPopMatrix();
+ /*
+ * draw sphere in first row, second column diffuse and specular
+ * reflection; low shininess; no ambient
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(-1.25f, 3.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, no_mat, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, low_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, no_mat, 0);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+ /*
+ * draw sphere in first row, third column diffuse and specular
+ * reflection; high shininess; no ambient
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(1.25f, 3.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, no_mat, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, high_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, no_mat, 0);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+ /*
+ * draw sphere in first row, fourth column diffuse reflection; emission;
+ * no ambient or specular reflection
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(3.75f, 3.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, no_mat, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, no_mat, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, no_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, mat_emission, 0);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+ /*
+ * draw sphere in second row, first column ambient and diffuse
+ * reflection; no specular
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(-3.75f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, no_mat, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, no_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, no_mat, 0);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+ /*
+ * draw sphere in second row, second column ambient, diffuse and
+ * specular reflection; low shininess
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(-1.25f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, low_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, no_mat, 0);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+ /*
+ * draw sphere in second row, third column ambient, diffuse and specular
+ * reflection; high shininess
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(1.25f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, high_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, no_mat, 0);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+ /*
+ * draw sphere in second row, fourth column ambient and diffuse
+ * reflection; emission; no specular
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(3.75f, 0.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, no_mat, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, no_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, mat_emission, 0);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+ /*
+ * draw sphere in third row, first column colored ambient and diffuse
+ * reflection; no specular
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(-3.75f, -3.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient_color, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, no_mat, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, no_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, no_mat, 0);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+ /*
+ * draw sphere in third row, second column colored ambient, diffuse and
+ * specular reflection; low shininess
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(-1.25f, -3.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient_color, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, low_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, no_mat, 0);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+ /*
+ * draw sphere in third row, third column colored ambient, diffuse and
+ * specular reflection; high shininess
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(1.25f, -3.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient_color, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, high_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, no_mat, 0);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+ /*
+ * draw sphere in third row, fourth column colored ambient and diffuse
+ * reflection; emission; no specular
+ */
+ gl.glPushMatrix();
+ gl.glTranslatef(3.75f, -3.0f, 0.0f);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient_color, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, no_mat, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, no_shininess, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_EMISSION, mat_emission, 0);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= (h * 2)) //
+ gl.glOrtho(-6.0, 6.0, -3.0 * ((float) h * 2) / (float) w, //
+ 3.0 * ((float) h * 2) / (float) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-6.0 * (float) w / ((float) h * 2), //
+ 6.0 * (float) w / ((float) h * 2), -3.0, 3.0, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent arg0) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent arg0) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook10/mipmap.java b/src/redbook/src/glredbook10/mipmap.java new file mode 100644 index 0000000..b051e11 --- /dev/null +++ b/src/redbook/src/glredbook10/mipmap.java @@ -0,0 +1,238 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import java.nio.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates using mipmaps for texture maps. To overtly show the
+ * effect of mipmaps, each mipmap reduction level has a solidly colored,
+ * contrasting texture image. Thus, the quadrilateral which is drawn is drawn
+ * with several different colors.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class mipmap//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private static final int color = 3;
+ // private byte mipmapImage32[][][] = new byte[32][32][color];
+ // private byte mipmapImage16[][][] = new byte[16][16][color];
+ // private byte mipmapImage8[][][] = new byte[8][8][color];
+ // private byte mipmapImage4[][][] = new byte[4][4][color];
+ // private byte mipmapImage2[][][] = new byte[2][2][color];
+ // private byte mipmapImage1[][][] = new byte[1][1][color];
+ private ByteBuffer mipmapImage32Buf = BufferUtil
+ .newByteBuffer(32 * 32 * color);
+ private ByteBuffer mipmapImage16Buf = BufferUtil
+ .newByteBuffer(16 * 16 * color);
+ private ByteBuffer mipmapImage8Buf = BufferUtil
+ .newByteBuffer(8 * 8 * color);
+ private ByteBuffer mipmapImage4Buf = BufferUtil
+ .newByteBuffer(4 * 4 * color);
+ private ByteBuffer mipmapImage2Buf = BufferUtil
+ .newByteBuffer(2 * 2 * color);
+ private ByteBuffer mipmapImage1Buf = BufferUtil
+ .newByteBuffer(1 * 1 * color);
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ mipmap demo = new mipmap();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("mipmap");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glShadeModel(GL2.GL_FLAT);
+
+ gl.glTranslatef(0.0f, 0.0f, -3.6f);
+
+ makeImages();
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, 3, 32, 32, 0, GL2.GL_RGB,
+ GL.GL_UNSIGNED_BYTE, mipmapImage32Buf);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 1, 3, 16, 16, 0, GL2.GL_RGB,
+ GL.GL_UNSIGNED_BYTE, mipmapImage16Buf);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 2, 3, 8, 8, 0, GL2.GL_RGB,
+ GL.GL_UNSIGNED_BYTE, mipmapImage8Buf);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 3, 3, 4, 4, 0, GL2.GL_RGB,
+ GL.GL_UNSIGNED_BYTE, mipmapImage4Buf);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 4, 3, 2, 2, 0, GL2.GL_RGB,
+ GL.GL_UNSIGNED_BYTE, mipmapImage2Buf);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 5, 3, 1, 1, 0, GL2.GL_RGB,
+ GL.GL_UNSIGNED_BYTE, mipmapImage1Buf);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
+ gl.glTexParameterf(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST_MIPMAP_NEAREST);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, //
+ GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2f(0.0f, 0.0f);
+ gl.glVertex3f(-2.0f, -1.0f, 0.0f);
+ gl.glTexCoord2f(0.0f, 8.0f);
+ gl.glVertex3f(-2.0f, 1.0f, 0.0f);
+ gl.glTexCoord2f(8.0f, 8.0f);
+ gl.glVertex3f(2000.0f, 1.0f, -6000.0f);
+ gl.glTexCoord2f(8.0f, 0.0f);
+ gl.glVertex3f(2000.0f, -1.0f, -6000.0f);
+ gl.glEnd();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(60.0, 1.0 * (float) w / (float) h, 1.0, 30000.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * 3D arrays are never used by gl command TexImage2D. it instead use byte
+ * buffer.
+ */
+ private void makeImages() {
+ int i, j;
+
+ for (i = 0; i < 32; i++) {
+ for (j = 0; j < 32; j++) {
+ // mipmapImage32[i][j][0] = (byte) 255;
+ // mipmapImage32[i][j][1] = (byte) 255;
+ // mipmapImage32[i][j][2] = (byte) 0;
+ //
+ mipmapImage32Buf.put((byte) 255);
+ mipmapImage32Buf.put((byte) 255);
+ mipmapImage32Buf.put((byte) 0);
+ }
+ }
+ for (i = 0; i < 16; i++) {
+ for (j = 0; j < 16; j++) {
+ // mipmapImage16[i][j][0] = (byte) 255;
+ // mipmapImage16[i][j][1] = (byte) 0;
+ // mipmapImage16[i][j][2] = (byte) 255;
+ //
+ mipmapImage16Buf.put((byte) 255);
+ mipmapImage16Buf.put((byte) 0);
+ mipmapImage16Buf.put((byte) 255);
+ }
+ }
+ for (i = 0; i < 8; i++) {
+ for (j = 0; j < 8; j++) {
+ // mipmapImage8[i][j][0] = (byte) 255;
+ // mipmapImage8[i][j][1] = (byte) 0;
+ // mipmapImage8[i][j][2] = (byte) 0;
+ //
+ mipmapImage8Buf.put((byte) 255);
+ mipmapImage8Buf.put((byte) 0);
+ mipmapImage8Buf.put((byte) 0);
+ }
+ }
+ for (i = 0; i < 4; i++) {
+ for (j = 0; j < 4; j++) {
+ // mipmapImage4[i][j][0] = (byte) 0;
+ // mipmapImage4[i][j][1] = (byte) 255;
+ // mipmapImage4[i][j][2] = (byte) 0;
+ //
+ mipmapImage4Buf.put((byte) 0);
+ mipmapImage4Buf.put((byte) 255);
+ mipmapImage4Buf.put((byte) 0);
+ }
+ }
+ for (i = 0; i < 2; i++) {
+ for (j = 0; j < 2; j++) {
+ // mipmapImage2[i][j][0] = (byte) 0;
+ // mipmapImage2[i][j][1] = (byte) 0;
+ // mipmapImage2[i][j][2] = (byte) 255;
+ //
+ mipmapImage2Buf.put((byte) 0);
+ mipmapImage2Buf.put((byte) 0);
+ mipmapImage2Buf.put((byte) 255);
+ }
+ }
+ // mipmapImage1[0][0][0] = (byte) 255;
+ // mipmapImage1[0][0][1] = (byte) 255;
+ // mipmapImage1[0][0][2] = (byte) 255;
+ //
+ mipmapImage1Buf.put((byte) 255);
+ mipmapImage1Buf.put((byte) 255);
+ mipmapImage1Buf.put((byte) 255);
+ // rewind all
+ mipmapImage32Buf.rewind();
+ mipmapImage16Buf.rewind();
+ mipmapImage8Buf.rewind();
+ mipmapImage4Buf.rewind();
+ mipmapImage2Buf.rewind();
+ mipmapImage1Buf.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/model.java b/src/redbook/src/glredbook10/model.java new file mode 100644 index 0000000..8ca110e --- /dev/null +++ b/src/redbook/src/glredbook10/model.java @@ -0,0 +1,131 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates the use of OpenGL modeling transformations. Four
+ * triangles are drawn, each with a different transformation.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class model//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ model demo = new model();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("model");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClearColor(0f, 0f, 0f, 0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ /*
+ * Clear the screen. For each triangle, set the current color and modify the
+ * modelview matrix.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1f, 1f, 1f);
+ //
+ gl.glLoadIdentity();
+ drawTriangle(gl);
+ //
+ gl.glEnable(GL2.GL_LINE_STIPPLE);
+ gl.glLineStipple(1, (short) 0xf0f0);
+ gl.glLoadIdentity();
+ gl.glTranslatef(-20f, 0f, 0f);
+ drawTriangle(gl);
+ gl.glLineStipple(1, (short) 0xF00F);
+ gl.glLoadIdentity();
+ gl.glScalef(1.5f, 0.5f, 1.0f);
+ drawTriangle(gl);
+ gl.glLineStipple(1, (short) 0x8888);
+ gl.glLoadIdentity();
+ gl.glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
+ drawTriangle(gl);
+ gl.glDisable(GL2.GL_LINE_STIPPLE);
+ //
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-50.0, 50.0, -50.0 * (float) h / (float) w, 50.0
+ * (float) h / (float) w, -1.0, 1.0);
+ else
+ gl.glOrtho(-50.0 * (float) w / (float) h, 50.0 * (float) w
+ / (float) h, -50.0, 50.0, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void drawTriangle(GL2 gl) {
+ gl.glBegin(GL.GL_LINE_LOOP);
+ gl.glVertex2f(0.0f, 25.0f);
+ gl.glVertex2f(25.0f, -25.0f);
+ gl.glVertex2f(-25.0f, -25.0f);
+ gl.glEnd();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ // TODO Auto-generated method stub
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ // TODO Auto-generated method stub
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/movelight.java b/src/redbook/src/glredbook10/movelight.java new file mode 100644 index 0000000..c8de5bc --- /dev/null +++ b/src/redbook/src/glredbook10/movelight.java @@ -0,0 +1,164 @@ +package glredbook10;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.gl2.GLUT;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates when to issue lighting and transformation commands
+ * to render a model with a light which is moved by a modeling transformation
+ * (rotate or translate). The light position is reset after the modeling
+ * transformation is called. The eye position does not change. <br>
+ * <br>
+ * A sphere is drawn using a grey material characteristic. A single light source
+ * illuminates the object. <br>
+ * <br>
+ * Interaction: pressing the left or middle mouse button alters the modeling
+ * transformation (x rotation) by 30 degrees. The scene is then redrawn with the
+ * light in a new position.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class movelight//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ private GLUT glut;
+
+ private static int spin = 0;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ movelight demo = new movelight();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("movelight");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ }
+
+ /*
+ * Here is where the light position is reset after the modeling
+ * transformation (glRotated) is called. This places the light at a new
+ * position in world coordinates. The cube represents the position of the
+ * light.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float position[] = { 0.0f, 0.0f, 1.5f, 1.0f };
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, 0.0f, -5.0f);
+
+ gl.glPushMatrix();
+ gl.glRotated((double) spin, 1.0, 0.0, 0.0);
+ gl.glRotated(0.0, 1.0, 0.0, 0.0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
+
+ gl.glTranslated(0.0, 0.0, 1.5);
+ gl.glDisable(GL2.GL_LIGHTING);
+ gl.glColor3f(0.0f, 1.0f, 1.0f);
+ glut.glutWireCube(0.1f);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glPopMatrix();
+
+ glut.glutSolidTorus(0.275f, 0.85f, 20, 20);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(40.0, (float) w / (float) h, 1.0, 20.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void move_light() {
+ spin = (spin + 30) % 360;
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ char ch = key.getKeyChar();
+ switch (ch) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent mouse) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ if (mouse.getButton() == MouseEvent.BUTTON1) //
+ move_light();
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/pickdepth.java b/src/redbook/src/glredbook10/pickdepth.java new file mode 100644 index 0000000..9e2f78e --- /dev/null +++ b/src/redbook/src/glredbook10/pickdepth.java @@ -0,0 +1,217 @@ +package glredbook10;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+
+import java.nio.*;
+
+import javax.media.opengl.*;
+
+import com.sun.opengl.util.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * Picking is demonstrated in this program. In rendering mode, three overlapping
+ * rectangles are drawn. When the left mouse button is pressed, selection mode
+ * is entered with the picking matrix. Rectangles which are drawn under the
+ * cursor position are "picked." Pay special attention to the depth value range,
+ * which is returned.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class pickdepth//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ private static final int BUFSIZE = 512;
+ private Point pickPoint = new Point();
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ pickdepth demo = new pickdepth();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("pickdepth");
+ frame.setSize(100, 100);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glDepthRange(0.0, 1.0); /* The default z mapping */
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ pickRects(gl);
+ drawRects(gl, GL2.GL_RENDER);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * The three rectangles are drawn. In selection mode, each rectangle is
+ * given the same name. Note that each rectangle is drawn with a different z
+ * value.
+ */
+ private void drawRects(GL2 gl, int mode) {
+ if (mode == GL2.GL_SELECT)
+ gl.glLoadName(1);
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glColor3f(1.0f, 1.0f, 0.0f);
+ gl.glVertex3i(2, 0, 0);
+ gl.glVertex3i(2, 6, 0);
+ gl.glVertex3i(6, 6, 0);
+ gl.glVertex3i(6, 0, 0);
+ gl.glColor3f(0.0f, 1.0f, 1.0f);
+ gl.glVertex3i(3, 2, -1);
+ gl.glVertex3i(3, 8, -1);
+ gl.glVertex3i(8, 8, -1);
+ gl.glVertex3i(8, 2, -1);
+ gl.glColor3f(1.0f, 0.0f, 1.0f);
+ gl.glVertex3i(0, 2, -2);
+ gl.glVertex3i(0, 7, -2);
+ gl.glVertex3i(5, 7, -2);
+ gl.glVertex3i(5, 2, -2);
+ gl.glEnd();
+ }
+
+ /*
+ * prints out the contents of the selection array.
+ */
+ private void processHits(int hits, int buffer[]) {
+ int names, ptr = 0;
+
+ System.out.println("hits = " + hits);
+ // ptr = (GLuint *) buffer;
+ for (int i = 0; i < hits; i++) { /* for each hit */
+ names = buffer[ptr];
+ System.out.println(" number of names for hit = " + names);
+ ptr++;
+ System.out.println(" z1 is " + buffer[ptr]);
+ ptr++;
+ System.out.println(" z2 is " + buffer[ptr]);
+ ptr++;
+ System.out.print("\n the name is ");
+ for (int j = 0; j < names; j++) { /* for each name */
+ System.out.println("" + buffer[ptr]);
+ ptr++;
+ }
+ System.out.println();
+ }
+ }
+
+ /*
+ * sets up selection mode, name stack, and projection matrix for picking.
+ * Then the objects are drawn.
+ */
+ private void pickRects(GL2 gl) {
+ int[] selectBuf = new int[BUFSIZE];
+ IntBuffer selectBuffer = BufferUtil.newIntBuffer(BUFSIZE);
+ int hits;
+ int viewport[] = new int[4];
+ // int x, y;
+
+ gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
+
+ gl.glSelectBuffer(BUFSIZE, selectBuffer);
+ gl.glRenderMode(GL2.GL_SELECT);
+
+ gl.glInitNames();
+ gl.glPushName(-1);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glPushMatrix();
+ gl.glLoadIdentity();
+ /* create 5x5 pixel picking region near cursor location */
+ glu.gluPickMatrix((double) pickPoint.x,
+ (double) (viewport[3] - pickPoint.y), //
+ 5.0, 5.0, viewport, 0);
+ gl.glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
+ drawRects(gl, GL2.GL_SELECT);
+ gl.glPopMatrix();
+ gl.glFlush();
+
+ hits = gl.glRenderMode(GL2.GL_RENDER);
+ selectBuffer.get(selectBuf);
+ processHits(hits, selectBuf);
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent mouse) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ pickPoint = mouse.getPoint();
+
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook10/pickline.java b/src/redbook/src/glredbook10/pickline.java new file mode 100644 index 0000000..01cb613 --- /dev/null +++ b/src/redbook/src/glredbook10/pickline.java @@ -0,0 +1,182 @@ +package glredbook10;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+
+import java.nio.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * Picking is demonstrated in this program. In rendering mode, three overlapping
+ * rectangles are drawn. When the left mouse button is pressed, selection mode
+ * is entered with the picking matrix. Rectangles which are drawn under the
+ * cursor position are "picked." Pay special attention to the depth value range,
+ * which is returned.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class pickline//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ private static final int BUFSIZE = 512;
+ private Point pickPoint = new Point();
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ pickline demo = new pickline();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("pickline");
+ frame.setSize(100, 100);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ pickLine(gl);
+ drawLine(gl, GL2.GL_RENDER);
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluOrtho2D(0.0, (double) w, 0.0, (double) h);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void drawLine(GL2 gl, int mode) {
+ if (mode == GL2.GL_SELECT)
+ gl.glLoadName(1);
+ gl.glBegin(GL.GL_LINES);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glVertex3f(30.0f, 30.0f, 0.0f);
+ gl.glVertex3f(50.0f, 60.0f, 0.0f);
+ gl.glEnd();
+
+ if (mode == GL2.GL_SELECT)
+ gl.glLoadName(2);
+ gl.glBegin(GL.GL_LINES);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glVertex3f(50.0f, 60.0f, 0.0f);
+ gl.glVertex3f(70.0f, 40.0f, 0.0f);
+ gl.glEnd();
+ }
+
+ /*
+ * pickline() is called when the mouse is pressed. The projection matrix is
+ * reloaded to include the picking matrix. The line is "redrawn" during
+ * selection mode, and names are sent to the buffer.
+ */
+ private void pickLine(GL2 gl) {
+ int selectBuf[] = new int[BUFSIZE];
+ IntBuffer selectBuffer = BufferUtil.newIntBuffer(BUFSIZE);
+ int hits;
+ int viewport[] = new int[4];
+
+ gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
+
+ gl.glSelectBuffer(BUFSIZE, selectBuffer);
+ gl.glRenderMode(GL2.GL_SELECT);
+
+ gl.glInitNames();
+ gl.glPushName(-1);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glPushMatrix();
+ gl.glLoadIdentity();
+ glu.gluPickMatrix((double) pickPoint.x,
+ (double) (viewport[3] - pickPoint.y),//
+ 5.0, 5.0, viewport, 0);
+ glu.gluOrtho2D(0.0, (double) viewport[2], //
+ 0.0, (double) viewport[3]);
+ drawLine(gl, GL2.GL_SELECT);
+ gl.glPopMatrix();
+ gl.glFlush();
+
+ hits = gl.glRenderMode(GL2.GL_RENDER);
+ selectBuffer.get(selectBuf);
+ System.out.println("hits is " + hits);
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent mouse) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ pickPoint = mouse.getPoint();
+
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook10/picksquare.java b/src/redbook/src/glredbook10/picksquare.java new file mode 100644 index 0000000..7881ca0 --- /dev/null +++ b/src/redbook/src/glredbook10/picksquare.java @@ -0,0 +1,229 @@ +package glredbook10;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+import java.nio.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * Use of multiple names and picking are demonstrated. A 3x3 grid of squares is
+ * drawn. When the left mouse button is pressed, all squares under the cursor
+ * position have their color changed.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class picksquare//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ //
+ private int board[][] = new int[3][3]; /* amount of color for each square */
+ private static final int BUFSIZE = 512;
+ private Point pickPoint;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+ public static void main(String[] args) {
+
+ picksquare demo = new picksquare();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("picksquare");
+ frame.setSize(512, 256);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /* Clear color value for every square on the board */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ int i, j;
+ for (i = 0; i < 3; i++)
+ for (j = 0; j < 3; j++)
+ board[i][j] = 0;
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ if (pickPoint != null)
+ pickSquares(gl);
+
+ drawSquares(gl, GL2.GL_RENDER);
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluOrtho2D(0.0, 3.0, 0.0, 3.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+
+ }
+
+ public void displayChanged(GLAutoDrawable drawable,//
+ boolean modeChanged, boolean deviceChanged) {
+ }
+
+ /*
+ * The nine squares are drawn. In selection mode, each square is given two
+ * names: one for the row and the other for the column on the grid. The
+ * color of each square is determined by its position on the grid, and the
+ * value in the board[][] array.
+ */
+ private void drawSquares(GL2 gl, int mode) {
+ int i, j;
+ for (i = 0; i < 3; i++) {
+ if (mode == GL2.GL_SELECT)
+ gl.glLoadName(i);
+ for (j = 0; j < 3; j++) {
+ if (mode == GL2.GL_SELECT)
+ gl.glPushName(j);
+ gl.glColor3f((float) i / 3.0f, (float) j / 3.0f,
+ (float) board[i][j] / 3.0f);
+ gl.glRecti(i, j, i + 1, j + 1);
+ if (mode == GL2.GL_SELECT)
+ gl.glPopName();
+ }
+ }
+ }
+
+ /*
+ * processHits prints out the contents of the selection array.
+ */
+ private void processHits(int hits, int buffer[]) {
+ int i, j;
+ int ii = 0, jj = 0, names, ptr = 0;
+
+ System.out.println("hits = " + hits);
+ // ptr = (GLuint *) buffer;
+ for (i = 0; i < hits; i++) { /* for each hit */
+ names = buffer[ptr];
+ System.out.println(" number of names for this hit = " + names);
+ ptr++;
+ System.out.println(" z1 is " + (float) buffer[ptr] / 0x7fffffff);
+ ptr++;
+ System.out.println(" z2 is " + (float) buffer[ptr] / 0x7fffffff);
+ ptr++;
+ System.out.println(" names are ");
+ for (j = 0; j < names; j++) { /* for each name */
+ System.out.println("" + buffer[ptr]);
+ if (j == 0) /* set row and column */
+ ii = buffer[ptr];
+ else if (j == 1)
+ jj = buffer[ptr];
+ ptr++;
+ }
+ System.out.println("\n");
+ board[ii][jj] = (board[ii][jj] + 1) % 3;
+ }
+ }
+
+ /*
+ * pickSquares() sets up selection mode, name stack, and projection matrix
+ * for picking. Then the objects are drawn.
+ */
+ // private void pickSquares(GL2 gl, int button, int state, int x, int y)
+ private void pickSquares(GL2 gl) {
+ int selectBuf[] = new int[BUFSIZE];
+ IntBuffer selectBuffer = BufferUtil.newIntBuffer(BUFSIZE);
+ int hits;
+ int viewport[] = new int[4];
+
+ // if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return;
+
+ gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
+
+ gl.glSelectBuffer(BUFSIZE, selectBuffer);
+ gl.glRenderMode(GL2.GL_SELECT);
+
+ gl.glInitNames();
+ gl.glPushName(0);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glPushMatrix();
+ gl.glLoadIdentity();
+ /* create 5x5 pixel picking region near cursor location */
+ glu.gluPickMatrix((double) pickPoint.x,
+ (double) (viewport[3] - pickPoint.y),//
+ 5.0, 5.0, viewport, 0);
+ glu.gluOrtho2D(0.0, 3.0, 0.0, 3.0);
+ drawSquares(gl, GL2.GL_SELECT);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glPopMatrix();
+ gl.glFlush();
+
+ hits = gl.glRenderMode(GL2.GL_RENDER);
+ selectBuffer.get(selectBuf);
+ processHits(hits, selectBuf);
+ }
+
+ public void keyTyped(KeyEvent key) {
+
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent arg0) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ if (mouse.getButton() == MouseEvent.BUTTON1) //
+ {
+ pickPoint = mouse.getPoint();
+ super.refresh();
+ }
+ }
+
+ public void mouseReleased(MouseEvent arg0) {
+ }
+
+ public void mouseEntered(MouseEvent arg0) {
+ }
+
+ public void mouseExited(MouseEvent arg0) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/plane.java b/src/redbook/src/glredbook10/plane.java new file mode 100644 index 0000000..b8a9f46 --- /dev/null +++ b/src/redbook/src/glredbook10/plane.java @@ -0,0 +1,164 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates the use of local versus infinite lighting on a flat
+ * plane.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class plane//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ plane demo = new plane();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("plane");
+ frame.setSize(500, 200);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize material property, light source, and lighting model.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float mat_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ /* mat_specular and mat_shininess are NOT default values */
+ float mat_diffuse[] = { 0.4f, 0.4f, 0.4f, 1.0f };
+ float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float mat_shininess[] = { 15.0f };
+
+ float light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float lmodel_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float infinite_light[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+ float local_light[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-1.5f, 0.0f, 0.0f);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, infinite_light, 0);
+ drawPlane(gl);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(1.5f, 0.0f, 0.0f);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, local_light, 0);
+ drawPlane(gl);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-1.5, 1.5, -1.5 * (double) h / (double) w, 1.5
+ * (double) h / (double) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-1.5 * (double) w / (double) h, //
+ 1.5 * (double) w / (double) h, -1.5, 1.5, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void drawPlane(GL2 gl) {
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glNormal3f(0.0f, 0.0f, 1.0f);
+ gl.glVertex3f(-1.0f, -1.0f, 0.0f);
+ gl.glVertex3f(0.0f, -1.0f, 0.0f);
+ gl.glVertex3f(0.0f, 0.0f, 0.0f);
+ gl.glVertex3f(-1.0f, 0.0f, 0.0f);
+
+ gl.glNormal3f(0.0f, 0.0f, 1.0f);
+ gl.glVertex3f(0.0f, -1.0f, 0.0f);
+ gl.glVertex3f(1.0f, -1.0f, 0.0f);
+ gl.glVertex3f(1.0f, 0.0f, 0.0f);
+ gl.glVertex3f(0.0f, 0.0f, 0.0f);
+
+ gl.glNormal3f(0.0f, 0.0f, 1.0f);
+ gl.glVertex3f(0.0f, 0.0f, 0.0f);
+ gl.glVertex3f(1.0f, 0.0f, 0.0f);
+ gl.glVertex3f(1.0f, 1.0f, 0.0f);
+ gl.glVertex3f(0.0f, 1.0f, 0.0f);
+
+ gl.glNormal3f(0.0f, 0.0f, 1.0f);
+ gl.glVertex3f(0.0f, 0.0f, 0.0f);
+ gl.glVertex3f(0.0f, 1.0f, 0.0f);
+ gl.glVertex3f(-1.0f, 1.0f, 0.0f);
+ gl.glVertex3f(-1.0f, 0.0f, 0.0f);
+ gl.glEnd();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook10/planet.java b/src/redbook/src/glredbook10/planet.java new file mode 100644 index 0000000..3938ce9 --- /dev/null +++ b/src/redbook/src/glredbook10/planet.java @@ -0,0 +1,146 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program shows how to composite modeling transformations to draw
+ * translated and rotated models. Interaction: pressing the d and y keys (day
+ * and year) alters the rotation of the planet around the sun.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class planet//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+ private int year = 0, day = 0;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ planet demo = new planet();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("planet");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+
+ /* draw sun */
+ gl.glPushMatrix();
+ glut.glutWireSphere(1.0, 20, 16);
+ /* draw smaller planet */
+ gl.glRotatef((float) year, 0.0f, 1.0f, 0.0f);
+ gl.glTranslatef(2.0f, 0.0f, 0.0f);
+ gl.glRotatef((float) day, 0.0f, 1.0f, 0.0f);
+ glut.glutWireSphere(0.2, 10, 10);
+ gl.glPopMatrix();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 20.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslatef(0.0f, 0.0f, -5.0f);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ case KeyEvent.VK_LEFT:
+ case KeyEvent.VK_A:
+ yearSubtract();
+ break;
+ case KeyEvent.VK_RIGHT:
+ case KeyEvent.VK_D:
+ yearAdd();
+ break;
+ case KeyEvent.VK_UP:
+ case KeyEvent.VK_W:
+ dayAdd();
+ break;
+ case KeyEvent.VK_DOWN:
+ case KeyEvent.VK_S:
+ daySubtract();
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ private void dayAdd() {
+ day = (day + 10) % 360;
+ }
+
+ private void daySubtract() {
+ day = (day - 10) % 360;
+ }
+
+ private void yearAdd() {
+ year = (year + 5) % 360;
+ }
+
+ private void yearSubtract() {
+ year = (year - 5) % 360;
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/planetup.java b/src/redbook/src/glredbook10/planetup.java new file mode 100644 index 0000000..ceecf16 --- /dev/null +++ b/src/redbook/src/glredbook10/planetup.java @@ -0,0 +1,149 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * The planets (from planet.c) have been rotated so their polar regions are
+ * north/south. Interaction: pressing the left, right, up, and down arrow keys
+ * alters the rotation of the planet around the sun.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class planetup//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+ private static int year = 0, day = 0;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ planetup demo = new planetup();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("planetup");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glPushMatrix();
+ /* draw sun */
+ gl.glPushMatrix();
+ gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f); /* rotate it upright */
+ glut.glutWireSphere(1.0f, 10, 10);
+ gl.glPopMatrix();
+ /* draw smaller planet */
+ gl.glRotatef((float) year, 0.0f, 1.0f, 0.0f);
+ gl.glTranslatef(2.0f, 0.0f, 0.0f);
+ gl.glRotatef((float) day, 0.0f, 1.0f, 0.0f);
+ gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f); /* rotate it upright */
+ glut.glutWireSphere(0.2f, 10, 10);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 20.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslatef(0.0f, 0.0f, -5.0f);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void dayAdd() {
+ day = (day + 10) % 360;
+ }
+
+ private void daySubtract() {
+ day = (day - 10) % 360;
+ }
+
+ private void yearAdd() {
+ year = (year + 5) % 360;
+ }
+
+ private void yearSubtract() {
+ year = (year - 5) % 360;
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ case KeyEvent.VK_LEFT:
+ case KeyEvent.VK_A:
+ yearSubtract();
+ break;
+ case KeyEvent.VK_RIGHT:
+ case KeyEvent.VK_D:
+ yearAdd();
+ break;
+ case KeyEvent.VK_UP:
+ case KeyEvent.VK_W:
+ dayAdd();
+ break;
+ case KeyEvent.VK_DOWN:
+ case KeyEvent.VK_S:
+ daySubtract();
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/polys.java b/src/redbook/src/glredbook10/polys.java new file mode 100644 index 0000000..1bcd723 --- /dev/null +++ b/src/redbook/src/glredbook10/polys.java @@ -0,0 +1,160 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+
+/**
+ * This program demonstrates polygon stippling.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes (Java conversin)
+ */
+public class polys//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private byte fly[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+ (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03,
+ (byte) 0x80, (byte) 0x01, (byte) 0xC0, (byte) 0x06, (byte) 0xC0,
+ (byte) 0x03, (byte) 0x60, (byte) 0x04, (byte) 0x60, (byte) 0x06,
+ (byte) 0x20, (byte) 0x04, (byte) 0x30, (byte) 0x0C, (byte) 0x20,
+ (byte) 0x04, (byte) 0x18, (byte) 0x18, (byte) 0x20, (byte) 0x04,
+ (byte) 0x0C, (byte) 0x30, (byte) 0x20, (byte) 0x04, (byte) 0x06,
+ (byte) 0x60, (byte) 0x20, (byte) 0x44, (byte) 0x03, (byte) 0xC0,
+ (byte) 0x22, (byte) 0x44, (byte) 0x01, (byte) 0x80, (byte) 0x22,
+ (byte) 0x44, (byte) 0x01, (byte) 0x80, (byte) 0x22, (byte) 0x44,
+ (byte) 0x01, (byte) 0x80, (byte) 0x22, (byte) 0x44, (byte) 0x01,
+ (byte) 0x80, (byte) 0x22, (byte) 0x44, (byte) 0x01, (byte) 0x80,
+ (byte) 0x22, (byte) 0x44, (byte) 0x01, (byte) 0x80, (byte) 0x22,
+ (byte) 0x66, (byte) 0x01, (byte) 0x80, (byte) 0x66, (byte) 0x33,
+ (byte) 0x01, (byte) 0x80, (byte) 0xCC, (byte) 0x19, (byte) 0x81,
+ (byte) 0x81, (byte) 0x98, (byte) 0x0C, (byte) 0xC1, (byte) 0x83,
+ (byte) 0x30, (byte) 0x07, (byte) 0xe1, (byte) 0x87, (byte) 0xe0,
+ (byte) 0x03, (byte) 0x3f, (byte) 0xfc, (byte) 0xc0, (byte) 0x03,
+ (byte) 0x31, (byte) 0x8c, (byte) 0xc0, (byte) 0x03, (byte) 0x33,
+ (byte) 0xcc, (byte) 0xc0, (byte) 0x06, (byte) 0x64, (byte) 0x26,
+ (byte) 0x60, (byte) 0x0c, (byte) 0xcc, (byte) 0x33, (byte) 0x30,
+ (byte) 0x18, (byte) 0xcc, (byte) 0x33, (byte) 0x18, (byte) 0x10,
+ (byte) 0xc4, (byte) 0x23, (byte) 0x08, (byte) 0x10, (byte) 0x63,
+ (byte) 0xC6, (byte) 0x08, (byte) 0x10, (byte) 0x30, (byte) 0x0c,
+ (byte) 0x08, (byte) 0x10, (byte) 0x18, (byte) 0x18, (byte) 0x08,
+ (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x08 };
+
+ private byte halftone[] = { (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x55,
+ (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0x55, (byte) 0x55, (byte) 0x55,
+ (byte) 0x55, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x55, (byte) 0x55,
+ (byte) 0x55, (byte) 0x55, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x55,
+ (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0x55, (byte) 0x55, (byte) 0x55,
+ (byte) 0x55, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x55, (byte) 0x55,
+ (byte) 0x55, (byte) 0x55, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x55,
+ (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0x55, (byte) 0x55, (byte) 0x55,
+ (byte) 0x55, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x55, (byte) 0x55,
+ (byte) 0x55, (byte) 0x55, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,
+ (byte) 0xAA, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55 };
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ polys demo = new polys();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("polys");
+ frame.setSize(350, 150);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+
+ /* draw one solid, unstippled rectangle, */
+ /* then two stippled rectangles */
+ gl.glRectf(25.0f, 25.0f, 125.0f, 125.0f);
+ gl.glEnable(GL2.GL_POLYGON_STIPPLE);
+ gl.glPolygonStipple(fly, 0);
+ gl.glRectf(125.0f, 25.0f, 225.0f, 125.0f);
+ gl.glPolygonStipple(halftone, 0);
+ gl.glRectf(225.0f, 25.0f, 325.0f, 125.0f);
+ gl.glDisable(GL2.GL_POLYGON_STIPPLE);
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluOrtho2D(0.0, (double) w, 0.0, (double) h);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/robot.java b/src/redbook/src/glredbook10/robot.java new file mode 100644 index 0000000..2575468 --- /dev/null +++ b/src/redbook/src/glredbook10/robot.java @@ -0,0 +1,163 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+
+import javax.swing.*;
+
+import javax.media.opengl.*;
+
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program shows how to composite modeling transformations to draw
+ * translated and rotated hierarchical models. Interaction: pressing the s
+ * and e keys (shoulder and elbow) alters the rotation of the robot arm.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class robot//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+ private static int shoulder = 0, elbow = 0;
+
+ //
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public void run() {
+ robot demo = new robot();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("robot");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public static void main(String[] args) {
+ new robot().run();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-1.0f, 0.0f, 0.0f);
+ gl.glRotatef((float) shoulder, 0.0f, 0.0f, 1.0f);
+ gl.glTranslatef(1.0f, 0.0f, 0.0f);
+ // gl.glPushMatrix();
+ gl.glScalef(2.0f, 0.4f, 1.0f);
+ glut.glutWireCube(1.0f);
+ // gl.glPopMatrix();
+
+ gl.glTranslatef(1.0f, 0.0f, 0.0f);
+ gl.glRotatef((float) elbow, 0.0f, 0.0f, 1.0f);
+ gl.glTranslatef(1.0f, 0.0f, 0.0f);
+ // gl.glPushMatrix();
+ gl.glScalef(2.0f, 0.4f, 1.0f);
+ glut.glutWireCube(1.0f);
+ // gl.glPopMatrix();
+
+ gl.glPopMatrix();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(65.0, (float) w / (float) h, 1.0, 20.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslatef(0.0f, 0.0f, -5.0f);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void elbowAdd() {
+ elbow = (elbow + 5) % 360;
+ }
+
+ private void elbowSubtract() {
+ elbow = (elbow - 5) % 360;
+ }
+
+ private void shoulderAdd() {
+ shoulder = (shoulder + 5) % 360;
+ }
+
+ private void shoulderSubtract() {
+ shoulder = (shoulder - 5) % 360;
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ case KeyEvent.VK_LEFT:
+ case KeyEvent.VK_A:
+ shoulderSubtract();
+ break;
+ case KeyEvent.VK_RIGHT:
+ case KeyEvent.VK_D:
+ shoulderAdd();
+ break;
+ case KeyEvent.VK_UP:
+ case KeyEvent.VK_W:
+ elbowAdd();
+ break;
+ case KeyEvent.VK_DOWN:
+ case KeyEvent.VK_S:
+ elbowSubtract();
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/sccolorlight.java b/src/redbook/src/glredbook10/sccolorlight.java new file mode 100644 index 0000000..59ac7ba --- /dev/null +++ b/src/redbook/src/glredbook10/sccolorlight.java @@ -0,0 +1,138 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates the use of a colored (magenta, in this example)
+ * light source. Objects are drawn using a grey material characteristic. A
+ * single light source illuminates the objects.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class sccolorlight//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ sccolorlight demo = new sccolorlight();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("sccolorlight");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize material property and light source.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float light_diffuse[] = { 1.0f, 0.0f, 1.0f, 1.0f };
+ float light_specular[] = { 1.0f, 0.0f, 1.0f, 1.0f };
+ /* light_position is NOT default value */
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glPushMatrix();
+ gl.glRotatef(20.0f, 1.0f, 0.0f, 0.0f);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.75f, 0.5f, 0.0f);
+ gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
+
+ glut.glutSolidTorus(0.275f, 0.85f, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.75f, -0.5f, 0.0f);
+ gl.glRotatef(270.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutSolidCone(1.0f, 2.0f, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.75f, 0.0f, -1.0f);
+ glut.glutSolidSphere(1.0f, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-2.5, 2.5, -2.5 * (float) h / (float) w, 2.5 * (float) h
+ / (float) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-2.5 * (float) w / (float) h, 2.5 * (float) w
+ / (float) h, -2.5, 2.5, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/scene.java b/src/redbook/src/glredbook10/scene.java new file mode 100644 index 0000000..03392bf --- /dev/null +++ b/src/redbook/src/glredbook10/scene.java @@ -0,0 +1,140 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates the use of the GL lighting model. Objects are drawn
+ * using a grey material characteristic. A single light source illuminates the
+ * objects.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class scene//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ scene demo = new scene();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("scene");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize material property and light source.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ /* light_position is NOT default value */
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glPushMatrix();
+ gl.glRotatef(20.0f, 1.0f, 0.0f, 0.0f);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.75f, 0.5f, 0.0f);
+ gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutSolidTorus(0.275, 0.85, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.75f, -0.5f, 0.0f);
+ gl.glRotatef(270.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutSolidCone(1.0, 2.0, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.75f, 0.0f, -1.0f);
+ glut.glutSolidSphere(1.0, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-2.5, 2.5, -2.5 * (float) h / (float) w, 2.5 * (float) h
+ / (float) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-2.5 * (float) w / (float) h, 2.5 * (float) w
+ / (float) h, -2.5, 2.5, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/scenebamb.java b/src/redbook/src/glredbook10/scenebamb.java new file mode 100644 index 0000000..a69759c --- /dev/null +++ b/src/redbook/src/glredbook10/scenebamb.java @@ -0,0 +1,148 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates use of a blue ambient light source.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class scenebamb//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+
+ public static void main(String[] args) {
+ scenebamb demo = new scenebamb();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("scenebamb");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ }
+
+ /*
+ * Initialize light source and lighting.
+ */
+ public void init(GLAutoDrawable drawable)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float light_ambient[] =
+ { 0.0f, 0.0f, 1.0f, 1.0f };
+ float light_diffuse[] =
+ { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_specular[] =
+ { 1.0f, 1.0f, 1.0f, 1.0f };
+ /* light_position is NOT default value */
+ float light_position[] =
+ { 1.0f, 1.0f, 1.0f, 0.0f };
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ }
+
+ public void display(GLAutoDrawable drawable)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glPushMatrix();
+ gl.glRotatef(20.0f, 1.0f, 0.0f, 0.0f);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.75f, 0.5f, 0.0f);
+ gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutSolidTorus(0.275, 0.85, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.75f, -0.5f, 0.0f);
+ gl.glRotatef(270.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutSolidCone(1.0, 2.0, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.75f, 0.0f, -1.0f);
+ glut.glutSolidSphere(1.0, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h) gl.glOrtho(-2.5, 2.5, -2.5 * (float) h / (float) w,
+ 2.5 * (float) h / (float) w, -10.0, 10.0);
+ else gl.glOrtho(-2.5 * (float) w / (float) h, 2.5 * (float) w / (float) h, //
+ -2.5, 2.5, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged)
+ {
+ }
+
+ public void keyTyped(KeyEvent key)
+ {
+ }
+
+ public void keyPressed(KeyEvent key)
+ {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key)
+ {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/sceneflat.java b/src/redbook/src/glredbook10/sceneflat.java new file mode 100644 index 0000000..7431fa0 --- /dev/null +++ b/src/redbook/src/glredbook10/sceneflat.java @@ -0,0 +1,138 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program draws lighted objects with flat shading.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class sceneflat//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+ public static void main(String[] args) {
+ sceneflat demo = new sceneflat();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("sceneflat");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ }
+
+ /*
+ * Initialize light source and shading model (GL2.GL_FLAT).
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ /* light_position is NOT default value */
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glPushMatrix();
+ gl.glRotatef(20.0f, 1.0f, 0.0f, 0.0f);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.75f, 0.5f, 0.0f);
+ gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutSolidTorus(0.275, 0.85, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(-0.75f, -0.5f, 0.0f);
+ gl.glRotatef(270.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutSolidCone(1.0, 2.0, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.75f, 0.0f, -1.0f);
+ glut.glutSolidSphere(1.0, 20, 20);
+ gl.glPopMatrix();
+
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-2.5, 2.5, -2.5 * (float) h / (float) w, 2.5 * (float) h
+ / (float) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-2.5 * (float) w / (float) h, 2.5 * (float) w
+ / (float) h, -2.5, 2.5, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/select.java b/src/redbook/src/glredbook10/select.java new file mode 100644 index 0000000..f192bc7 --- /dev/null +++ b/src/redbook/src/glredbook10/select.java @@ -0,0 +1,252 @@ +package glredbook10;
+
+import javax.swing.*;
+
+import java.awt.event.*;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+import java.nio.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This is an illustration of the selection mode and name stack, which detects
+ * whether objects which collide with a viewing volume. First, four triangles
+ * and a rectangular box representing a viewing volume are drawn (drawScene
+ * routine). The green triangle and yellow triangles appear to lie within the
+ * viewing volume, but the red triangle appears to lie outside it. Then the
+ * selection mode is entered (selectObjects routine). Drawing to the screen
+ * ceases. To see if any collisions occur, the four triangles are called. In
+ * this example, the green triangle causes one hit with the name 1, and the
+ * yellow triangles cause one hit with the name 3.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+public class select//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private static final int BUFSIZE = 512;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+
+ public static void main(String[] args)
+ {
+ select demo = new select();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("select");
+ frame.setSize(200, 200);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ }
+
+ public void init(GLAutoDrawable drawable)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ drawScene(gl);
+ selectObjects(gl);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width,
+ int height)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged)
+ {
+ }
+
+ /*
+ * draw a triangle with vertices at (x1, y1), (x2, y2) and (x3, y3) at z units
+ * away from the origin.
+ */
+ private void drawTriangle(GL2 gl, float x1, float y1, float x2, float y2,
+ float x3, float y3, float z)
+ {
+ gl.glBegin(GL2.GL_TRIANGLES);
+ gl.glVertex3f(x1, y1, z);
+ gl.glVertex3f(x2, y2, z);
+ gl.glVertex3f(x3, y3, z);
+ gl.glEnd();
+ }
+
+ /* draw a rectangular box with these outer x, y, and z values */
+ private void drawViewVolume(GL2 gl, float x1, float x2, float y1, float y2,
+ float z1, float z2)
+ {
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glBegin(GL.GL_LINE_LOOP);
+ gl.glVertex3f(x1, y1, -z1);
+ gl.glVertex3f(x2, y1, -z1);
+ gl.glVertex3f(x2, y2, -z1);
+ gl.glVertex3f(x1, y2, -z1);
+ gl.glEnd();
+
+ gl.glBegin(GL.GL_LINE_LOOP);
+ gl.glVertex3f(x1, y1, -z2);
+ gl.glVertex3f(x2, y1, -z2);
+ gl.glVertex3f(x2, y2, -z2);
+ gl.glVertex3f(x1, y2, -z2);
+ gl.glEnd();
+
+ gl.glBegin(GL.GL_LINES); /* 4 lines */
+ gl.glVertex3f(x1, y1, -z1);
+ gl.glVertex3f(x1, y1, -z2);
+ gl.glVertex3f(x1, y2, -z1);
+ gl.glVertex3f(x1, y2, -z2);
+ gl.glVertex3f(x2, y1, -z1);
+ gl.glVertex3f(x2, y1, -z2);
+ gl.glVertex3f(x2, y2, -z1);
+ gl.glVertex3f(x2, y2, -z2);
+ gl.glEnd();
+ }
+
+ /*
+ * drawScene draws 4 triangles and a wire frame which represents the viewing
+ * volume.
+ */
+ void drawScene(GL2 gl)
+ {
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(40.0, 4.0 / 3.0, 1.0, 100.0);
+
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ glu.gluLookAt(7.5, 7.5, 12.5, 2.5, 2.5, -5.0, 0.0, 1.0, 0.0);
+ gl.glColor3f(0.0f, 1.0f, 0.0f); /* green triangle */
+ drawTriangle(gl, 2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, -5.0f);
+ gl.glColor3f(1.0f, 0.0f, 0.0f); /* red triangle */
+ drawTriangle(gl, 2.0f, 7.0f, 3.0f, 7.0f, 2.5f, 8.0f, -5.0f);
+ gl.glColor3f(1.0f, 1.0f, 0.0f); /* yellow triangles */
+ drawTriangle(gl, 2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, 0.0f);
+ drawTriangle(gl, 2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, -10.0f);
+ drawViewVolume(gl, 0.0f, 5.0f, 0.0f, 5.0f, 0.0f, 10.0f);
+ }
+
+ /*
+ * processHits prints out the contents of the selection array
+ */
+ private void processHits(int hits, int buffer[])
+ {
+ int names;
+ int ptr;
+
+ System.out.println("hits = " + hits);
+ // ptr = buffer;
+ ptr = 0;
+ for (int i = 0; i < hits; i++)
+ { /* for each hit */
+ names = buffer[i];
+ System.out.println(" number of names for hit = " + names);
+ ptr++;
+ System.out.print(" z1 is " + (float) buffer[ptr] / 0x7fffffff);
+ ptr++;
+ System.out.println(" z2 is " + (float) buffer[ptr] / 0x7fffffff);
+ ptr++;
+ System.out.print("\tthe name is ");
+ for (int j = 0; j < buffer.length /* names */; j++)
+ { /* for each name */
+ System.out.print(" " + buffer[--ptr]);
+ ptr++;
+ }
+ System.out.println();
+ }
+ }
+
+ /*
+ * selectObjects "draws" the triangles in selection mode, assigning names for
+ * the triangles. Note that the third and fourth triangles share one name, so
+ * that if either or both triangles intersects the viewing/clipping volume,
+ * only one hit will be registered.
+ */
+ private void selectObjects(GL2 gl)
+ {
+ int selectBuf[] = new int[BUFSIZE];
+ IntBuffer selectBuffer = BufferUtil.newIntBuffer(BUFSIZE);
+ int hits;
+
+ gl.glSelectBuffer(BUFSIZE, selectBuffer);
+ gl.glRenderMode(GL2.GL_SELECT);
+
+ gl.glInitNames();
+ gl.glPushName(0);
+
+ gl.glPushMatrix();
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0.0, 5.0, 0.0, 5.0, 0.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glLoadName(1);
+ drawTriangle(gl, 2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, -5.0f);
+ gl.glLoadName(2);
+ drawTriangle(gl, 2.0f, 7.0f, 3.0f, 7.0f, 2.5f, 8.0f, -5.0f);
+ gl.glLoadName(3);
+ drawTriangle(gl, 2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, 0.0f);
+ drawTriangle(gl, 2.0f, 2.0f, 3.0f, 2.0f, 2.5f, 3.0f, -10.0f);
+ gl.glPopMatrix();
+ gl.glFlush();
+
+ hits = gl.glRenderMode(GL2.GL_RENDER);
+ selectBuffer.get(selectBuf);
+ processHits(hits, selectBuf);
+ }
+
+ public void keyTyped(KeyEvent key)
+ {
+ }
+
+ public void keyPressed(KeyEvent key)
+ {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key)
+ {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/simple.java b/src/redbook/src/glredbook10/simple.java new file mode 100644 index 0000000..e9bd48d --- /dev/null +++ b/src/redbook/src/glredbook10/simple.java @@ -0,0 +1,76 @@ +package glredbook10;
+/**
+ * This program draws a white rectangle on a black background.
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.swing.*;
+
+
+public class simple
+{
+ public static void main(String[] args)
+ {
+ /*
+ * remove/commet out to be able to resize window smaller
+ */
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ // name of class as title
+ JFrame jframe = new JFrame("simple");
+ jframe.setSize(500, 500);
+ jframe.setLocationRelativeTo(null); // center of screen
+
+ GLJPanel canvas = new GLJPanel();
+ //GLJPanel jcanvas = new GLJPanel();
+ // anonymous object of GLEventListener interface
+// jcanvas.addGLEventListener(new GLEventListener()
+ canvas.addGLEventListener(new GLEventListener()
+ {
+ public void init(GLAutoDrawable drawable)
+ {
+ // TODO Auto-generated method stub
+ }
+
+ public void display(GLAutoDrawable drawable)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
+ gl.glBegin(GL2.GL_POLYGON);
+ gl.glVertex2f(-0.5f, -0.5f);
+ gl.glVertex2f(-0.5f, 0.5f);
+ gl.glVertex2f(0.5f, 0.5f);
+ gl.glVertex2f(0.5f, -0.5f);
+ gl.glEnd();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width,
+ int height)
+ {
+ // TODO Auto-generated method stub
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged)
+ {
+ // TODO Auto-generated method stub
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+ });
+
+ jframe.getContentPane().add(canvas);// put the canvas into a JFrame window
+ //jframe.add(jcanvas);
+ jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ jframe.setVisible(true); // show window
+ }
+}
diff --git a/src/redbook/src/glredbook10/smooth.java b/src/redbook/src/glredbook10/smooth.java new file mode 100644 index 0000000..65b6cf4 --- /dev/null +++ b/src/redbook/src/glredbook10/smooth.java @@ -0,0 +1,110 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates smooth shading. A smooth shaded polygon is drawn in
+ * a 2-D projection.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class smooth//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ smooth demo = new smooth();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("smooth");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ // gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_SMOOTH);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ triangle(gl);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ glu.gluOrtho2D(0.0, 30.0, 0.0, 30.0 * (float) h / (float) w);
+ else
+ glu.gluOrtho2D(0.0, 30.0 * (float) w / (float) h, 0.0, 30.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void triangle(GL2 gl) {
+ gl.glBegin(GL2.GL_TRIANGLES);
+ gl.glColor3f(1.0f, 0.0f, 0.0f);
+ gl.glVertex2f(5.0f, 5.0f);
+ gl.glColor3f(0.0f, 1.0f, 0.0f);
+ gl.glVertex2f(25.0f, 5.0f);
+ gl.glColor3f(0.0f, 0.0f, 1.0f);
+ gl.glVertex2f(5.0f, 25.0f);
+ gl.glEnd();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/sphere.java b/src/redbook/src/glredbook10/sphere.java new file mode 100644 index 0000000..30fd514 --- /dev/null +++ b/src/redbook/src/glredbook10/sphere.java @@ -0,0 +1,105 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program draws a wire frame sphere. It uses glTranslatef() as a viewing
+ * transformation.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class sphere//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ sphere demo = new sphere();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("sphere");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glPushMatrix();
+ /* glTranslatef() as viewing transformation */
+ gl.glTranslatef(0.0f, 0.0f, -5.0f);
+ glut.glutWireSphere(1.0f, 10, 10);
+ gl.glPopMatrix();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 20.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/stencil.java b/src/redbook/src/glredbook10/stencil.java new file mode 100644 index 0000000..a2a7c76 --- /dev/null +++ b/src/redbook/src/glredbook10/stencil.java @@ -0,0 +1,181 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import javax.swing.*;
+
+import java.awt.event.*;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates use of the stencil buffer for masking
+ * nonrectangular regions. Whenever the window is redrawn, a value of 1 is drawn
+ * into a diamond-shaped region in the stencil buffer. Elsewhere in the stencil
+ * buffer, the value is 0. Then a blue sphere is drawn where the stencil value
+ * is 1, and yellow torii are drawn where the stencil value is not 1.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class stencil//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+
+ private static final int YELLOWMAT = 1;
+ private static final int BLUEMAT = 2;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setStencilBits(8);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ stencil demo = new stencil();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("stencil");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ float yellow_diffuse[] = new float[] { 0.7f, 0.7f, 0.0f, 1.0f };
+ float yellow_specular[] = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
+ float blue_diffuse[] = new float[] { 0.1f, 0.1f, 0.7f, 1.0f };
+ float blue_specular[] = new float[] { 0.1f, 1.0f, 1.0f, 1.0f };
+ float position_one[] = new float[] { 1.0f, 1.0f, 1.0f, 0.0f };
+ //
+ gl.glNewList(YELLOWMAT, GL2.GL_COMPILE);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, yellow_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, yellow_specular, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 64.0f);
+ gl.glEndList();
+
+ gl.glNewList(BLUEMAT, GL2.GL_COMPILE);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, blue_diffuse, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, blue_specular, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 45.0f);
+ gl.glEndList();
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position_one, 0);
+
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ gl.glClearStencil(0x0);
+ gl.glEnable(GL2.GL_STENCIL_TEST);
+
+ }
+
+ /*
+ * Draw a sphere in a diamond-shaped section in the middle of a window with
+ * 2 torii.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ /* draw blue sphere where the stencil is 1 */
+ gl.glStencilFunc(GL.GL_EQUAL, 0x1, 0x1);
+ gl.glStencilOp(GL.GL_KEEP, GL.GL_KEEP, GL.GL_KEEP);
+ gl.glCallList(BLUEMAT);
+ glut.glutSolidSphere(0.5, 20, 20);
+
+ /* draw the tori where the stencil is not 1 */
+ gl.glStencilFunc(GL.GL_NOTEQUAL, 0x1, 0x1);
+ gl.glPushMatrix();
+ gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
+ gl.glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
+ gl.glCallList(YELLOWMAT);
+ glut.glutSolidTorus(0.275, 0.85, 20, 20);
+ gl.glPushMatrix();
+ gl.glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
+ glut.glutSolidTorus(0.275, 0.85, 20, 20);
+ gl.glPopMatrix();
+ gl.glPopMatrix();
+ }
+
+ /*
+ * Whenever the window is reshaped, redefine the coordinate system and
+ * redraw the stencil area.
+ */
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glClear(GL2.GL_STENCIL_BUFFER_BIT);
+ /* create a diamond shaped stencil area */
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0);
+ // if (w <= h) glu.gluOrtho2D(-3.0, 3.0, -3.0 * (float) h / (float) w,
+ // 3.0 * (float) h / (float) w);
+ // else glu.gluOrtho2D(-3.0 * (float) w / (float) h, //
+ // 3.0 * (float) w / (float) h, -3.0, 3.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+
+ gl.glStencilFunc(GL.GL_ALWAYS, 0x1, 0x1);
+ gl.glStencilOp(GL2.GL_REPLACE, GL2.GL_REPLACE, GL2.GL_REPLACE);
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glVertex2f(-1.0f, 0.0f);
+ gl.glVertex2f(0.0f, 1.0f);
+ gl.glVertex2f(1.0f, 0.0f);
+ gl.glVertex2f(0.0f, -1.0f);
+ gl.glEnd();
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(45.0, (float) w / (float) h, 3.0, 7.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslatef(0.0f, 0.0f, -5.0f);
+
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/stroke.java b/src/redbook/src/glredbook10/stroke.java new file mode 100644 index 0000000..6574da0 --- /dev/null +++ b/src/redbook/src/glredbook10/stroke.java @@ -0,0 +1,235 @@ +package glredbook10;
+
+/**
+ * This program demonstrates some characters of a stroke (vector) font. The
+ * characters are represented by display lists, which are given numbers which
+ * correspond to the ASCII values of the characters. Use of glCallLists() is
+ * demonstrated.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+import java.awt.event.*;
+import javax.swing.*;
+
+import java.nio.*;
+
+
+import com.sun.opengl.util.*;
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+public class stroke//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ //
+ private static final int PT = 1;
+ private static final int STROKE = 2;
+ private static final int END = 3;
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+ // * C struct. Compare this to charpoint class.
+ // typedef struct charpoint
+ // {
+ // GLfloat x, y;
+ // int type;
+ // } CP;
+ private class charpoint {
+ public float x;
+ public float y;
+ public int type;
+
+ public charpoint(float x, float y, int type) {
+ this.x = x;
+ this.y = y;
+ this.type = type;
+ }
+ }
+
+ // * Saved here from original for you to see the difference
+ // CP Adata[] =
+ // {
+ // { 0, 0, PT}, {0, 9, PT}, {1, 10, PT}, {4, 10, PT},
+ // {5, 9, PT}, {5, 0, STROKE}, {0, 5, PT}, {5, 5, END}
+ // };
+ charpoint Adata[] = { new charpoint(0, 0, PT), new charpoint(0, 9, PT), //
+ new charpoint(1, 10, PT), new charpoint(4, 10, PT),//
+ new charpoint(5, 9, PT), new charpoint(5, 0, STROKE),//
+ new charpoint(0, 5, PT), new charpoint(5, 5, END) };
+
+ charpoint Edata[] = { new charpoint(5, 0, PT), new charpoint(0, 0, PT), //
+ new charpoint(0, 10, PT), new charpoint(5, 10, STROKE),//
+ new charpoint(0, 5, PT), new charpoint(4, 5, END) };
+
+ charpoint Pdata[] = { new charpoint(0, 0, PT), new charpoint(0, 10, PT),//
+ new charpoint(4, 10, PT), new charpoint(5, 9, PT),//
+ new charpoint(5, 6, PT), new charpoint(4, 5, PT), //
+ new charpoint(0, 5, END) };
+
+ charpoint Rdata[] = { new charpoint(0, 0, PT), new charpoint(0, 10, PT),//
+ new charpoint(4, 10, PT), new charpoint(5, 9, PT),//
+ new charpoint(5, 6, PT), new charpoint(4, 5, PT),//
+ new charpoint(0, 5, STROKE), new charpoint(3, 5, PT),//
+ new charpoint(5, 0, END) };
+
+ charpoint Sdata[] = { new charpoint(0, 1, PT), new charpoint(1, 0, PT), //
+ new charpoint(4, 0, PT), new charpoint(5, 1, PT),//
+ new charpoint(5, 4, PT), new charpoint(4, 5, PT), //
+ new charpoint(1, 5, PT), new charpoint(0, 6, PT), //
+ new charpoint(0, 9, PT), new charpoint(1, 10, PT),//
+ new charpoint(4, 10, PT), new charpoint(5, 9, END) };
+
+ // char *test1 = "A SPARE SERAPE APPEARS AS"; C char ptr to str ...
+ // char *test2 = "APES PREPARE RARE PEPPERS";
+ private String test1 = "A SPARE SERAPE APPEARS AS";// String object
+ private String test2 = "APES PREPARE RARE PEPPERS";
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ stroke demo = new stroke();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("stroke");
+ frame.setSize(440, 120);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /* Create a display list for each of 6 characters */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ int base;
+
+ gl.glShadeModel(GL2.GL_FLAT);
+
+ base = gl.glGenLists(128);
+ gl.glListBase(base);
+ gl.glNewList(base + 'A', GL2.GL_COMPILE);
+ drawLetter(gl, Adata);
+ gl.glEndList();
+ gl.glNewList(base + 'E', GL2.GL_COMPILE);
+ drawLetter(gl, Edata);
+ gl.glEndList();
+ gl.glNewList(base + 'P', GL2.GL_COMPILE);
+ drawLetter(gl, Pdata);
+ gl.glEndList();
+ gl.glNewList(base + 'R', GL2.GL_COMPILE);
+ drawLetter(gl, Rdata);
+ gl.glEndList();
+ gl.glNewList(base + 'S', GL2.GL_COMPILE);
+ drawLetter(gl, Sdata);
+ gl.glEndList();
+ gl.glNewList(base + ' ', GL2.GL_COMPILE);
+ gl.glTranslatef(8.0f, 0.0f, 0.0f);
+ gl.glEndList();
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+
+ gl.glPushMatrix();
+ gl.glScalef(2.0f, 2.0f, 2.0f);
+ gl.glTranslatef(10.0f, 30.0f, 0.0f);
+ this.printStrokedString(gl, test1);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glScalef(2.0f, 2.0f, 2.0f);
+ gl.glTranslatef(10.0f, 13.0f, 0.0f);
+ this.printStrokedString(gl, test2);
+ gl.glPopMatrix();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluOrtho2D(0, w, 0, h);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * interprets the instructions from the array for that letter and renders
+ * the letter with line segments.
+ */
+ private void drawLetter(GL2 gl, charpoint[] l) {
+ int i = 0;
+ gl.glBegin(GL.GL_LINE_STRIP);
+ while (i < l.length) {
+ switch (l[i].type) {
+ case PT:
+ gl.glVertex2f(l[i].x, l[i].y);
+ break;
+ case STROKE:
+ gl.glVertex2f(l[i].x, l[i].y);
+ gl.glEnd();
+ gl.glBegin(GL.GL_LINE_STRIP);
+ break;
+ case END:
+ gl.glVertex2f(l[i].x, l[i].y);
+ gl.glEnd();
+ gl.glTranslatef(8.0f, 0.0f, 0.0f);
+ return;
+ }
+ i++;// System.out.println(i+" ");
+ }
+ }
+
+ // private void printStrokedString(char *s)
+ private void printStrokedString(GL2 gl, String s) {
+ int len = s.length();
+ ByteBuffer str = BufferUtil.newByteBuffer(len);
+ str.put(s.getBytes());
+ str.rewind();
+ gl.glCallLists(len, GL.GL_BYTE, str);
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+}
diff --git a/src/redbook/src/glredbook10/tea.java b/src/redbook/src/glredbook10/tea.java new file mode 100644 index 0000000..5deb456 --- /dev/null +++ b/src/redbook/src/glredbook10/tea.java @@ -0,0 +1,158 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import javax.swing.JFrame;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates two-sided lighting and compares it with one-sided
+ * lighting. Three teapots are drawn, with a clipping plane to expose the
+ * interior of the objects.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class tea//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ tea demo = new tea();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("tea");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ }
+
+ /*
+ * Initialize light source.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ /* light_position is NOT default value */
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+
+ gl.glFrontFace(GL.GL_CW);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ double eqn[] = { 1.0, 0.0, -1.0, 1.0 };
+ // float two_side_on[] = { 1f };//never used by original example
+ // float two_side_off[] = { 0 };
+ float mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
+ float back_diffuse[] = { 0.8f, 0.2f, 0.8f, 1.0f };
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glPushMatrix();
+ gl.glClipPlane(GL2.GL_CLIP_PLANE0, eqn, 0); /* slice objects */
+ gl.glEnable(GL2.GL_CLIP_PLANE0);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, 2.0f, 0.0f);
+ glut.glutSolidTeapot(1.0); /* one-sided lighting */
+ gl.glPopMatrix();
+
+ /* two-sided lighting, but same material */
+ gl.glLightModelf(GL2.GL_LIGHT_MODEL_TWO_SIDE, GL2.GL_TRUE);
+ gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, 0.0f, 0.0f);
+ glut.glutSolidTeapot(1.0);
+ gl.glPopMatrix();
+
+ /* two-sided lighting, two different materials */
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat_diffuse, 0);
+ gl.glMaterialfv(GL.GL_BACK, GL2.GL_DIFFUSE, back_diffuse, 0);
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, -2.0f, 0.0f);
+ glut.glutSolidTeapot(1.0);
+ gl.glPopMatrix();
+
+ gl.glLightModelf(GL2.GL_LIGHT_MODEL_TWO_SIDE, GL.GL_FALSE);
+ gl.glDisable(GL2.GL_CLIP_PLANE0);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-4.0, 4.0, -4.0 * (float) h / (float) w, 4.0 * (float) h
+ / (float) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-4.0 * (float) w / (float) h, 4.0 * (float) w
+ / (float) h, -4.0, 4.0, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/teaambient.java b/src/redbook/src/glredbook10/teaambient.java new file mode 100644 index 0000000..c9ddf88 --- /dev/null +++ b/src/redbook/src/glredbook10/teaambient.java @@ -0,0 +1,147 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*; import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program renders three lighted, shaded teapots, with different ambient
+ * values.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class teaambient //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ teaambient demo = new teaambient();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("teaambient");
+ frame.setSize(512, 512);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ }
+
+ /*
+ * Initialize light source and lighting model.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ /* light_position is NOT default value */
+ float light_position[] = { 1.0f, 0.0f, 0.0f, 0.0f };
+ float global_ambient[] = { 0.75f, 0.75f, 0.75f, 1.0f };
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, global_ambient, 0);
+
+ gl.glFrontFace(GL.GL_CW);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float low_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
+ float more_ambient[] = { 0.4f, 0.4f, 0.4f, 1.0f };
+ float most_ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ /* material has small ambient reflection */
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, low_ambient, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 40.0f);
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, 2.0f, 0.0f);
+ glut.glutSolidTeapot(1.0);
+ gl.glPopMatrix();
+
+ /* material has moderate ambient reflection */
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, more_ambient, 0);
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, 0.0f, 0.0f);
+ glut.glutSolidTeapot(1.0);
+ gl.glPopMatrix();
+
+ /* material has large ambient reflection */
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, most_ambient, 0);
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, -2.0f, 0.0f);
+ glut.glutSolidTeapot(1.0);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-4.0, 4.0, -4.0 * (float) h / (float) w, 4.0 * (float) h
+ / (float) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-4.0 * (float) w / (float) h, 4.0 * (float) w
+ / (float) h, -4.0, 4.0, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook10/teapots.java b/src/redbook/src/glredbook10/teapots.java new file mode 100644 index 0000000..ffdd9f5 --- /dev/null +++ b/src/redbook/src/glredbook10/teapots.java @@ -0,0 +1,206 @@ +package glredbook10;
+
+import com.sun.opengl.util.gl2.GLUT;
+import java.awt.event.*;
+import javax.swing.*;
+
+import javax.media.opengl.*; import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program demonstrates lots of material properties. A single light source
+ * illuminates the objects.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class teapots//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ teapots demo = new teapots();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("teapots");
+ frame.setSize(500, 600);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ }
+
+ /*
+ * Initialize depth buffer, projection matrix, light source, and lighting
+ * model. Do not specify a material property here.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ float ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float position[] = { 0.0f, 3.0f, 3.0f, 0.0f };
+
+ float lmodel_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+ float local_view[] = { 0.0f };
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, position, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_LOCAL_VIEWER, local_view, 0);
+
+ gl.glFrontFace(GL.GL_CW);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+ }
+
+ /*
+ * First column: emerald, jade, obsidian, pearl, ruby, turquoise 2nd column:
+ * brass, bronze, chrome, copper, gold, silver 3rd column: black, cyan,
+ * green, red, white, yellow plastic 4th column: black, cyan, green, red,
+ * white, yellow rubber
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ renderTeapot(gl, 2.0, 17.0, 0.0215, 0.1745, 0.0215, 0.07568, 0.61424,
+ 0.07568, 0.633, 0.727811, 0.633, 0.6);
+ renderTeapot(gl, 2.0, 14.0, 0.135, 0.2225, 0.1575, 0.54, 0.89, 0.63,
+ 0.316228, 0.316228, 0.316228, 0.1);
+ renderTeapot(gl, 2.0, 11.0, 0.05375, 0.05, 0.06625, 0.18275, 0.17,
+ 0.22525, 0.332741, 0.328634, 0.346435, 0.3);
+ renderTeapot(gl, 2.0, 8.0, 0.25, 0.20725, 0.20725, 1, 0.829, 0.829,
+ 0.296648, 0.296648, 0.296648, 0.088);
+ renderTeapot(gl, 2.0, 5.0, 0.1745, 0.01175, 0.01175, 0.61424, 0.04136,
+ 0.04136, 0.727811, 0.626959, 0.626959, 0.6);
+ renderTeapot(gl, 2.0, 2.0, 0.1, 0.18725, 0.1745, 0.396, 0.74151,
+ 0.69102, 0.297254, 0.30829, 0.306678, 0.1);
+ renderTeapot(gl, 6.0, 17.0, 0.329412, 0.223529, 0.027451, 0.780392,
+ 0.568627, 0.113725, 0.992157, 0.941176, 0.807843, 0.21794872);
+ renderTeapot(gl, 6.0, 14.0, 0.2125, 0.1275, 0.054, 0.714, 0.4284,
+ 0.18144, 0.393548, 0.271906, 0.166721, 0.2);
+ renderTeapot(gl, 6.0, 11.0, 0.25, 0.25, 0.25, 0.4, 0.4, 0.4, 0.774597,
+ 0.774597, 0.774597, 0.6);
+ renderTeapot(gl, 6.0, 8.0, 0.19125, 0.0735, 0.0225, 0.7038, 0.27048,
+ 0.0828, 0.256777, 0.137622, 0.086014, 0.1);
+ renderTeapot(gl, 6.0, 5.0, 0.24725, 0.1995, 0.0745, 0.75164, 0.60648,
+ 0.22648, 0.628281, 0.555802, 0.366065, 0.4);
+ renderTeapot(gl, 6.0, 2.0, 0.19225, 0.19225, 0.19225, 0.50754, 0.50754,
+ 0.50754, 0.508273, 0.508273, 0.508273, 0.4);
+ renderTeapot(gl, 10.0, 17.0, 0.0, 0.0, 0.0, 0.01, 0.01, 0.01, 0.50,
+ 0.50, 0.50, .25);
+ renderTeapot(gl, 10.0, 14.0, 0.0, 0.1, 0.06, 0.0, 0.50980392,
+ 0.50980392, 0.50196078, 0.50196078, 0.50196078, .25);
+ renderTeapot(gl, 10.0, 11.0, 0.0, 0.0, 0.0, 0.1, 0.35, 0.1, 0.45, 0.55,
+ 0.45, .25);
+ renderTeapot(gl, 10.0, 8.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.7, 0.6,
+ 0.6, .25);
+ renderTeapot(gl, 10.0, 5.0, 0.0, 0.0, 0.0, 0.55, 0.55, 0.55, 0.70,
+ 0.70, 0.70, .25);
+ renderTeapot(gl, 10.0, 2.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.60, 0.60,
+ 0.50, .25);
+ renderTeapot(gl, 14.0, 17.0, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01, 0.4,
+ 0.4, 0.4, .078125);
+ renderTeapot(gl, 14.0, 14.0, 0.0, 0.05, 0.05, 0.4, 0.5, 0.5, 0.04, 0.7,
+ 0.7, .078125);
+ renderTeapot(gl, 14.0, 11.0, 0.0, 0.05, 0.0, 0.4, 0.5, 0.4, 0.04, 0.7,
+ 0.04, .078125);
+ renderTeapot(gl, 14.0, 8.0, 0.05, 0.0, 0.0, 0.5, 0.4, 0.4, 0.7, 0.04,
+ 0.04, .078125);
+ renderTeapot(gl, 14.0, 5.0, 0.05, 0.05, 0.05, 0.5, 0.5, 0.5, 0.7, 0.7,
+ 0.7, .078125);
+ renderTeapot(gl, 14.0, 2.0, 0.05, 0.05, 0.0, 0.5, 0.5, 0.4, 0.7, 0.7,
+ 0.04, .078125);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(0.0, 16.0, 0.0, 16.0 * (float) h / (float) w, -10.0,
+ 10.0);
+ else
+ gl.glOrtho(0.0, 16.0 * (float) w / (float) h, 0.0, 16.0, -10.0,
+ 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * Move object into position. Use 3rd through 12th parameters to specify the
+ * material property. Draw a teapot.
+ */
+ private void renderTeapot(GL2 gl, double x, double y, double ambr,
+ double ambg, double ambb, double difr, double difg, double difb,
+ double specr, double specg, double specb, double shine) {
+ float mat[] = new float[4];
+
+ gl.glPushMatrix();
+ gl.glTranslatef((float) x, (float) y, 0.0f);
+ mat[0] = (float) ambr;
+ mat[1] = (float) ambg;
+ mat[2] = (float) ambb;
+ mat[3] = 1.0f;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0);
+ mat[0] = (float) difr;
+ mat[1] = (float) difg;
+ mat[2] = (float) difb;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, mat, 0);
+ mat[0] = (float) specr;
+ mat[1] = (float) specg;
+ mat[2] = (float) specb;
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, (float) (shine * 128.0f));
+ glut.glutSolidTeapot(1.0f);
+ gl.glPopMatrix();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook10/texgen.java b/src/redbook/src/glredbook10/texgen.java new file mode 100644 index 0000000..54118ab --- /dev/null +++ b/src/redbook/src/glredbook10/texgen.java @@ -0,0 +1,156 @@ +package glredbook10;
+
+import java.awt.event.*;
+import javax.swing.*;
+
+import java.nio.*;
+
+import com.sun.opengl.util.*;
+import com.sun.opengl.util.gl2.GLUT;
+import javax.media.opengl.*; import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program draws a texture mapped teapot with automatically generated
+ * texture coordinates. The texture is rendered as stripes on the teapot.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class texgen//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+
+ private static final int stripeImageWidth = 32;
+ private byte stripeImage[] = new byte[3 * stripeImageWidth];
+ private ByteBuffer stripeImageBuf = BufferUtil
+ .newByteBuffer(stripeImage.length);
+ /* glTexGen stuff: */
+ private float sgenparams[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ texgen demo = new texgen();
+
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("texgen");
+ frame.setSize(512, 512);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ makeStripeImage();
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_MODULATE);
+ gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_LINEAR);
+ gl.glTexParameterf(GL2.GL_TEXTURE_1D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_LINEAR);
+ gl.glTexImage1D(GL2.GL_TEXTURE_1D, 0, 3, stripeImageWidth, 0, GL2.GL_RGB,
+ GL.GL_UNSIGNED_BYTE, stripeImageBuf);
+
+ gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_OBJECT_LINEAR);
+ gl.glTexGenfv(GL2.GL_S, GL2.GL_OBJECT_PLANE, sgenparams, 0);
+
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL2.GL_TEXTURE_GEN_S);
+ gl.glEnable(GL2.GL_TEXTURE_1D);
+ gl.glEnable(GL.GL_CULL_FACE);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+ gl.glFrontFace(GL.GL_CW);
+ gl.glCullFace(GL.GL_BACK);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 64.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glPushMatrix();
+ gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
+ glut.glutSolidTeapot(2.0f);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-3.5, 3.5, -3.5 * (float) h / (float) w, 3.5 * (float) h
+ / (float) w, -3.5, 3.5);
+ else
+ gl.glOrtho(-3.5 * (float) w / (float) h, //
+ 3.5 * (float) w / (float) h, -3.5, 3.5, -3.5, 3.5);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeStripeImage() {
+ for (int j = 0; j < stripeImageWidth; j++) {
+ // stripeImage[3 * j] = (j <= 4) ? 255 : 0;
+ // stripeImage[3 * j + 1] = (j > 4) ? 255 : 0;
+ // stripeImage[3 * j + 2] = 0;
+ stripeImageBuf.put(((j <= 4) ? (byte) 255 : (byte) 0));
+ stripeImageBuf.put(((j > 4) ? (byte) 255 : (byte) 0));
+ stripeImageBuf.put((byte) 0);
+ }
+ stripeImageBuf.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook10/texturesurf.java b/src/redbook/src/glredbook10/texturesurf.java new file mode 100644 index 0000000..fcd6e28 --- /dev/null +++ b/src/redbook/src/glredbook10/texturesurf.java @@ -0,0 +1,206 @@ +package glredbook10;
+
+import javax.swing.*;
+
+import java.awt.event.*;
+
+import java.nio.*;
+
+import com.sun.opengl.util.*;
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program uses evaluators to generate a curved surface and automatically
+ * generated texture coordinates.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+public class texturesurf//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ // as from C version of file
+ private static final float ctrlpoints[][][] = new float[][][] {
+ { { -1.5f, -1.5f, 4.0f }, { -0.5f, -1.5f, 2.0f },
+ { 0.5f, -1.5f, -1.0f }, { 1.5f, -1.5f, 2.0f } },
+ { { -1.5f, -0.5f, 1.0f }, { -0.5f, -0.5f, 3.0f },
+ { 0.5f, -0.5f, 0.0f }, { 1.5f, -0.5f, -1.0f } },
+ { { -1.5f, 0.5f, 4.0f }, { -0.5f, 0.5f, 0.0f },
+ { 0.5f, 0.5f, 3.0f }, { 1.5f, 0.5f, 4.0f } },
+ { { -1.5f, 1.5f, -2.0f }, { -0.5f, 1.5f, -2.0f },
+ { 0.5f, 1.5f, 0.0f }, { 1.5f, 1.5f, -1.0f } } };
+ // need float buffer instead of n-dimensional array above
+ private FloatBuffer ctrlpointsBuf = BufferUtil
+ .newFloatBuffer(ctrlpoints.length * ctrlpoints[0].length
+ * ctrlpoints[0][0].length);
+ {// SO copy 4x4x3 array above to float buffer
+ for (int i = 0; i < ctrlpoints.length; i++)
+ // System.out.print(ctrlpoints.length+ " ");
+ for (int j = 0; j < ctrlpoints[0].length; j++) {
+ // System.out.println(ctrlpoints[0][0].length+" ");
+ for (int k = 0; k < ctrlpoints[0][0].length; k++) {
+ ctrlpointsBuf.put(ctrlpoints[i][j][k]);
+ // System.out.print(ctrlpoints[i][j][k] + " ");
+ }
+ // System.out.println(ctrlpointsBuf.toString());
+ }
+ // THEN rewind it before use
+ ctrlpointsBuf.rewind();
+ }
+ private float[][][] texpts = new float[][][] {
+ { { 0.0f, 0.0f }, { 0.0f, 1.0f } },
+ { { 1.0f, 0.0f }, { 1.0f, 1.0f } } };
+ private FloatBuffer texptsBuf = BufferUtil
+ //
+ .newFloatBuffer(texpts.length * texpts[0].length * texpts[1].length);
+ {
+ for (int i = 0; i < texpts.length; i++)
+ // System.out.print(ctrlpoints.length+ " ");
+ for (int j = 0; j < texpts[0].length; j++) {
+ // System.out.println(ctrlpoints[0][0].length+" ");
+ for (int k = 0; k < texpts[0][0].length; k++) {
+ texptsBuf.put(texpts[i][j][k]);
+ // System.out.print(texpts[i][j][k] + " ");
+ }
+ // System.out.println(texptsBuf.toString());
+ }
+ // THEN rewind it before use
+ texptsBuf.rewind();
+
+ }
+ private static final int imageWidth = 64;
+ private static final int imageHeight = 64;
+ private static byte image[] = new byte[3 * imageWidth * imageHeight];
+ private static ByteBuffer imageBuf = BufferUtil.newByteBuffer(image.length);
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ texturesurf demo = new texturesurf();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("texturesurf");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glMap2f(GL2.GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, ctrlpointsBuf);
+ gl.glMap2f(GL2.GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2,
+ texptsBuf);
+ gl.glEnable(GL2.GL_MAP2_TEXTURE_COORD_2);
+ gl.glEnable(GL2.GL_MAP2_VERTEX_3);
+ gl.glMapGrid2f(20, 0.0f, 1.0f, 20, 0.0f, 1.0f);
+ makeImage();
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
+ GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGB, imageWidth,
+ imageHeight, 0, GL2.GL_RGB, GL.GL_UNSIGNED_BYTE, imageBuf);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glShadeModel(GL2.GL_FLAT);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glEvalMesh2(GL2.GL_FILL, 0, 20, 0, 20);
+ gl.glFlush();
+
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-4.0, 4.0, -4.0 * (float) h / (float) w, 4.0 * (float) h
+ / (float) w, -4.0, 4.0);
+ else
+ gl.glOrtho(-4.0 * (float) w / (float) h, //
+ 4.0 * (float) w / (float) h, -4.0, 4.0, -4.0, 4.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glRotatef(85.0f, 1.0f, 1.0f, 1.0f);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeImage() {
+ float ti, tj;
+
+ for (int i = 0; i < imageWidth; i++) {
+ ti = 2.0f * (float) Math.PI * i / imageWidth;
+ for (int j = 0; j < imageHeight; j++) {
+ tj = 2.0f * (float) Math.PI * j / imageHeight;
+
+ // image[3 * (imageHeight * i + j)] = (byte) (255 * (1.0 +
+ // sin(ti)));
+ // image[3 * (imageHeight * i + j) + 1] = (byte) (255 * (1.0 +
+ // cos(2 *
+ // tj)));
+ // image[3 * (imageHeight * i + j) + 2] = (byte) (255 * (1.0 +
+ // cos(ti +
+ // tj)));
+ // image[3 * (imageHeight * i + j) + 2] = (byte)0xff;
+ imageBuf.put((byte) (127 * (1.0 + Math.sin(ti))));
+ imageBuf.put((byte) (127 * (1.0 + Math.cos(2 * tj))));
+ imageBuf.put((byte) (127 * (1.0 + Math.cos(ti + tj))));
+ }
+ }
+ imageBuf.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/README.txt b/src/redbook/src/glredbook11/README.txt new file mode 100644 index 0000000..8225c0b --- /dev/null +++ b/src/redbook/src/glredbook11/README.txt @@ -0,0 +1,73 @@ +This README is for the glredbook11 package.
+
+These are port of the examples programs which are featured
+in the OpenGL Programming Guide, 2nd Edition corresponding
+to OpenGL version 1.1.
+
+- All examples requires JDK 1.4.2 and javax.media.opengl (jogl)
+ and a system dependent jogl-natives-*.jar to compile.
+- No ant build file is yet available.
+
+New in the second edition demostrating new features of OpenGL 1.1:
+
+1. polyoff.c --> polyoff.java
+2. texbind.c --> texbind.java
+3. texprox.c --> texprox.java
+4. texsub.c --> texsub.java
+5. varray.c --> varray.java
+
+These may not be able to run in OpenGL 1.1, unless modified
+to support extensions.
+
+OpenGL 1.0 to 1.1 compatibility issues:
+There are nine programs that are modify for 1.1 features
+but they will also run in 1.0.
+
+Four programs--checker.c, mipmap.c, texgen.c, and wrap.c--using
+texture objects have been modified so that they will avoid
+the use of texture objects on OpenGL 1.0 machines.
+
+Best regards,
+
+Kiet Le (Java port 200604)
+ak.kiet.le '@' gmail.com
+http://ak.kiet.le.googlepages.com/theredbookinjava.html
+
+
+/* ALL PROGRAMS IN PACKAGE HAS THE FOLLOWING LICENSE.
+ *
+ * Copyright (c) 1993-1997, Silicon Graphics, Inc.
+ * ALL RIGHTS RESERVED
+ * Permission to use, copy, modify, and distribute this software for
+ * any purpose and without fee is hereby granted, provided that the above
+ * copyright notice appear in all copies and that both the copyright notice
+ * and this permission notice appear in supporting documentation, and that
+ * the name of Silicon Graphics, Inc. not be used in advertising
+ * or publicity pertaining to distribution of the software without specific,
+ * written prior permission.
+ *
+ * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
+ * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
+ * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
+ * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
+ * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
+ * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
+ * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
+ * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
+ * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * US Government Users Restricted Rights
+ * Use, duplication, or disclosure by the Government is subject to
+ * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
+ * (c)(1)(ii) of the Rights in Technical Data and Computer Software
+ * clause at DFARS 252.227-7013 and/or in similar or successor
+ * clauses in the FAR or the DOD or NASA FAR Supplement.
+ * Unpublished-- rights reserved under the copyright laws of the
+ * United States. Contractor/manufacturer is Silicon Graphics,
+ * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
+ *
+ * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
+ */
diff --git a/src/redbook/src/glredbook11/aargb.java b/src/redbook/src/glredbook11/aargb.java new file mode 100644 index 0000000..71ae3b8 --- /dev/null +++ b/src/redbook/src/glredbook11/aargb.java @@ -0,0 +1,135 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import javax.media.opengl.*;
+ import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+import javax.swing.JFrame;
+
+/**
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+public class aargb//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private float rotAngle = 0f;
+ private boolean rotate = false;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setNumSamples(2);
+ caps.setSampleBuffers(true);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ aargb demo = new aargb();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("aargb");
+ frame.setSize(512, 512);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float values[] = new float[2];
+ gl.glGetFloatv(GL2.GL_LINE_WIDTH_GRANULARITY, values, 0);
+ System.out
+ .println("GL.GL_LINE_WIDTH_GRANULARITY value is " + values[0]);
+ gl.glGetFloatv(GL2.GL_LINE_WIDTH_RANGE, values, 0);
+ System.out.println("GL.GL_LINE_WIDTH_RANGE values are " + values[0]
+ + ", " + values[1]);
+ gl.glEnable(GL2.GL_LINE_SMOOTH);
+ gl.glEnable(GL.GL_BLEND);
+ gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
+ gl.glHint(GL2.GL_LINE_SMOOTH_HINT, GL.GL_DONT_CARE);
+ gl.glLineWidth(1.5f);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(0.0f, 1.0f, 0.0f);
+ gl.glPushMatrix();
+ gl.glRotatef(-rotAngle, 0.0f, 0.0f, 0.1f);
+ gl.glBegin(GL.GL_LINES);
+ gl.glVertex2f(-0.5f, 0.5f);
+ gl.glVertex2f(0.5f, -0.5f);
+ gl.glEnd();
+ gl.glPopMatrix();
+ gl.glColor3f(0.0f, 0.0f, 1.0f);
+ gl.glPushMatrix();
+ gl.glRotatef(rotAngle, 0.0f, 0.0f, 0.1f);
+ gl.glBegin(GL.GL_LINES);
+ gl.glVertex2f(0.5f, 0.5f);
+ gl.glVertex2f(-0.5f, -0.5f);
+ gl.glEnd();
+ gl.glPopMatrix();
+ gl.glFlush();
+ if (rotate)
+ rotAngle += 1f;
+ if (rotAngle >= 360f)
+ rotAngle = 0f;
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ GLU glu = new GLU();
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h) //
+ glu.gluOrtho2D(-1.0, 1.0, -1.0 * (float) h / (float) w, //
+ 1.0 * (float) h / (float) w);
+ else
+ glu.gluOrtho2D(-1.0 * (float) w / (float) h, //
+ 1.0 * (float) w / (float) h, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ case KeyEvent.VK_R:
+ rotate = !rotate;
+ super.refresh();
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook11/hello.java b/src/redbook/src/glredbook11/hello.java new file mode 100644 index 0000000..f7ddfb1 --- /dev/null +++ b/src/redbook/src/glredbook11/hello.java @@ -0,0 +1,121 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*;import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+
+
+/**
+ * This is a simple, introductory OpenGL program in Java using the
+ * javax.media.opengl extension library.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class hello //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ @Override
+ protected GLJPanel createDrawable() {
+ // 0. optionally set additional canvas capabilities
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);// enable sample buffers for aliasing
+ caps.setNumSamples(2);
+ //
+ // 1. create canvas with the desired optional capabilities above
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ hello demo = new hello();
+
+ /*
+ * 2. Declare initial window size, position, and set frame's close
+ * behavior. Open window with "hello" in its title bar. Call
+ * initialization routines. Register callback function to display
+ * graphics. Enter main loop and process events.
+ */
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("hello");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ // 3. add canvas to frame, set it visible, and have input focus on the
+ // canvas
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ /* select clearing color (background) color */
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ /* initialize viewing values */
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ /* clear all pixels */
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ /*
+ * draw white polygon (rectangle) with corners at (0.25, 0.25, 0.0) and
+ * (0.75, 0.75, 0.0)
+ */
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glBegin(GL2.GL_POLYGON);
+ gl.glVertex3f(0.25f, 0.25f, 0.0f);
+ gl.glVertex3f(0.75f, 0.25f, 0.0f);
+ gl.glVertex3f(0.75f, 0.75f, 0.0f);
+ gl.glVertex3f(0.25f, 0.75f, 0.0f);
+ gl.glEnd();
+
+ /*
+ * don't wait! start processing buffered OpenGL routines
+ */
+ gl.glFlush();
+
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width,
+ int height) {
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void keyPressed(KeyEvent e) {
+ if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
+ System.exit(0);
+
+ }
+
+ public void keyReleased(KeyEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/image.java b/src/redbook/src/glredbook11/image.java new file mode 100644 index 0000000..857c1b1 --- /dev/null +++ b/src/redbook/src/glredbook11/image.java @@ -0,0 +1,206 @@ +package glredbook11;
+
+/**
+ * This program demonstrates drawing pixels and shows the effect of
+ * glDrawPixels(), glCopyPixels(), and glPixelZoom(). Interaction: moving the
+ * mouse while pressing the mouse button will copy the image in the lower-left
+ * corner of the window to the mouse position, using the current pixel zoom
+ * factors. There is no attempt to prevent you from drawing over the original
+ * image. If you press the 'r' key, the original image and zoom factors are
+ * reset. If you press the 'z' or 'Z' keys, you change the zoom factors.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+import java.awt.Point;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseMotionListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+
+/**
+ * This program demonstrates drawing pixels and shows the effect of
+ * glDrawPixels(), glCopyPixels(), and glPixelZoom(). Interaction: moving the
+ * mouse while pressing the mouse button will copy the image in the lower-left
+ * corner of the window to the mouse position, using the current pixel zoom
+ * factors. There is no attempt to prevent you from drawing over the original
+ * image. If you press the 'r' key, the original image and zoom factors are
+ * reset. If you press the 'z' or 'Z' keys, you change the zoom factors.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class image //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseMotionListener {
+ private GLU glu;
+ //
+ private static final int checkImageWidth = 64;
+ private static final int checkImageHeight = 64;
+ private static final int rgb = 3;
+ // private byte checkImage[][][];
+ private ByteBuffer checkImageBuf = //
+ BufferUtil.newByteBuffer(checkImageHeight * checkImageWidth * rgb);
+
+ private static float zoomFactor = 1.0f;
+ private static int height;
+ private Point mousePoint;// = new Point();
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseMotionListener(this);
+ return panel;
+ }
+ public static void main(String[] args) {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);// enable sample buffers for aliasing
+ caps.setNumSamples(caps.getNumSamples() * 2);
+
+ image demo = new image();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("image");
+ frame.setSize(250, 250);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ this.makeCheckImage();
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ if (mousePoint != null) {
+ int screeny = height - (int) mousePoint.getY();
+ gl.glRasterPos2i(mousePoint.x, screeny);
+ gl.glPixelZoom(zoomFactor, zoomFactor);
+ gl.glCopyPixels(0, 0, checkImageWidth, checkImageHeight,
+ GL2.GL_COLOR);
+ // gl.glPixelZoom(1.0f, 1.0f);
+ // mousePoint = null;
+ } else
+ gl.glRasterPos2i(0, 0);
+
+ gl.glDrawPixels(checkImageWidth, checkImageHeight, GL2.GL_RGB,
+ GL.GL_UNSIGNED_BYTE, checkImageBuf);
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ height = h;
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluOrtho2D(0.0, (double) w, 0.0, (double) h);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * 3D array won't be used. I left it here for you to see.
+ */
+ private void makeCheckImage() {
+ byte c = (byte) 0xFF;
+
+ for (int i = 0; i < checkImageWidth; i++) {
+ for (int j = 0; j < checkImageHeight; j++) {
+ // c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;
+ c = (byte) ((((byte) ((i & 0x8) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x8) == 0 ? 0x00 : 0xff))));
+ // checkImage[i][j][0] = (byte) c;
+ // checkImage[i][j][1] = (byte) c;
+ // checkImage[i][j][2] = (byte) c;
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ }
+ }
+ checkImageBuf.rewind();
+ }//
+
+ public void keyTyped(KeyEvent e) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ case 'r':
+ case 'R':
+ zoomFactor = 1.0f;
+ System.out.println("zoomFactor reset to 1.0\n");
+ break;
+ case 'z':
+ zoomFactor += 0.5;
+ if (zoomFactor >= 3.0)
+ zoomFactor = 3.0f;
+ System.out.println("zoomFactor is now " + zoomFactor);
+ break;
+ case 'Z':
+ zoomFactor -= 0.5;
+ if (zoomFactor <= 0.5)
+ zoomFactor = 0.5f;
+ System.out.println("zoomFactor is now " + zoomFactor);
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent e) {
+ }
+
+ public void mouseDragged(MouseEvent e) {
+
+ }
+
+ public void mouseMoved(MouseEvent mouse) {
+ mousePoint = mouse.getPoint();
+ // screeny = height - (int) mouse.getY();
+ super.refresh();
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/polyoff.java b/src/redbook/src/glredbook11/polyoff.java new file mode 100644 index 0000000..d90edce --- /dev/null +++ b/src/redbook/src/glredbook11/polyoff.java @@ -0,0 +1,223 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.gl2.GLUT;
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates polygon offset to draw a shaded polygon and its
+ * wireframe counterpart without ugly visual artifacts ("stitching").
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class polyoff//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ private GLUT glut;
+
+ private int list;
+ private int spinx = 0;
+ private int spiny = 0;
+ private float tdist = 0.0f;
+ private float polyfactor = 1.0f;
+ private float polyunits = 1.0f;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ polyoff demo = new polyoff();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("polyoff");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * specify initial properties create display list with sphere initialize
+ * lighting and depth buffer
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+
+ float light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+
+ float global_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+
+ list = gl.glGenLists(1);
+ gl.glNewList(list, GL2.GL_COMPILE);
+ glut.glutSolidSphere(1.0, 20, 12);
+ gl.glEndList();
+
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_ambient, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, light_diffuse, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, light_specular, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, global_ambient, 0);
+ }
+
+ /*
+ * display() draws two spheres, one with a gray, diffuse material, the other
+ * sphere with a magenta material with a specular highlight.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ /* clear all pixels */
+ // double mat_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
+ // double mat_diffuse[] = { 1.0, 0.0, 0.5, 1.0 };
+ // double mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
+ float gray[] = { 0.8f, 0.8f, 0.8f, 1.0f };
+ float black[] = { 0.0f, 0.0f, 0.0f, 1.0f };
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glPushMatrix();
+ gl.glTranslated(0.0, 0.0, tdist);
+ gl.glRotated((double) spinx, 1.0, 0.0, 0.0);
+ gl.glRotated((double) spiny, 0.0, 1.0, 0.0);
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR & GL2.GL_DIFFUSE, gray, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, black, 0);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 0.0f);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_POLYGON_OFFSET_FILL);
+ gl.glPolygonOffset(polyfactor, polyunits);
+ gl.glCallList(list);
+ gl.glDisable(GL2.GL_POLYGON_OFFSET_FILL);
+
+ gl.glDisable(GL2.GL_LIGHTING);
+ gl.glDisable(GL2.GL_LIGHT0);
+ gl.glColor3d(1.0, 1.0, 1.0);
+ gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_LINE);
+ gl.glCallList(list);
+ gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_FILL);
+
+ gl.glPopMatrix();
+ gl.glFlush();
+
+ }
+
+ /* call when window is resized */
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width,
+ int height) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, width, height);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(45.0, (double) width / (double) height, 1.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ glu.gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case 't':
+ if (tdist < 4.0)
+ tdist = (tdist + 0.5f);
+ break;
+ case 'T':
+ if (tdist > -5.0)
+ tdist = (tdist - 0.5f);
+ break;
+ case 'F':
+ polyfactor = polyfactor + 0.1f;
+ System.out.println("polyfactor is " + polyfactor);
+ break;
+ case 'f':
+ polyfactor = polyfactor - 0.1f;
+ System.out.println("polyfactor is " + polyfactor);
+ break;
+ case 'U':
+ polyunits = polyunits + 1.0f;
+ System.out.println("polyunits is " + polyunits);
+ break;
+ case 'u':
+ polyunits = polyunits - 1.0f;
+ System.out.println("polyunits is " + polyunits);
+ break;
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent mouse) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ switch (mouse.getButton()) {
+ case MouseEvent.BUTTON1:
+ spinx = (spinx + 5) % 360;
+ break;
+ case MouseEvent.BUTTON2:
+ spiny = (spiny + 5) % 360;
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/tess.java b/src/redbook/src/glredbook11/tess.java new file mode 100644 index 0000000..e2610fd --- /dev/null +++ b/src/redbook/src/glredbook11/tess.java @@ -0,0 +1,263 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*;
+
+import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+import javax.media.opengl.glu.GLUtessellator;
+import javax.media.opengl.glu.GLUtessellatorCallback;
+
+/**
+ * This program demonstrates polygon tessellation. Two tesselated objects are
+ * drawn. The first is a rectangle with a triangular hole. The second is a
+ * smooth shaded, self-intersecting star. Note the exterior rectangle is drawn
+ * with its vertices in counter-clockwise order, but its interior clockwise.
+ * Note the combineCallback is needed for the self-intersecting star. Also note
+ * that removing the TessProperty for the star will make the interior unshaded
+ * (WINDING_ODD).
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ * @NOTE Java arrays are column major whereas C arrays are row major
+ */
+public class tess//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private int startList;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ tess demo = new tess();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("tess");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ /*
+ * jogl specific addition for tessellation
+ */
+ tessellCallBack tessCallback = new tessellCallBack(gl, glu);
+ //
+ double rect[][] = new double[][] { // [4][3] in C; reverse here
+ { 50.0, 50.0, 0.0 }, { 200.0, 50.0, 0.0 }, { 200.0, 200.0, 0.0 },
+ { 50.0, 200.0, 0.0 } };
+ double tri[][] = new double[][] {// [3][3]
+ { 75.0, 75.0, 0.0 }, { 125.0, 175.0, 0.0 }, { 175.0, 75.0, 0.0 } };
+ double star[][] = new double[][] {// [5][6]; 6x5 in java
+ { 250.0, 50.0, 0.0, 1.0, 0.0, 1.0 },
+ { 325.0, 200.0, 0.0, 1.0, 1.0, 0.0 },
+ { 400.0, 50.0, 0.0, 0.0, 1.0, 1.0 },
+ { 250.0, 150.0, 0.0, 1.0, 0.0, 0.0 },
+ { 400.0, 150.0, 0.0, 0.0, 1.0, 0.0 } };
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ startList = gl.glGenLists(2);
+
+ GLUtessellator tobj = glu.gluNewTess();
+
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_VERTEX, tessCallback);// glVertex3dv);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_BEGIN, tessCallback);// beginCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_END, tessCallback);// endCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_ERROR, tessCallback);// errorCallback);
+
+ /* rectangle with triangular hole inside */
+ gl.glNewList(startList, GL2.GL_COMPILE);
+ gl.glShadeModel(GL2.GL_FLAT);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ glu.gluTessVertex(tobj, rect[0], 0, rect[0]);
+ glu.gluTessVertex(tobj, rect[1], 0, rect[1]);
+ glu.gluTessVertex(tobj, rect[2], 0, rect[2]);
+ glu.gluTessVertex(tobj, rect[3], 0, rect[3]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ glu.gluTessVertex(tobj, tri[0], 0, tri[0]);
+ glu.gluTessVertex(tobj, tri[1], 0, tri[1]);
+ glu.gluTessVertex(tobj, tri[2], 0, tri[2]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_VERTEX, tessCallback);// vertexCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_BEGIN, tessCallback);// beginCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_END, tessCallback);// endCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_ERROR, tessCallback);// errorCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_COMBINE, tessCallback);// combineCallback);
+
+ /* smooth shaded, self-intersecting star */
+ gl.glNewList(startList + 1, GL2.GL_COMPILE);
+ gl.glShadeModel(GL2.GL_SMOOTH);
+ glu.gluTessProperty(tobj, //
+ GLU.GLU_TESS_WINDING_RULE, //
+ GLU.GLU_TESS_WINDING_POSITIVE);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ glu.gluTessVertex(tobj, star[0], 0, star[0]);
+ glu.gluTessVertex(tobj, star[1], 0, star[1]);
+ glu.gluTessVertex(tobj, star[2], 0, star[2]);
+ glu.gluTessVertex(tobj, star[3], 0, star[3]);
+ glu.gluTessVertex(tobj, star[4], 0, star[4]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+ glu.gluDeleteTess(tobj);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glCallList(startList);
+ gl.glCallList(startList + 1);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluOrtho2D(0.0, (double) w, 0.0, (double) h);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent e) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent e) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+ /*
+ * Tessellator callback implemenation with all the callback routines. YOu
+ * could use GLUtesselatorCallBackAdapter instead. But
+ */
+ class tessellCallBack //
+ implements GLUtessellatorCallback //
+ {
+ private GL2 gl;
+ private GLU glu;
+
+ public tessellCallBack(GL2 gl, GLU glu) {
+ this.gl = gl;
+ this.glu = glu;
+ }
+
+ public void begin(int type) {
+ gl.glBegin(type);
+ }
+
+ public void end() {
+ gl.glEnd();
+ }
+
+ public void vertex(Object vertexData) {
+ double[] pointer;
+ if (vertexData instanceof double[]) {
+ pointer = (double[]) vertexData;
+ if (pointer.length == 6)
+ gl.glColor3dv(pointer, 3);
+ gl.glVertex3dv(pointer, 0);
+ }
+
+ }
+
+ public void vertexData(Object vertexData, Object polygonData) {
+ }
+
+ /*
+ * combineCallback is used to create a new vertex when edges intersect.
+ * coordinate location is trivial to calculate, but weight[4] may be
+ * used to average color, normal, or texture coordinate data. In this
+ * program, color is weighted.
+ */
+ public void combine(double[] coords, Object[] data, //
+ float[] weight, Object[] outData) {
+ double[] vertex = new double[6];
+ int i;
+
+ vertex[0] = coords[0];
+ vertex[1] = coords[1];
+ vertex[2] = coords[2];
+ for (i = 3; i < 6/* 7OutOfBounds from C! */; i++)
+ vertex[i] = weight[0] //
+ * ((double[]) data[0])[i] + weight[1]
+ * ((double[]) data[1])[i] + weight[2]
+ * ((double[]) data[2])[i] + weight[3]
+ * ((double[]) data[3])[i];
+ outData[0] = vertex;
+ }
+
+ public void combineData(double[] coords, Object[] data, //
+ float[] weight, Object[] outData, Object polygonData) {
+ }
+
+ public void error(int errnum) {
+ String estring;
+
+ estring = glu.gluErrorString(errnum);
+ System.err.println("Tessellation Error: " + estring);
+ System.exit(0);
+ }
+
+ public void beginData(int type, Object polygonData) {
+ }
+
+ public void endData(Object polygonData) {
+ }
+
+ public void edgeFlag(boolean boundaryEdge) {
+ }
+
+ public void edgeFlagData(boolean boundaryEdge, Object polygonData) {
+ }
+
+ public void errorData(int errnum, Object polygonData) {
+ }
+ }// tessellCallBack
+
+}// tess
diff --git a/src/redbook/src/glredbook11/tesswind.java b/src/redbook/src/glredbook11/tesswind.java new file mode 100644 index 0000000..548f519 --- /dev/null +++ b/src/redbook/src/glredbook11/tesswind.java @@ -0,0 +1,375 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+import javax.media.opengl.glu.GLUtessellator;
+import javax.media.opengl.glu.GLUtessellatorCallback;
+
+/**
+ * This program demonstrates the winding rule polygon tessellation property.
+ * Four tessellated objects are drawn, each with very different contours. When
+ * the w key is pressed, the objects are drawn with a different winding rule.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ * @NOTE Java arrays are column major whereas C arrays are row major
+ */
+public class tesswind//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUtessellator tobj;
+ private int list;
+// private int currentShape = 0;
+ private double currentWinding = GLU.GLU_TESS_WINDING_ODD;
+ private KeyEvent key;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);// enable sample buffers for aliasing
+ caps.setNumSamples(2);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args)
+ {
+ tesswind demo = new tesswind();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("tesswind");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ /*
+ * jogl specific addition for tessellation
+ */
+ tessell tessCallback = new tessell(gl, glu);
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+
+ tobj = glu.gluNewTess();
+
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_VERTEX, tessCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_BEGIN, tessCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_END, tessCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_ERROR, tessCallback);
+ glu.gluTessCallback(tobj, GLU.GLU_TESS_COMBINE, tessCallback);
+
+ list = gl.glGenLists(4);
+ makeNewLists(gl, glu);
+ }
+
+ public void display(GLAutoDrawable drawable)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ if (key != null)
+ {
+ makeNewLists(gl, glu);
+ key = null;// keyPressed re-reference again
+ }
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glPushMatrix();
+ gl.glCallList(list);
+ gl.glTranslatef(0.0f, 500.0f, 0.0f);
+ gl.glCallList(list + 1);
+ gl.glTranslatef(500.0f, -500.0f, 0.0f);
+ gl.glCallList(list + 2);
+ gl.glTranslatef(0.0f, 500.0f, 0.0f);
+ gl.glCallList(list + 3);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h) glu.gluOrtho2D(0.0, 1000.0, //
+ 0.0, 1000.0 * (double) h / (double) w);
+ else glu.gluOrtho2D(0.0, 1000.0 * (double) w / (double) h,//
+ 0.0, 1000.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged)
+ {
+ }
+
+ /*
+ * Make four display lists, each with a different tessellated object.
+ */
+ private void makeNewLists(GL2 gl, GLU glu)
+ {
+ int i;
+ final/* static */double rects[][] = new double[][]
+ { // [12][3]
+ { 50.0, 50.0, 0.0 },
+ { 300.0, 50.0, 0.0 },
+ { 300.0, 300.0, 0.0 },
+ { 50.0, 300.0, 0.0 },
+ { 100.0, 100.0, 0.0 },
+ { 250.0, 100.0, 0.0 },
+ { 250.0, 250.0, 0.0 },
+ { 100.0, 250.0, 0.0 },
+ { 150.0, 150.0, 0.0 },
+ { 200.0, 150.0, 0.0 },
+ { 200.0, 200.0, 0.0 },
+ { 150.0, 200.0, 0.0 } };
+ final/* static */double spiral[][] = new double[][]
+ {// [16][3] =
+ { 400.0, 250.0, 0.0 },
+ { 400.0, 50.0, 0.0 },
+ { 50.0, 50.0, 0.0 },
+ { 50.0, 400.0, 0.0 },
+ { 350.0, 400.0, 0.0 },
+ { 350.0, 100.0, 0.0 },
+ { 100.0, 100.0, 0.0 },
+ { 100.0, 350.0, 0.0 },
+ { 300.0, 350.0, 0.0 },
+ { 300.0, 150.0, 0.0 },
+ { 150.0, 150.0, 0.0 },
+ { 150.0, 300.0, 0.0 },
+ { 250.0, 300.0, 0.0 },
+ { 250.0, 200.0, 0.0 },
+ { 200.0, 200.0, 0.0 },
+ { 200.0, 250.0, 0.0 } };
+ final/* static */double quad1[][] = new double[][]
+ {// [4][3] =
+ { 50.0, 150.0, 0.0 },
+ { 350.0, 150.0, 0.0 },
+ { 350.0, 200.0, 0.0 },
+ { 50.0, 200.0, 0.0 } };
+ final/* static */double quad2[][] = new double[][]
+ { // [4][3] =
+ { 100.0, 100.0, 0.0 },
+ { 300.0, 100.0, 0.0 },
+ { 300.0, 350.0, 0.0 },
+ { 100.0, 350.0, 0.0 } };
+ final/* static */double tri[][] = new double[][]
+ {// [3][3] =
+ { 200.0, 50.0, 0.0 },
+ { 250.0, 300.0, 0.0 },
+ { 150.0, 300.0, 0.0 } };
+
+ glu.gluTessProperty(tobj, //
+ GLU.GLU_TESS_WINDING_RULE, currentWinding);
+
+ gl.glNewList(list, GL2.GL_COMPILE);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 4; i++)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 4; i < 8; i++)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 8; i < 12; i++)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+
+ gl.glNewList(list + 1, GL2.GL_COMPILE);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 4; i++)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 7; i >= 4; i--)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 11; i >= 8; i--)
+ glu.gluTessVertex(tobj, rects[i], 0, rects[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+
+ gl.glNewList(list + 2, GL2.GL_COMPILE);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 16; i++)
+ glu.gluTessVertex(tobj, spiral[i], 0, spiral[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+
+ gl.glNewList(list + 3, GL2.GL_COMPILE);
+ glu.gluTessBeginPolygon(tobj, null);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 4; i++)
+ glu.gluTessVertex(tobj, quad1[i], 0, quad1[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 4; i++)
+ glu.gluTessVertex(tobj, quad2[i], 0, quad2[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessBeginContour(tobj);
+ for (i = 0; i < 3; i++)
+ glu.gluTessVertex(tobj, tri[i], 0, tri[i]);
+ glu.gluTessEndContour(tobj);
+ glu.gluTessEndPolygon(tobj);
+ gl.glEndList();
+ }
+
+ public void keyTyped(KeyEvent e)
+ {
+ }
+
+ public void keyPressed(KeyEvent key)
+ {
+ this.key = key;
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+
+ case KeyEvent.VK_W:
+ if (currentWinding == GLU.GLU_TESS_WINDING_ODD) currentWinding = GLU.GLU_TESS_WINDING_NONZERO;
+ else if (currentWinding == GLU.GLU_TESS_WINDING_NONZERO) currentWinding = GLU.GLU_TESS_WINDING_POSITIVE;
+ else if (currentWinding == GLU.GLU_TESS_WINDING_POSITIVE) currentWinding = GLU.GLU_TESS_WINDING_NEGATIVE;
+ else if (currentWinding == GLU.GLU_TESS_WINDING_NEGATIVE) currentWinding = GLU.GLU_TESS_WINDING_ABS_GEQ_TWO;
+ else if (currentWinding == GLU.GLU_TESS_WINDING_ABS_GEQ_TWO) currentWinding = GLU.GLU_TESS_WINDING_ODD;
+ drawable.display();
+ break;
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent e)
+ {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+ /*
+ * Tessellator callback implemenation with all the callback routines. YOu
+ * could use GLUtesselatorCallBackAdapter instead. But
+ */
+ class tessell
+ implements GLUtessellatorCallback
+ {
+ private GL2 gl;
+ private GLU glu;
+
+ public tessell(GL2 gl, GLU glu)
+ {
+ this.gl = gl;
+ this.glu = glu;
+ }
+
+ public void begin(int type)
+ {
+ gl.glBegin(type);
+ }
+
+ public void end()
+ {
+ gl.glEnd();
+ }
+
+ public void vertex(Object vertexData)
+ {
+ double[] pointer;
+ if (vertexData instanceof double[])
+ {
+ pointer = (double[]) vertexData;
+ if (pointer.length == 6) gl.glColor3dv(pointer, 3);
+ gl.glVertex3dv(pointer, 0);
+ }
+
+ }
+
+ public void vertexData(Object vertexData, Object polygonData)
+ {
+ }
+
+ /*
+ * combineCallback is used to create a new vertex when edges intersect.
+ * coordinate location is trivial to calculate, but weight[4] may be used to
+ * average color, normal, or texture coordinate data. In this program, color
+ * is weighted.
+ */
+ public void combine(double[] coords, Object[] data, //
+ float[] weight, Object[] outData)
+ {
+ double[] vertex = new double[3];
+
+ vertex[0] = coords[0];
+ vertex[1] = coords[1];
+ vertex[2] = coords[2];
+ outData[0] = vertex;
+ }
+
+ public void combineData(double[] coords, Object[] data, //
+ float[] weight, Object[] outData, Object polygonData)
+ {
+ }
+
+ public void error(int errnum)
+ {
+ String estring;
+
+ estring = glu.gluErrorString(errnum);
+ System.err.println("Tessellation Error: " + estring);
+ System.exit(0);
+ }
+
+ public void beginData(int type, Object polygonData)
+ {
+ }
+
+ public void endData(Object polygonData)
+ {
+ }
+
+ public void edgeFlag(boolean boundaryEdge)
+ {
+ }
+
+ public void edgeFlagData(boolean boundaryEdge, Object polygonData)
+ {
+ }
+
+ public void errorData(int errnum, Object polygonData)
+ {
+ }
+ }// tessellCallBack
+
+}// tess
diff --git a/src/redbook/src/glredbook11/texbind.java b/src/redbook/src/glredbook11/texbind.java new file mode 100644 index 0000000..9cbdd90 --- /dev/null +++ b/src/redbook/src/glredbook11/texbind.java @@ -0,0 +1,192 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates using glBindTexture() by creating and managing two
+ * textures.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class texbind //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ //
+ private static final int rgba = 4;
+ private static final int checkImageWidth = 64;
+ private static final int checkImageHeight = 64;
+ // private byte checkImage[][][];
+ // private byte otherImage[][][];
+ private ByteBuffer checkImageBuf = //
+ BufferUtil.newByteBuffer(checkImageWidth * checkImageHeight * rgba);
+ private ByteBuffer otherImageBuf = //
+ BufferUtil.newByteBuffer(checkImageWidth * checkImageHeight * rgba);
+ private int[] texName = new int[2];
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ texbind demo = new texbind();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("texbind");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ makeCheckImages();
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ gl.glGenTextures(2, texName, 0);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, checkImageWidth,
+ checkImageHeight, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ checkImageBuf);
+
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[1]);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, checkImageWidth,
+ checkImageHeight, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ otherImageBuf);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(-2.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 1.0);
+ gl.glVertex3d(-2.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 1.0);
+ gl.glVertex3d(0.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 0.0);
+ gl.glVertex3d(0.0, -1.0, 0.0);
+ gl.glEnd();
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[1]);
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(1.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 1.0);
+ gl.glVertex3d(1.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 1.0);
+ gl.glVertex3d(2.41421, 1.0, -1.41421);
+ gl.glTexCoord2d(1.0, 0.0);
+ gl.glVertex3d(2.41421, -1.0, -1.41421);
+ gl.glEnd();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 30.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslated(0.0, 0.0, -3.6);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeCheckImages() {
+ byte c = 0x00;
+
+ for (int i = 0; i < checkImageWidth; i++) {
+ for (int j = 0; j < checkImageHeight; j++) {
+
+ c = (byte) ((((byte) ((i & 0x8) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x8) == 0 ? 0x00 : 0xff))));
+ // checkImage[i][j][0] = (byte) c;
+ // checkImage[i][j][1] = (byte) c;
+ // checkImage[i][j][2] = (byte) c;
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) 0xff);
+
+ c = (byte) ((((byte) ((i & 0x10) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x10) == 0 ? 0x00 : 0xff))));
+
+ otherImageBuf.put((byte) c);
+ otherImageBuf.put((byte) 0);
+ otherImageBuf.put((byte) 0);
+ otherImageBuf.put((byte) 0xff);
+ }
+ }
+ checkImageBuf.rewind();
+ otherImageBuf.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook11/texgen.java b/src/redbook/src/glredbook11/texgen.java new file mode 100644 index 0000000..aabda01 --- /dev/null +++ b/src/redbook/src/glredbook11/texgen.java @@ -0,0 +1,202 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+
+import com.sun.opengl.util.gl2.GLUT;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * This program draws a texture mapped teapot with automatically generated
+ * texture coordinates. The texture is rendered as stripes on the teapot.
+ * Initially, the object is drawn with texture coordinates based upon the object
+ * coordinates of the vertex and distance from the plane x = 0. Pressing the 'e'
+ * key changes the coordinate generation to eye coordinates of the vertex.
+ * Pressing the 'o' key switches it back to the object coordinates. Pressing the
+ * 's' key changes the plane to a slanted one (x + y + z = 0). Pressing the 'x'
+ * key switches it back to x = 0.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class texgen//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLUT glut;
+ private static final int stripeImageWidth = 32;
+ private byte stripeImage[] = new byte[3 * stripeImageWidth];
+ private ByteBuffer stripeImageBuf = BufferUtil
+ .newByteBuffer(stripeImage.length);
+ /* glTexGen stuff: */
+ // private float sgenparams[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+ private int texName[] = new int[1];
+ private static double xequalzero[] = { 1.0, 0.0, 0.0, 0.0 };
+ private static double slanted[] = { 1.0, 1.0, 1.0, 0.0 };
+ private static double currentCoeff[];
+ private static int currentPlane;
+ private static int currentGenMode;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);// enable sample buffers for aliasing
+ caps.setNumSamples(2);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ texgen demo = new texgen();
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("texgen");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glut = new GLUT();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ makeStripeImage();
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ gl.glGenTextures(1, texName, 0);
+ gl.glBindTexture(GL2.GL_TEXTURE_1D, texName[0]);
+
+ gl
+ .glTexParameterf(GL2.GL_TEXTURE_1D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ gl.glTexParameterf(GL2.GL_TEXTURE_1D, //
+ GL2.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
+ gl.glTexParameterf(GL2.GL_TEXTURE_1D, //
+ GL2.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
+ gl.glTexImage1D(GL2.GL_TEXTURE_1D, 0, GL2.GL_RGBA, stripeImageWidth, //
+ 0, GL2.GL_RGB, GL.GL_UNSIGNED_BYTE, stripeImageBuf);
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_MODULATE);
+ currentCoeff = xequalzero;
+ currentGenMode = GL2.GL_OBJECT_LINEAR;
+ currentPlane = GL2.GL_OBJECT_PLANE;
+ gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_OBJECT_LINEAR);
+ gl.glTexGendv(GL2.GL_S, GL2.GL_OBJECT_PLANE, currentCoeff, 0);
+
+ // gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glDepthFunc(GL.GL_LESS);
+ gl.glEnable(GL2.GL_TEXTURE_GEN_S);
+ gl.glEnable(GL2.GL_TEXTURE_1D);
+ gl.glEnable(GL.GL_CULL_FACE);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+ gl.glFrontFace(GL.GL_CW);
+ gl.glCullFace(GL.GL_BACK);
+ gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 64.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glPushMatrix();
+ gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
+
+ gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, currentGenMode);
+ gl.glTexGendv(GL2.GL_S, currentPlane, currentCoeff, 0);
+
+ gl.glBindTexture(GL2.GL_TEXTURE_1D, texName[0]);
+ glut.glutSolidTeapot(2.0f);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-3.5, 3.5, -3.5 * (float) h / (float) w, 3.5 * (float) h
+ / (float) w, -3.5, 3.5);
+ else
+ gl.glOrtho(-3.5 * (float) w / (float) h, //
+ 3.5 * (float) w / (float) h, -3.5, 3.5, -3.5, 3.5);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeStripeImage() {
+ for (int j = 0; j < stripeImageWidth; j++) {
+ // stripeImage[3 * j] = (j <= 4) ? 255 : 0;
+ // stripeImage[3 * j + 1] = (j > 4) ? 255 : 0;
+ // stripeImage[3 * j + 2] = 0;
+ stripeImageBuf.put(((j <= 4) ? (byte) 255 : (byte) 0));
+ stripeImageBuf.put(((j > 4) ? (byte) 255 : (byte) 0));
+ stripeImageBuf.put((byte) 0);
+ }
+ stripeImageBuf.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ case 'e':
+ case 'E':
+ currentGenMode = GL2.GL_EYE_LINEAR;
+ currentPlane = GL2.GL_EYE_PLANE;
+ break;
+ case 'o':
+ case 'O':
+ currentGenMode = GL2.GL_OBJECT_LINEAR;
+ currentPlane = GL2.GL_OBJECT_PLANE;
+ break;
+ case 's':
+ case 'S':
+ currentCoeff = slanted;
+ break;
+ case 'x':
+ case 'X':
+ currentCoeff = xequalzero;
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/texprox.java b/src/redbook/src/glredbook11/texprox.java new file mode 100644 index 0000000..2004645 --- /dev/null +++ b/src/redbook/src/glredbook11/texprox.java @@ -0,0 +1,113 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*; import javax.media.opengl.awt.GLJPanel;
+import javax.swing.JFrame;
+
+/**
+ * The brief program illustrates use of texture proxies. This program only
+ * prints out some messages about whether certain size textures are supported
+ * and then exits.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class texprox//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ texprox demo = new texprox();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("texprox");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+
+ System.out.println("This program demonstrates a feature "
+ + "which is not in OpenGL Version 1.0.");
+ System.out.println("If your implementation of OpenGL "
+ + "Version 1.0 has the right extensions,");
+ System.out.println("you may be able to modify this "
+ + "program to make it run.");
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ int proxyComponents[] = new int[1];
+
+ gl.glTexImage2D(GL2.GL_PROXY_TEXTURE_2D, 0, GL2.GL_RGBA8, 64, 64, 0,
+ GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE, null);
+ gl.glGetTexLevelParameteriv(GL2.GL_PROXY_TEXTURE_2D, 0,
+ GL2.GL_TEXTURE_COMPONENTS, proxyComponents, 0);
+ System.out.println("proxyComponents are " + proxyComponents[0]);
+ if (proxyComponents[0] == GL2.GL_RGBA8) //
+ System.out.println("proxy allocation succeeded");
+ else
+ System.out.println("proxy allocation failed");
+ //
+ gl.glTexImage2D(GL2.GL_PROXY_TEXTURE_2D, 0, GL2.GL_RGBA16, 2048, 2048, 0,
+ GL2.GL_RGBA, GL.GL_UNSIGNED_SHORT, null);
+ gl.glGetTexLevelParameteriv(GL2.GL_PROXY_TEXTURE_2D, 0,
+ GL2.GL_TEXTURE_COMPONENTS, proxyComponents, 0);
+ System.out.println("proxyComponents are " + proxyComponents[0]);
+ if (proxyComponents[0] == GL2.GL_RGBA16) //
+ System.out.println("proxy allocation succeeded");
+ else
+ System.out.println("proxy allocation failed");
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ drawable.getGL().glClear(GL.GL_COLOR_BUFFER_BIT);
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook11/texsub.java b/src/redbook/src/glredbook11/texsub.java new file mode 100644 index 0000000..93a2d21 --- /dev/null +++ b/src/redbook/src/glredbook11/texsub.java @@ -0,0 +1,219 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program texture maps a checkerboard image onto two rectangles. This
+ * program clamps the texture, if the texture coordinates fall outside 0.0 and
+ * 1.0. If the s key is pressed, a texture subimage is used to alter the
+ * original texture. If the r key is pressed, the original texture is restored.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class texsub
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private KeyEvent key;
+ //
+ private static final int rgba = 4;
+ private static final int checkImageWidth = 64;
+ private static final int checkImageHeight = 64;
+ private static final int subImageWidth = 16;
+ private static final int subImageHeight = 16;
+ // private byte checkImage[][][];
+ // private byte otherImage[][][];
+ private ByteBuffer checkImageBuf = //
+ BufferUtil.newByteBuffer(checkImageWidth * checkImageHeight * rgba);
+ private ByteBuffer subImageBuf = //
+ BufferUtil.newByteBuffer(subImageWidth * subImageHeight * rgba);
+ private int[] texName = new int[2];
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ texsub demo = new texsub();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("texsub");
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ makeCheckImages();
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ gl.glGenTextures(2, texName, 0);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl
+ .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ gl
+ .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
+ GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, checkImageWidth,
+ checkImageHeight, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ checkImageBuf);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ if (key != null) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_S:
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glTexSubImage2D(GL2.GL_TEXTURE_2D, 0, 12, 44, subImageWidth,
+ subImageHeight, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ subImageBuf);
+ break;
+ case KeyEvent.VK_R:
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA,
+ checkImageWidth, checkImageHeight, 0, GL2.GL_RGBA,
+ GL.GL_UNSIGNED_BYTE, checkImageBuf);
+ break;
+
+ default:
+ break;
+ }
+ key = null;
+ }
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(-2.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 1.0);
+ gl.glVertex3d(-2.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 1.0);
+ gl.glVertex3d(0.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 0.0);
+ gl.glVertex3d(0.0, -1.0, 0.0);
+
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(1.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 1.0);
+ gl.glVertex3d(1.0, 1.0, 0.0);
+ gl.glTexCoord2d(1.0, 1.0);
+ gl.glVertex3d(2.41421, 1.0, -1.41421);
+ gl.glTexCoord2d(1.0, 0.0);
+ gl.glVertex3d(2.41421, -1.0, -1.41421);
+ gl.glEnd();
+ gl.glFlush();
+
+ gl.glDisable(GL2.GL_TEXTURE_2D);
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 30.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslated(0.0, 0.0, -3.6);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeCheckImages() {
+ byte c = 0x00;
+
+ for (int i = 0; i < checkImageWidth; i++) {
+ for (int j = 0; j < checkImageHeight; j++) {
+
+ c = (byte) ((((byte) ((i & 0x8) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x8) == 0 ? 0x00 : 0xff))));
+ // checkImage[i][j][0] = (byte) c;
+ // checkImage[i][j][1] = (byte) c;
+ // checkImage[i][j][2] = (byte) c;
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) 0xff);
+ }
+ }
+
+ for (int i = 0; i < subImageHeight; i++) {
+ for (int j = 0; j < subImageWidth; j++) {
+ c = (byte) ((((byte) ((i & 0x4) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x4) == 0 ? 0x00 : 0xff))));
+ subImageBuf.put((byte) c);
+ subImageBuf.put((byte) 0);
+ subImageBuf.put((byte) 0);
+ subImageBuf.put((byte) 0xff);
+ }
+ }
+ checkImageBuf.rewind();
+ subImageBuf.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ this.key = key;
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook11/torus.java b/src/redbook/src/glredbook11/torus.java new file mode 100644 index 0000000..414d1f5 --- /dev/null +++ b/src/redbook/src/glredbook11/torus.java @@ -0,0 +1,150 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import javax.swing.*;
+import javax.media.opengl.*;
+import java.awt.event.*;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates the creation of a display list.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class torus//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private int theTorus;
+ private KeyEvent key;
+ private float angleX = 30f;
+ private float angleY = 30f;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ torus demo = new torus();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("torus");
+ frame.setSize(512, 512);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ private void drawTorus(GL2 gl, int numc, int numt) {
+ double s, t, x, y, z, twopi = 2 * Math.PI;
+ for (int i = 0; i < numc; i++) {
+ gl.glBegin(GL2.GL_QUAD_STRIP);
+ for (int j = 0; j <= numt; j++) {
+ for (int k = 1; k >= 0; k--) {
+ s = (i + k) % numc + 0.5;
+ t = j % numt;
+ x = (1 + 0.1 * Math.cos(s * twopi / numc))
+ * Math.cos(t * twopi / numt);
+ y = (1 + 0.1 * Math.cos(s * twopi / numc))
+ * Math.sin(t * twopi / numt);
+ z = 0.1 * Math.sin(s * twopi / numc);
+ gl.glVertex3d(x, y, z);
+ }// k
+ }// j
+ gl.glEnd();
+ }// i
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ theTorus = gl.glGenLists(1);
+ gl.glNewList(theTorus, GL2.GL_COMPILE);
+ drawTorus(gl, 8, 25);
+ gl.glEndList();
+ //
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ GLU glu = new GLU();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ //
+ if (key != null) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_X:
+ gl.glRotated(angleX, 1f, 0f, 0f);
+ break;
+ case KeyEvent.VK_Y:
+ gl.glRotated(angleY, 0f, 1f, 0f);
+ break;
+ case KeyEvent.VK_I:
+ gl.glLoadIdentity();
+ glu.gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
+ break;
+ default:
+ break;
+ }
+ }
+
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glCallList(theTorus);
+ gl.glPopMatrix();
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width,
+ int height) {
+ GL2 gl = drawable.getGL().getGL2();
+ GLU glu = new GLU();
+ //
+ gl.glViewport(0, 0, width, height);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(30, (float) width / (float) height, 1.0, 100.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ glu.gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ this.key = key;
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook11/unproject.java b/src/redbook/src/glredbook11/unproject.java new file mode 100644 index 0000000..4f5514d --- /dev/null +++ b/src/redbook/src/glredbook11/unproject.java @@ -0,0 +1,165 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+
+/**
+ * When the left mouse button is pressed, this program reads the mouse position
+ * and determines two 3D points from which it was transformed. Very little is
+ * displayed.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class unproject//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ private MouseEvent mouse;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ unproject demo = new unproject();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("unproject");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+// GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ int viewport[] = new int[4];
+ double mvmatrix[] = new double[16];
+ double projmatrix[] = new double[16];
+ int realy = 0;// GL y coord pos
+ double wcoord[] = new double[4];// wx, wy, wz;// returned xyz coords
+ if (mouse != null) {
+ int x = mouse.getX(), y = mouse.getY();
+ switch (mouse.getButton()) {
+ case MouseEvent.BUTTON1:
+ gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
+ gl.glGetDoublev(GL2.GL_MODELVIEW_MATRIX, mvmatrix, 0);
+ gl.glGetDoublev(GL2.GL_PROJECTION_MATRIX, projmatrix, 0);
+ /* note viewport[3] is height of window in pixels */
+ realy = viewport[3] - (int) y - 1;
+ System.out.println("Coordinates at cursor are (" + x + ", "
+ + realy);
+ glu.gluUnProject((double) x, (double) realy, 0.0, //
+ mvmatrix, 0,//
+ projmatrix, 0, //
+ viewport, 0, //
+ wcoord, 0);
+ System.out
+ .println("World coords at z=0.0 are ( " //
+ + wcoord[0] + ", " + wcoord[1] + ", "
+ + wcoord[2] + ")");
+ glu.gluUnProject((double) x, (double) realy, 1.0, //
+ mvmatrix, 0,//
+ projmatrix, 0,//
+ viewport, 0, //
+ wcoord, 0);
+ System.out
+ .println("World coords at z=1.0 are (" //
+ + wcoord[0] + ", " + wcoord[1] + ", "
+ + wcoord[2] + ")");
+ break;
+ case MouseEvent.BUTTON2:
+ break;
+ default:
+ break;
+ }
+ }
+
+ gl.glFlush();
+ }
+
+ /* Change these values for a different transformation */
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(45.0, (float) w / (float) h, 1.0, 100.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent e) {
+ }
+
+ public void mousePressed(MouseEvent e) {
+ mouse = e;
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent e) {
+ }
+
+ public void mouseEntered(MouseEvent e) {
+ }
+
+ public void mouseExited(MouseEvent e) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/varray.java b/src/redbook/src/glredbook11/varray.java new file mode 100644 index 0000000..7e9379a --- /dev/null +++ b/src/redbook/src/glredbook11/varray.java @@ -0,0 +1,228 @@ +package glredbook11;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.nio.FloatBuffer;
+import java.nio.IntBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates vertex arrays.
+ *
+ * @author Chris Brown (bug fix)
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+public class varray//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener, MouseListener {
+ private GLU glu;
+ private final int POINTER = 1;
+ private final int INTERLEAVED = 2;
+
+ private final int DRAWARRAY = 1;
+ private final int ARRAYELEMENT = 2;
+ private final int DRAWELEMENTS = 3;
+
+ private int setupMethod = POINTER;
+ private int derefMethod = DRAWARRAY;
+
+ private IntBuffer indicesBuf;
+ private IntBuffer verticesBuf;
+ private FloatBuffer colorsBuf;
+ private FloatBuffer intertwinedBuf;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ panel.addMouseListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);// enable sample buffers for aliasing
+ caps.setNumSamples(2);
+
+ varray demo = new varray();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("varray");
+ frame.setSize(512, 256);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_SMOOTH);
+ setupPointers(gl);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ if (derefMethod == DRAWARRAY) {
+ gl.glDrawArrays(GL2.GL_TRIANGLES, 0, 6);
+ } else if (derefMethod == ARRAYELEMENT) {
+ gl.glBegin(GL2.GL_TRIANGLES);
+ gl.glArrayElement(2);
+ gl.glArrayElement(3);
+ gl.glArrayElement(5);
+ gl.glEnd();
+ } else if (derefMethod == DRAWELEMENTS) {
+ int indices[] = new int[] { 0, 1, 3, 4 };
+ if (indicesBuf == null) {
+ indicesBuf = BufferUtil.newIntBuffer(indices.length);
+ indicesBuf.put(indices);
+ }
+ indicesBuf.rewind();
+ gl.glDrawElements(GL2.GL_POLYGON, 4, GL2.GL_UNSIGNED_INT, indicesBuf);
+ }
+ gl.glFlush();
+
+ // gl calls from C example's mouse routine are moved here
+ if (setupMethod == INTERLEAVED)
+ setupInterleave(gl);
+ else if (setupMethod == POINTER)
+ setupPointers(gl);
+
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluOrtho2D(0.0, (double) w, 0.0, (double) h);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void setupPointers(GL2 gl) {
+ int vertices[] = new int[] { 25, 25, 100, 325, 175, 25, 175, 325, 250,
+ 25, 325, 325 };
+ float colors[] = new float[] { 1.0f, 0.2f, 0.2f, 0.2f, 0.2f, 1.0f,
+ 0.8f, 1.0f, 0.2f, 0.75f, 0.75f, 0.75f, 0.35f, 0.35f, 0.35f,
+ 0.5f, 0.5f, 0.5f };
+
+ if (verticesBuf == null) {// IntBuffer tmpVerticesBuf
+ verticesBuf = BufferUtil.newIntBuffer(vertices.length);
+ verticesBuf.put(vertices);
+ }
+ if (colorsBuf == null) {
+ colorsBuf = BufferUtil.newFloatBuffer(colors.length);
+ colorsBuf.put(colors);
+ }
+ verticesBuf.rewind();
+ colorsBuf.rewind();
+ //
+ gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
+ gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
+ //
+ gl.glVertexPointer(2, GL2.GL_INT, 0, verticesBuf);
+ gl.glColorPointer(3, GL.GL_FLOAT, 0, colorsBuf);
+
+ }
+
+ private void setupInterleave(GL2 gl) {
+ float intertwined[] = new float[] { 1.0f, 0.2f, 1.0f, 100.0f, 100.0f,
+ 0.0f, 1.0f, 0.2f, 0.2f, 0.0f, 200.0f, 0.0f, 1.0f, 1.0f, 0.2f,
+ 100.0f, 300.0f, 0.0f, 0.2f, 1.0f, 0.2f, 200.0f, 300.0f, 0.0f,
+ 0.2f, 1.0f, 1.0f, 300.0f, 200.0f, 0.0f, 0.2f, 0.2f, 1.0f,
+ 200.0f, 100.0f, 0.0f };
+ if (intertwinedBuf == null) {
+ intertwinedBuf = BufferUtil.newFloatBuffer(intertwined.length);
+ intertwinedBuf.put(intertwined);
+ }
+
+ intertwinedBuf.rewind();
+ gl.glInterleavedArrays(GL2.GL_C3F_V3F, 0, intertwinedBuf);
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void mouseClicked(MouseEvent mouse) {
+ }
+
+ public void mousePressed(MouseEvent mouse) {
+ if (mouse.getButton() == MouseEvent.BUTTON1) {
+ if (setupMethod == POINTER) {
+ setupMethod = INTERLEAVED;
+ // setupInterleave(gl);don't call
+ } else if (setupMethod == INTERLEAVED) {
+ setupMethod = POINTER;
+ // setupPointers(gl);
+ }
+ // validate();
+ }
+ if (mouse.getButton() == MouseEvent.BUTTON2
+ || mouse.getButton() == MouseEvent.BUTTON3) {
+ if (derefMethod == DRAWARRAY)
+ derefMethod = ARRAYELEMENT;
+ else if (derefMethod == ARRAYELEMENT)
+ derefMethod = DRAWELEMENTS;
+ else if (derefMethod == DRAWELEMENTS)
+ derefMethod = DRAWARRAY;
+ // validate();
+ }
+ super.refresh();
+ }
+
+ public void mouseReleased(MouseEvent mouse) {
+ }
+
+ public void mouseEntered(MouseEvent mouse) {
+ }
+
+ public void mouseExited(MouseEvent mouse) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook11/wrap.java b/src/redbook/src/glredbook11/wrap.java new file mode 100644 index 0000000..33e7281 --- /dev/null +++ b/src/redbook/src/glredbook11/wrap.java @@ -0,0 +1,210 @@ +package glredbook11;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program texture maps a checkerboard image onto two rectangles. This
+ * program demonstrates the wrapping modes, if the texture coordinates fall
+ * outside 0.0 and 1.0. Interaction: Pressing the 's' and 'S' keys switch the
+ * wrapping between clamping and repeating for the s parameter. The 't' and 'T'
+ * keys control the wrapping for the t parameter. If running this program on
+ * OpenGL 1.0, texture objects are not used.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class wrap//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+
+ private int texName[] = new int[1];
+ private static final int checkImageWidth = 64;
+ private static final int checkImageHeight = 64;
+ private static final int rgba = 4;
+ // private byte[][][] checkImage;
+ private ByteBuffer checkImageBuf = //
+ BufferUtil.newByteBuffer(checkImageHeight * checkImageWidth * rgba);
+ private KeyEvent key;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ wrap demo = new wrap();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("wrap");
+ frame.setSize(250, 250);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ System.out.println("" + GL.GL_VERSION);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ makeCheckImage();
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ gl.glGenTextures(1, texName, 0);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+
+ gl
+ .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ gl
+ .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
+ GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, checkImageWidth,
+ checkImageHeight, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ checkImageBuf);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ if (key != null) {
+ switch (key.getKeyChar()) {
+ case 's':
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_CLAMP);
+ break;
+ case 'S':
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ break;
+ case 't':
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
+ GL2.GL_CLAMP);
+ break;
+ case 'T':
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
+ GL2.GL_REPEAT);
+ break;
+ default:
+ break;
+ }
+ }
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_DECAL);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(-2.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 3.0);
+ gl.glVertex3d(-2.0, 1.0, 0.0);
+ gl.glTexCoord2d(3.0, 3.0);
+ gl.glVertex3d(0.0, 1.0, 0.0);
+ gl.glTexCoord2d(3.0, 0.0);
+ gl.glVertex3d(0.0, -1.0, 0.0);
+
+ gl.glTexCoord2d(0.0, 0.0);
+ gl.glVertex3d(1.0, -1.0, 0.0);
+ gl.glTexCoord2d(0.0, 3.0);
+ gl.glVertex3d(1.0, 1.0, 0.0);
+ gl.glTexCoord2d(3.0, 3.0);
+ gl.glVertex3d(2.41421, 1.0, -1.41421);
+ gl.glTexCoord2d(3.0, 0.0);
+ gl.glVertex3d(2.41421, -1.0, -1.41421);
+ gl.glEnd();
+ gl.glFlush();
+ gl.glDisable(GL2.GL_TEXTURE_2D);
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 30.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslated(0.0, 0.0, -3.6);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeCheckImage() {
+ // byte c = (~(i & 0x8) ^ ~(j & 0x8)) * 255;
+ byte c = 0;
+ for (int i = 0; i < checkImageHeight; i++) {
+ for (int j = 0; j < checkImageWidth; j++) {
+ // c = ((((i&0x8)==0)^((j&0x8))==0))*255;C' version
+ c = (byte) ((((byte) ((i & 0x8) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x8) == 0 ? 0x00 : 0xff))));
+ System.out.print("" + (byte) c + " ");
+ // checkImage[i][j][0] = ( byte) c;
+ // checkImage[i][j][1] = ( byte) c;
+ // checkImage[i][j][2] = ( byte) c;
+ // checkImage[i][j][3] = ( byte) 0xff;
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) c);
+ checkImageBuf.put((byte) 0xff);
+ }
+ System.out.println();
+ }
+ checkImageBuf.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ this.key = key;
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook12x/README.txt b/src/redbook/src/glredbook12x/README.txt new file mode 100644 index 0000000..4c82de2 --- /dev/null +++ b/src/redbook/src/glredbook12x/README.txt @@ -0,0 +1,37 @@ +These are the example programs which are featured in the OpenGL
+Programming Guide, Version 1.2. To compile these programs, you
+need OpenGL development libraries for your machine using the
+Java language and platform. Namely, JOGL/JSR-231 from
+http://jogl-dev.java.net.
+
+
+All programs has keyboard listener to exit on ESC press.
+
+* OpenGL 1.1 to 1.2 compatiblity issues
+
+Most of the programs included in this distribution will work with
+OpenGL 1.1.
+
+* Notes for programs demonstrating the ARB Imaging Subset
+
+With the introduction of OpenGL 1.2, the OpenGL Architecture Review
+Board added the ARB Imaging Subset. The imaging subset is not part of
+the core OpenGL functionality, and as such, your implementation may not
+support it. Some vendors implement only choose a subset of the Imaging
+functions, so some program here will not display the expected results.
+These programs check for the Imaging extension as well as the functions
+it needs to call inorder to run; otherwise, the program just display
+the original image.
+
+These programs are:
+colormatrix.c,
+colortable.c,
+convolution.c,
+histogram.c,
+minmax.c, and
+blendeqn.c.
+
+Thank you.
+
+Kiet Le - [email protected] (200607)
+Red Book Porter
\ No newline at end of file diff --git a/src/redbook/src/glredbook12x/blendeqn.java b/src/redbook/src/glredbook12x/blendeqn.java new file mode 100644 index 0000000..7b979fc --- /dev/null +++ b/src/redbook/src/glredbook12x/blendeqn.java @@ -0,0 +1,179 @@ +package glredbook12x;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.awt.event.MouseWheelListener;
+
+import javax.media.opengl.*; import javax.media.opengl.awt.GLJPanel;
+import javax.swing.JFrame;
+
+/**
+ * Demonstrate the different blending functions available with the OpenGL
+ * imaging subset. This program demonstrates use of the glBlendEquation() call.
+ * The following keys change the selected blend equation function: <br>
+ * <ul>
+ * <li>'a' -> GL_FUNC_ADD
+ * <li>'s' -> GL_FUNC_SUBTRACT
+ * <li>'r' -> GL_FUNC_REVERSE_SUBTRACT
+ * <li>'m' -> GL_MIN 'x' -> GL_MAX
+ * </ul>
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+
+public class blendeqn//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private KeyEvent key;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args)
+ {
+ blendeqn demo = new blendeqn();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("blendeqn");
+ frame.setSize(640, 480);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClearColor(1.0f, 1.0f, 0.0f, 0.0f);
+
+ gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE);
+ gl.glEnable(GL.GL_BLEND);
+ }
+
+ public void display(GLAutoDrawable drawable)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ if (key != null)
+ {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_A:
+ /*
+ * Colors are added as: (1, 1, 0) + (0, 0, 1) = (1, 1, 1) which will
+ * produce a white square on a yellow background.
+ */
+ gl.glBlendEquation(GL.GL_FUNC_ADD);
+ break;
+
+ case KeyEvent.VK_S:
+ /*
+ * Colors are subtracted as: (0, 0, 1) - (1, 1, 0) = (-1, -1, 1) which
+ * is clamped to (0, 0, 1), producing a blue square on a yellow
+ * background
+ */
+ gl.glBlendEquation(GL.GL_FUNC_SUBTRACT);
+ break;
+
+ case KeyEvent.VK_R:
+ /*
+ * Colors are subtracted as: (1, 1, 0) - (0, 0, 1) = (1, 1, -1) which
+ * is clamed to (1, 1, 0). This produces yellow for both the square
+ * and the background.
+ */
+ gl.glBlendEquation(GL.GL_FUNC_REVERSE_SUBTRACT);
+ break;
+
+ case KeyEvent.VK_M:
+
+ /*
+ * The minimum of each component is computed, as [min(1, 0), min(1,
+ * 0), min(0, 1)] which equates to (0, 0, 0). This will produce a
+ * black square on the yellow background.
+ */
+ gl.glBlendEquation(GL2.GL_MIN);
+ break;
+
+ case KeyEvent.VK_X:
+ /*
+ * The minimum of each component is computed, as [max(1, 0), max(1,
+ * 0), max(0, 1)] which equates to (1, 1, 1) This will produce a white
+ * square on the yellow background.
+ */
+ gl.glBlendEquation(GL2.GL_MAX);
+ break;
+ }
+ key = null;
+ }
+ gl.glColor3f(0.0f, 0.0f, 1.0f);
+ gl.glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
+
+ gl.glFlush();
+
+ }
+
+ public void reshape(GLAutoDrawable drawable, //
+ int x, int y, int w, int h)
+ {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ double aspect = (double) w / (double) h;
+
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (aspect < 1.0)
+ {
+ aspect = 1.0 / aspect;
+ gl.glOrtho(-aspect, aspect, -1.0, 1.0, -1.0, 1.0);
+ }
+ else gl.glOrtho(-1.0, 1.0, -aspect, aspect, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, //
+ boolean deviceChanged, boolean modeChanged)
+ {
+ }
+
+ public void keyTyped(KeyEvent e)
+ {
+ }
+
+ public void keyPressed(KeyEvent e)
+ {
+ this.key = e;
+ switch (e.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent e)
+ {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook12x/colormatrix.java b/src/redbook/src/glredbook12x/colormatrix.java new file mode 100644 index 0000000..5415173 --- /dev/null +++ b/src/redbook/src/glredbook12x/colormatrix.java @@ -0,0 +1,179 @@ +package glredbook12x;
+
+import java.awt.Dimension;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.awt.event.MouseWheelListener;
+import java.io.DataInputStream;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+
+
+/**
+ * This program uses the color matrix to exchange the color channels of an
+ * image. <br>
+ * <br>
+ * Red -> Green <br>
+ * Green -> Blue <br>
+ * Blue -> Red
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class colormatrix//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ //
+ private ByteBuffer pixels;
+ // private int width; not reference as params...
+ // private int height;...as are all Java primitives
+ private Dimension dim = new Dimension(0, 0);
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+
+ public static void main(String[] args) {
+
+ colormatrix demo = new colormatrix();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("colormatrix");
+ frame.setSize(640, 480);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ float m[] = { 0.0f, 1.0f, 0.0f, 0.0f,//
+ 0.0f, 0.0f, 1.0f, 0.0f,//
+ 1.0f, 0.0f, 0.0f, 0.0f,//
+ 0.0f, 0.0f, 0.0f, 1.0f };
+
+ if (pixels == null) {
+ pixels = readImage("Data/leeds.bin", dim);
+ System.out.println(pixels.toString());
+ }
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ gl.glMatrixMode(GL2.GL_COLOR);
+ gl.glLoadMatrixf(m, 0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ gl.glRasterPos2i(1, 1);
+ gl.glDrawPixels(dim.width, dim.height, //
+ GL2.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
+
+ gl.glFlush();
+
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0, w, 0, h, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /**
+ * @author Mike "Top Coder" Butler : fucking signed byte is gay!
+ * @author Kiet "Abysmal Coder" Le : major. gay. primitive.
+ */
+ private ByteBuffer readImage(String filename, Dimension dim) {
+ if (dim == null)
+ dim = new Dimension(0, 0);
+ ByteBuffer bytes = null;
+ try {
+ // InputStream is = getClass().getClassLoader()
+ // .getResourceAsStream(filename);
+
+ // FileInputStream fis = new FileInputStream(filename);
+ DataInputStream dis = new DataInputStream(getClass()
+ .getClassLoader().getResourceAsStream(filename));
+ // DataInputStream dis = new DataInputStream(fis);
+
+ // int width = 0, height = 0;
+ dim.width = dis.readInt();
+ dim.height = dis.readInt();
+ System.out.println("Creating buffer, width: " + dim.width
+ + " height: " + dim.height);
+ // byte[] buf = new byte[3 * dim.height * dim.width];
+ bytes = BufferUtil.newByteBuffer(3 * dim.width * dim.height);
+ for (int i = 0; i < bytes.capacity(); i++) {
+ bytes.put(dis.readByte());
+ // int b = dis.readByte();// dis.read();
+ // System.out.print(b + " ");
+ // if (i % 3 == 0) System.out.println();
+ // bytes.put((byte) b);
+ // System.out.print(bytes.get(i) + " . ");
+ // if (i %3 ==0) System.out.println();
+ }
+ // fis.close();
+ dis.close();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ bytes.rewind();
+ return bytes;
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook12x/colortable.java b/src/redbook/src/glredbook12x/colortable.java new file mode 100644 index 0000000..cd86fc0 --- /dev/null +++ b/src/redbook/src/glredbook12x/colortable.java @@ -0,0 +1,203 @@ +package glredbook12x;
+
+import java.awt.Dimension;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.awt.event.MouseWheelListener;
+import java.io.DataInputStream;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;import javax.swing.JFrame;
+import javax.swing.SwingUtilities;
+
+import com.sun.opengl.util.BufferUtil;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+
+
+/**
+ * Invert a passed block of pixels. This program illustrates the use of the
+ * glColorTable|EXT() function.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class colortable//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private JFrame frame;
+
+ //
+ private ByteBuffer pixels;
+ // private int width; not reference as params...
+ // private int height;...as are all Java primitives
+ private Dimension dim = new Dimension(0, 0);
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ protected void setFrame(JFrame frame) {
+ this.frame = frame;
+ }
+
+ public static void main(String[] args) {
+
+ colortable demo = new colortable();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("colortable");
+ frame.setSize(640, 480);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ demo.setFrame(frame );
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ if (pixels == null) {
+ pixels = readImage("Data/leeds.bin", dim);
+ System.out.println(pixels.toString());
+ }
+
+ // byte colorTable[][] = new byte[256][3];
+ ByteBuffer colorTableBuf = BufferUtil.newByteBuffer(256 * 3);
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ /* Set up an inverse color table */
+
+ for (int i = 0; i < 256; ++i) {
+ // colorTable[i][0] = (byte)(255 - i);
+ // colorTable[i][1] = (byte)(255 - i);
+ // colorTable[i][2] = (byte)(255 - i);
+ colorTableBuf.put((byte) (255 - i));
+ colorTableBuf.put((byte) (255 - i));
+ colorTableBuf.put((byte) (255 - i));
+ }
+ colorTableBuf.rewind();
+
+ if (gl.isExtensionAvailable("GL_ARB_imaging")) {
+ if (gl.isFunctionAvailable("glColorTable")) {
+ gl.glColorTable(GL2.GL_COLOR_TABLE, GL2.GL_RGB, 256, //
+ GL2.GL_RGB, GL.GL_UNSIGNED_BYTE, colorTableBuf);
+ } else {
+ gl.glColorTable(GL2.GL_COLOR_TABLE, GL2.GL_RGB, 256, //
+ GL2.GL_RGB, GL.GL_UNSIGNED_BYTE, colorTableBuf);
+ }
+ gl.glEnable(GL2.GL_COLOR_TABLE);
+ } else {
+ frame.setTitle("colortable: NO ARB Imaging Subset");
+ SwingUtilities.updateComponentTreeUI(frame);
+ }
+
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ gl.glRasterPos2i(1, 1);
+ gl.glDrawPixels(dim.width, dim.height, //
+ GL2.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
+
+ gl.glFlush();
+
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0, w, 0, h, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /**
+ * @author Mike "Top Coder" Butler : fucking signed byte is gay!
+ * @author Kiet "Abysmal Coder" Le : major. gay. primitive.
+ */
+ private ByteBuffer readImage(String filename, Dimension dim) {
+ if (dim == null)
+ dim = new Dimension(0, 0);
+ ByteBuffer bytes = null;
+ try {
+ // InputStream is = getClass().getClassLoader()
+ // .getResourceAsStream(filename);
+
+ // FileInputStream fis = new FileInputStream(filename);
+ DataInputStream dis = new DataInputStream(getClass()
+ .getClassLoader().getResourceAsStream(filename));
+ // DataInputStream dis = new DataInputStream(fis);
+
+ // int width = 0, height = 0;
+ dim.width = dis.readInt();
+ dim.height = dis.readInt();
+ System.out.println("Creating buffer, width: " + dim.width
+ + " height: " + dim.height);
+ // byte[] buf = new byte[3 * dim.height * dim.width];
+ bytes = BufferUtil.newByteBuffer(3 * dim.width * dim.height);
+ for (int i = 0; i < bytes.capacity(); i++) {
+ bytes.put(dis.readByte());
+ // int b = dis.readByte();// dis.read();
+ // System.out.print(b + " ");
+ // if (i % 3 == 0) System.out.println();
+ // bytes.put((byte) b);
+ // System.out.print(bytes.get(i) + " . ");
+ // if (i %3 ==0) System.out.println();
+ }
+ // fis.close();
+ dis.close();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ bytes.rewind();
+ return bytes;
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook12x/convolution.java b/src/redbook/src/glredbook12x/convolution.java new file mode 100644 index 0000000..daed275 --- /dev/null +++ b/src/redbook/src/glredbook12x/convolution.java @@ -0,0 +1,234 @@ +package glredbook12x;
+
+import java.awt.Dimension;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.awt.event.MouseWheelListener;
+import java.io.DataInputStream;
+import java.nio.ByteBuffer;
+import java.nio.FloatBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+import javax.swing.SwingUtilities;
+
+import com.sun.opengl.util.BufferUtil;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * Use various 2D convolutions filters to find edges in an image.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class convolution//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private JFrame frame;
+ private KeyEvent key;
+ //
+ private ByteBuffer pixels;
+ // private int width; not reference as params...
+ // private int height;...as are all Java primitives
+ private Dimension dim = new Dimension(0, 0);
+ private float horizontal[][] = { { 0, -1, 0 }, { 0, 1, 0 }, { 0, 0, 0 } };
+
+ private float vertical[][] = { { 0, 0, 0 }, { -1, 1, 0 }, { 0, 0, 0 } };
+
+ private float laplacian[][] = { { -0.125f, -0.125f, -0.125f },
+ { -0.125f, 1.0f, -0.125f }, { -0.125f, -0.125f, -0.125f } };
+
+ private FloatBuffer horizontalBuf = BufferUtil
+ .newFloatBuffer(horizontal.length * horizontal[0].length);
+ private FloatBuffer verticalBuf = BufferUtil.newFloatBuffer(vertical.length
+ * vertical[0].length);
+ private FloatBuffer laplacianBuf = BufferUtil
+ .newFloatBuffer(laplacian.length * laplacian[0].length);
+ {
+ for (int i = 0; i < 3; i++) {
+ horizontalBuf.put(horizontal[i]);
+ verticalBuf.put(vertical[i]);
+ laplacianBuf.put(laplacian[i]);
+ }
+ horizontalBuf.rewind();
+ verticalBuf.rewind();
+ laplacianBuf.rewind();
+ }
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ protected void setFrame(JFrame frame) {
+ this.frame = frame;
+ }
+
+ public static void main(String[] args) {
+ GLCapabilities caps = new GLCapabilities(null);
+
+ convolution demo = new convolution();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("convolution");
+ frame.setSize(640, 480);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ demo.setFrame(frame);
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ if (gl.isExtensionAvailable("GL_ARB_imaging")) {
+ if (gl.isFunctionAvailable("glConvolutionFilter2D")) {
+ System.out.println("Using the horizontal filter");
+ gl.glConvolutionFilter2D(GL2.GL_CONVOLUTION_2D, GL.GL_LUMINANCE, //
+ 3, 3, GL.GL_LUMINANCE, GL.GL_FLOAT, horizontalBuf);
+ gl.glEnable(GL2.GL_CONVOLUTION_2D);
+ }
+ } else {
+ frame.setTitle("convolution: NO ARB Imaging Subset");
+ SwingUtilities.updateComponentTreeUI(frame);
+ }
+
+ if (pixels == null) {
+ pixels = readImage("Data/leeds.bin", dim);
+ System.out.println(pixels.toString());
+ }
+
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ if (gl.isFunctionAvailable("glConvolutionFilter2D"))
+ if (key != null)
+ switch (key.getKeyChar()) {
+ case 'h':
+ System.out.println("Using a horizontal filter");
+ gl.glConvolutionFilter2D(GL2.GL_CONVOLUTION_2D,
+ GL.GL_LUMINANCE, //
+ 3, 3, GL.GL_LUMINANCE, GL.GL_FLOAT, horizontalBuf);
+ break;
+
+ case 'v':
+ System.out.println("Using the vertical filter\n");
+ gl.glConvolutionFilter2D(GL2.GL_CONVOLUTION_2D,
+ GL.GL_LUMINANCE, //
+ 3, 3, GL.GL_LUMINANCE, GL.GL_FLOAT, verticalBuf);
+ break;
+
+ case 'l':
+ System.out.println("Using the laplacian filter\n");
+ gl.glConvolutionFilter2D(GL2.GL_CONVOLUTION_2D,
+ GL.GL_LUMINANCE, //
+ 3, 3, GL.GL_LUMINANCE, GL.GL_FLOAT, laplacianBuf);
+ break;
+
+ }// key sw
+
+ gl.glRasterPos2i(1, 1);
+ gl.glDrawPixels(dim.width, dim.height, //
+ GL2.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
+
+ gl.glFlush();
+
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0, w, 0, h, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /**
+ * @author Mike "Top Coder" Butler : fucking signed byte is gay!
+ * @author Kiet "Abysmal Coder" Le : major. gay. primitive.
+ */
+ private ByteBuffer readImage(String filename, Dimension dim) {
+ if (dim == null)
+ dim = new Dimension(0, 0);
+ ByteBuffer bytes = null;
+ try {
+ // InputStream is = getClass().getClassLoader()
+ // .getResourceAsStream(filename);
+
+ // FileInputStream fis = new FileInputStream(filename);
+ DataInputStream dis = new DataInputStream(getClass()
+ .getClassLoader().getResourceAsStream(filename));
+ // DataInputStream dis = new DataInputStream(fis);
+
+ // int width = 0, height = 0;
+ dim.width = dis.readInt();
+ dim.height = dis.readInt();
+ System.out.println("Creating buffer, width: " + dim.width
+ + " height: " + dim.height);
+ // byte[] buf = new byte[3 * dim.height * dim.width];
+ bytes = BufferUtil.newByteBuffer(3 * dim.width * dim.height);
+ for (int i = 0; i < bytes.capacity(); i++) {
+ bytes.put(dis.readByte());
+ // int b = dis.readByte();// dis.read();
+ // System.out.print(b + " ");
+ // if (i % 3 == 0) System.out.println();
+ // bytes.put((byte) b);
+ // System.out.print(bytes.get(i) + " . ");
+ // if (i %3 ==0) System.out.println();
+ }
+ // fis.close();
+ dis.close();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ bytes.rewind();
+ return bytes;
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ this.key = key;
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook12x/histogram.java b/src/redbook/src/glredbook12x/histogram.java new file mode 100644 index 0000000..676d993 --- /dev/null +++ b/src/redbook/src/glredbook12x/histogram.java @@ -0,0 +1,227 @@ +package glredbook12x;
+
+import java.awt.Dimension;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.awt.event.MouseWheelListener;
+import java.io.DataInputStream;
+import java.nio.ByteBuffer;
+import java.nio.ShortBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+import javax.swing.SwingUtilities;
+
+import com.sun.opengl.util.BufferUtil;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * Compute the histogram of the image. This program illustrates the use of the
+ * glHistogram() function.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class histogram //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private JFrame frame;
+ private KeyEvent key;
+ //
+ private ByteBuffer pixels;
+ // private int width; not reference as params...
+ // private int height;...as are all Java primitives
+ private Dimension dim = new Dimension(0, 0);
+ private static final int HISTOGRAM_SIZE = 256;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ protected void setFrame(JFrame frame) {
+ this.frame = frame;
+ }
+
+ public static void main(String[] args) {
+
+ histogram demo = new histogram();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("histogram");
+ frame.setSize(640, 480);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ demo.setFrame(frame);
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ if (gl.isExtensionAvailable("GL_ARB_imaging") //
+ && gl.isFunctionAvailable("glHistogram")) {
+ gl.glHistogram(GL2.GL_HISTOGRAM, HISTOGRAM_SIZE, GL2.GL_RGB, false);
+ gl.glEnable(GL2.GL_HISTOGRAM);
+ } else {
+ frame.setTitle("histogram: NO (optional) ARB Imaging Subset");
+
+ SwingUtilities.updateComponentTreeUI(frame);
+ }
+
+ if (pixels == null) {
+ pixels = readImage("Data/leeds.bin", dim);
+ System.out.println(pixels.toString());
+ }
+
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ boolean sink = false;
+ if (key != null && gl.isFunctionAvailable("glHistogram"))
+ switch (key.getKeyChar()) {
+ case 's':
+ if (gl.isExtensionAvailable("GL_ARB_imaging") && //
+ gl.isFunctionAvailable("glHistogram")) {
+ sink = !sink;
+ gl.glHistogram(GL2.GL_HISTOGRAM, HISTOGRAM_SIZE, GL2.GL_RGB,
+ sink);
+ }
+ break;
+
+ }// key sw
+
+ // short values[][] = new short[HISTOGRAM_SIZE][3];
+ ShortBuffer values = BufferUtil.newShortBuffer(HISTOGRAM_SIZE * 3);
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glRasterPos2i(1, 1);
+ gl.glDrawPixels(dim.width, dim.height, //
+ GL2.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
+
+ if (gl.isExtensionAvailable("GL_ARB_imaging")//
+ && gl.isFunctionAvailable("glGetHistogram")) {
+ gl.glGetHistogram(GL2.GL_HISTOGRAM, true, GL2.GL_RGB,//
+ GL.GL_UNSIGNED_SHORT, values);
+ }
+
+ /* Plot histogram */
+ gl.glBegin(GL.GL_LINE_STRIP);
+ gl.glColor3f(1.0f, 0.0f, 0.0f);
+ for (int i = 0; i < HISTOGRAM_SIZE; i++)
+ gl.glVertex2s((short) i, values.get(i * 2));
+ gl.glEnd();
+
+ gl.glBegin(GL.GL_LINE_STRIP);
+ gl.glColor3f(0.0f, 1.0f, 0.0f);
+ for (int i = 0; i < HISTOGRAM_SIZE; i++)
+ gl.glVertex2s((short) i, values.get(i * 2 + 1));// [i][1]);
+ gl.glEnd();
+
+ gl.glBegin(GL.GL_LINE_STRIP);
+ gl.glColor3f(0.0f, 0.0f, 1.0f);
+ for (int i = 0; i < HISTOGRAM_SIZE; i++)
+ gl.glVertex2s((short) i, values.get(i * 2 + 2));// [i][2]);
+ gl.glEnd();
+
+ gl.glFlush();
+
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0, 256, 0, 10000, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /**
+ * @author Mike "Top Coder" Butler : fucking signed byte is gay!
+ * @author Kiet "Abysmal Coder" Le : major. gay. primitive.
+ */
+ private ByteBuffer readImage(String filename, Dimension dim) {
+ if (dim == null)
+ dim = new Dimension(0, 0);
+ ByteBuffer bytes = null;
+ try {
+ // InputStream is = getClass().getClassLoader()
+ // .getResourceAsStream(filename);
+
+ // FileInputStream fis = new FileInputStream(filename);
+ DataInputStream dis = new DataInputStream(getClass()
+ .getClassLoader().getResourceAsStream(filename));
+ // DataInputStream dis = new DataInputStream(fis);
+
+ // int width = 0, height = 0;
+ dim.width = dis.readInt();
+ dim.height = dis.readInt();
+ System.out.println("Creating buffer, width: " + dim.width
+ + " height: " + dim.height);
+ // byte[] buf = new byte[3 * dim.height * dim.width];
+ bytes = BufferUtil.newByteBuffer(3 * dim.width * dim.height);
+ for (int i = 0; i < bytes.capacity(); i++) {
+ bytes.put(dis.readByte());
+ // int b = dis.readByte();// dis.read();
+ // System.out.print(b + " ");
+ // if (i % 3 == 0) System.out.println();
+ // bytes.put((byte) b);
+ // System.out.print(bytes.get(i) + " . ");
+ // if (i %3 ==0) System.out.println();
+ }
+ // fis.close();
+ dis.close();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ bytes.rewind();
+ return bytes;
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ this.key = key;
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ }
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook12x/minmax.java b/src/redbook/src/glredbook12x/minmax.java new file mode 100644 index 0000000..1837f35 --- /dev/null +++ b/src/redbook/src/glredbook12x/minmax.java @@ -0,0 +1,177 @@ +package glredbook12x;
+
+import java.awt.Dimension;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.awt.event.MouseWheelListener;
+import java.io.DataInputStream;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;import javax.swing.JFrame;
+import javax.swing.SwingUtilities;
+
+import com.sun.opengl.util.BufferUtil;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+
+/**
+ * Determine the minimum and maximum values of a group of pixels. This
+ * demonstrates use of the glMinmax() call.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class minmax//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private JFrame frame;
+
+ private ByteBuffer pixels;
+ // private int width; not reference as params...
+ // private int height;...as are all Java primitives
+ private Dimension dim = new Dimension(0, 0);
+
+ // private static final int HISTOGRAM_SIZE = 256;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ protected void setFrame(JFrame frame) {
+ this.frame = frame;
+ }
+
+ public static void main(String[] args) {
+
+ minmax demo = new minmax();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("minmax");
+ frame.setSize(640, 480);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ demo.setFrame(frame);
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ if (gl.isExtensionAvailable("GL_ARB_imaging") //
+ && gl.isFunctionAvailable("glMinmax")) {
+ gl.glMinmax(GL2.GL_MINMAX, GL2.GL_RGB, false);
+ gl.glEnable(GL2.GL_MINMAX);
+ } else {
+ frame.setTitle("minmax: NO (optional) ARB Imaging Subset");
+ SwingUtilities.updateComponentTreeUI(frame);
+ }
+
+ pixels = readImage("Data/leeds.bin", dim);
+ System.out.println(pixels.toString());
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ // byte [] values=new byte[6];
+ ByteBuffer values = BufferUtil.newByteBuffer(6);
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glRasterPos2i(1, 1);
+ gl.glDrawPixels(dim.width, dim.height, //
+ GL2.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);
+ gl.glFlush();
+
+ if (gl.isExtensionAvailable("GL_ARB_imaging") //
+ && gl.isFunctionAvailable("glGetMinmax")) {
+ gl.glGetMinmax(GL2.GL_MINMAX, true, //
+ GL2.GL_RGB, GL.GL_UNSIGNED_BYTE, values);
+ System.out.println(" Red : min = %d " //
+ + values.get(0) + ", max = " + values.get(3));
+ System.out.println(" Green : min = %d " //
+ + values.get(1) + ", max = " + values.get(4));
+ System.out.println(" Blue : min = %d " //
+ + values.get(2) + " max = " + values.get(5));
+ }
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ gl.glOrtho(0, w, 0, h, -1.0, 1.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /**
+ * @author Mike "Top Coder" Butler : fucking signed byte is gay!
+ * @author Kiet "Abysmal Coder" Le : major. gay. primitive.
+ */
+ private ByteBuffer readImage(String filename, Dimension dim) {
+ if (dim == null)
+ dim = new Dimension(0, 0);
+ ByteBuffer bytes = null;
+ try {
+ DataInputStream dis = new DataInputStream(getClass()
+ .getClassLoader().getResourceAsStream(filename));
+ dim.width = dis.readInt();
+ dim.height = dis.readInt();
+ System.out.println("Creating buffer, width: " + dim.width
+ + " height: " + dim.height);
+
+ bytes = BufferUtil.newByteBuffer(3 * dim.width * dim.height);
+ for (int i = 0; i < bytes.capacity(); i++) {
+ bytes.put(dis.readByte());
+
+ }
+ dis.close();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ bytes.rewind();
+ return bytes;
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook12x/multitex.java b/src/redbook/src/glredbook12x/multitex.java new file mode 100644 index 0000000..33b9f63 --- /dev/null +++ b/src/redbook/src/glredbook12x/multitex.java @@ -0,0 +1,227 @@ +package glredbook12x;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.awt.event.MouseWheelListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+public class multitex //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ // private byte[][][] texels0 = new byte[32][32][4];
+ // private byte[][][] texels1 = new byte[16][16][4];
+ private ByteBuffer texelsBuf0 = BufferUtil.newByteBuffer(32 * 32 * 4);
+ private ByteBuffer texelsBuf1 = BufferUtil.newByteBuffer(16 * 16 * 4);
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ GLCapabilities caps = new GLCapabilities(null);
+ GLJPanel canvas = new GLJPanel(caps);
+
+ multitex demo = new multitex();
+ canvas.addGLEventListener(demo);
+ if (demo instanceof KeyListener)
+ canvas.addKeyListener(demo);
+ // explicit cast for class not impl'ing listeners
+ // to make it compile,
+ if (demo instanceof MouseListener)
+ canvas.addMouseListener((MouseListener) demo);
+ if (demo instanceof MouseMotionListener)
+ canvas.addMouseMotionListener((MouseMotionListener) demo);
+ if (demo instanceof MouseWheelListener)
+ canvas.addMouseWheelListener((MouseWheelListener) demo);
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("multitex");
+ frame.setSize(512, 512);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(canvas);
+ frame.setVisible(true);
+ canvas.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ int texNames[] = new int[2];
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ makeCheckImages();
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ gl.glGenTextures(2, texNames, 0);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texNames[0]);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, //
+ 32, 32, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE, texelsBuf0);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
+ gl
+ .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ gl
+ .glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T,
+ GL2.GL_REPEAT);
+
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texNames[1]);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, //
+ 16, 16, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE, texelsBuf1);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP_TO_EDGE);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP_TO_EDGE);
+ /*
+ * Use the two texture objects to define two texture units for use in
+ * multitexturing
+ */
+
+ // gl.glActiveTextureARB(GL2.GL_TEXTURE0_ARB);// deprecated
+ gl.glActiveTexture(GL2.GL_TEXTURE0);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texNames[0]);
+ gl.glTexEnvi(GL2.GL_TEXTURE_ENV, //
+ GL2.GL_TEXTURE_ENV_MODE, GL2.GL_REPLACE);
+ gl.glMatrixMode(GL2.GL_TEXTURE);
+ gl.glLoadIdentity();
+ gl.glTranslatef(0.5f, 0.5f, 0.0f);
+ gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
+ gl.glTranslatef(-0.5f, -0.5f, 0.0f);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ // gl.glActiveTextureARB (GL2.GL_TEXTURE1_ARB); deprecated
+ gl.glActiveTexture(GL2.GL_TEXTURE1);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texNames[1]);
+ gl.glTexEnvi(GL2.GL_TEXTURE_ENV, //
+ GL2.GL_TEXTURE_ENV_MODE, GL2.GL_MODULATE);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glBegin(GL2.GL_TRIANGLES);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0.0f, 0.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 1.0f, 0.0f);
+ gl.glVertex2f(0.0f, 0.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0.5f, 1.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 0.5f, 0.0f);
+ gl.glVertex2f(50.0f, 100.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 1.0f, 0.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 1.0f, 1.0f);
+ gl.glVertex2f(100.0f, 0.0f);
+ gl.glEnd();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ glu.gluOrtho2D(0.0, 100.0, //
+ 0.0, 100.0 * (double) h / (double) w);
+ else
+ glu.gluOrtho2D(0.0, 100.0 * (double) w / (double) h, 0.0, 100.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ void makeCheckImages() {
+ for (int i = 0; i < 32; i++) {
+ for (int j = 0; j < 32; j++) {
+ // texels0[i][j][0] = (byte) i;
+ // texels0[i][j][1] = (byte) j;
+ // texels0[i][j][2] = (byte) ((i*j)/255);
+ // texels0[i][j][3] = (byte) 255;
+ // changed from above for more visible result
+ texelsBuf0.put((byte) (i * i * i));
+ texelsBuf0.put((byte) (j * j * j));
+ texelsBuf0.put((byte) ((i * j) / 255));
+ texelsBuf0.put((byte) 0xFF);
+ }
+ }
+
+ for (int i = 0; i < 16; i++) {
+ for (int j = 0; j < 16; j++) {
+ // texels1[i][j][0] = (byte) 255;
+ // texels1[i][j][1] = (byte) i;
+ // texels1[i][j][2] = (byte) j;
+ // texels1[i][j][3] = (byte) 255;
+ // changed from above for more visible result
+ texelsBuf1.put((byte) 0xFF);
+ texelsBuf1.put((byte) (i * i));
+ texelsBuf1.put((byte) (j * j));
+ texelsBuf1.put((byte) 0xFF);
+ }
+ }
+ texelsBuf0.rewind();
+ texelsBuf1.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook12x/quadric.java b/src/redbook/src/glredbook12x/quadric.java new file mode 100644 index 0000000..7c8e0e8 --- /dev/null +++ b/src/redbook/src/glredbook12x/quadric.java @@ -0,0 +1,212 @@ +package glredbook12x;
+
+import glredbook10.GLSkeleton;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.awt.event.MouseWheelListener;
+
+import javax.media.opengl.*;
+
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+import javax.media.opengl.glu.GLUquadric;
+import javax.swing.JFrame;
+
+/**
+ * This program demonstrates the use of some of the gluQuadric* routines.
+ * Quadric objects are created with some quadric properties and the callback
+ * routine to handle errors. Note that the cylinder has no top or bottom and the
+ * circle has a hole in it.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+
+public class quadric //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+
+ private int startList;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ GLCapabilities caps = new GLCapabilities(null);
+ GLJPanel canvas = new GLJPanel(caps);
+
+ quadric demo = new quadric();
+ canvas.addGLEventListener(demo);
+ if (demo instanceof KeyListener)
+ canvas.addKeyListener(demo);
+ // explicit cast for class not impl'ing listeners
+ // to make it compile,
+ if (demo instanceof MouseListener)
+ canvas.addMouseListener((MouseListener) demo);
+ if (demo instanceof MouseMotionListener)
+ canvas.addMouseMotionListener((MouseMotionListener) demo);
+ if (demo instanceof MouseWheelListener)
+ canvas.addMouseWheelListener((MouseWheelListener) demo);
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("quadric");
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(canvas);
+ frame.setVisible(true);
+ canvas.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ GLUquadric qobj;
+ float mat_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
+ float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+ float mat_shininess[] = { 50.0f };
+ float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
+ float model_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_ambient, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, mat_specular, 0);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SHININESS, mat_shininess, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, light_position, 0);
+ gl.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, model_ambient, 0);
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ /*
+ * Create 4 display lists, each with a different quadric object.
+ * Different drawing styles and surface normal specifications are
+ * demonstrated.
+ */
+ startList = gl.glGenLists(4);
+ qobj = glu.gluNewQuadric();
+
+ /*
+ * glu.gluQuadricCallback(qobj, GLU.GLU_ERROR, errorCallback); <br>
+ * Quadric call backs have yet been implemented in JOGL. But this
+ * program still work.
+ */
+ glu.gluQuadricDrawStyle(qobj, GLU.GLU_FILL); /* smooth shaded */
+ glu.gluQuadricNormals(qobj, GLU.GLU_SMOOTH);
+ gl.glNewList(startList, GL2.GL_COMPILE);
+ glu.gluSphere(qobj, 0.75, 15, 10);
+ gl.glEndList();
+
+ glu.gluQuadricDrawStyle(qobj, GLU.GLU_FILL); /* flat shaded */
+ glu.gluQuadricNormals(qobj, GLU.GLU_FLAT);
+ gl.glNewList(startList + 1, GL2.GL_COMPILE);
+ glu.gluCylinder(qobj, 0.5, 0.3, 1.0, 15, 5);
+ gl.glEndList();
+
+ glu.gluQuadricDrawStyle(qobj, GLU.GLU_LINE); /*
+ * all polygons
+ * wireframe
+ */
+ glu.gluQuadricNormals(qobj, GLU.GLU_NONE);
+ gl.glNewList(startList + 2, GL2.GL_COMPILE);
+ glu.gluDisk(qobj, 0.25, 1.0, 20, 4);
+ gl.glEndList();
+
+ glu.gluQuadricDrawStyle(qobj, GLU.GLU_SILHOUETTE); /* boundary only */
+ glu.gluQuadricNormals(qobj, GLU.GLU_NONE);
+ gl.glNewList(startList + 3, GL2.GL_COMPILE);
+ glu.gluPartialDisk(qobj, 0.0, 1.0, 20, 4, 0.0, 225.0);
+ gl.glEndList();
+
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ gl.glPushMatrix();
+
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glShadeModel(GL2.GL_SMOOTH);
+ gl.glTranslatef(-1.0f, -1.0f, 0.0f);
+ gl.glCallList(startList);
+
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glTranslatef(0.0f, 2.0f, 0.0f);
+ gl.glPushMatrix();
+ gl.glRotatef(300.0f, 1.0f, 0.0f, 0.0f);
+ gl.glCallList(startList + 1);
+ gl.glPopMatrix();
+
+ gl.glDisable(GL2.GL_LIGHTING);
+ gl.glColor3f(0.0f, 1.0f, 1.0f);
+ gl.glTranslatef(2.0f, -2.0f, 0.0f);
+ gl.glCallList(startList + 2);
+
+ gl.glColor3f(1.0f, 1.0f, 0.0f);
+ gl.glTranslatef(0.0f, 2.0f, 0.0f);
+ gl.glCallList(startList + 3);
+
+ gl.glPopMatrix();
+ gl.glFlush();
+
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= h)
+ gl.glOrtho(-2.5, 2.5, -2.5 * (float) h / (float) w, 2.5 * (float) h
+ / (float) w, -10.0, 10.0);
+ else
+ gl.glOrtho(-2.5 * (float) w / (float) h, 2.5 * (float) w
+ / (float) h, -2.5, 2.5, -10.0, 10.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ System.out.println("nothin pressed.");
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook12x/texture3d.java b/src/redbook/src/glredbook12x/texture3d.java new file mode 100644 index 0000000..96ce680 --- /dev/null +++ b/src/redbook/src/glredbook12x/texture3d.java @@ -0,0 +1,193 @@ +package glredbook12x;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseMotionListener;
+import java.awt.event.MouseWheelListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates using a three-dimensional texture. It creates a 3D
+ * texture and then renders two rectangles with different texture coordinates to
+ * obtain different "slices" of the 3D texture.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class texture3d //
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private static final int iWidth = 16;
+ private static final int iHeight = 16;
+ private static final int iDepth = 16;
+ private static final int iRgb = 3;
+ private ByteBuffer image
+ = BufferUtil.newByteBuffer(iRgb * iWidth * iHeight * iDepth);
+ private int texName[] = new int[1];
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+
+ GLCapabilities caps = new GLCapabilities(null);
+ GLJPanel canvas = new GLJPanel(caps);
+
+ texture3d demo = new texture3d();
+ canvas.addGLEventListener(demo);
+ if (demo instanceof KeyListener)
+ canvas.addKeyListener(demo);
+ // explicit cast for class not impl'ing listeners
+ // to make it compile,
+ if (demo instanceof MouseListener)
+ canvas.addMouseListener((MouseListener) demo);
+ if (demo instanceof MouseMotionListener)
+ canvas.addMouseMotionListener((MouseMotionListener) demo);
+ if (demo instanceof MouseWheelListener)
+ canvas.addMouseWheelListener((MouseWheelListener) demo);
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("texture3d");
+ frame.setSize(250, 250);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(canvas);
+ frame.setVisible(true);
+ canvas.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_FLAT);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+
+ makeImage();
+
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ gl.glGenTextures(1, texName, 0);
+ gl.glBindTexture(GL2.GL_TEXTURE_3D, texName[0]);
+ gl.glTexParameteri(GL2.GL_TEXTURE_3D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP);
+ gl.glTexParameteri(GL2.GL_TEXTURE_3D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP);
+ gl.glTexParameteri(GL2.GL_TEXTURE_3D, GL2.GL_TEXTURE_WRAP_R, GL2.GL_CLAMP);
+ gl.glTexParameteri(GL2.GL_TEXTURE_3D, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_3D, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexImage3D(GL2.GL_TEXTURE_3D, 0, GL2.GL_RGB,//
+ iWidth, iHeight, iDepth, 0, GL2.GL_RGB, GL.GL_UNSIGNED_BYTE,
+ image);
+ gl.glEnable(GL2.GL_TEXTURE_3D);
+ }//
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glTexCoord3f(0.0f, 0.0f, 0.0f);
+ gl.glVertex3f(-2.25f, -1.0f, 0.0f);
+ gl.glTexCoord3f(0.0f, 1.0f, 0.0f);
+ gl.glVertex3f(-2.25f, 1.0f, 0.0f);
+ gl.glTexCoord3f(1.0f, 1.0f, 1.0f);
+ gl.glVertex3f(-0.25f, 1.0f, 0.0f);
+ gl.glTexCoord3f(1.0f, 0.0f, 1.0f);
+ gl.glVertex3f(-0.25f, -1.0f, 0.0f);
+
+ gl.glTexCoord3f(0.0f, 0.0f, 1.0f);
+ gl.glVertex3f(0.25f, -1.0f, 0.0f);
+ gl.glTexCoord3f(0.0f, 1.0f, 1.0f);
+ gl.glVertex3f(0.25f, 1.0f, 0.0f);
+ gl.glTexCoord3f(1.0f, 1.0f, 0.0f);
+ gl.glVertex3f(2.25f, 1.0f, 0.0f);
+ gl.glTexCoord3f(1.0f, 0.0f, 0.0f);
+ gl.glVertex3f(2.25f, -1.0f, 0.0f);
+ gl.glEnd();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 30.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+
+ gl.glTranslatef(0.0f, 0.0f, -4.0f);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * Create a 16x16x16x3 array with different color values in each array
+ * element [r, g, b]. Values range from 0 to 255.
+ */
+
+ private void makeImage() {
+ int ss = 0, tt = 0, rr = 0;
+ for (int s = 0; s < 16; s++)
+ for (int t = 0; t < 16; t++)
+ for (int r = 0; r < 16; r++) {
+ // image[r][t][s][0] = (GLubyte) (s * 17);
+ // image[r][t][s][1] = (GLubyte) (t * 17);
+ // image[r][t][s][2] = (GLubyte) (r * 17);
+ ss = s * 17;
+ tt = t * 17;
+ rr = r * 17;
+ // System.out.println("s" + ss + "." + ss//
+ // + "t" + tt + "." + tt//
+ // + "r" + rr + "." + rr);
+ image.put((byte) (ss * 17));
+ image.put((byte) tt);
+ image.put((byte) rr);
+ }
+ image.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}
diff --git a/src/redbook/src/glredbook1314/README.txt b/src/redbook/src/glredbook1314/README.txt new file mode 100644 index 0000000..3ed447e --- /dev/null +++ b/src/redbook/src/glredbook1314/README.txt @@ -0,0 +1,17 @@ +These are the example programs which are featured in the OpenGL
+Programming Guide, Fourth Edition (covering OpenGL verions 1.0 through
+1.4). To compile these programs, you need OpenGL development libraries
+from jogl.dev.java.net for your machine.
+
+This package glredbook1314.jar only contain programs there new to the book.
+
+All programs quit on ESC key.
+
+* OpenGL 1.2 to 1.3 / 1.4 compatiblity issues
+
+
+Regards,
+
+Kiet Le (jogl port)
+http://ak.kiet.le.googlepages.com/theredbookinjava.html
\ No newline at end of file diff --git a/src/redbook/src/glredbook1314/combiner.java b/src/redbook/src/glredbook1314/combiner.java new file mode 100644 index 0000000..b24061b --- /dev/null +++ b/src/redbook/src/glredbook1314/combiner.java @@ -0,0 +1,427 @@ +package glredbook1314;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program renders a variety of quads showing different effects of texture
+ * combiner functions. The first row renders an untextured polygon (so you can
+ * compare the fragment colors) and then the 2 textures. The second row shows
+ * several different combiner functions on a single texture: replace, modulate,
+ * add, add-signed, and subtract. The third row shows the interpolate combiner
+ * function on a single texture with a constant color/alpha value, varying the
+ * amount of interpolation. The fourth row uses multitexturing with two textures
+ * and different combiner functions. The fifth row are some combiner
+ * experiments: using the scaling factor and reversing the order of subtraction
+ * for a combination function.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class combiner//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private static final int imageWidth = 8;
+ private static final int imageHeight = 8;
+ private static final int imageColor = 4;
+ /* arrays for two textures */
+ // private static byte[][][] image0 = new
+ // byte[imageHeight][imageWidth][imageColor];
+ // private static byte[][][] image1 = new
+ // byte[imageHeight][imageWidth][imageColor];
+ private static ByteBuffer imageBuf0 //
+ = BufferUtil.newByteBuffer(imageWidth * imageHeight * imageColor);
+ private static ByteBuffer imageBuf1 //
+ = BufferUtil.newByteBuffer(imageWidth * imageHeight * imageColor);
+ private static int texName[] = new int[4];
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ combiner demo = new combiner();
+
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);
+ // GLCanvas canvas = new GLCanvas(caps);
+ GLJPanel canvas = new GLJPanel(caps);
+ canvas.addGLEventListener(demo);
+ canvas.addKeyListener(demo);
+
+ /* metal/ocean LAF can't handle heavy-weight canvas */
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("combiner");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+
+ frame.getContentPane().add(canvas);
+
+ frame.setVisible(true);
+ canvas.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ // int numunits[] = new int[1];
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_SMOOTH);
+
+ makeImages();
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+
+ gl.glGenTextures(4, texName, 0);
+
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, imageWidth,
+ imageHeight, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE, imageBuf0);
+
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[1]);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, imageWidth,
+ imageHeight, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE, imageBuf1);
+
+ /* smooth-shaded polygon with multiple texture coordinates */
+ gl.glNewList(1, GL2.GL_COMPILE);
+ gl.glBegin(GL2.GL_QUADS);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0.0f, 0.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 0.0f, 0.0f);
+ gl.glColor3f(0.5f, 1.0f, 0.25f);
+ gl.glVertex3f(0.0f, 0.0f, 0.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0.0f, 2.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 0.0f, 2.0f);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glVertex3f(0.0f, 1.0f, 0.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 2.0f, 2.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 2.0f, 2.0f);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glVertex3f(1.0f, 1.0f, 0.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 2.0f, 0.0f);
+ gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 2.0f, 0.0f);
+ gl.glColor3f(1.0f, 0.5f, 0.25f);
+ gl.glVertex3f(1.0f, 0.0f, 0.0f);
+ gl.glEnd();
+ gl.glEndList();
+
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ // for use as constant texture color
+ float constColor[] = new float[] { 0.0f, 0.0f, 0.0f, 0.0f };
+
+ // untextured polygon--see the "fragment" colors
+ gl.glDisable(GL2.GL_TEXTURE_2D);
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, 5.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ /* draw ordinary textured polys; 1 texture unit; combine mode disabled */
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_MODULATE);
+ gl.glPushMatrix();
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glTranslatef(1.0f, 5.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[1]);
+ gl.glTranslatef(2.0f, 5.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ /*
+ * different combine modes enabled; 1 texture unit defaults are:
+ * glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE0_RGB_ARB, GL2.GL_TEXTURE);
+ * glTexEnvf(GL2.GL_TEXTURE_ENV, GL.GL_OPERAND0_RGB_ARB,
+ * GL2.GL_SRC_COLOR); glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE1_RGB_ARB,
+ * GL.GL_PREVIOUS_ARB); glTexEnvf(GL2.GL_TEXTURE_ENV,
+ * GL.GL_OPERAND1_RGB_ARB, GL2.GL_SRC_COLOR);
+ */
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_COMBINE);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_REPLACE);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE0_RGB, GL2.GL_TEXTURE);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_OPERAND0_RGB, GL2.GL_SRC_COLOR);
+ gl.glPushMatrix();
+ gl.glTranslatef(1.0f, 4.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_MODULATE);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE1_RGB, GL2.GL_PREVIOUS);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_OPERAND1_RGB, GL2.GL_SRC_COLOR);
+ gl.glPushMatrix();
+ gl.glTranslatef(2.0f, 4.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_ADD);
+ gl.glPushMatrix();
+ gl.glTranslatef(3.0f, 4.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_ADD_SIGNED);
+ gl.glPushMatrix();
+ gl.glTranslatef(4.0f, 4.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_SUBTRACT);
+ gl.glPushMatrix();
+ gl.glTranslatef(5.0f, 4.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ /*
+ * interpolate combine with constant color; 1 texture unit use different
+ * alpha values for constant color defaults are:
+ * glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE0_RGB_ARB, GL2.GL_TEXTURE);
+ * glTexEnvf(GL2.GL_TEXTURE_ENV, GL.GL_OPERAND0_RGB_ARB,
+ * GL2.GL_SRC_COLOR); glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE1_RGB_ARB,
+ * GL.GL_PREVIOUS_ARB); glTexEnvf(GL2.GL_TEXTURE_ENV,
+ * GL.GL_OPERAND1_RGB_ARB, GL2.GL_SRC_COLOR);
+ * glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE2_RGB_ARB,
+ * GL.GL_CONSTANT_ARB); glTexEnvf(GL2.GL_TEXTURE_ENV,
+ * GL.GL_OPERAND2_RGB_ARB, GL2.GL_SRC_ALPHA);
+ */
+ constColor[3] = 0.2f;
+ gl.glTexEnvfv(GL2.GL_TEXTURE_ENV, //
+ GL2.GL_TEXTURE_ENV_COLOR, constColor, 0);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_COMBINE);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_INTERPOLATE);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE0_RGB, GL2.GL_TEXTURE);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_OPERAND0_RGB, GL2.GL_SRC_COLOR);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE1_RGB, GL2.GL_PREVIOUS);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_OPERAND1_RGB, GL2.GL_SRC_COLOR);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE2_RGB, GL2.GL_CONSTANT);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_OPERAND2_RGB, GL2.GL_SRC_ALPHA);
+ gl.glPushMatrix();
+ gl.glTranslatef(1.0f, 3.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ constColor[3] = 0.4f;
+ gl.glTexEnvfv(GL2.GL_TEXTURE_ENV, //
+ GL2.GL_TEXTURE_ENV_COLOR, constColor, 0);
+ gl.glPushMatrix();
+ gl.glTranslatef(2.0f, 3.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ constColor[3] = 0.6f;
+ gl.glTexEnvfv(GL2.GL_TEXTURE_ENV, //
+ GL2.GL_TEXTURE_ENV_COLOR, constColor, 0);
+ gl.glPushMatrix();
+ gl.glTranslatef(3.0f, 3.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ constColor[3] = 0.8f;
+ gl.glTexEnvfv(GL2.GL_TEXTURE_ENV, //
+ GL2.GL_TEXTURE_ENV_COLOR, constColor, 0);
+ gl.glPushMatrix();
+ gl.glTranslatef(4.0f, 3.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ /*
+ * combine textures 0 & 1 defaults are: glTexEnvf(GL2.GL_TEXTURE_ENV,
+ * GL2.GL_SOURCE0_RGB_ARB, GL2.GL_TEXTURE); glTexEnvf(GL2.GL_TEXTURE_ENV,
+ * GL.GL_OPERAND0_RGB_ARB, GL2.GL_SRC_COLOR);
+ * glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE1_RGB_ARB,
+ * GL.GL_PREVIOUS_ARB); glTexEnvf(GL2.GL_TEXTURE_ENV,
+ * GL.GL_OPERAND1_RGB_ARB, GL2.GL_SRC_COLOR);
+ */
+
+ gl.glActiveTexture(GL2.GL_TEXTURE0);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[0]);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_MODULATE);
+
+ gl.glActiveTexture(GL2.GL_TEXTURE1);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ gl.glBindTexture(GL2.GL_TEXTURE_2D, texName[1]);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_COMBINE);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_REPLACE);
+ gl.glPushMatrix();
+ gl.glTranslatef(1.0f, 2.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ /* try different combiner modes of texture unit 1 */
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_MODULATE);
+ gl.glPushMatrix();
+ gl.glTranslatef(2.0f, 2.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_ADD);
+ gl.glPushMatrix();
+ gl.glTranslatef(3.0f, 2.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_ADD_SIGNED);
+ gl.glPushMatrix();
+ gl.glTranslatef(4.0f, 2.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_SUBTRACT);
+ gl.glPushMatrix();
+ gl.glTranslatef(5.0f, 2.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ /* some experiments */
+
+ /* see the effect of RGB_SCALE */
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_RGB_SCALE, 2.0f);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_REPLACE);
+ gl.glPushMatrix();
+ gl.glTranslatef(1.0f, 1.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_MODULATE);
+ gl.glPushMatrix();
+ gl.glTranslatef(2.0f, 1.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_RGB_SCALE, 1.0f);
+
+ /* using SOURCE0 and SOURCE1, reverse the order of subtraction Arg1-Arg0 */
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_COMBINE);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_COMBINE_RGB, GL2.GL_SUBTRACT);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE0_RGB, GL2.GL_PREVIOUS);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_OPERAND0_RGB, GL2.GL_SRC_COLOR);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_SOURCE1_RGB, GL2.GL_TEXTURE);
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_OPERAND1_RGB, GL2.GL_SRC_COLOR);
+ gl.glPushMatrix();
+ gl.glTranslatef(5.0f, 1.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glActiveTexture(GL2.GL_TEXTURE1); /* deactivate multitexturing */
+ gl.glDisable(GL2.GL_TEXTURE_2D);
+ gl.glActiveTexture(GL2.GL_TEXTURE0); /* activate single texture unit */
+
+ gl.glFlush();
+
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+
+ glu.gluOrtho2D(0.0f, 7.0f, 0.0f, 7.0f);
+
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeImages() {
+ int c = 0;
+ for (int i = 0; i < imageHeight; i++) {
+ for (int j = 0; j < imageWidth; j++) {
+ c = (i & 2) == 0 ? c * 255 : 0;// ) * 255; /* horiz b & w
+ // stripes */
+ // image0[i][j][0] = (byte) c;
+ // image0[i][j][1] = (byte) c;
+ // image0[i][j][2] = (byte) c;
+ // image0[i][j][3] = (byte) 255;
+ imageBuf0.put((byte) c);
+ imageBuf0.put((byte) c);
+ imageBuf0.put((byte) c);
+ imageBuf0.put((byte) 255);
+ c = (j & 4) != 0 ? c * 128 : 0;// )*128; /* wider vertical 50%
+ // cyan and
+ // black stripes */
+ // image1[i][j][0] = (byte) 0;
+ // image1[i][j][1] = (byte) c;
+ // image1[i][j][2] = (byte) c;
+ // image1[i][j][3] = (byte) 255;
+ imageBuf1.put((byte) 0);
+ imageBuf1.put((byte) c);
+ imageBuf1.put((byte) c);
+ imageBuf1.put((byte) 255);
+ }
+ }
+ imageBuf0.rewind();
+ imageBuf1.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook1314/cubemap.java b/src/redbook/src/glredbook1314/cubemap.java new file mode 100644 index 0000000..8f7a39e --- /dev/null +++ b/src/redbook/src/glredbook1314/cubemap.java @@ -0,0 +1,253 @@ +package glredbook1314;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.ByteBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+
+import com.sun.opengl.util.*;
+import com.sun.opengl.util.gl2.GLUT;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates cube map textures. Six different colored checker
+ * board textures are created and applied to a lit sphere. Pressing the 'f' and
+ * 'b' keys translate the viewer forward and backward.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class cubemap//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+ // reference to the canvas in main() in order to refresh in input
+
+ private GLU glu;
+ private GLUT glut;
+ private static int imageSize = 4;
+ // private static byte image1[][][] = new byte[imageSize][imageSize][4];
+ // private static byte image2[][][] = new byte[imageSize][imageSize][4];
+ // private static byte image3[][][] = new byte[imageSize][imageSize][4];
+ // private static byte image4[][][] = new byte[imageSize][imageSize][4];
+ // private static byte image5[][][] = new byte[imageSize][imageSize][4];
+ // private static byte image6[][][] = new byte[imageSize][imageSize][4];
+ private static ByteBuffer imageBuf1 //
+ = BufferUtil.newByteBuffer(imageSize * imageSize * 4);
+ private static ByteBuffer imageBuf2 //
+ = BufferUtil.newByteBuffer(imageSize * imageSize * 4);
+ private static ByteBuffer imageBuf3 //
+ = BufferUtil.newByteBuffer(imageSize * imageSize * 4);
+ private static ByteBuffer imageBuf4 //
+ = BufferUtil.newByteBuffer(imageSize * imageSize * 4);
+ private static ByteBuffer imageBuf5 //
+ = BufferUtil.newByteBuffer(imageSize * imageSize * 4);
+ private static ByteBuffer imageBuf6 //
+ = BufferUtil.newByteBuffer(imageSize * imageSize * 4);
+
+ private static double ztrans = 0.0;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+ /*
+ * Demonstrate this example.
+ */
+ public static void main(String[] args) {
+
+ cubemap demo = new cubemap();
+
+ /* metal/ocean LAF can't handle heavy-weight canvas */
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("cubemap");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setSize(400, 400);
+ frame.setLocationRelativeTo(null);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ float diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glShadeModel(GL2.GL_SMOOTH);
+
+ makeImages();
+ gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
+ gl.glTexParameteri(GL2.GL_TEXTURE_CUBE_MAP, GL2.GL_TEXTURE_WRAP_S,
+ GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_CUBE_MAP, GL2.GL_TEXTURE_WRAP_T,
+ GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_CUBE_MAP, GL2.GL_TEXTURE_WRAP_R,
+ GL2.GL_REPEAT);
+ gl.glTexParameteri(GL2.GL_TEXTURE_CUBE_MAP, GL2.GL_TEXTURE_MAG_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexParameteri(GL2.GL_TEXTURE_CUBE_MAP, GL2.GL_TEXTURE_MIN_FILTER,
+ GL.GL_NEAREST);
+ gl.glTexImage2D(GL2.GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL2.GL_RGBA,
+ imageSize, imageSize, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ imageBuf1);
+ gl.glTexImage2D(GL2.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL2.GL_RGBA,
+ imageSize, imageSize, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ imageBuf4);
+ gl.glTexImage2D(GL2.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL2.GL_RGBA,
+ imageSize, imageSize, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ imageBuf2);
+ gl.glTexImage2D(GL2.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL2.GL_RGBA,
+ imageSize, imageSize, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ imageBuf5);
+ gl.glTexImage2D(GL2.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL2.GL_RGBA,
+ imageSize, imageSize, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ imageBuf3);
+ gl.glTexImage2D(GL2.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL2.GL_RGBA,
+ imageSize, imageSize, 0, GL2.GL_RGBA, GL.GL_UNSIGNED_BYTE,
+ imageBuf6);
+ gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_NORMAL_MAP);
+ gl.glTexGeni(GL2.GL_T, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_NORMAL_MAP);
+ gl.glTexGeni(GL2.GL_R, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_NORMAL_MAP);
+ gl.glEnable(GL2.GL_TEXTURE_GEN_S);
+ gl.glEnable(GL2.GL_TEXTURE_GEN_T);
+ gl.glEnable(GL2.GL_TEXTURE_GEN_R);
+
+ gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_MODULATE);
+
+ gl.glEnable(GL2.GL_TEXTURE_CUBE_MAP);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_AUTO_NORMAL);
+ gl.glEnable(GL2.GL_NORMALIZE);
+ gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, diffuse, 0);
+
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(0.0f, 0.0f, (float) ztrans);
+ glut.glutSolidSphere(5.0, 20, 10);
+ gl.glPopMatrix();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+
+ glu.gluPerspective(40.0, (float) w / (float) h, 1.0, 300.0);
+
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+
+ gl.glTranslatef(0.0f, 0.0f, -20.0f);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void makeImages() {
+ int c = 0;
+ for (int i = 0; i < imageSize; i++) {
+ for (int j = 0; j < imageSize; j++) {
+ // c = ((((i&0x1)==0)^((j&0x1))==0))*255;
+ c = (byte) ((((byte) ((i & 0x1) == 0 ? 0x00 : 0xff)//
+ ^ (byte) ((j & 0x1) == 0 ? 0x00 : 0xff))));
+ imageBuf1.put((byte) c);
+ imageBuf1.put((byte) c);
+ imageBuf1.put((byte) c);
+ imageBuf1.put((byte) 255);
+
+ imageBuf2.put((byte) c);
+ imageBuf2.put((byte) c);
+ imageBuf2.put((byte) 0);
+ imageBuf2.put((byte) 255);
+
+ imageBuf3.put((byte) c);
+ imageBuf3.put((byte) 0);
+ imageBuf3.put((byte) c);
+ imageBuf3.put((byte) 255);
+
+ imageBuf4.put((byte) 0);
+ imageBuf4.put((byte) c);
+ imageBuf4.put((byte) c);
+ imageBuf4.put((byte) 255);
+
+ imageBuf5.put((byte) 255);
+ imageBuf5.put((byte) c);
+ imageBuf5.put((byte) c);
+ imageBuf5.put((byte) 255);
+
+ imageBuf6.put((byte) c);
+ imageBuf6.put((byte) c);
+ imageBuf6.put((byte) 255);
+ imageBuf6.put((byte) 255);
+ }
+ }
+ imageBuf1.rewind();
+ imageBuf2.rewind();
+ imageBuf3.rewind();
+ imageBuf4.rewind();
+ imageBuf5.rewind();
+ imageBuf6.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ case KeyEvent.VK_F:
+ case KeyEvent.VK_UP:
+ ztrans = ztrans - 0.2;
+ break;
+
+ case KeyEvent.VK_B:
+ case KeyEvent.VK_DOWN:
+ ztrans = ztrans + 0.2;
+ break;
+
+ default:
+ break;
+ }
+
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}//
\ No newline at end of file diff --git a/src/redbook/src/glredbook1314/fogcoord.java b/src/redbook/src/glredbook1314/fogcoord.java new file mode 100644 index 0000000..b40dca9 --- /dev/null +++ b/src/redbook/src/glredbook1314/fogcoord.java @@ -0,0 +1,190 @@ +package glredbook1314;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*;
+import javax.media.opengl.awt.GLCanvas;
+
+import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.glu.GLU;
+
+
+/**
+ * This program demonstrates the use of explicit fog coordinates. You can press
+ * the keyboard and change the fog coordinate value at any vertex. You can also
+ * switch between using explicit fog coordinates and the default fog generation
+ * mode. <br>
+ * Pressing the 'f' and 'b' keys move the viewer forward and backwards. Pressing
+ * 'c' initiates the default fog generation. Pressing capital 'C' restores
+ * explicit fog coordinates. Pressing '1', '2', '3', '8', '9', and '0' add or
+ * subtract from the fog coordinate values at one of the three vertices of the
+ * triangle.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class fogcoord//
+ extends GLSkeleton<GLCanvas>
+ implements GLEventListener, KeyListener {
+
+ private GLU glu;
+ private float f1, f2, f3;
+ private KeyEvent key;
+
+ @Override
+ protected GLCanvas createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);
+ //
+ GLCanvas panel = new GLCanvas(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ GLCapabilities caps = new GLCapabilities(null);
+ GLCanvas canvas = new GLCanvas(caps);
+
+ fogcoord demo = new fogcoord();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("fogcoord");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Initialize fog
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ float fogColor[] = { 0.0f, 0.25f, 0.25f, 1.0f };
+ f1 = 1.0f;
+ f2 = 5.0f;
+ f3 = 10.0f;
+
+ gl.glEnable(GL2.GL_FOG);
+ gl.glFogi(GL2.GL_FOG_MODE, GL2.GL_EXP);
+ gl.glFogfv(GL2.GL_FOG_COLOR, fogColor, 0);
+ gl.glFogf(GL2.GL_FOG_DENSITY, 0.25f);
+ gl.glHint(GL2.GL_FOG_HINT, GL.GL_DONT_CARE);
+ gl.glFogi(GL2.GL_FOG_COORDINATE_SOURCE, GL2.GL_FOG_COORDINATE);
+ gl.glClearColor(0.0f, 0.25f, 0.25f, 1.0f); /* fog color */
+ }
+
+ /*
+ * display() draws a triangle at an angle.
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+
+ if (key != null)
+ switch (key.getKeyChar()) {
+ case 'c':
+ gl.glFogi(GL2.GL_FOG_COORDINATE_SOURCE,
+ GL2.GL_FRAGMENT_DEPTH);
+ break;
+ case 'C':
+ gl.glFogi(GL2.GL_FOG_COORDINATE_SOURCE,
+ GL2.GL_FOG_COORDINATE);
+ break;
+
+ case 'b':
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glTranslatef(0.0f, 0.0f, -0.25f);
+ break;
+ case 'f':
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glTranslatef(0.0f, 0.0f, 0.25f);
+ break;
+ default:
+ break;
+ }
+
+ gl.glColor3f(1.0f, 0.75f, 0.0f);
+ gl.glBegin(GL2.GL_TRIANGLES);
+ gl.glFogCoordf(f1);
+ gl.glVertex3f(2.0f, -2.0f, 0.0f);
+ gl.glFogCoordf(f2);
+ gl.glVertex3f(-2.0f, 0.0f, -5.0f);
+ gl.glFogCoordf(f3);
+ gl.glVertex3f(0.0f, 2.0f, -10.0f);
+ gl.glEnd();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+
+ glu.gluPerspective(45.0, 1.0, 0.25, 25.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ gl.glTranslatef(0.0f, 0.0f, -5.0f);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, //
+ boolean modeChanged, boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ this.key = key;
+ switch (key.getKeyChar()) {
+
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ case '1':
+ f1 = f1 + 0.25f;
+ break;
+ case '2':
+ f2 = f2 + 0.25f;
+ break;
+ case '3':
+ f3 = f3 + 0.25f;
+ break;
+ case '8':
+ if (f1 > 0.25)
+ f1 = f1 - 0.25f;
+ break;
+ case '9':
+ if (f2 > 0.25)
+ f2 = f2 - 0.25f;
+
+ break;
+ case '0':
+ if (f3 > 0.25)
+ f3 = f3 - 0.25f;
+ break;
+ }
+
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+}//
\ No newline at end of file diff --git a/src/redbook/src/glredbook1314/multisamp.java b/src/redbook/src/glredbook1314/multisamp.java new file mode 100644 index 0000000..16eb43c --- /dev/null +++ b/src/redbook/src/glredbook1314/multisamp.java @@ -0,0 +1,189 @@ +package glredbook1314;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*; import javax.media.opengl.awt.GLCanvas;
+
+import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program draws shows how to use multisampling to draw anti-aliased
+ * geometric primitives. The same display list, a pinwheel of triangles and
+ * lines of varying widths, is rendered twice. Multisampling is enabled when the
+ * left side is drawn. Multisampling is disabled when the right side is drawn.
+ * <br>
+ * Pressing the 'b' key toggles drawing of the checkerboard background.
+ * Antialiasing is sometimes easier to see when objects are rendered over a
+ * contrasting background.
+ */
+public class multisamp //
+ extends GLSkeleton<GLCanvas>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private boolean bgtoggle = true;
+
+ @Override
+ protected GLCanvas createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setNumSamples(2);
+ caps.setSampleBuffers(true);
+ //
+ GLCanvas panel = new GLCanvas(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ multisamp demo = new multisamp();
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ // Frame frame = new Frame("multisamp");
+ JFrame frame = new JFrame("multisamp");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ // frame.addWindowListener(new WindowAdapter() {
+ // public void windowClosing(WindowEvent e) {
+ // System.exit(0);
+ // }
+ // });
+ frame.setSize(600, 300);
+ frame.setLocationRelativeTo(null);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ /*
+ * Print out state values related to multisampling. Create display list with
+ * "pinwheel" of lines and triangles.
+ */
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ int buf[] = new int[1];
+ int sbuf[] = new int[1];
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glGetIntegerv(GL2.GL_SAMPLE_BUFFERS, buf, 0);
+ System.out.println("number of sample buffers is " + buf[0]);
+ gl.glGetIntegerv(GL2.GL_SAMPLES, sbuf, 0);
+ System.out.println("number of samples is " + sbuf[0]);
+
+ gl.glNewList(1, GL2.GL_COMPILE);
+ for (int i = 0; i < 19; i++) {
+ gl.glPushMatrix();
+ gl.glRotatef(360.0f * (float) i / 19.0f, 0.0f, 0.0f, 1.0f);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+ gl.glLineWidth((i % 3) + 1.0f);
+ gl.glBegin(GL.GL_LINES);
+ gl.glVertex2f(0.25f, 0.05f);
+ gl.glVertex2f(0.9f, 0.2f);
+ gl.glEnd();
+ gl.glColor3f(0.0f, 1.0f, 1.0f);
+ gl.glBegin(GL2.GL_TRIANGLES);
+ gl.glVertex2f(0.25f, 0.0f);
+ gl.glVertex2f(0.9f, 0.0f);
+ gl.glVertex2f(0.875f, 0.10f);
+ gl.glEnd();
+ gl.glPopMatrix();
+ }
+ gl.glEndList();
+
+ gl.glNewList(2, GL2.GL_COMPILE);
+ gl.glColor3f(1.0f, 0.5f, 0.0f);
+ gl.glBegin(GL2.GL_QUADS);
+ for (int i = 0; i < 16; i++)
+ for (int j = 0; j < 16; j++)
+ if (((i + j) % 2) == 0) {
+ gl.glVertex2f(-2.0f + (i * 0.25f), -2.0f + (j * 0.25f));
+ gl.glVertex2f(-2.0f + (i * 0.25f), -1.75f + (j * 0.25f));
+ gl.glVertex2f(-1.75f + (i * 0.25f), -1.75f + (j * 0.25f));
+ gl.glVertex2f(-1.75f + (i * 0.25f), -2.0f + (j * 0.25f));
+ }
+
+ gl.glEnd();
+ gl.glEndList();
+ }
+
+ /*
+ * Draw two sets of primitives, so that you can compare the user of
+ * multisampling against its absence. This code enables antialiasing and
+ * draws one display list and disables and draws the other display list
+ */
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ //
+ if (bgtoggle)
+ gl.glCallList(2);
+
+ gl.glEnable(GL2.GL_MULTISAMPLE);
+ gl.glPushMatrix();
+ gl.glTranslatef(-1.0f, 0.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+ gl.glDisable(GL2.GL_MULTISAMPLE);
+
+ gl.glPushMatrix();
+ gl.glTranslatef(1.0f, 0.0f, 0.0f);
+ gl.glCallList(1);
+ gl.glPopMatrix();
+
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+ if (w <= (2 * h))
+ glu.gluOrtho2D(-2.0, 2.0, //
+ -2.0 * (float) h / (float) w, 2.0 * (float) h / (float) w);
+ else
+ glu.gluOrtho2D(-2.0 * (float) w / (float) h, //
+ 2.0 * (float) w / (float) h, -2.0, 2.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ switch (key.getKeyChar()) {
+
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+ case 'b':
+ case 'B':
+ bgtoggle = !bgtoggle;
+ break;
+
+ default:
+ break;
+ }
+
+ super.refresh();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook1314/mvarray.java b/src/redbook/src/glredbook1314/mvarray.java new file mode 100644 index 0000000..fe96532 --- /dev/null +++ b/src/redbook/src/glredbook1314/mvarray.java @@ -0,0 +1,198 @@ +/**
+ *
+ */
+package glredbook1314;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+import com.sun.opengl.util.BufferUtil;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates multiple vertex arrays, specifically the OpenGL
+ * routine glMultiDrawElements(), but it's a bitch to setup--so I use
+ * DrawElements in a loop instead.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes (JOGL port)
+ */
+public class mvarray //
+ implements GLEventListener //
+ , KeyListener {
+ private GLU glu;
+
+ private int vertices[] = { 25, 25,//
+ 75, 75,//
+ 100, 125,//
+ 150, 75,//
+ 200, 175,//
+ 250, 150,//
+ 300, 125,//
+ 100, 200,//
+ 150, 250,//
+ 200, 225,//
+ 250, 300,//
+ 300, 250 };
+
+ private IntBuffer vertexBuf = //
+ BufferUtil.newIntBuffer(vertices.length);
+
+ private byte oneIndices[] = { 0, 1, 2, 3, 4, 5, 6 };
+
+ private byte twoIndices[] = { 1, 7, 8, 9, 10, 11 };
+
+ private int count[] = { 7, 6 };
+
+ private ByteBuffer indices[] = {//
+ BufferUtil.newByteBuffer(oneIndices.length),
+ BufferUtil.newByteBuffer(twoIndices.length) };
+
+ // static GLvoid * indices[2] = {oneIndices, twoIndices};
+ {
+ vertexBuf.put(vertices);
+ indices[0].put(oneIndices);
+ indices[1].put(twoIndices);
+
+ vertexBuf.rewind();
+ indices[0].rewind();
+ indices[1].rewind();
+ }
+
+ private boolean mde_bug;
+
+ /**
+ *
+ */
+ public mvarray() {
+ }
+
+ private void setupPointer(GL2 gl) {
+ gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
+ gl.glVertexPointer(2, GL2.GL_INT, 0, vertexBuf);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.media.opengl.GLEventListener#display(javax.media.opengl.GLAutoDrawable)
+ */
+ public void display(GLAutoDrawable drawable) {
+ final GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT);
+ gl.glColor3f(1.0f, 1.0f, 1.0f);
+
+ if (mde_bug)
+ gl.glMultiDrawElements(GL.GL_LINE_STRIP, count, 0,//
+ GL.GL_UNSIGNED_BYTE, indices, 2);
+ else {
+ // workaround for glMultiDrawElem bug before July
+ for (int i = 0; i < indices.length; i++)
+ gl.glDrawElements(GL.GL_LINE_STRIP, count[i], //
+ GL.GL_UNSIGNED_BYTE, indices[i]);
+ }
+ gl.glFlush();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.media.opengl.GLEventListener#displayChanged(javax.media.opengl.GLAutoDrawable,
+ * boolean, boolean)
+ */
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.media.opengl.GLEventListener#init(javax.media.opengl.GLAutoDrawable)
+ */
+ public void init(GLAutoDrawable drawable) {
+ final GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+
+ mde_bug = !gl.isFunctionAvailable("glMultiDrawElements");
+ System.out.println("glMultiDrawElements bug: " + mde_bug);
+
+ gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+ gl.glShadeModel(GL2.GL_SMOOTH);
+ setupPointer(gl);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.media.opengl.GLEventListener#reshape(javax.media.opengl.GLAutoDrawable,
+ * int, int, int, int)
+ */
+ public void reshape(GLAutoDrawable drawable, int x, int y, //
+ int width, int height) {
+ final GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, width, height);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+
+ glu.gluOrtho2D(0.0, (double) width, 0.0, (double) height);
+
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);
+ GLJPanel canvas = new GLJPanel(caps);
+
+ mvarray demo = new mvarray();
+ canvas.addGLEventListener(demo);
+ canvas.addKeyListener(demo);
+
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("mvarray");
+ frame.setSize(350, 350);
+ frame.setLocationRelativeTo(null);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ frame.getContentPane().add(canvas);
+ frame.setVisible(true);
+ frame.setFocusable(false);
+ canvas.requestFocusInWindow();
+ }
+
+ public void keyPressed(KeyEvent e) {
+ switch (e.getKeyCode()) {
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ public void keyReleased(KeyEvent e) {
+ // TODO Auto-generated method stub
+ }
+
+ public void keyTyped(KeyEvent e) {
+ // TODO Auto-generated method stub
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook1314/pointp.java b/src/redbook/src/glredbook1314/pointp.java new file mode 100644 index 0000000..dedfaf9 --- /dev/null +++ b/src/redbook/src/glredbook1314/pointp.java @@ -0,0 +1,197 @@ +package glredbook1314;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+import javax.media.opengl.*;
+import javax.swing.JFrame;
+
+import glredbook10.GLSkeleton;
+import javax.media.opengl.awt.GLJPanel;
+import javax.media.opengl.glu.GLU;
+
+/**
+ * This program demonstrates point parameters and their effect on point
+ * primitives. 250 points are randomly generated within a 10 by 10 by 40 region,
+ * centered at the origin. In some modes (including the default), points that
+ * are closer to the viewer will appear larger. Pressing the 'l', 'q', and 'c'
+ * keys switch the point parameters attenuation mode to linear, quadratic, or
+ * constant, respectively. Pressing the 'f' and 'b' keys move the viewer forward
+ * and backwards. In either linear or quadratic attenuation mode, the distance
+ * from the viewer to the point will change the size of the point primitive.
+ * Pressing the '+' and '-' keys will change the current point size. In this
+ * program, the point size is bounded, so it will not get less than 2.0, nor
+ * greater than GL_POINT_SIZE_MAX.
+ *
+ * @author Kiet Le (Java port) Ported to JOGL 2.x by Claudio Eduardo Goes
+ */
+public class pointp//
+ extends GLSkeleton<GLJPanel>
+ implements GLEventListener, KeyListener {
+
+ private GLU glu;
+
+ //
+ private static float psize = 7.0f;
+ private static float pmax[] = new float[1];
+ private static float constant[] = { 1.0f, 0.0f, 0.0f };
+ private static float linear[] = { 0.0f, 0.12f, 0.0f };
+ private static float quadratic[] = { 0.0f, 0.0f, 0.01f };
+ private KeyEvent key;
+
+ @Override
+ protected GLJPanel createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ caps.setSampleBuffers(true);
+ //
+ GLJPanel panel = new GLJPanel(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public static void main(String[] args) {
+ pointp demo = new pointp();
+
+ /* metal/ocean LAF can't handle heavy-weight canvas on resize */
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("pointp");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setSize(500, 500);
+ frame.setLocationRelativeTo(null);
+
+ frame.getContentPane().add(demo.drawable);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ //
+ gl.glNewList(1, GL2.GL_COMPILE);
+ gl.glBegin(GL.GL_POINTS);
+
+ float min = -5;
+ float max = 5;
+ for (int i = 0; i < 250; i++) {
+ gl.glColor3f(1.0f, (float) Math.random(), (float) Math.random());
+ /*
+ * randomly generated vertices: -5 < x < 5; -5 < y < 5; -5 < z < -45
+ */
+ gl.glVertex3f((float) ((Math.random() * (max - min)) - max),//
+ (float) ((Math.random() * (max - min)) - max),//
+ (float) (((Math.random() * (max - min)) - max)));
+ }
+ gl.glEnd();
+ gl.glEndList();
+
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glEnable(GL2.GL_POINT_SMOOTH);
+ gl.glEnable(GL.GL_BLEND);
+ gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
+ gl.glPointSize(psize);
+ gl.glGetFloatv(GL2.GL_POINT_SIZE_MAX, pmax, 0);
+
+ gl.glPointParameterfv(GL2.GL_ATTENUATION_EXT, linear, 0);
+ gl.glPointParameterf(GL2.GL_POINT_FADE_THRESHOLD_SIZE, 2.0f);
+
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ if (key != null)
+ switch (key.getKeyChar()) {
+ case 'b':
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glTranslatef(0.0f, 0.0f, -0.5f);
+
+ break;
+ case 'c':
+ gl.glPointParameterfv(GL2.GL_ATTENUATION_EXT,
+ constant, 0);
+
+ break;
+ case 'f':
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glTranslatef(0.0f, 0.0f, 0.5f);
+
+ break;
+ case 'l':
+ gl.glPointParameterfv(GL2.GL_ATTENUATION_EXT,
+ linear, 0);
+
+ break;
+ case 'q':
+ gl.glPointParameterfv(GL2.GL_ATTENUATION_EXT,
+ quadratic, 0);
+
+ break;
+ case '+':
+ if (psize < (pmax[0] + 1.0f))
+ psize = psize + 1.0f;
+ gl.glPointSize(psize);
+
+ break;
+ case '-':
+ if (psize >= 2.0f)
+ psize = psize - 1.0f;
+ gl.glPointSize(psize);
+
+ break;
+
+ }
+
+ gl.glCallList(1);
+ gl.glFlush();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+
+ glu.gluPerspective(35, (float) w / (float) h, 0.25, 200);
+
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+
+ gl.glTranslatef(0.0f, 0.0f, -10.0f);
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+ this.key = key;
+ switch (key.getKeyCode()) {
+
+ case KeyEvent.VK_ESCAPE:
+ System.exit(0);
+ break;
+
+ default:
+ break;
+ }
+
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
diff --git a/src/redbook/src/glredbook1314/shadowmap.java b/src/redbook/src/glredbook1314/shadowmap.java new file mode 100644 index 0000000..3dc79cc --- /dev/null +++ b/src/redbook/src/glredbook1314/shadowmap.java @@ -0,0 +1,418 @@ +package glredbook1314;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.nio.FloatBuffer;
+
+import javax.media.opengl.*; import javax.media.opengl.awt.GLCanvas;
+
+import javax.swing.JFrame;
+
+import com.sun.opengl.util.BufferUtil;
+import com.sun.opengl.util.FPSAnimator;
+
+
+import com.sun.opengl.util.gl2.GLUT;
+import glredbook10.GLSkeleton;
+import javax.media.opengl.glu.GLU;
+
+
+public class shadowmap //
+ extends GLSkeleton<GLCanvas>
+ implements GLEventListener, KeyListener {
+ private GLU glu;
+ private GLUT glut;
+ private FPSAnimator animator;
+// private KeyEvent key;
+ private boolean textureOn = true;
+ private boolean compareMode = true;
+ private boolean funcMode = true;
+ private boolean animate = true;
+
+ private static final int SHADOW_MAP_WIDTH = 256;
+ private static final int SHADOW_MAP_HEIGHT = 256;
+
+ private double fovy = 60.0;
+ private double nearPlane = 10.0;
+ private double farPlane = 100.0;
+
+ private float angle = 0.0f;
+ private float torusAngle = 0.0f;
+
+ private float lightPos[] = { 25.0f, 25.0f, 25.0f, 1.0f };
+ private float lookat[] = { 0.0f, 0.0f, 0.0f };
+ private float up[] = { 0.0f, 0.0f, 1.0f };
+
+ private boolean showShadow = false;
+
+ @Override
+ protected GLCanvas createDrawable() {
+ GLCapabilities caps = new GLCapabilities(null);
+ //
+ GLCanvas panel = new GLCanvas(caps);
+ panel.addGLEventListener(this);
+ panel.addKeyListener(this);
+ return panel;
+ }
+
+ public void run() {
+ shadowmap demo = new shadowmap();
+ //
+ JFrame.setDefaultLookAndFeelDecorated(true);
+ JFrame frame = new JFrame("shadowmap");
+ frame.getContentPane().add(demo.drawable);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setSize(512, 512);
+ frame.setLocationRelativeTo(null);
+ frame.setVisible(true);
+ demo.drawable.requestFocusInWindow();
+ //
+ animator = new FPSAnimator(demo.drawable, 30);
+ animator.start();
+ }
+
+ public static void main(String[] args) {
+ new shadowmap().run();
+ }
+
+ public void init(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+ glu = new GLU();
+ glut = new GLUT();
+ //
+ final float white[] = { 1.0f, 1.0f, 1.0f, 1.0f };
+
+ gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_DEPTH_COMPONENT,
+ SHADOW_MAP_WIDTH, SHADOW_MAP_HEIGHT, 0, GL2.GL_DEPTH_COMPONENT,
+ GL.GL_UNSIGNED_BYTE, null);
+
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPos, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_SPECULAR, white, 0);
+ gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_DIFFUSE, white, 0);
+
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP_TO_EDGE);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP_TO_EDGE);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D,//
+ GL2.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D,//
+ GL2.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_TEXTURE_COMPARE_FUNC, GL.GL_LEQUAL);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, //
+ GL2.GL_DEPTH_TEXTURE_MODE, GL.GL_LUMINANCE);
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D,//
+ GL2.GL_TEXTURE_COMPARE_MODE, GL2.GL_COMPARE_R_TO_TEXTURE);
+
+ gl.glTexGeni(GL2.GL_S, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_OBJECT_LINEAR);
+ gl.glTexGeni(GL2.GL_T, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_OBJECT_LINEAR);
+ gl.glTexGeni(GL2.GL_R, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_OBJECT_LINEAR);
+ gl.glTexGeni(GL2.GL_Q, GL2.GL_TEXTURE_GEN_MODE, GL2.GL_OBJECT_LINEAR);
+
+ gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL2.GL_DIFFUSE & GL2.GL_SPECULAR);
+
+ gl.glCullFace(GL.GL_BACK);
+
+ gl.glEnable(GL.GL_DEPTH_TEST);
+ gl.glEnable(GL2.GL_LIGHT0);
+ gl.glEnable(GL2.GL_LIGHTING);
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ gl.glEnable(GL2.GL_TEXTURE_GEN_S);
+ gl.glEnable(GL2.GL_TEXTURE_GEN_T);
+ gl.glEnable(GL2.GL_TEXTURE_GEN_R);
+ gl.glEnable(GL2.GL_TEXTURE_GEN_Q);
+ gl.glEnable(GL2.GL_COLOR_MATERIAL);
+ gl.glEnable(GL.GL_CULL_FACE);
+ }
+
+ public void display(GLAutoDrawable drawable) {
+ GL2 gl = drawable.getGL().getGL2();
+
+ float radius = 30;
+
+ if (textureOn)
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ else
+ gl.glDisable(GL2.GL_TEXTURE_2D);
+
+ if (compareMode)
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_COMPARE_MODE,
+ GL2.GL_COMPARE_R_TO_TEXTURE );
+ else
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_COMPARE_MODE,
+ GL.GL_NONE );
+
+ if (funcMode)
+ gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_COMPARE_FUNC,
+ GL.GL_LEQUAL );
+ else gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_COMPARE_FUNC,
+ GL.GL_GEQUAL);
+
+
+// if (showShadow)
+// {
+// return;}
+
+ if (showShadow)
+ return;
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+ generateShadowMap(gl);
+ generateTextureMatrix(gl);
+
+ gl.glPushMatrix();
+ glu.gluLookAt(radius * Math.cos(angle), radius * Math.sin(angle), 30,
+ lookat[0], lookat[1], lookat[2], //
+ up[0], up[1], up[2]);
+ drawObjects(gl, false);
+ gl.glPopMatrix();
+
+ gl.glFlush();
+ // angle += (float) Math.PI / 10000;
+ // torusAngle += .1;
+ idle();
+ }
+
+ public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
+ GL2 gl = drawable.getGL().getGL2();
+ //
+ gl.glViewport(0, 0, w, h);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glLoadIdentity();
+
+ glu.gluPerspective(fovy, (double) w / h, nearPlane, farPlane);
+
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+ gl.glLoadIdentity();
+ }
+
+ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
+ boolean deviceChanged) {
+ }
+
+ private void idle() {
+ angle += (float) Math.PI / 10000;
+ torusAngle += .1;
+ }
+
+ private void drawObjects(GL2 gl, boolean shadowRender) {
+ boolean textureOn = gl.glIsEnabled(GL2.GL_TEXTURE_2D);
+
+ if (shadowRender)
+ gl.glDisable(GL2.GL_TEXTURE_2D);
+
+ if (!shadowRender) {
+ gl.glNormal3f(0, 0, 1);
+ gl.glColor3f(1, 1, 1);
+ gl.glRectf(-20.0f, -20.0f, 20.0f, 20.0f);
+ }
+
+ gl.glPushMatrix();
+ gl.glTranslatef(11, 11, 11);
+ gl.glRotatef(54.73f, -5, 5, 0);
+ gl.glRotatef(torusAngle, 1, 0, 0);
+ gl.glColor3f(1, 0, 0);
+ glut.glutSolidTorus(1, 4, 8, 36);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(2, 2, 2);
+ gl.glColor3f(0, 0, 1);
+ glut.glutSolidCube(4);
+ gl.glPopMatrix();
+
+ gl.glPushMatrix();
+ gl.glTranslatef(lightPos[0], lightPos[1], lightPos[2]);
+ gl.glColor3f(1, 1, 1);
+ glut.glutWireSphere(0.5, 6, 6);
+ gl.glPopMatrix();
+
+ if (shadowRender && textureOn)
+ gl.glEnable(GL2.GL_TEXTURE_2D);
+ }
+ FloatBuffer depthImageBuf = BufferUtil.newFloatBuffer(SHADOW_MAP_WIDTH * SHADOW_MAP_HEIGHT);
+
+ private void generateShadowMap(GL2 gl) {
+ int viewport[] = {0,0,0,0};//new int[4];
+ float lightPos[] = {0,0,0,0};//new float[4];
+
+ gl.glGetLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, lightPos, 0);
+ gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
+
+ gl.glViewport(0, 0, SHADOW_MAP_WIDTH, SHADOW_MAP_HEIGHT);
+
+ gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glPushMatrix();
+ gl.glLoadIdentity();
+ glu.gluPerspective(80.0, 1.0, 10.0, 1000.0);
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+
+ gl.glPushMatrix();
+ gl.glLoadIdentity();
+ glu.gluLookAt(lightPos[0], lightPos[1], lightPos[2],//
+ lookat[0], lookat[1], lookat[2], //
+ up[0], up[1], up[2]);
+
+ drawObjects(gl, true);
+
+ gl.glPopMatrix();
+ gl.glMatrixMode(GL2.GL_PROJECTION);
+ gl.glPopMatrix();
+ gl.glMatrixMode(GL2.GL_MODELVIEW);
+
+ gl.glCopyTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_DEPTH_COMPONENT, 0, 0,
+ SHADOW_MAP_WIDTH, SHADOW_MAP_HEIGHT, 0);
+
+ gl.glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
+
+ if (showShadow) {
+ // float depthImage[][] = new
+ // float[SHADOW_MAP_WIDTH][SHADOW_MAP_HEIGHT];
+// FloatBuffer depthImageBuf = BufferUtil//
+// .newFloatBuffer(SHADOW_MAP_HEIGHT * SHADOW_MAP_WIDTH);
+ gl.glReadPixels(0, 0, SHADOW_MAP_WIDTH, SHADOW_MAP_HEIGHT,
+ GL2.GL_DEPTH_COMPONENT, GL.GL_FLOAT, depthImageBuf);
+ gl.glWindowPos2f(viewport[2] / 2, 0);
+ depthImageBuf.rewind();
+// gl.glDrawPixels(SHADOW_MAP_WIDTH, SHADOW_MAP_HEIGHT,
+// GL.GL_LUMINANCE, GL.GL_FLOAT, depthImageBuf);
+ gl.glDrawPixels(SHADOW_MAP_HEIGHT, SHADOW_MAP_WIDTH,
+ GL.GL_LUMINANCE, GL.GL_FLOAT, depthImageBuf);
+// depthImageBuf = null;
+ // glutSwapBuffers();
+// demo.canvas.display();
+ // demo.canvas.display();causes overflow!
+ }
+ }
+ FloatBuffer tmpMatbuf = BufferUtil.newFloatBuffer(16);
+
+ private void generateTextureMatrix(GL2 gl) {
+ // float tmpMatrix[] = new float[16];
+
+ /*
+ * Set up projective texture matrix. We use the GL_MODELVIEW matrix
+ * stack and OpenGL matrix commands to make the matrix.
+ */
+ gl.glPushMatrix();
+ gl.glLoadIdentity();
+ gl.glTranslatef(0.5f, 0.5f, 0.0f);
+ gl.glScalef(0.5f, 0.5f, 1.0f);
+ glu.gluPerspective(60.0f, 1.0, 1.0, 1000.0);
+ glu.gluLookAt(lightPos[0], lightPos[1], lightPos[2], //
+ lookat[0],
+ lookat[1], lookat[2], //
+ up[0], up[1], up[2]);
+ // gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, tmpMatrix, 0);
+ gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, tmpMatbuf);
+ gl.glPopMatrix();
+
+ // System.out.println(tmpMatbuf.toString());
+ // for (int i =0; i < tmpMatbuf.capacity(); i++)
+ // System.out.println(tmpMatbuf.get(i)+".");
+ // transposeMatrix(FloatBuffer.wrap(tmpMatrix));
+// tmpMatbuf.rewind();
+ // transposeMatrix(tmpMatbuf);
+ transpose(tmpMatbuf);
+
+ gl.glTexGenfv(GL2.GL_S, GL2.GL_OBJECT_PLANE, tmpMatbuf);// , 0);
+ tmpMatbuf.position(4);
+ // System.out.println(tmpMatbuf.position());
+ gl.glTexGenfv(GL2.GL_T, GL2.GL_OBJECT_PLANE, tmpMatbuf);// , 4);
+ tmpMatbuf.position(8);
+ // System.out.println(tmpMatbuf.position());
+ gl.glTexGenfv(GL2.GL_R, GL2.GL_OBJECT_PLANE, tmpMatbuf);// , 8);
+ tmpMatbuf.position(12);
+ // System.out.println(tmpMatbuf.position());
+ gl.glTexGenfv(GL2.GL_Q, GL2.GL_OBJECT_PLANE, tmpMatbuf);// , 12);
+ // System.out.println(tmpMatbuf.position());
+ tmpMatbuf.rewind();
+ }
+
+ private void transpose(FloatBuffer mat) {
+
+ float tmp = mat.get(1);
+ mat.put(1, mat.get(4));
+ mat.put(4, tmp);
+
+ tmp= mat.get(2);
+ mat.put(2, mat.get(8));
+ mat.put(8, tmp);
+
+ tmp = mat.get(3 );
+ mat.put(3, mat.get(12));
+ mat.put(12, tmp);
+
+ tmp = mat.get(6);
+ mat.put(6, mat.get(9));
+ mat.put(9, tmp);
+
+ tmp = mat.get(7);
+ mat.put(7, mat.get(13));
+ mat.put(13, tmp);
+
+ tmp = mat.get(11);
+ mat.put(11, mat.get(14));
+ mat.put(14, tmp);mat.rewind();
+ }
+
+ public void keyTyped(KeyEvent key) {
+ }
+
+ public void keyPressed(KeyEvent key) {
+// this.key = key;
+ switch (key.getKeyChar()) {
+
+ case KeyEvent.VK_ESCAPE:
+ new Thread() {
+ public void run() {
+ animator.stop();
+ }
+ }.start();
+ System.exit(0);
+ break;
+
+ case 's':
+ showShadow = !showShadow;
+ break;
+
+ case 'p':
+ animate = !animate;
+ if (animate)
+ animator.start();
+ else
+ animator.stop();
+ break;
+
+ case 't':
+ textureOn = !textureOn;
+ System.out.println("textureOn: " + textureOn);
+ break;
+
+ case 'm':
+ compareMode = !compareMode;
+ System.out.println("Compare mode " + (compareMode ? "On" : "Off"));
+ break;
+
+ case 'f':
+ funcMode = !funcMode;
+ System.out.println//
+ ("Operator " + (funcMode ? "GL_LEQUAL" : "GL_GEQUAL"));
+ break;
+
+ }
+
+ super.refresh();
+ }
+
+ public void keyReleased(KeyEvent key) {
+ }
+
+ public void dispose(GLAutoDrawable arg0) {
+
+ }
+
+}
|