We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › NeHe lesson 2 question
Page Index Toggle Pages: 1
NeHe lesson 2 question (Read 1193 times)
NeHe lesson 2 question
Sep 23rd, 2009, 9:47am
 
After getting some clarification on NeHe lesson 1 I have moved on to lesson 2 and have run into more strange issues.

Quote:
import processing.opengl.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;

void setup() {
  size(400, 400, OPENGL);
  PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
  GL gl = pgl.gl;
  GLU glu = pgl.glu;
  resizeGLScene(gl, glu);
  initGL(gl);
}

void draw() {
  working();
//  broken();
}

void working() {
  PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
  GL gl = pgl.beginGL();
  gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  gl.glBegin(GL.GL_TRIANGLES);
  gl.glVertex3f(0, 100, 0);
  gl.glVertex3f(-100, -100, 0);
  gl.glVertex3f(100, -100, 0);
  gl.glEnd();
  pgl.endGL();
}

void broken() {
  PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
  GL gl = pgl.gl;
  gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  gl.glLoadIdentity();
  gl.glTranslatef(-1.5, 0.0, -6.0);
  gl.glBegin(GL.GL_TRIANGLES);
  gl.glVertex3f(0, 1, 0);
  gl.glVertex3f(-1, -1, 0);
  gl.glVertex3f(1, -1, 0);
  gl.glEnd();
}

void resizeGLScene(GL gl, GLU glu) {
  gl.glViewport(0, 0, width, height);
  gl.glMatrixMode(GL.GL_PROJECTION);
  gl.glLoadIdentity();
  glu.gluPerspective(45, (float)width/(float)height, 0.1, 100);
  gl.glMatrixMode(GL.GL_MODELVIEW);
  gl.glLoadIdentity();
}

boolean initGL(GL gl) {
  gl.glShadeModel(GL.GL_SMOOTH);
  gl.glClearColor(0, 0, 0, 0);
  gl.glClearDepth(1);
  gl.glEnable(GL.GL_DEPTH_TEST);
  gl.glDepthFunc(GL.GL_LEQUAL);
  gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
  return true;
}



The problem lies in the working and broken methods.  broken is the direct NeHe lesson 2 implementation.  Running that method in draw produces no output.  That actually doesn't surprise me because I'm not quite sure how a triangle that is 1 pixel big would show up.  In any case the differences between the two methods are as follows:

working uses beginGL/endGL and does not use glLoadIdentity or glTranslate.  It also creates a triangle that is 100 pixels big.

Now, I've combed through forum posts, done much reading on google and understand I have a TON to learn.  Most responses to people on this forum asking "how do I learn opengl" point them towards these NeHe tutorials and the JOGL ports.  It seems like I am missing some crucial bit of documentation here because I have no idea how a 1x1x1 triangle would ever be visible.  Are there any "opengl for processing" tutorials that explain the differences between the two?  I can't seem to find any.  As always, any help on figuring out lesson 2 or just general help and/or resources would be greatly appreciated.  Thanks!
Re: NeHe lesson 2 question
Reply #1 - Sep 23rd, 2009, 10:31am
 
>  I'm not quite sure how a triangle that is 1 pixel big would show up.

who said anything about those triangle's measurements being in pixels? how something gets displayed on screen depends on how big the screen is and how close you are to it.

forget pixels, you're in 3d now 8)
Re: NeHe lesson 2 question
Reply #2 - Sep 23rd, 2009, 10:37am
 
change the translate in broken() to

gl.glTranslatef(0.0, 0.0, -35.0);

and you'll see your triangle. it doesn't work with -30 however. i think this is a clipping problem.
Re: NeHe lesson 2 question
Reply #3 - Sep 23rd, 2009, 11:14am
 
units! yes, not pixels, ok...based on your discovery around the clipping plane I continued to mess around with perspective call and realized that somewhere between setup and draw the perspective was being reset.  This code refactors some stuff so that the perspective code which sets the clipping plane is called in draw.  Everything looks great now!  I could hug yalls, seriously.

Quote:
import processing.opengl.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;

void setup() {
  size(600, 500, OPENGL);
  PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
  initGL(pgl);
}

void draw() {
  PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
  GL gl = pgl.gl;

  initPerspective(pgl, gl);

  gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  gl.glLoadIdentity();
  gl.glTranslatef(-1.5, 0, -6);
  gl.glBegin(GL.GL_TRIANGLES);
  gl.glVertex3f(0, 1, 0);
  gl.glVertex3f(-1, -1, 0);
  gl.glVertex3f(1, -1, 0);
  gl.glEnd();

  gl.glTranslatef(3, 0, 0);
  gl.glBegin(GL.GL_QUADS);
  gl.glVertex3f(-1, 1, 0);
  gl.glVertex3f(1, 1, 0);
  gl.glVertex3f(1, -1, 0);
  gl.glVertex3f(-1, -1, 0);
  gl.glEnd();
}

void initPerspective(PGraphicsOpenGL pgl, GL gl) {
  GLU glu = pgl.glu;
  gl.glMatrixMode(GL.GL_PROJECTION);
  gl.glLoadIdentity();
  glu.gluPerspective(45, (float)width/(float)height, 0.1, 100);
  gl.glMatrixMode(GL.GL_MODELVIEW);
  gl.glLoadIdentity();
}

boolean initGL(PGraphicsOpenGL pgl) {
  GL gl = pgl.gl;
  gl.glViewport(0, 0, width, height);
  gl.glShadeModel(GL.GL_SMOOTH);
  gl.glClearColor(0, 0, 0, 0);
  gl.glClearDepth(1);
  gl.glEnable(GL.GL_DEPTH_TEST);
  gl.glDepthFunc(GL.GL_LEQUAL);
  gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
  return true;
}

Page Index Toggle Pages: 1