I am trying to load earth.png and place it above the triangle. Image is 256x256. I followed the online tutorial and played with it for hours, but the triangle still remains white. Can someone point me in the right direction.
import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.io.InputStream; import javax.media.opengl.*; import javax.media.opengl.awt.GLCanvas; import com.jogamp.opengl.util.texture.Texture; import com.jogamp.opengl.util.texture.TextureData; import com.jogamp.opengl.util.texture.TextureIO; public class test implements GLEventListener { private Texture earthTexture; public static void main(String[] args) { GLProfile glp = GLProfile.getDefault(); GLCapabilities caps = new GLCapabilities(glp); GLCanvas canvas = new GLCanvas(caps); final Frame frame = new Frame("AWT Window Test111"); frame.setSize(700, 700); frame.add(canvas); frame.setVisible(true); // by default, an AWT Frame doesn't do anything when you click // the close button; this bit of code will terminate the program when // the window is asked to close frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { frame.dispose(); System.exit(0); } }); canvas.addGLEventListener(new test()); } @Override public void display(GLAutoDrawable arg0) { update(); render(arg0); } private void update() { // TODO Auto-generated method stub } private void render(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glEnable(GL.GL_TEXTURE_2D); gl.glBegin(GL2.GL_TRIANGLES); // Begin drawing triangle sides earthTexture.enable(); earthTexture.bind(); // gl.glColor3f( 1.0f, 0.0f, 0.0f); // Set colour to red gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 0.0f, 1.0f, 1.0f); // Top vertex gl.glTexCoord2f(-1.0f, -2.0f); gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom left vertex gl.glTexCoord2f(1.0f, -2.0f); gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom right vertex gl.glEnd(); } @Override public void dispose(GLAutoDrawable arg0) { // TODO Auto-generated method stub } @Override public void init(GLAutoDrawable arg0) { GL2 gl = arg0.getGL().getGL2(); // Load texture. try { InputStream stream = getClass().getResourceAsStream("earth.png"); TextureData data = TextureIO.newTextureData(gl.getGLProfile(), stream, 100, 200, false, "png"); earthTexture = TextureIO.newTexture(data); } catch (IOException exc) { exc.printStackTrace(); System.exit(1); } } @Override public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { // TODO Auto-generated method stub } }
A few things I noticed:
You need to explicitly enable texturing in OpenGL using something like:
gl.glEnable(GL.GL_TEXTURE_2D);
You will also need to specify the coordinates for the textures (usually expressed as u, v coordinates), this must be done for each three-dimensional point:
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 0.0f, 1.0f, 1.0f); ...
Great NeHe tutorials also have an example of JOGL code these days, which will look in more detail:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=07
This article also has some good information about understanding texture coordinators:
http://www.opengl.org/resources/code/samples/sig99/advanced99/notes/node52.html
You bind your texture between the glBegin / glEnd statements. This must be done before glBegin. Texture switches between start / end pairs are likely to be ignored.
Source: https://habr.com/ru/post/1342734/More articles:jQuery UI draggable does not work properly in IE7. Works in IE8 - cssWhat output should I see from getFft? - javaWhat additional privileges does the Editor and Contributor tab require to insert and paste into content? - securitytransition through greasemonkey script - javascriptHow can I find out that activity will be killed by the OS? - androidGet Salesforce value for custom field - soapIt is not possible to see my custom navigation button, but it is viewable. - objective-cWhat is the maximum number of methods in a Python class? - pythonWhen I publish a release version through web deployment, I get an error - visual-studio-2010How can I debug problems with move_uploaded_file? - phpAll Articles