Reputation: 122450
I'm new to OpenGL. I'm playing around with JOGL.
I have a .obj model that I'm drawing as polygons. It looks ok, except that most of it gets clipped. So I think that I need to increase the draw distance.
I'm not really sure how to do it. Here is my render code:
private void render(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
GLUgl2 glu = new GLUgl2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
// Position the camera
glu.gluLookAt(cameraPos.x, cameraPos.y, cameraPos.z, cameraPos.x
+ cameraForward.x, cameraPos.y + cameraForward.y, cameraPos.z
+ cameraForward.z, cameraUp.x, cameraUp.y, cameraUp.z);
// uncommenting out this line will make everything disappear.
// glu.gluPerspective(2, 1, 10, 1000);
// ****** Start of example code ******
gl.glCallList(axes); // Show X, Y, Z axes
gl.glCallList(secondLines);
gl.glCallList(triangle);
gazebo.render(gl);
// ...
It's possible that my problem is something else entirely. gazebo
is of type ObjModel
, a class that reads and represents .obj
files. Here are its render and build draw list methods:
private int drawID = 0;
public ObjModel(String fn, GL2 gl) {
// ...
readFile(fn);
drawID = buildDrawList(gl);
}
public void render(GL2 gl) {
gl.glCallList(drawID);
}
private int buildDrawList(GL2 gl) {
int result = gl.glGenLists(1);
gl.glNewList(result, GL2.GL_COMPILE);
gl.glPushMatrix();
gl.glBegin(GL2.GL_POLYGON);
for (Point3f vert : vertices) {
gl.glVertex3f(vert.x, vert.y, vert.z);
}
gl.glEnd();
gl.glPopMatrix();
gl.glEndList();
return result;
}
private void readFile(String filename) {
try {
BufferedReader input = new BufferedReader(new FileReader(fileName));
try {
String newLine = null;
while ((newLine = input.readLine()) != null) {
int indVn = newLine.indexOf("vn ");
if (indVn != -1) {
readNormal(newLine);
continue;
}
int indV = newLine.indexOf("v ");
if (indV != -1) {
readVertex(newLine);
continue;
}
int indF = newLine.indexOf("f ");
if (indF != -1) {
readPolygon(newLine);
continue;
}
int indVt = newLine.indexOf("vt ");
if (indVt != -1) {
readTexture(newLine);
continue;
}
}
} finally {
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
I can post screenshots if more information is needed.
Any advice? Thanks.
Upvotes: 0
Views: 3069
Reputation: 19837
It sounds like your model has vertices outside the clipping plane. There are a few solutions for this.
1) scale down your model so that all vertices fit within the clip coordinates (use glScale()
)
2) Use gluPerspective to set up a 'camera' which has a deeper clipping plane. Though you've tried this, I suspect your unwanted results are because you're not modifying the projection matrix when using this call. Try:
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 60, 1, 0.1, 1000.0 );
3) Use glFrustrum()
to modify the clipping planes. This is similar to gluPerspective but provides greater flexibility at the expensive of some added complexity.
FYI you can still use gluPerspective or glFrustrum when in modelview matrix mode, but keep in mind that the order with which you make those calls and other matrix transformation calls (eg. glScale, glTranslate, glRotate) is important.
Upvotes: 3