0

I have a simple parallelepiped with texture.

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();
}

}

The main problem that texture stretches on every face of the parallelepiped on the fullface. And so every face looks different.

enter image description here

But actually I don't want my texture be stretched. I want that all surfaces filled with my texture in pieces and looks similary.

Pablo
  • 161
  • 11
  • 1
    can you add some screenshots? – starmole Feb 15 '17 at 09:17
  • In this case the better question is: Why isn't the cube a cube? – BDL Feb 15 '17 at 09:20
  • @BDL I need exactly parallelepiped - different surfaces. I have more complex app, but this only small example – Pablo Feb 15 '17 at 09:23
  • What I meant: In the code, you define a cube with all sides having a length of 2. But in your screenshot it doesn't look as if all sides are having the same length. In case of a cube, all sides have to look exactly identical. In general: You will have to specify the tex-coords depending on the size of the faces. – BDL Feb 15 '17 at 09:26
  • @BDL Can you explain how can I influence on stretching with glTexCoord2f function? – Pablo Feb 15 '17 at 09:30
  • 1
    Your sample code has no problems, but also isn't the code used to produce the screenshot. In general you can change the values of glTexCoord2f to accomodate non-square surfaces by simply changing the numbers. (0, 0) to (1, 1) is a square. (0, 0) to (0.5, 1) is a rectangle that is twice as high as it is wide. – Robert Rouhani Feb 15 '17 at 12:43
  • You shouldn't use [deprecated OpenGL](https://www.khronos.org/opengl/wiki/Legacy_OpenGL). Use buffers and matrices in your own. – elect Feb 21 '17 at 17:12

1 Answers1

0

You defined a box where it's x and y size range from -1 to 1 but the z axis ranges from -1 to 0. While the texture coordinates are all the same.

Either adjust the texture coordinates on top, bottom, left & right faces or make the z range from -1 to 0.

Magnavode
  • 53
  • 6