Using textures in glsl version #410

Hello I’m am struggling to convert a simple shader using textures from glsl 120 to glsl 410. gl_TexCoord[0] cannnot be used anymore by the looks of things.

Here is my code which works with the 120 shaders.
Draw function

 void Path::draw() {
    
    if (totalPoints <=0) return; //if no points don't draw anything
    
    ofEnableBlendMode(OF_BLENDMODE_ADD);
    ofEnablePointSprites();
    
    shader.begin();

    shader.setUniform4f("uColor", pathColour); // set via ofApp
    shader.setUniform1f("pointSize", 60.);
    
    // bind the texture
    texture.bind();
    vbo.draw(GL_POINTS, 0, totalPoints);
    texture.unbind();
    
    shader.end();
    
    ofDisablePointSprites();
    ofDisableBlendMode();
}

working shaders 120
vert

uniform vec4 uColor;
uniform float pointSize;

void main() {
    
    gl_Position   = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_PointSize  = pointSize;
    gl_FrontColor = uColor;
    
}

frag

uniform sampler2D tex;

void main (void) {
    
    gl_FragColor = texture2D(tex, gl_TexCoord[0].st) * gl_Color;
    
}

not working version 410

#version 410

uniform sampler2DRect tex0;

uniform mat4 modelViewProjectionMatrix;
uniform vec4 uColor;
uniform float pointSize;

in vec4 position;
in vec2 texcoord;

out vec2 texCoordVarying;
out vec4 colorVarying;

void main() {
    vec4 modPos   = modelViewProjectionMatrix * position;
    gl_PointSize  = pointSize;
    
    texCoordVarying = texcoord;
    colorVarying = uColor;
    gl_Position = modPos;
}

#version 410

uniform sampler2DRect tex0;

in vec2 texCoordVarying;
in vec4 colorVarying;
out vec4 outputColor;

void main () {
    outputColor = texture(tex0, texCoordVarying)* colorVarying;
}

I can get the color to come though if i remove all the texture references so there is something funny with tex0 or texCoordVarying.

Thanks!

the shaders look correct, how are you loading the data in the vbo? also how are you loading the shader?

here are my setup and where i add the vertices for the VBO. I don’t really understand how to add any other data to a VBO I thought the texture would be attached when you do texture.bind() texture.unbind(). but I don’t know how the uniforms are set in the shaders.

void Path::setup() {

// load the shader
shader.load("shaders410/pathShader");

// load the texure
ofDisableArbTex();
ofLoadImage(texture, "dot.png");

totalPoints = 0;
// upload the data to the vbo

vbo.setVertexData(&positions[0], totalPoints, GL_STATIC_DRAW);

drawLines = true;

}

//--------------------------------------------------------------
void Path::addPoint(float x, float y, float z) {

ofVec3f newPos(x, y, z);
 positions.push_back(newPos);

totalPoints = positions.size();

vbo.setVertexData(&positions[0], totalPoints, GL_STATIC_DRAW);

}

if you only have 1 texture bind/unbind should be enough but as far as i can see you aren’t loading any texture coordinates in the vbo?

Yeah That’s very possible. It didn’t seem to need it in 120. How do you do that and what might be the best way to go with more than 1 texture, or bypassing the vbo? Thanks!

1 Like

Hi again, just looking at the vbo.setTexCoordData method, If was just using a point sprite i.e. just want to place a texture at each point added to the VBO. How could I add the Texture coordinates? Sorry fairly new to this. Thanks

ah ok in that case you don’t need to feed texture coordinates of course, in the fragment shader just use:

color = texture(tex, gl_PointCoord);

with that also the texCoordVarying and texcorrd in and outs can go away

2 Likes

OK i’ve tried that, getting closer though unfortunatly i’m still getting a black screen. Would this mean my sampler2Drect isn’t working properly? How does the ofTexture link to the shader, and why would it be tex in version #120 and tex0 in #410?

point textures need to be sampler2D, it doesn’t support rectangular textures, in any case you’ve already disabled rectangular textures with the call to ofDisableArbTex() just need to change the sampler in the fragment shader with sampler2D

1 Like

Amazing. Thanks that’s working now!