Skip to main content
gamedev.net
🔒 Locked

Render to Texture

Started by Geometrian Apr 9, 2009 at 12:45 AM 2 replies 1k views
Original Post
Geometrian
Geometrian
Hi, I'm having problems with rendering to textures. I want to render n*n vertices onto an n*n texture, so I want to establish a 1:1 correspondence between vertices and texels. An Ortho projection is being used, so there should be no perspective distortion. The vertices are passed as points of size 1 to where the center of each texel is (an offset of 0.5 opengl space = 0.5/n texture sampling space. So, for a texture of 100 on a side, a vertex (0,0) I would corresponding to (0.005,0.005), which is the center of the texel at (1,1) on the texture). A nice render of size n*n (that fills the whole viewport) is produced, but there is a cross-shaped portion directly in the middle where no vertices were rendered, which is unacceptable. The viewport is sized to n*n, and I've been pulling my hair out (literally) trying to get this working. I can't think of a way to make the points line up exactly with the texels. What should I do? All transformations are done in shaders:
//Vertex shader relevant parts
//vertex = gl_Vertex (later "vertex" is used for the transformation)
//size = size of one dimension of the (square) texture
coord = vertex.xy+vec2(0.5/size); //the coordinates with which the texture is sampled later
vertex.xy = vec2(vertex.xy*(size))+vec2(0.5); //the vertices are shifted.  I think this line is the problem.  
If I need to provide more, I'd be happy to do so. Thanks for any help, -Geometrian
[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]
soconne
soconne
You could try using integer texture coordinates. I'm doing that in my current project b/c its A LOT easier to get correct coordinates that way.
Author Freeworld3Dhttp://www.freeworld3d.org
dpadam450
dpadam450
glOrtho(0,n, 0, n, ..,...)

if you call glVertex(0,0) does it output the pixel in the bottom left? If it does you are fine, you can just multiply the vertex with the projection matrix.
NBA2K, Madden, Maneater, Killing Floor, Sims 
Geometrian
Geometrian
Thanks,

I played around with it a bit and eventually solved it by using:
coord = vec2(vertex.xy*vec2(1.0-(1.0/size)))+vec2(0.5/size);vertex.xy = vec2(vertex.xy*(size-1.0))+vec2(0.5);


Here's some eye candy of the result with some post-processing:
65,536 particles, ~50fps

-G
[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.