各位大师,帮我生成一个三维图 用java3d 或者jogl
各位大师,帮我生成一个三维图 用java3d 或者jogl 我是图像菜鸟!!!
图像,就是图中所示,我用java 3d 写了一点,jogl 一点也没看懂
java 3d 代码:
package a;
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.LineArray;
import javax.media.j3d.LineAttributes;
import javax.media.j3d.LineStripArray;
import javax.media.j3d.Shape3D;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class lineShape extends Shape3D{
private float vert[]=
{
-0.6f,0.4f,0.0f, -0.6f,-0.4f,0.0f, -0.2f,-0.4f,0.0f,
};
private float vert2[]=
{
0.5f,0.8f,0.1f,0.2f,0.4f,0.0f, 0.2f,-0.4f,0.0f, 0.6f,-0.4f,0.0f,
};
private float color[]=
{
0.0f,0.5f,1.0f, 0.5f,0.0f,1.0f,
0.0f,0.8f,2.0f, 1.0f,0.0f,0.3f,
0.0f,1.0f,0.3f, 0.3f,0.8f,0.0f,
0.5f,0.3f,0.3f, 0.0f,0.4f,0.1f,
};
public lineShape()
{
int sCount[]=new int[1];
sCount[0]=3;
LineStripArray line=new LineStripArray(8,LineStripArray.COORDINATES|LineStripArray.COLOR_3,sCount);
//LineArray line=new LineArray(8,LineArray.COORDINATES|LineArray.COLOR_3);
line.setCoordinates(0, vert);
line.setColors(0, color);
LineAttributes la=new LineAttributes();
la.setLineWidth(5.0f);
la.setLineAntialiasingEnable(true);
Appearance ap=new Appearance();
ap.setLineAttributes(la);
this.setGeometry(line);
this.setAppearance(ap);
int sCount2[]=new int[1];
//曲线顶点数目
sCount2[0]=4;
LineStripArray line2=new LineStripArray(8,LineStripArray.COORDINATES|LineStripArray.COLOR_3,sCount2);
//LineArray line=new LineArray(8,LineArray.COORDINATES|LineArray.COLOR_3);
line2.setCoordinates(0, vert2);
line2.setColors(0, color);
LineAttributes la2=new LineAttributes();
la2.setLineWidth(5.0f);
la2.setLineAntialiasingEnable(true);
Appearance ap2=new Appearance();
ap.setLineAttributes(la2);
this.setGeometry(line2);
this.setAppearance(ap2);
}
public static void main(String[] args){
SimpleUniverse universe = new SimpleUniverse();
BranchGroup group=new BranchGroup();
Shape3D shape=new lineShape();
group.addChild(shape);
universe.getViewingPlatform().setNominalViewingTransform();
// 把group加入到虚拟空间中
universe.addBranchGraph(group);
}
}
//import javax.media.j3d.Appearance;
//import javax.media.j3d.BranchGroup;
//import javax.media.j3d.LineArray;
//import javax.media.j3d.LineAttributes;
//import javax.media.j3d.Shape3D;
//
//import com.sun.j3d.utils.universe.SimpleUniverse;
//
//public class lineShape extends Shape3D{
// // 直线的定点坐标
// private float vert[ ]={.5f,0.5f,0.0f, -0.3f,0.2f,0.0f,
// 0.5f,0.5f,0.2f, -0.2f,0.3f,0.9f,
// -0.5f,-0.7f,0.0f, 0.5f,-0.5f,0.0f};
// // 各定点的颜色
// private float color[ ]={
// 0.0f,0.5f,1.0f, 0.0f,0.5f,1.0f,
// 0.0f,0.8f,2.0f, 1.0f,0.0f,0.3f,
// 0.0f,1.0f,0.3f, 0.3f,0.8f,0.0f};
// public lineShape( ) {
// // 创建直线数组对象
// LineArray line=new LineArray(6,LineArray.COORDINATES |LineArray.COLOR_3);
// // 设置直线对象的坐标数组
// line.setCoordinates(0,vert);
// // 设置直线对象的颜色数组
// line.setColors(0,color);
// // 创建直线属性对象
// LineAttributes linea=new LineAttributes( );
// // 设置线宽
// linea.setLineWidth(10.0f);
// // 设置直线的渲染效果
// linea.setLineAntialiasingEnable(true);
// Appearance app=new Appearance( );
// app.setLineAttributes(linea);
// this.setGeometry(line);
// this.setAppearance(app);
// }
// public static void main(String[] args){
// SimpleUniverse universe = new SimpleUniverse();
// BranchGroup group=new BranchGroup();
// Shape3D shape=new lineShape();
// group.addChild(shape);
// universe.getViewingPlatform().setNominalViewingTransform();
// // 把group加入到虚拟空间中
// universe.addBranchGraph(group);
// }
// }
各位大师,快来帮帮我,谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.gl2.GLUT;
public class Renderer implements GLEventListener {
private GLU glu = new GLU();
private GLUT glut = new GLUT();
float rx = 20f;
Vertice[] temp;
int[] multiple = {6,200,6};
int[] grid = {10,5,10};
@Override
public void display(GLAutoDrawable gLDrawable) {
final GL2 gl = gLDrawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -25);
gl.glRotatef(20, 1, 0, 0);
gl.glRotatef(-rx, 0, 1, 0);
drawMap(gl);
rx+=0.5f;
}
public void drawMap(GL2 gl) {
for(int i=0;i<grid[0];i++){
drawLineX(gl,i,grid[0],multiple[0]); drawLineX(gl,-i,grid[0],multiple[0]);
drawLineZ(gl,i,grid[0],multiple[2]); drawLineZ(gl,-i,grid[0],multiple[2]);
}
drawLineY(gl, 10,grid[1],multiple[1]);
for(int i=0;i<temp.length;i++){
drawLineY(gl,temp[i]);
}
drawLine(gl,temp);
}
private void initVertices(int size){
temp = new Vertice[size];
for(int i=0;i<size;i++) {
float x = 0;
float y = (float)(Math.random()*1000);
float z = (float)(Math.random()*60);
temp[i] = new Vertice(x,y,z);
}
for(int i =0;i<temp.length-1;i++)
for(int j=0;j<temp.length-i-1;j++)
if(temp[j].z>temp[j+1].z) {
Vertice verTemp = temp[j];
temp[j]=temp[j+1];
temp[j+1]=verTemp;
}
}
private void drawPoint(GL2 gl,float xpos, float ypos, float zpos) {
gl.glPushMatrix();
gl.glTranslatef(xpos, ypos, zpos);
gl.glColor3f(0, 0, 0);
glut.glutWireSphere(0.2f, 2, 17);
gl.glPopMatrix();
}
private void drawLine(GL2 gl, Vertice[] vertices) {
gl.glPushMatrix();
gl.glColor3f(0, 0, 0);
gl.glBegin(GL2.GL_LINE_STRIP);
for(Vertice ver: vertices)
gl.glVertex3f(ver.x/multiple[1], 10-ver.y/multiple[1], ver.z/multiple[2]);
gl.glEnd();
gl.glPopMatrix();
}
private void drawLineX(GL2 gl,float zpos, int length, int beilv) {
gl.glPushMatrix();
gl.glTranslatef(0, 0, zpos);
gl.glColor3f(1.0f, 0, 0);
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(length-1, 0, 0); gl.glVertex3f(-length+1, 0, 0);
gl.glEnd();
if((int)zpos%2!=0)glPrint(gl,length,0,0,(int)-zpos*beilv+"");
gl.glPopMatrix();
}
private void drawLineY(GL2 gl, Vertice ver) {
gl.glPushMatrix();
gl.glTranslatef(ver.x/multiple[0], 0, ver.z/multiple[2]);
gl.glColor3f(0, 1.0f, 0);
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(0, 0, 0); gl.glVertex3f(0, 10-ver.y/multiple[1], 0);
gl.glEnd();
gl.glPopMatrix();
}
private void drawLineY(GL2 gl, float ypos, int index ,int beilv) {
gl.glPushMatrix();
gl.glColor3f(0, 1.0f, 0);
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(0, 0, 0); gl.glVertex3f(0, ypos, 0);
gl.glEnd();
for(int i=0;i<index;i++) {
float temp = ypos/index;
drawPoint(gl,0,0,0);
glPrint(gl,0,0,0,(index-i)*beilv+"");
gl.glTranslatef(0, temp, 0);
}
gl.glPopMatrix();
}
private void drawLineZ(GL2 gl, float xpos,int length,int beilv) {
gl.glPushMatrix();
gl.glTranslatef(xpos, 0, 0);
gl.glColor3f(0, 0, 1.0f);
gl.glBegin(GL2.GL_LINES);
gl.glVertex3f(0, 0, length-1); gl.glVertex3f(0, 0, -length+1);
gl.glEnd();
if((int)xpos%2!=0)glPrint(gl,0,0,length,(int)xpos*beilv+"");
gl.glPopMatrix();
}
private void glPrint(GL2 gl, float xpos, float ypos, float zpos, String string) {
gl.glPushMatrix();
gl.glTranslatef(xpos, ypos, zpos);
gl.glRasterPos3f(0f , 0f , 0f);
gl.glScalef(0.5f, 0.5f, 0.5f);
glut.glutBitmapString(GLUT.BITMAP_TIMES_ROMAN_24, string);
gl.glPopMatrix();
}
@Override
public void init(GLAutoDrawable gLDrawable) {
GL2 gl = gLDrawable.getGL().getGL2();
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.5f);
initVertices(50);
}
@Override
public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) {
final GL2 gl = gLDrawable.getGL().getGL2();
if (height <= 0) // avoid a divide by zero error!
height = 1;
final float h = (float) width / (float) height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 40.0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
class Vertice {
float x,y,z;
public Vertice(float x,float y,float z) {
this.x = x;
this.y = y;
this.z = z;
}
}
}