MyFirstTriangle in GLES2 ... help!

Anything libgdx related goes here!

MyFirstTriangle in GLES2 ... help!

Postby oldskool73 » Thu Nov 18, 2010 11:47 pm

Hi all,
Got a copy of 'OpenGL ES 2.0 Programming Guide' from Amazon (seems good so far) and I'm trying to get my head around GLES2 starting with reworking the 'MyFirstTriangle' tutorial. Should be easy eh?... but I'm not having much luck. All I get is a black screen... :?

Code: Select all
package com.dustypixels.games;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.utils.GdxRuntimeException;

public class MyFirstTriangle implements ApplicationListener {

   private ShaderProgram shaderProgram;
   private Mesh mesh;

   @Override
   public void create() {
      Gdx.app.log("GDX", "create...");

      //check we can use GLES2
      if (!Gdx.app.getGraphics().isGL20Available()) {
         throw new GdxRuntimeException("GLES2 Not Avialable!");
      }
      
      //create shader program
      String fragmentShader =
         "attribute vec4 vPosition;       \n" +
         "void main()               \n" +
         "{                        \n" +
         "   gl_Position = vPosition;   \n" +
         "}                        \n";
      String vertexShader =
         "precision mediump float;               \n"+
         "void main()                        \n"+
         "{                                 \n"+
         "   gl_FragColor = vec4(1.0,0.0,0.0,1.0);   \n"+
         "}                                 \n";
      shaderProgram = new ShaderProgram(vertexShader, fragmentShader);
      
      mesh = new Mesh(true, 3, 3,
                new VertexAttribute(VertexAttributes.Usage.Position, 3, "vPosition"));         

        mesh.setVertices(new float[] { -0.5f, -0.5f, 0,
                                        0.5f, -0.5f, 0,
                                        0,     0.5f, 0 });   
        mesh.setIndices(new short[] { 0, 1, 2 });       
   }

   @Override
   public void dispose() {
        shaderProgram.dispose();
        mesh.dispose();
   }

   @Override
   public void pause() {
      // TODO Auto-generated method stub

   }

   @Override
   public void render() {
      Gdx.app.log("GDX", "render...");
      
      Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
      Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

      //draw
      shaderProgram.begin();
      mesh.render(shaderProgram, GL20.GL_TRIANGLES);
      shaderProgram.end();      
   }

   @Override
   public void resize(int width, int height) {
      // TODO Auto-generated method stub

   }

   @Override
   public void resume() {
      Mesh.invalidateAllMeshes();
   }

}



I'm sure its something really stupid, but any help appreciated.

By the way, are there any tests or demos using GLES2? I couldn't find any in SVN... when I get this working I'll be happy to add it to the Wiki as a tutorial.

Cheers.
oldskool73
 
Posts: 21
Joined: Thu Nov 11, 2010 2:17 pm

Re: MyFirstTriangle in GLES2 ... help!

Postby consiliens » Fri Nov 19, 2010 12:34 am

There are many tests using shaders, just look for references to ShaderProgram (in Eclipse right click on a ShaderProgram type declaration then select References and finally Workspace).

Here's a triangle in OpenGL ES 2 based on MeshShaderTest.

Code: Select all
/*******************************************************************************
 * Copyright 2010 Mario Zechner (contact@badlogicgames.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.tests;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.backends.jogl.JoglApplication;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.SpriteBatch;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.tests.utils.GdxTest;

public class MeshShaderTest extends GdxTest {
   ShaderProgram shader;
   Mesh mesh;

    public static void main(String[] argv) {
        final boolean useGL20IfAvailable = true;

        try {
            new JoglApplication(new MeshShaderTest(), MeshShaderTest.class.getSimpleName(), 500, 500,
                    useGL20IfAvailable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
   @Override
   public void create() {
      String vertexShader = "attribute vec4 a_position;       \n"
           + "void main()                  \n"
            + "{                            \n"
            + "   gl_Position =  a_position;  \n"
            + "}                            \n";
      String fragmentShader = "precision mediump float;\n"

            + "void main()                                  \n"
            + "{                                            \n"
            + "  gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
            + "}";

      shader = new ShaderProgram(vertexShader, fragmentShader);
      if (shader.isCompiled() == false) {
         Gdx.app.log("ShaderTest", shader.getLog());
         System.exit(0);
      }

      mesh = new Mesh(true, 3, 3, new VertexAttribute(Usage.Position, 3,
            "a_position"));

        mesh.setVertices(new float[] { -0.5f, -0.5f, 0,
                                        0.5f, -0.5f, 0,
                                        0,     0.5f, 0 });   
      mesh.setIndices(new short[] { 0, 1, 2 });
   }

   @Override
   public void render() {
      shader.begin();
      mesh.render(shader, GL10.GL_TRIANGLES);
      shader.end();
   }

   @Override
   public boolean needsGL20() {   
      return true;
   }
}


The below error message from the code is a known issue in the current audio backend.
Code: Select all
java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian is supported.
   at javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:459)
   at javax.sound.sampled.AudioSystem.getSourceDataLine(AudioSystem.java:587)
   at com.badlogic.gdx.backends.jogl.JoglAudio.<init>(JoglAudio.java:99)
   at com.badlogic.gdx.backends.jogl.JoglApplication.initialize(JoglApplication.java:86)
   at com.badlogic.gdx.backends.jogl.JoglApplication$1.run(JoglApplication.java:70)
   at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
consiliens
 
Posts: 177
Joined: Wed Oct 27, 2010 6:55 am

Re: MyFirstTriangle in GLES2 ... help!

Postby mzechner » Fri Nov 19, 2010 1:54 am

Thanks Consiliens! (Darn, i really really need to rewrite the audo backend asap, stupid Java Sound on Ubuntu 10.10...).

You will also notice that a lot of the other classes in the graphics package work with OpenGL ES 2.0 out of the box. Most prominently SpriteBatch and BitmapFont. That should make your life a lot easier if you want to output things like text and so on. At the moment you can't change the shaders those classes use internally though. I might add support for that in a future release.

Note that you might not be able to try out a few examples from that book as we do not expose extensions (yet).
mzechner
Site Admin
 
Posts: 4593
Joined: Sat Jul 10, 2010 3:50 pm

Re: MyFirstTriangle in GLES2 ... help!

Postby oldskool73 » Fri Nov 19, 2010 1:59 am

Thanks for taking the time to try to help, unfortunately the code you posted is generally exactly the same as mine in the first post, except you've got the check to see if the shader compiled.

Adding that test to mine I get:
ShaderTest: ERROR: 0:1: 'precision' : syntax error syntax error
ERROR: Parser found no code to compile in source strings.
ERROR: 0:1: 'attribute' : supported in vertex shaders only
ERROR: 0:4: 'gl_Position' : undeclared identifier
ERROR: 0:4: 'assign' : cannot convert from 'attribute 4-component vector of float' to 'float'

If I cut'n'paste your code (just removing the 'tests' ref and implementing ApplicationListener directly) I still get:
ShaderTest: ERROR: 0:1: 'precision' : syntax error syntax error
ERROR: Parser found no code to compile in source strings.

Looks like the precision statement is causing the error, although that's what your code, my book and a million examples on google use so .... ?

I'm on a mac, using the libgdx.dylib built the other day and linked in the post at the top of this forum, maybe that's something to do with it?

//---------

I've searched the code and also tried searching on google code and it only shows up 4 instances of ShaderProgram among the other tests, maybe I'm missing something?
http://www.google.com/codesearch?hl=en& ... p://libgdx\.googlecode\.com&sbtn=Search
oldskool73
 
Posts: 21
Joined: Thu Nov 11, 2010 2:17 pm

Re: MyFirstTriangle in GLES2 ... help!

Postby consiliens » Fri Nov 19, 2010 2:11 am

The code I posted works. Here's a screenshot.
Image

Setup your development environment so that the libgdx tests can run. Once the libgdx tests run properly, replace MeshShaderTest with the code I've posted. Now run the new MeshShaderTest through JoglTestStarter.java.

There are currently four tests in libgdx that use shaders.
    FrameBufferTest
    IndexBufferObjectShaderTest
    MeshShaderTest
    VertexBufferObjectShaderTest

The OpenGL Extensions Viewer should confirm that your graphics card supports OpenGL ES 2.0.
Last edited by consiliens on Fri Nov 19, 2010 2:43 am, edited 2 times in total.
consiliens
 
Posts: 177
Joined: Wed Oct 27, 2010 6:55 am

Re: MyFirstTriangle in GLES2 ... help!

Postby oldskool73 » Fri Nov 19, 2010 2:27 am

I appreciate the help, but wouldn't the same shader compile error happening in your and my code I posted point to there being something up other than I've not set up the project properly?

Anyway, I'll download a fresh copy of the tests and try that now, will let you know how I get on...
oldskool73
 
Posts: 21
Joined: Thu Nov 11, 2010 2:17 pm

Re: MyFirstTriangle in GLES2 ... help!

Postby oldskool73 » Fri Nov 19, 2010 2:33 am

according to that tool I'm 100% on core features up to 2.1, and shader language version 1.20
oldskool73
 
Posts: 21
Joined: Thu Nov 11, 2010 2:17 pm

Re: MyFirstTriangle in GLES2 ... help!

Postby consiliens » Fri Nov 19, 2010 2:36 am

The ability to run any of the ES 2.0 tests will be a helpful data point. If you can't then I suspect the issue is the mac build of libgdx.
consiliens
 
Posts: 177
Joined: Wed Oct 27, 2010 6:55 am

Re: MyFirstTriangle in GLES2 ... help!

Postby oldskool73 » Fri Nov 19, 2010 3:08 am

Yup, looks that way, same precision syntax errors & crash when I try to run any of the 4 shader tests, everything else seems to run ok.

Oh well, at least it's not me being completely stoopid :)
oldskool73
 
Posts: 21
Joined: Thu Nov 11, 2010 2:17 pm

Re: MyFirstTriangle in GLES2 ... help!

Postby consiliens » Fri Nov 19, 2010 3:16 am

mzechner should run a pledgie to buy a libgdx mac development machine. Pledges have worked in the Ruby community.
consiliens
 
Posts: 177
Joined: Wed Oct 27, 2010 6:55 am

Next

Return to Libgdx

Who is online

Users browsing this forum: No registered users and 8 guests

x