strange shader behaviour

hey all

i’m having a weird problem with a shader loading vertex locations from a float texture.

Basically the particles should be randomly distributed, but when I draw them via a shader they end up kinda circular.

I have a feeling I’m missing something when I set gl_Position in the shader, but I am stumped.

This is what i am expecting

This is what I see

This is the shader

  
  
#version 120  
#extension GL_ARB_texture_rectangle : enable  
  
uniform sampler2DRect partloc;  
  
void main(void)  
{  
    vec2 loc = gl_Vertex.xy;  
    vec4 pixpos = texture2DRect(partloc, loc);  
    gl_Position = gl_ModelViewProjectionMatrix * pixpos;  
    gl_FrontColor = vec4(1.0);  
}  
  

testApp.cpp

  
  
#include "testApp.h"  
  
#define MAX_PARTICLES	10000  
  
float pos[MAX_PARTICLES][3];  
int	numParticles = MAX_PARTICLES;  
  
int mode = 0;  
  
//--------------------------------------------------------------  
void testApp::setup(){  
    ofBackground(0, 0, 0);  
    ofSetBackgroundAuto(true);  
      
    int w = ofGetWidth();  
    int h = ofGetHeight();  
    parts_res = (int)sqrt((float)numParticles);  
    numParticles = parts_res * parts_res;  
      
    for (int x = 0; x < parts_res; x++) {  
        for (int y = 0; y < parts_res; y++) {  
            int i = x * parts_res + y;  
            pos[i][0] = ofRandom(0.0, w);  
            pos[i][1] = ofRandom(0.0, h);  
            pos[i][2] = 0.0f;  
        }  
    }  
    partsFbo.allocate(parts_res, parts_res, GL_RGB32F);  
    partsFbo.getTextureReference().loadData((float *)&pos[0], parts_res, parts_res, GL_RGB);  
      
    partshader.load("particle.vert", "");  
      
    renderFbo.allocate(ofGetWidth(), ofGetHeight());  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
    renderFbo.begin();  
    ofClear(0);  
    partshader.begin();  
    partshader.setUniformTexture("partloc", partsFbo.getTextureReference(), 0);  
  
    glBegin(GL_POINTS);  
    for (int x = 0; x < parts_res; x++) {  
        for (int y = 0; y < parts_res; y++) {  
            glVertex2d(x, y);  
        }  
    }  
    glEnd();  
    partshader.end();  
    renderFbo.end();  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
      
    switch (mode) {  
        case 0:  
            renderFbo.draw(0, 0);  
            ofDrawBitmapString("shader fbo", 20, 20);  
            break;  
        case 1:  
            glEnableClientState(GL_VERTEX_ARRAY);  
            glVertexPointer(3, GL_FLOAT, 0, pos);  
            glDrawArrays(GL_POINTS, 0, numParticles);  
            glDisableClientState(GL_VERTEX_ARRAY);  
            ofDrawBitmapString("initial points", 20, 20);  
            break;  
        case 2:  
  
            int possz = parts_res;  
            int pimg_sz = ofGetWidth();  
            ofImage posImg;  
            posImg.allocate(pimg_sz, pimg_sz, OF_IMAGE_COLOR);  
            ofFloatPixels ppix;  
            ppix.allocate(possz, possz, 3);  
            partsFbo.getTextureReference().readToPixels(ppix);  
            for (int x = 0; x < possz; x++) {  
                for (int y = 0; y < possz; y++) {  
                    ofFloatColor fc = ppix.getColor(x, y);  
                    int xloc = fc.r;  
                    int yloc = fc.g;  
                    posImg.setColor(xloc, yloc, ofColor(255, 255, 255));  
                }  
            }  
            posImg.update();  
            posImg.draw(0, 0);  
            ofDrawBitmapString("particle location fbo", 20, 20);  
            break;  
    }  
  
  
      
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
    mode = ++mode % 3;  
}  
  

testApp.h

  
  
#pragma once  
  
#include "ofMain.h"  
  
class testApp : public ofBaseApp{  
  
	public:  
		void setup();  
		void update();  
		void draw();  
  
		void keyPressed  (int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
		void dragEvent(ofDragInfo dragInfo);  
		void gotMessage(ofMessage msg);  
		  
    ofFbo partsFbo;  
    ofFbo renderFbo;  
    ofShader partshader;  
    int parts_res;  
};  
  

main

  
  
#include "ofMain.h"  
#include "testApp.h"  
#include "ofAppGlutWindow.h"  
  
//========================================================================  
int main( ){  
  
    ofAppGlutWindow window;  
	ofSetupOpenGL(&window, 700, 700, OF_WINDOW);			// <-------- setup the GL context  
  
	// this kicks off the running of my app  
	// can be OF_WINDOW or OF_FULLSCREEN  
	// pass in width and height too:  
	ofRunApp( new testApp());  
  
}  
  

Thanking you in advance …

particle_location_shdr.zip

I tried using equally spaced points and they weren’t turned into a circle. So maybe the shader is OK. It still happens with the random points though, really stumped with this.

Someone else pointed out it was bilinear filtering being on.

  
  
    partsFbo.getTextureReference().setTextureMinMagFilter(GL_NEAREST, GL_NEAREST);  
  

turned it off. The More You Know™