Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (531)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  vertex shader problem  (Read 1652 times)
0 Members and 1 Guest are viewing this topic.
Offline Irbis

Senior Newbie





« Posted 2011-05-08 23:36:53 »

I have started learning modern opengl (version 3.3). I use JOGL2 RC2 Signed Released (jogl-2.0-b23-20110303-windows-i586). I have a problem with vertex shader - uniform vec2 offset doesn't work. The triangle doesn't move .... My initShader function works properly. I don't know what is wrong.

Tutorial: http://arcsynthesis.org/gltut/Positioning/Tut03%20A%20Better%20Way.html

Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
public class Scene3D implements GLEventListener 
{
    private Frame frame;
    private FPSAnimator animator;
    private int vertexBufferObject;
    private int shaderProgramID;
    private FloatBuffer vertexDataBuffer;
    private float fXOffset = 0.01f;
    private float fYOffset = 0.01f;
    private int offsetLocation;
    private float vertices[];

    private static String vertexShader =
    "#version 330" + "\n" +
    "layout (location = 0) in vec4 position;" + "\n" +
    "uniform vec2 offset;" + "\n" +      
    "void main()" + "\n" +
    "{" + "\n" +
    "vec4 totalOffset = vec4(offset.x, offset.y, 0.0, 0.0);" + "\n" +      
    "gl_Position = position + totalOffset;" + "\n" +
    "}";
   
    private static String fragmentShader =
    "#version 330" + "\n" +  
    "out vec4 outputColor;" + "\n" +
    "void main()" + "\n" +
    "{" + "\n" +      
    "outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);" + "\n" +
    "}";        
   
    public Scene3D(GLCapabilities capabilities)
    {
        GLCanvas glComponent = new GLCanvas(capabilities);
        glComponent.setFocusable(true);
        glComponent.addGLEventListener(this);
       
        frame = new JFrame("JOGL");
        frame.addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowClosing(WindowEvent e)
            {
                runExit();
            }
        });
        frame.setLayout(new BorderLayout());
        glComponent.setPreferredSize(new Dimension(800, 600));
        frame.add(glComponent, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        glComponent.requestFocus();
        animator = new FPSAnimator(glComponent, 60, true);
        animator.start();
    }

    public static void main(String[] args)
    {
       
        GLProfile profile = GLProfile.get(GLProfile.GL3);
        final GLCapabilities capabilities = new GLCapabilities(profile);
       
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new Scene3D(capabilities);
               
            }
        });
       
    }

    @Override
    public void init(GLAutoDrawable glad)
    {
        GL3 gl = glad.getGL().getGL3();
        gl.setSwapInterval(1);
        gl.glEnable(GL3.GL_DEPTH_TEST);
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        vertices = new float[]{0.0f, 0.5f, 0.0f, 1.0f,
                                       0.5f, -0.366f, 0.0f, 1.0f,
                                       -0.5f, -0.366f, 0.0f, 1.0f};
       
        vertexDataBuffer = Buffers.newDirectFloatBuffer(vertices.length);
        vertexDataBuffer.put(vertices);
        vertexDataBuffer.flip();

        initShaders(gl);
        initVBO(gl);
        offsetLocation = gl.glGetUniformLocation(shaderProgramID, "offset");
    }

    @Override
    public void dispose(GLAutoDrawable glad)
    {
       
    }

    @Override
    public void display(GLAutoDrawable glad)
    {
        GL3 gl = glad.getGL().getGL3();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glUseProgram(shaderProgramID);
        gl.glUniform2f(offsetLocation, fXOffset, fYOffset);
        gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject);
        gl.glEnableVertexAttribArray(0);
        gl.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
        gl.glDrawArrays(GL3.GL_TRIANGLES, 0, 3);
        gl.glDisableVertexAttribArray(0);
        gl.glUseProgram(0);
    }

    @Override
    public void reshape(GLAutoDrawable glad, int x, int y, int width, int height)
    {
       
    }
   
    private void initVBO(GL3 gl)
    {
        int buffer[] = new int[1];
        gl.glGenBuffers(1, IntBuffer.wrap(buffer));
        vertexBufferObject = buffer[0];
        gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject);
        gl.glBufferData(GL3.GL_ARRAY_BUFFER, Buffers.SIZEOF_FLOAT * vertexDataBuffer.capacity(), vertexDataBuffer, GL3.GL_STREAM_DRAW);
        gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
    }
   
    private void initShaders(GL3 gl)
    {
        shaderProgramID = gl.glCreateProgram();

        int vertexShaderID = gl.glCreateShader(GL3.GL_VERTEX_SHADER);
        gl.glShaderSource(vertexShaderID, 1,
            new String[]{vertexShader}, null);
        gl.glCompileShader(vertexShaderID);
        gl.glAttachShader(shaderProgramID, vertexShaderID);
        gl.glDeleteShader(vertexShaderID);

        int fragmentShaderID = gl.glCreateShader(GL3.GL_FRAGMENT_SHADER);
        gl.glShaderSource(fragmentShaderID, 1,
            new String[]{fragmentShader}, null);
        gl.glCompileShader(fragmentShaderID);
        gl.glAttachShader(shaderProgramID, fragmentShaderID);
        gl.glDeleteShader(fragmentShaderID);
       
        gl.glLinkProgram(shaderProgramID);
        gl.glValidateProgram(shaderProgramID);
       
    }

    private void runExit()
    {
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                animator.stop();
                System.exit(0);
            }
        }).start();
    }

}
Offline delt0r

JGO Knight


Medals: 26
Exp: 18 years


Computers can do that?


« Reply #1 - Posted 2011-05-09 14:26:26 »

At first glance i cannot see where you check for compile or linking errors. When doing shaders you should *always* check. I use a method that i got off lwjgl wiki a while back IIRC. Here is the method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
private static void printLogInfo(int obj) {
      IntBuffer iVal =BufferUtils.createIntBuffer(1);
      ARBShaderObjects.glGetObjectParameterARB(obj, ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);

      int length =iVal.get();
      System.out.println("Info log length:" + length);
      if (length > 0) {
         // We have some info we need to output.
        ByteBuffer infoLog =BufferUtils.createByteBuffer(length);
         iVal.flip();
         ARBShaderObjects.glGetInfoLogARB(obj, iVal, infoLog);
         byte[] infoBytes =new byte[length];
         infoLog.get(infoBytes);
         String out =new String(infoBytes);

         System.out.println("Info log:\n" + out);
      }

      Util.checkGLError();
   }

I then call this after linking like so (simplified so you get the idea).
1  
2  
3  
4  
5  
6  
7  
glLinkProgram(program);
glValidateProgram(program);
System.out.println("Log of vshader");
printLogInfo(vshader);
System.out.println("Log of fshader");
printLogInfo(fshader);
org.lwjgl.opengl.Util.checkGLError();

Then at least i can be confident about the shader compile process.

I have no special talents. I am only passionately curious.--Albert Einstein
Offline Irbis

Senior Newbie





« Reply #2 - Posted 2011-05-09 17:02:50 »

Ok, I added to my init shaders function simple error checking code and now I'm getting the message: "linker error: 0". I still can't understand what is wrong ....

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
private void initShaders(GL3 gl)
    {
        shaderProgramID = gl.glCreateProgram();

        int vertexShaderID = gl.glCreateShader(GL3.GL_VERTEX_SHADER);
        gl.glShaderSource(vertexShaderID, 1,
            new String[]{vertexShader}, null);
        gl.glCompileShader(vertexShaderID);
       
       
       
        int statusVertex[] = new int[1];
        gl.glGetShaderiv(vertexShaderID, GL3.GL_COMPILE_STATUS, IntBuffer.wrap(statusVertex));
        if(statusVertex[0] == GL3.GL_FALSE)
        {
            int infoLogLenght[] = new int[1];
            gl.glGetShaderiv(vertexShaderID, GL3.GL_INFO_LOG_LENGTH, IntBuffer.wrap(infoLogLenght));
           
            byte infLog[] = new byte[1];
            gl.glGetShaderInfoLog(vertexShaderID, infoLogLenght[0], null, ByteBuffer.wrap(infLog));
            System.out.println("vertex shader error:" + Byte.toString(infLog[0]));
           
        }
       
       
       
        gl.glAttachShader(shaderProgramID, vertexShaderID);
        gl.glDeleteShader(vertexShaderID);

       
       
       
        int fragmentShaderID = gl.glCreateShader(GL3.GL_FRAGMENT_SHADER);
        gl.glShaderSource(fragmentShaderID, 1,
            new String[]{fragmentShader}, null);
        gl.glCompileShader(fragmentShaderID);
       
       
       
        int statusFragment[] = new int[1];
        gl.glGetShaderiv(fragmentShaderID, GL3.GL_COMPILE_STATUS, IntBuffer.wrap(statusFragment));
        if(statusFragment[0] == GL3.GL_FALSE)
        {
            int infoLogLenght[] = new int[1];
            gl.glGetShaderiv(fragmentShaderID, GL3.GL_INFO_LOG_LENGTH, IntBuffer.wrap(infoLogLenght));
           
            byte infLog[] = new byte[1];
            gl.glGetShaderInfoLog(fragmentShaderID, infoLogLenght[0], null, ByteBuffer.wrap(infLog));
            System.out.println("fragment shader error:" + Byte.toString(infLog[0]));
           
        }
       
       
       
        gl.glAttachShader(shaderProgramID, fragmentShaderID);
        gl.glDeleteShader(fragmentShaderID);
       
        gl.glLinkProgram(shaderProgramID);
       
       
       
        int statusLinker[] = new int[1];
        gl.glGetShaderiv(shaderProgramID, GL3.GL_LINK_STATUS, IntBuffer.wrap(statusLinker));
        if(statusLinker[0] == GL3.GL_FALSE)
        {
            int infoLogLenght[] = new int[1];
            gl.glGetShaderiv(shaderProgramID, GL3.GL_INFO_LOG_LENGTH, IntBuffer.wrap(infoLogLenght));
           
            byte infLog[] = new byte[1];
            gl.glGetShaderInfoLog(shaderProgramID, infoLogLenght[0], null, ByteBuffer.wrap(infLog));
            System.out.println("linker error:" + Byte.toString(infLog[0]));
           
        }
        gl.glValidateProgram(shaderProgramID);
       
    }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline delt0r

JGO Knight


Medals: 26
Exp: 18 years


Computers can do that?


« Reply #3 - Posted 2011-05-09 20:10:28 »

Why do you call glDeleteShader()? I do not do this, and AFAIK should generally done once you are finished with your sharers. ie not using them anymore.

Also you never update xOffset or yoffset. You perhaps want to add a xoffset+=.01 or something.

I have no special talents. I am only passionately curious.--Albert Einstein
Offline Irbis

Senior Newbie





« Reply #4 - Posted 2011-05-09 21:47:50 »

First I had an another code for update vertices and vertex shader without uniform vec2 offset:
 
Vertex Shader
1  
2  
3  
4  
5  
6  
7  
8  
#version 330

layout(location = 0) in vec4 position;

void main()
{
   gl_Position = position;
}


Function for update vertices which was being executed in display:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
private void adjustVertexData(GL3 gl)
{
         
     for(int i = 0; i < fNewData.capacity(); i += 4)
     {
          tempVertexData[i] += fXOffset;
          tempVertexData[i + 1] += fXOffset;
     }
       
       
     for(int i = 0; i < fNewData.capacity(); i++)
     {
          fNewData.put(tempVertexData[i]);
     }
       
     fNewData.rewind();
     gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject);
     gl.glBufferSubData(GL3.GL_ARRAY_BUFFER, 0, Buffers.SIZEOF_FLOAT * fNewData.capacity(), fNewData);
     gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
}


Fields fXOffset and fYOffset was initialized ( private float fXOffset = 0.01f; private float fYOffset = 0.01f;) in both cases. I assumed that I don't need update fXOffset and fYOffset in the next version of my program which has another vertex shader and use glUniform2f. Now everything works fine with fXOffset += 0.01f; and fYOffset += 0.01 at the begining of display. I don't clearly understand why earlier my code doesn't work properly. Is it because of uniform variable nature ?

With glDeleteShader the program also works. I saw this way of using glDeleteShaders in some sample program and I coded my program similar. Is it mistake ? When using glDeleteShader in that way can bring me some troubles ?
Offline lhkbob

JGO Knight


Medals: 32



« Reply #5 - Posted 2011-05-09 21:52:15 »

If I remember correctly, a shader won't actually be deleted until it glDeleteShader has been called AND it has been unattached from all un-deleted programs.  I think by calling glDeleteShader right after attaching will mean that when the glsl program object is deleted, all of its shaders will be cleaned up automatically.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (13 views)
2014-07-24 01:59:36

Riven (13 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (14 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (39 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (35 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!