纹理均匀拉伸

时间:2017-02-15 09:13:46

标签: java opengl textures jogl stretching

我有一个带纹理的简单平行六面体。

import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;

import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;

public class CubeTexture implements GLEventListener {

private GLU glu = new GLU();
private float xrot,yrot,zrot;
private int texture;

@Override
public void display(GLAutoDrawable drawable) {

    final GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity(); // Reset The View
    gl.glTranslatef(0f, 0f, -5.0f);

    gl.glRotatef(xrot, 1.0f, 1.0f, 1.0f);
    gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
    gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);

    gl.glBindTexture(GL2.GL_TEXTURE_2D, texture);
    gl.glBegin(GL2.GL_QUADS);

    // Front Face
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 0f);
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 0f);
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 0f);
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 0f);

    // Back Face
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);

    // Top Face
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, 1.0f, 0f);
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, 1.0f, 0f);
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);

    // Bottom Face
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 0f);
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 0f);

    // Right face
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 0f);
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 0f);

    // Left Face
    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 0f);
    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 0f);
    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
    gl.glEnd();
    gl.glFlush();

    //change the speeds here
    xrot += .1f;
    yrot += .1f;
    zrot += .1f;
}

@Override
public void dispose(GLAutoDrawable drawable) {
    // method body
}

@Override
public void init(GLAutoDrawable drawable) {

    final GL2 gl = drawable.getGL().getGL2();

    gl.glShadeModel(GL2.GL_SMOOTH);
    gl.glClearColor(0f, 0f, 0f, 0f);
    gl.glClearDepth(1.0f);
    gl.glEnable(GL2.GL_DEPTH_TEST);
    gl.glDepthFunc(GL2.GL_LEQUAL);
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);

    gl.glEnable(GL2.GL_TEXTURE_2D);
    try{

        File im = new File("res/textures/PARED2.jpg");
        Texture t = TextureIO.newTexture(im, true);
        texture= t.getTextureObject(gl);

    }catch(IOException e){
        e.printStackTrace();
    }
}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    final GL2 gl = drawable.getGL().getGL2();
    if(height == 0)
    height = 1;

    final float h = (float) width / (float) height;
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();

    glu.gluPerspective(45.0f, h, 1.0, 20.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
}

public static void main(String[] args) {
    final GLProfile profile = GLProfile.get(GLProfile.GL2);
    GLCapabilities capabilities = new GLCapabilities(profile);

    // The canvas
    final GLCanvas glcanvas = new GLCanvas(capabilities);
    CubeTexture r = new CubeTexture();

    glcanvas.addGLEventListener(r);
    glcanvas.setSize(400, 400);

    final JFrame frame = new JFrame (" Textured Cube");
    frame.getContentPane().add(glcanvas);
    frame.setSize(frame.getContentPane().getPreferredSize());
    frame.setVisible(true);
    final FPSAnimator animator = new FPSAnimator(glcanvas, 300, true);

    animator.start();
}

}

纹理在整个面上的平行六面体的每个面上伸展的主要问题。所以每张脸看起来都不一样。

enter image description here

但实际上我不希望我的纹理被拉伸。我希望所有表面都充满了我的纹理,看起来很相似。

1 个答案:

答案 0 :(得分:0)

您定义了一个框,其中x和y的大小范围从-1到1,但z轴的范围是-1到0.而纹理坐标都是相同的。

调整顶部,底部,左侧和上方的纹理坐标。右面或使z范围从-1到0。