PDA

View Full Version : sphere mapping doesn't work but cube



swatkdw
07-02-2009, 05:10 AM
hi. It successfully does texuremapping on the cube but shpere.
Can anyone stop where the problem occurs in the following code?
it's quite long . sorry for that. if you prefer a link for this code http://pastebin.com/d39efdf97

package practice;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLJPanel;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;
import javax.swing.JFrame;

import com.sun.opengl.util.FPSAnimator;
import com.sun.opengl.util.GLUT;

public class Practice implements GLEventListener, MouseListener {
private GLU glu = new GLU();
private float spin = 0.0f, spinDelta = 0.0f;
private GLUquadric quadratic = glu.gluNewQuadric() ;;
int texture[];
Texturereader.Texture[] createdtexture = new Texturereader.Texture[3];

public static void main(String args[]){

JFrame jframe = new JFrame("this is for practice");
jframe.setSize(800,600);
jframe.setLocationRelativeTo(null); // centre of the screen
GLCapabilities caps = new GLCapabilities();
caps.setDoubleBuffered(true); // GLCapabilities enable the program to run smoothly
caps.setHardwareAccelerated(true);

GLJPanel canvas= new GLJPanel(caps);
Practice prax = new Practice();
canvas.addGLEventListener(prax);
canvas.addMouseListener(prax);

FPSAnimator animator = new FPSAnimator(canvas,50);

jframe.getContentPane().add(canvas); // add canvas to the jframe
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
jframe.setVisible(true); //show window

animator.start();
}
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
light_ini(gl);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
float zPLANE[] = {0.0f, 0.0f, 1.0f, 0.0f};
//gl.glShadeModel(GL.GL_FLAT); // polygons will have the same colour.
gl.glDepthFunc(GL.GL_LESS);
gl.glEnable(GL.GL_DEPTH_TEST);

gl.glEnable(GL.GL_FOG);
float fogColour[] = {0.0f,0.0f,0.0f,0.0f};
gl.glFogi(GL.GL_FOG_MODE,GL.GL_EXP);
gl.glFogfv(GL.GL_FOG_COLOR, fogColour, 0);
gl.glFogf(GL.GL_FOG_DENSITY, 0.0255f);
gl.glHint(GL.GL_FOG_HINT, GL.GL_NICEST);

String textureNames[] = new String[]{

"practice/demos/data/images/pyramids4.jpg", // for the cube
"images/mars_1k_color.jpg" ,// for the sphere
"practice/demos/data/images/earth3.jpg" // for the background
};
texture = new int[textureNames.length];

for(int textureIndex = 0; textureIndex < textureNames.length; textureIndex++){
try{
createdtexture[textureIndex] = Texturereader.readTexture(textureNames[textureIndex]);
}
catch(IOException e){
e.printStackTrace();
throw new RuntimeException(e);
}
}

gl.glGenTextures(3, texture, 0);
gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture[0]);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0 , GL.GL_RGB, createdtexture[0].getWidth(),
createdtexture[0].getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, createdtexture[0].getPixels());

gl.glBindTexture(GL.GL_TEXTURE_2D, texture[1]);
//float planeCoefficients[] = {1.0f, 0.0f, 0.0f, 0.0f};
gl.glTexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE, GL.GL_OBJECT_LINEAR);
gl.glTexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE, GL.GL_OBJECT_LINEAR);

// gl.glTexGenfv(GL.GL_S, GL.GL_OBJECT_PLANE, planeCoefficients);
// gl.glTexGenfv(GL.GL_T, GL.GL_OBJECT_PLANE, zPLANE);

gl.glBindTexture(GL.GL_TEXTURE_2D, texture[2]); // This Will Select The BG Maps...
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, createdtexture[2].getWidth(), createdtexture[2].getHeight(),
0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, createdtexture[2].getPixels());

// gl.glPushMatrix();
/* gl.glTranslatef(0.0f, 0.0f, -24.0f);
gl.glBegin(GL.GL_QUADS);
gl.glNormal3f(0.0f, 0.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-13.3f, -10.0f, 10.0f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(13.3f, -10.0f, 10.0f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(13.3f, 10.0f, 10.0f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-13.3f, 10.0f, 10.0f);
gl.glEnd(); */
// gl.glPopMatrix();
}
public void light_ini(GL gl) {
float specular0[] = {0.9f, 0.4f, 0.7f, 1.0f};
float ambient0[] = {0.5f, 0.8f, 0.8f, 1.0f};
float diffuse0[] = {0.9f, 0.9f, 0.9f, 1.0f};

float specular[] = {0.9f, 0.9f, 0.7f, 1.0f};
float ambient[] = {0.5f, 0.3f, 0.2f, 1.0f};
float shiness = 40.0f;

gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, specular0, 0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, ambient0, 0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, diffuse0, 0);

gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, specular, 0);
gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, ambient, 0);
gl.glMaterialf(GL.GL_FRONT, GL.GL_SHININESS, shiness);

gl.glEnable(GL.GL_LIGHT0);
gl.glEnable(GL.GL_LIGHTING);

}
public void display(GLAutoDrawable drawable){
GL gl = drawable.getGL();
GLUT glut = new GLUT();

gl.glClear(GL.GL_COLOR_BUFFER_BIT |GL.GL_DEPTH_BUFFER_BIT);
float position0[] = {0.0f, 1.0f, 0.2f, 1.0f};

gl.glPushMatrix();
gl.glTranslatef(0, 0,-4);
gl.glPushMatrix();
gl.glRotatef(2*spin, 0.0f, 1.0f, 1.0f);

gl.glEnable(GL.GL_TEXTURE_GEN_S);
gl.glEnable(GL.GL_TEXTURE_GEN_T);
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture[1]); // sphere mapping
glu.gluSphere(quadratic, 0.6, 100,100);
gl.glDisable(GL.GL_TEXTURE_2D);
gl.glDisable(GL.GL_TEXTURE_GEN_S);
gl.glDisable(GL.GL_TEXTURE_GEN_T);


gl.glPopMatrix();

gl.glTranslatef(1.5f,0.0f,0.0f);

gl.glPushMatrix();
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture[0]); // cube mapping
gl.glRotatef(2*spin, 0.0f, 1.0f, 1.0f);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, position0, 0);

gl.glBegin(GL.GL_QUADS);
// Front Face
gl.glNormal3f(0.0f, 0.0f, 0.5f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-0.5f, -0.5f, 0.5f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(0.5f, -0.5f, 0.5f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(0.5f, 0.5f, 0.5f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-0.5f, 0.5f, 0.5f);
// Back Face
gl.glNormal3f(0.0f, 0.0f, -0.5f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(-0.5f, -0.5f, -0.5f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(-0.5f, 0.5f, -0.5f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(0.5f, 0.5f, -0.5f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(0.5f, -0.5f, -0.5f);
// Top Face
gl.glNormal3f(0.0f, 0.5f, 0.0f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-0.5f, 0.5f, -0.5f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-0.5f, 0.5f, 0.5f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(0.5f, 0.5f, 0.5f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(0.5f, 0.5f, -0.5f);
// Bottom Face
gl.glNormal3f(0.0f, -0.5f, 0.0f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(-0.5f, -0.5f, -0.5f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(0.5f, -0.5f, -0.5f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(0.5f, -0.5f, 0.5f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(-0.5f, -0.5f, 0.5f);
// Right face
gl.glNormal3f(0.5f, 0.0f, 0.0f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(0.5f, -0.5f, -0.5f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(0.5f, 0.5f, -0.5f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(0.5f, 0.5f, 0.5f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(0.5f, -0.5f, 0.5f);
// Left Face
gl.glNormal3f(-0.5f, 0.0f, 0.0f);
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-0.5f, -0.5f, -0.5f);
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(-0.5f, -0.5f, 0.5f);
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(-0.5f, 0.5f, 0.5f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-0.5f, 0.5f, -0.5f);
gl.glEnd();
gl.glDisable(GL.GL_TEXTURE_2D);

gl.glPopMatrix();
gl.glPopMatrix();

spinDisplay();

gl.glFlush();
}

public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {
}

//whenever the windows size is changed
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
final GL gl = drawable.getGL();

if(height <=0)
height=1;
final float h = (float)width / (float)height;

gl.glViewport(0, 0, width, height);
/*
* x, y Specify the lower left corner of the viewport
rectangle, in pixels. The initial value is (0,0).

width, height Specify the width and height of the viewport. When a
GL context is first attached to a window, width and
height are set to the dimensions of that window.

*/
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
/*fovy Specifies the field of view angle, in degrees, in
they direction.
aspect Specifies the aspect ratio that determines the field
of view in the x direction. The aspect ratio is the
ratio of x (width) to y (height).
zNear Specifies the distance from the viewer to the near
clipping plane (always positive).
zFar Specifies the distance from the viewer to the far
clipping plane (always positive).
*/
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}

public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void spinDisplay(){
spin = spin+spinDelta;
if(spin >360f)
spin = spin -360f;
}

public void mousePressed(MouseEvent mouse) {
switch (mouse.getButton()){
case MouseEvent.BUTTON1:
spinDelta = 2f;
break;
case MouseEvent.BUTTON2:
case MouseEvent.BUTTON3:
spinDelta = 0f;
break;
}
}
public void mouseReleased(MouseEvent arg0) {
}
}

dletozeun
07-02-2009, 06:20 AM
Does that help you:
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Main=50422&amp;Number=2598 50

V-man
07-02-2009, 09:07 AM
Or this
http://www.opengl.org/wiki/Texturing_a_Sphere

swatkdw
07-02-2009, 10:53 AM
thank you very much. I had solved it anyway. and It would help me.