Hi !
I have a problem with an applet and JOGL. Z axis is rotated (+ is towards the screen, - is towards the user), all solids are drawn in a strange way - coordinate is percentage of the GLcanvas width or height (depending on the current coordinate, e.g. when I call glu.gluSphere(quadric, 1, 40, 40) then the radius of sphere is the width or height of GLcanvas)- ;), z i scaled to the range from -1 to 1 too. Solids are clipped when they are translated out of this range:-/ If canvas isn’t a square then solids are stretched - calling glViewport() doesn’t fix this. I called glLoadIdentity() (in both modes - GL_MODELVIEW and GL_PROJECTION) before drawing… I tried calling glFrustum(), glDepthRange(), tried to rotate the view in GL_PROJECTION matrix mode, also I tried calling gluLookAt(). No result ! Only one call solves this - gluPerspective() in GL_PROJECTION mode. But… when I call it, and the coordinate system is fixed, gluViewport() is working right - then texture mapping becomes really nasty. And now is the tricky part - when I enable blending (after calling gluPerspective()) everything becomes right (check out the applet - blending is enabled using the radio buttons in upper right part of the panel). I checked Nehe tutorials, Red Book, few other tutorials (e.g. JPot) and I don’t know what’s going on ??? I’m a noooooob so it may be something very stupid
Here is the code for the applet:
public void init(net.java.games.jogl.GLDrawable gLDrawable)
{
//pobieramy łączenia
GL gl = gLDrawable.getGL();
GLU glu = gLDrawable.getGLU();
//cienioweanie
gl.glShadeModel(GL.GL_SMOOTH);
//zbufor
gl.glEnable(GL.GL_DEPTH_TEST);
//funkcja testowania widoczności
gl.glDepthFunc(GL.GL_LEQUAL);
//ukrywanie tylnych ścian
gl.glDisable(GL.GL_CULL_FACE);
//korekcja pertspektywy
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
//wypełniamy obraz kolorem tła - kolor czarny
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
//określa funkcje nakładania
//pierwszy człon określa współczynnik dla powierzchni nakładanej (źródło), drugi określa współczynnik dla
//powierzchni na którą nakładamy (oczywiście mam na myśli barwę pikseli)
//szersze omówienie
//http://www.rush3d.com/reference/opengl-redbook-1.1/chapter07.html
gl.glBlendFunc(gl.GL_SRC_COLOR, gl.GL_ONE_MINUS_DST_COLOR);
gl.glDisable( GL.GL_BLEND);
//oświetlenie
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPos);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, ambColor);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, diffColor);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, specColor);
gl.glEnable(GL.GL_LIGHT0);
gl.glEnable(GL.GL_LIGHTING);
//teksturowanie
gl.glEnable(GL.GL_TEXTURE_2D);
//nakładanie się kolorów teksury i powierzchni - włączona jest funkcja modulacji
gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
//rezerwujemy miejsce dla tekstur
gl.glGenTextures(2, textureId);
build2DTextures("/pw/mchtr/book/JOGLBlending/iair.jpg", 0, gl, glu, false);
build2DTextures("/pw/mchtr/book/JOGLBlending/iair.jpg", 1, gl, glu, true);
// float aspect = (float)glCanvas.getWidth()/(float)glCanvas.getHeight();
// gl.glMatrixMode(GL.GL_PROJECTION);
// gl.glLoadIdentity();
// glu.gluPerspective(45.0f, aspect, 0, 100);
// gl.glMatrixMode(GL.GL_MODELVIEW);
// gl.glLoadIdentity();
}
public void display(net.java.games.jogl.GLDrawable gLDrawable)
{
GL gl = gLDrawable.getGL();
//czyścimy bufory
gl.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT);
//przygotowujemy macierz modelu
gl.glLoadIdentity();
//aby efekt nakładania się barw pikseli był widoczny musimy wyłączyć zbufor, inaczej nici z przezorczystości
if (blending)
{
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(srcFactor, dstFactor);
gl.glDisable(GL.GL_DEPTH_TEST);
}
else
{
gl.glDisable(GL.GL_BLEND);
gl.glEnable(GL.GL_DEPTH_TEST);
}
//cofamy sześcian o z
gl.glTranslatef(0.0f, 0.0f, z);
//obracamy
gl.glRotatef(angleX, 1.0f, 0.0f, 0.0f);
gl.glRotatef(angleY, 0.0f, 1.0f, 0.0f);
//filtr zmiejszający pracować będzie interpolując piksele - da to lepszy efekt
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, textureFilter);
//filtr zwiększający również pracować będzie interpolując piksele
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, textureFilter);
//wskazujemy teksturę którą OpenGL ma zampować
gl.glBindTexture(GL.GL_TEXTURE_2D, textureId[current]);
//ustawiamy kolor pikseli - szary, półprzezroczysty
gl.glColor4f(0.75f, 0.75f, 0.75f, 0.75f);
//ustawiamy materiał - zarówno dla przednich jak i tylnich części polygonów
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE, diffColor);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, specColor);
gl.glScalef(0.75f, 0.75f, 0.75f);
drawCube(gl);
//w ten sposób można wykonać animację obortu - po prostu zwiększamy kąt obrotu
// angleX += rotSpeedX;
// angleY += rotSpeedY;
if (angleX > 360)
angleX = 0.0f;
if (angleX < 0)
angleX = 360.0f;
if (angleY > 360)
angleY = 0.0f;
if (angleY < 0)
angleY = 360.0f;
}
Sorry, the comments are in Polish.
Link for the applet without calling gluPerspective() - http://grafit.mech.pw.edu.pl/~jaz/blend2/JOGLBlending.html
and with this method being called… - http://grafit.mech.pw.edu.pl/~jaz/blend/JOGLBlending.html
Source - http://grafit.mech.pw.edu.pl/~jaz/blend/JOGLBlending.java
Another applet - I call glFrustum() here and no effect ! :-/
http://grafit.mech.pw.edu.pl/~jaz/jogl/JOGLTextureMapping.html
Source for this one:
http://grafit.mech.pw.edu.pl/~jaz/jogl/JOGLTextureMapping.java
Sorry if it’s something really stupid.
Thank for the help in advance !
Maciek.
ps. I use JOGL 1.1b05