Re: what's wrong with this code?
Posted by Ususuus on Oct 30, 2018; 8:41am
URL: https://forum.jogamp.org/what-s-wrong-with-this-code-tp4039269p4039277.html
i'm still stucked here, and it's good to see your reply...(reading the examples right now...^_^)
i just want to know why it doesn't work, my program is very simple in the case of 3D programs, needs very few functions, i'm planning to move to modern OPENGL after get this issue done.
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
setLight(gl, lightAmb, lightDif);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
GLU glu = GLU.createGLU();
float widthHeightRatio = (float) getWidth() / (float) getHeight();
glu.gluPerspective(45, widthHeightRatio, 1, 1000);
glu.gluLookAt(0, 0, 500, 0, 0, 0, 0, 1, 0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glTranslatef(transf.x, transf.y, transf.z);
gl.glRotatef(rotatef.y, 1f, 0f, 0f);
gl.glRotatef(rotatef.x, 0f, 1f, 0f);
gl.glRotatef(rotatef.z, 0f, 0f, 1f);
gl.glScalef(fScaleRate, fScaleRate, fScaleRate);
drawAsixes(gl);
gl.glEnable(GL2.GL_BLEND);
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
A.Draw();
B.Draw();
gl.glFlush();
if (animator.isPaused() == false) {
animator.pause();
}
}
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glEnable(GL2.GL_NORMALIZE); // 这行要看看是干什么用的
// ----- Your OpenGL initialization code here -----
gl.glClearColor(0.9f, 0.9f, 0.9f, 0.8f); // set background (clear) color
gl.glClearDepth(1.0f); // set clear depth value to farthest
gl.glEnable(GL2.GL_BLEND);
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL2.GL_DEPTH_TEST); // enables depth testing
gl.glDepthFunc(GL2.GL_LEQUAL); // the type of depth test to do
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT,GL2.GL_NICEST); /* best perspective correction */
gl.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out
// lighting
gl.glEnable(GL2.GL_LIGHTING);
gl.glEnable(GL2.GL_LIGHT0);
loadTriangle(gl);
GLU glu = GLU.createGLU();
glu.gluPerspective(45, (double) getWidth() / getHeight(), 0.01, 5000);
gl.glMatrixMode(GL2.GL_MODELVIEW);
}
public void loadTriangle(GL2 gl) {
va = new float[] { -1f, -1f, 0.5f, 1f, -1f, 0.5f, 0f, 1f, 0.5f };
vb = new float[] { -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0f, 0.5f, -0.5f };
ca = new float[] { 1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f };
cb = new float[] { 1f, 1f, 1f, 0f, 1f, 0f, 0f, 0f, 0f };
A=new Triangle(gl);
B=new Triangle(gl);
//**********************************************************************************
// if reverse below two, it will show different triangles, what i'm trying to do is to display them together. but only show the last one (in right postion and with right color)
B.genBuffers(vb, cb);
A.genBuffers(va, ca);
}
//**********************************************************************************
/*
Triangle class
*/
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.GL2;
public class Triangle {
public GL2 gl;
private IntBuffer vbo,vao;
public int count = 0;
public Triangle(GL2 gl) {
this.gl = gl;
}
public void genBuffers(float[] verArr, float[] colorArr) {
FloatBuffer colorBuf, verBuf;
verBuf = FloatBuffer.allocate(verArr.length);
colorBuf = FloatBuffer.allocate(colorArr.length);
for (int j = 0; j < verArr.length; j++) {
verBuf.put((float)verArr[j]);
colorBuf.put(colorArr[j]);
}
verBuf.flip();
colorBuf.flip();
count = 3;
vao=Buffers.newDirectIntBuffer(1);
gl.glGenBuffers(1, vao);
gl.glBindVertexArray(vao.get(0));
vbo = Buffers.newDirectIntBuffer(2);
gl.glGenBuffers(2, vbo);
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo.get(0));
gl.glBufferData(GL2.GL_ARRAY_BUFFER, verArr.length * Buffers.SIZEOF_FLOAT, verBuf, GL2.GL_STATIC_DRAW);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, 0);
gl.glEnableClientState(GL2.GL_COLOR_ARRAY);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo.get(1));
gl.glBufferData(GL2.GL_ARRAY_BUFFER, colorArr.length * Buffers.SIZEOF_FLOAT, colorBuf, GL2.GL_STATIC_DRAW);
gl.glColorPointer(3, GL2.GL_FLOAT, 0, 0);
verBuf = null;
colorBuf = null;
gl.glBindVertexArray(0);
}
public void Draw() {
gl.glEnable(GL2.GL_COLOR_MATERIAL);
gl.glBindVertexArray(vao.get(0));
gl.glDrawArrays(GL2.GL_TRIANGLES, 0, count);
gl.glBindVertexArray(0);
gl.glDisable(GL2.GL_COLOR_MATERIAL);
}
}