LWJGL Forum

Programming => OpenGL => Topic started by: ic5y on April 15, 2013, 15:20:57

Title: Basic vertex shader not working
Post by: ic5y on April 15, 2013, 15:20:57
Hi!

I try to use a very basic vertex shader, but it won't show the correct output (I see nothing from my hexagon). Without it, I can successfully draw it with DrawArrays. If I comment out not to include the vertex shader in the shader program, the fragment shader correctly colors the shape

#version 150

in vec3 inPosition;
out vec4 passColor;

void main()
{
gl_Position = vec4(inPosition,1.0);
passColor = vec4(0.5,0.3,0.8,1.0);
}


Code for the generation and draw of hexa:


public void render()
{
GL20.glUseProgram(ShaderManager.getShader("SimpleColor").getID());
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboid);
GL20.glEnableVertexAttribArray(0);
GL11.glDrawArrays(GL11.GL_POLYGON, 0, 6);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
GL20.glUseProgram(0);
}

public void generateMesh(float ox, float oz)
{
vboid = glGenBuffersARB();

FloatBuffer vb = BufferUtils.createFloatBuffer(6 * 3);

vb.put(ox + r).put(0).put(oz);
vb.put(ox + a).put(0).put(oz + h);
vb.put(ox + a).put(0).put(oz + h + s);
vb.put(ox + r).put(0).put(oz + b);
vb.put(ox).put(0).put(oz + h + s);
vb.put(ox).put(0).put(oz + h);

vb.flip();

GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboid);

glBufferDataARB(GL_ARRAY_BUFFER_ARB, vb, GL_STATIC_DRAW_ARB);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);

glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);

center = new Vector2f(ox + r, oz + h + (s / 2));
}
Title: Re: Basic vertex shader not working
Post by: quew8 on April 15, 2013, 15:27:40
As far as I can see you do not setup your pointers to inPosition (glVertexAttribPointer)
Title: Re: Basic vertex shader not working
Post by: ic5y on April 15, 2013, 15:40:05
And where should I put the pointer setup?
Title: Re: Basic vertex shader not working
Post by: quew8 on April 15, 2013, 16:30:23
Right after binding the vbo. http://www.lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_(VBO) (http://www.lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_(VBO)) I linked the legacy since your using arb extensions.
Title: Re: Basic vertex shader not working
Post by: ic5y on April 16, 2013, 15:21:40
Okay, yesterday and today I tried to solve it, but without any success...

I modified the Node class to use the GL15 vbos, not the ARB ones.

public void render()
{
GL20.glUseProgram(ShaderManager.getShader("SimpleColor").getID());

GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboid);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
GL11.glDrawArrays(GL11.GL_POLYGON, 0, 6);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
GL20.glUseProgram(0);
}

public void generateMesh(float ox, float oz)
{
vboid = GL15.glGenBuffers();

FloatBuffer vb = BufferUtils.createFloatBuffer(6 * 3);

vb.put(ox + r).put(0).put(oz);
vb.put(ox + a).put(0).put(oz + h);
vb.put(ox + a).put(0).put(oz + h + s);
vb.put(ox + r).put(0).put(oz + b);
vb.put(ox).put(0).put(oz + h + s);
vb.put(ox).put(0).put(oz + h);

vb.flip();

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboid);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vb, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

center = new Vector2f(ox + r, oz + h + (s / 2));
}


the vertex shader is unmodified. I drop the shader class of mine, maybe the error is in that one:
package com.ic5y.shaders;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;

public class Shader {

private int pid;
private int vsid;
private int fsid;
private String name = "";

public Shader(String name, String vssource, String fssource)
{
this.name = name;

vsid = this.loadShader("src/com/ic5y/shaders/shader_src/" + vssource, GL20.GL_VERTEX_SHADER);
fsid = this.loadShader("src/com/ic5y/shaders/shader_src/" + fssource, GL20.GL_FRAGMENT_SHADER);
pid = GL20.glCreateProgram();
GL20.glAttachShader(pid, vsid);
GL20.glAttachShader(pid, fsid);
GL20.glLinkProgram(pid);
GL20.glValidateProgram(pid);
}

public int loadShader(String filename, int type) {
StringBuilder shaderSource = new StringBuilder();
int shaderID = 0;

try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
shaderSource.append(line).append("\n");
}
reader.close();
} catch (IOException e) {
System.err.println("Could not read file.");
e.printStackTrace();
System.exit(-1);
}

shaderID = GL20.glCreateShader(type);
GL20.glShaderSource(shaderID, shaderSource);
GL20.glCompileShader(shaderID);

return shaderID;
}

public int getID()
{
return pid;
}

public String getName()
{
return name;
}

}
Title: Re: Basic vertex shader not working
Post by: ic5y on April 20, 2013, 10:00:06
okay, i solved it, i forgot to set the matrices.. :/