PDA

View Full Version : GLSL 3.30 vs 1.50



Jaguar
05-08-2011, 03:57 AM
I have started learning modern opengl (version 3.3) which using GLSL 3.30. In the tutorial are two simple shaders for a triangle:

Vertex Shader:


#version 330

layout (location = 0) in vec4 position;
layout (location = 1) in vec4 color;

smooth out vec4 theColor;

void main()
{
gl_Position = position;
theColor = color;
}


Fragment Shader:


#version 330

smooth in vec4 theColor;

out vec4 outputColor;

void main()
{
outputColor = theColor;
}


This works fine , but I need to transform these shaders to GLSL 1.50 and opengl 3.2. Is it possible ? How about difference between opengl 3.2 and opengl 3.3? Here is a fragment of code from the display function working together with these shaders:



glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)48);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

Alfonse Reinheart
05-08-2011, 04:46 AM
This works fine , but I need to transform these shaders to GLSL 1.50 and opengl 3.2. Is it possible ?

Yes, it is possible.





Oh, you wanted to know how to do it. ;)

First, you change the version declaration to 150 rather than 330.

Second, you have to remove the layout(location) qualifier. Now, since that is how OpenGL knows what attribute index maps with which attribute name, you now need to provide that name in another way.

That way is in the program object, before linking it. You call glBindAttribLocation, which takes a vertex shader input name and an attribute index to associate with that name.

However, there's not much reason to bother. Any GL 3.2 hardware can also do GL 3.3, and will almost certainly have 3.3 drivers for it.

Jaguar
05-09-2011, 03:18 AM
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.




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

}

Jaguar
05-09-2011, 12:57 PM
Solved. At the begining of display should be:



fXOffset += 0.01f;
fYOffset += 0.01f;

Zenja
05-23-2011, 06:58 AM
However, there's not much reason to bother. Any GL 3.2 hardware can also do GL 3.3, and will almost certainly have 3.3 drivers for it.

You're fortunate to not have to deal with laptop drivers. The nVidia/AMD supplied drivers will not work for laptop chipsets, instead you have beg HP/Dell/(whoever) to update their laptop GPU drivers. I've seen HP laptops with 3.2 OpenGL drivers only (no 3.3 drivers), and we've had no success in convincing HP to update their drivers. One of our engineers found a set of drivers from another manufacturer (can't remember if it was Fujitsu or someone else), which managed to get OpenGL 3.3 drivers on HP laptops.

So just because AMD/nVidia hardware support 3.3, it doesn't mean that they're available for all users ... Another mob I don't want to talk about are Apple who do the exact same thing...

Aleksandar
05-23-2011, 10:17 AM
What OS are you using? I have no problem to run NV drivers on my laptop (with Vista) even when there is no adequate profile. That was a year or more ago. Nowadays, you can just start installation and everything runs nice and smooth, even on 8xxxM cards.

Zenja
05-23-2011, 11:35 PM
What OS are you using?
Considering that laptop drivers come from manufacturer, you're most probably dealing with Win7 drivers. The stock AMD/nVidia windows driver will not work (it asks you to use manufacturer supplied driver). As I pointed out, we got drivers from another laptop manufacturer, HP were useless wankers (and still only ship 3.2)

Aleksandar
05-24-2011, 01:05 AM
Would you specify your graphics card and OS? I won't to check what your problem is. In order to do that I need: VendorID, DeviceID and SubSystemID for your graphics card. The chipsets and graphics cards built in laptops are more or less standard. I have no experience with AMD, but have no problems to update NV drivers. If VendorID = 10DE (means NVIDIA) and you are using MS Windows, then maybe I could help...

npapier
05-24-2011, 02:54 AM
Depending of the manufacturer policy about drivers installation, you are not always limited to their old notebook drivers. NVidia is providing generic notebook drivers from GeForce 7 GO to GeForce 500M.