/*
Copyright (c) 2004, Bradley Jones
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the nondot.org nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package org.nondot.mesh;
import org.nondot.math.*;
import java.awt.Color;
import javax.vecmath.Color4b;
import javax.vecmath.TexCoord2f;
import javax.vecmath.TexCoord3f;
import javax.vecmath.TexCoord4f;
import org.nondot.primitives.RenderContext;
import net.java.games.jogl.*;
public interface Vertex {
public void setPosition(float x, float y, float z);
public void setPosition(float x, float y, float z, float w);
public void setNormal(float x, float y, float z);
public void setPointSize(float x);
public void setDiffuseColor(Color color);
public void setSpecularColor(Color color);
public void setTextureCoordinate(int set, float r);
public void setTextureCoordinate(int set, float r, float s);
public void setTextureCoordinate(int set, float r, float s, float t);
public void setTextureCoordinate(
int set,
float r,
float s,
float t,
float u);
public int getNumberOfTextureCoordinates();
public void issue(GL gl);
public void describe(MeshBuilder mb);
}
abstract class AbstractVertex implements Vertex {
public void setPosition(float x, float y, float z) {
}
public void setPosition(float x, float y, float z, float w) {
}
public void setNormal(float x, float y, float z) {
}
public void setPointSize(float x) {
}
public void setDiffuseColor(Color color) {
}
public void setSpecularColor(Color color) {
}
public void setTextureCoordinate(int set, float r) {
}
public void setTextureCoordinate(int set, float r, float s) {
}
public void setTextureCoordinate(int set, float r, float s, float t) {
}
public void setTextureCoordinate(
int set,
float r,
float s,
float t,
float u) {
}
public int getNumberOfTextureCoordinates() {
return 0;
}
public abstract void issue(GL gl);
public abstract void describe(MeshBuilder mb);
}
class UntransformedVertex extends AbstractVertex {
public void setPosition(float x, float y, float z) {
position = new Vector3f(x, y, z);
}
public void issue(GL gl) {
gl.glVertex3f(position.getX(), position.getY(), position.getZ());
}
public void describe(MeshBuilder mb) {
mb.addVertex(position.getX(), position.getY(), position.getZ());
}
Vector3f position = new Vector3f(0.0f,0.0f,0.0f);
}
class TransformedVertex extends AbstractVertex {
public void setPosition(float x, float y, float z, float w) {
position = new Vector4f(x, y, z, w);
}
public void issue(GL gl) {
gl.glVertex4f(position.getX(), position.getY(), position.getZ(), position.getW());
}
public void describe(MeshBuilder mb) {
mb.addVertex(position.getX(), position.getY(), position.getZ(), position.getW());
}
Vector4f position = new Vector4f(0, 0, 0, 1);
}
abstract class AttributeVertex extends AbstractVertex {
AttributeVertex(Vertex parent) {
this.parent = parent;
}
public void setPosition(float x, float y, float z) {
parent.setPosition(x, y, z);
}
public void setPosition(float x, float y, float z, float w) {
parent.setPosition(x, y, z, w);
}
public void setNormal(float x, float y, float z) {
parent.setNormal(x, y, z);
}
public void setPointSize(float x) {
parent.setPointSize(x);
}
public void setDiffuseColor(Color color) {
parent.setDiffuseColor(color);
}
public void setSpecularColor(Color color) {
parent.setSpecularColor(color);
}
public void setTextureCoordinate(int set, float r) {
parent.setTextureCoordinate(set, r);
}
public void setTextureCoordinate(int set, float r, float s) {
parent.setTextureCoordinate(set, r, s);
}
public void setTextureCoordinate(int set, float r, float s, float t) {
parent.setTextureCoordinate(set, r, s, t);
}
public void setTextureCoordinate(
int set,
float r,
float s,
float t,
float u) {
parent.setTextureCoordinate(set, r, s, t, u);
}
protected Vertex parent;
}
class NormalVertex extends AttributeVertex {
public NormalVertex(Vertex parent) {
super(parent);
}
public void setNormal(float x, float y, float z) {
normal = new Vector3f(x, y, z);
}
public void issue(GL gl) {
gl.glNormal3f(normal.getX(), normal.getY(), normal.getZ());
parent.issue(gl);
}
public void describe(MeshBuilder mb) {
parent.describe(mb);
mb.setNormal(normal.getX(), normal.getY(), normal.getZ());
}
Vector3f normal = new Vector3f(0, 0, 1);
}
class PointSizeVertex extends AttributeVertex {
PointSizeVertex(Vertex parent) {
super(parent);
}
public void setPointSize(float size) {
this.size = size;
}
public void issue(GL gl) {
// TODO: Lookup how to issue point sizes
parent.issue(gl);
}
public void describe(MeshBuilder mb) {
parent.describe(mb);
mb.setPointSize(size);
}
float size = 1.0f;
}
class DiffuseColorVertex extends AttributeVertex {
public DiffuseColorVertex(Vertex parent) {
super(parent);
}
public void setDiffuseColor(Color color) {
this.color = new Color4b(color);
}
public void issue(GL gl) {
gl.glColor4b(color.x, color.y, color.z, color.w);
parent.issue(gl);
}
public void describe(MeshBuilder mb) {
parent.describe(mb);
mb.setDiffuseColor(color.get());
}
Color4b color = new Color4b();
}
class SpecularColorVertex extends AttributeVertex {
public SpecularColorVertex(Vertex parent) {
super(parent);
}
public void setSpecularColor(Color color) {
this.color = new Color4b(color);
}
public void issue(GL gl) {
gl.glSecondaryColor3bEXT(color.x,color.y,color.z);
parent.issue(gl);
}
public void describe(MeshBuilder mb) {
parent.describe(mb);
mb.setSpecularColor(color.get());
}
Color4b color = new Color4b();
}
abstract class TextureCoordinateVertex extends AttributeVertex {
TextureCoordinateVertex(Vertex parent) {
super(parent);
texSet = parent.getNumberOfTextureCoordinates();
}
public int getNumberOfTextureCoordinates() {
return parent.getNumberOfTextureCoordinates() + 1;
}
public int getTextureSet() {
return texSet;
}
public int getGLTexSet() {
return translateTexSet(getTextureSet());
}
public static int translateTexSet(int texSet) {
switch(texSet) {
case 0:
return GL.GL_TEXTURE0;
case 1:
return GL.GL_TEXTURE1;
case 2:
return GL.GL_TEXTURE2;
case 3:
return GL.GL_TEXTURE3;
case 4:
return GL.GL_TEXTURE4;
case 5:
return GL.GL_TEXTURE5;
case 6:
return GL.GL_TEXTURE6;
case 7:
return GL.GL_TEXTURE7;
default:
throw new IllegalArgumentException();
}
}
int texSet;
}
class TextureCoordinate1DVertex extends TextureCoordinateVertex {
TextureCoordinate1DVertex(Vertex parent) {
super(parent);
}
public void setTextureCoordinates(int set, float r) {
if (set == getTextureSet()) {
this.r = r;
}
}
public void issue(GL gl) {
gl.glMultiTexCoord1f(getGLTexSet(),r);
parent.issue(gl);
}
public void describe(MeshBuilder mb) {
parent.describe(mb);
mb.setTextureCoordinate(getTextureSet(),r);
}
float r = 0.0f;
}
class TextureCoordinate2DVertex extends TextureCoordinateVertex {
TextureCoordinate2DVertex(Vertex parent) {
super(parent);
}
public void setTextureCoordinates(int set, float r, float s) {
if (set == getTextureSet()) {
texCoords = new TexCoord2f(r, s);
}
}
public void issue(GL gl) {
gl.glMultiTexCoord2f(getGLTexSet(), texCoords.x, texCoords.y);
parent.issue(gl);
}
public void describe(MeshBuilder mb) {
parent.describe(mb);
mb.setTextureCoordinate(getTextureSet(),texCoords.x, texCoords.y);
}
TexCoord2f texCoords = new TexCoord2f();
}
class TextureCoordinate3DVertex extends TextureCoordinateVertex {
TextureCoordinate3DVertex(Vertex parent) {
super(parent);
}
public void setTextureCoordinates(int set, float r, float s, float t) {
if (set == getTextureSet()) {
texCoords = new TexCoord3f(r, s, t);
}
}
public void sendTo(RenderContext rc) {
rc.setMultiTexCoord(
getTextureSet(),
texCoords.x,
texCoords.y,
texCoords.z);
}
public void issue(GL gl) {
gl.glMultiTexCoord3f(getGLTexSet(), texCoords.x, texCoords.y,texCoords.z);
parent.issue(gl);
}
public void describe(MeshBuilder mb) {
parent.describe(mb);
mb.setTextureCoordinate(getTextureSet(),texCoords.x, texCoords.y,texCoords.z);
}
TexCoord3f texCoords = new TexCoord3f();
}
class TextureCoordinate4DVertex extends TextureCoordinateVertex {
TextureCoordinate4DVertex(Vertex parent) {
super(parent);
}
public void setTextureCoordinates(
int set,
float r,
float s,
float t,
float u) {
if (set == getTextureSet()) {
texCoords = new TexCoord4f(r, s, t, u);
}
}
public void sendTo(RenderContext rc) {
rc.setMultiTexCoord(
getTextureSet(),
texCoords.x,
texCoords.y,
texCoords.z,
texCoords.w);
}
public void issue(GL gl) {
gl.glMultiTexCoord4f(getGLTexSet(), texCoords.x, texCoords.y,texCoords.z,texCoords.w);
parent.issue(gl);
}
public void describe(MeshBuilder mb) {
parent.describe(mb);
mb.setTextureCoordinate(getTextureSet(),texCoords.x, texCoords.y,texCoords.z,texCoords.w);
}
TexCoord4f texCoords = new TexCoord4f();
}
|