PDA

View Full Version : Fast OpenGL doubts



arthurprs
16-07-2008, 06:15 PM
On my 2d OpenGL render:

my setup

glShadeModel(GL_SMOOTH);

glClearColor(0.0, 0.0, 0.0, 0);

glDisable(GL_DEPTH_TEST);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);

glEnable(GL_LINE_SMOOTH);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity;

glOrtho(0, w, h, 0, -1, 1);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity;

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
It works, but it's ok?


and i set those filter on textures

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
what filter gives me the best quality?


now, a problem, i can render some lines with

glBegin(GL_LINES);
glColor4f(1, 1, 1, 0);
glVertex2f(50, 50);
glColor4f(1, 0, 1, 1);
glVertex2f(400, 300);
glEnd;

but if i load a texture i can't see the lines i render with this code anymore :? , why?

Thanks.

waran
16-07-2008, 06:26 PM
The setup is the wrong place for glTexParameteri. It works only if a texture is bound.

Watch this:
{$mode objfpc}
unit TGATexture;

interface
type
TTGAFile = record
iType: byte; // Should be 2
w, h: word; // Width, Height
bpp: byte; // Byte per Pixel
data: ^byte; // Pixels, dynamic length
end;

function TGAtoTex(const filename: string): longword;
function TGAtoTex(const tex: TTGAFile): longword;
function LoadTGA(const filename: string): TTGAFile;

implementation
uses dglOpenGL;

function LoadTGA(const filename: string): TTGAFile;
var f: file; bytes: longword;
begin
assign(f, filename);
reset(f, 1);

// type
seek(f, 2);
blockread(f, result.iType, 1);

// w, h, bpp
seek(f, 12);
blockread(f, result.w, 5);
result.bpp := result.bpp div 8;

// data
bytes := result.w * result.h * result.bpp;
getmem(result.data, bytes);
seek(f, 18);
blockread(f, result.data^, bytes);

close(f);
end;

function TGAtoTex(const filename: string): longword;
var
tex: TTGAFile;
begin
tex := LoadTGA(filename);
result := TGAtoTex(tex);
FreeMem(tex.data);
end;

function TGAtoTex(const tex: TTGAFile): longword;
var
glFormat: longword;
begin
if tex.iType = 2 then
begin
glGenTextures(1, @result);
glBindTexture(GL_TEXTURE_2D, result);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

if tex.bpp = 3 then glFormat := GL_BGR
else glFormat := GL_BGRA;

glTexImage2D(GL_TEXTURE_2D, 0, tex.bpp, tex.w, tex.h,
0, glFormat, GL_UNSIGNED_BYTE, tex.data);
end;
end;

end.

GL_LINEAR gives the nicest looking result (with Filtering).

arthurprs
16-07-2008, 06:34 PM
Thanks :) ,

and what about my lines not showing after loading a texture? :(

arthurprs
16-07-2008, 06:51 PM
another doubt,

why glTexCoord2f works with positions [0 .. 1] and glVertex2f works with [0 .. screensize] :scratch: :think: ?

waran
16-07-2008, 06:57 PM
Both work with any float you provide; however:

A vertex defines a point of a 3D-model in your 3D-room.
A texture coord defines a reference point for your texture on your model.
Even if you don't use the 3rd dimension always remember GL is a 3D-api
and so based on models, not pixels.

arthurprs
16-07-2008, 07:13 PM
Both work with any float you provide; however:

A vertex defines a point of a 3D-model in your 3D-room.
A texture coord defines a reference point for your texture on your model.
Even if you don't use the 3rd dimension always remember GL is a 3D-api
and so based on models, not pixels.

if i use glTexCoord2i i can use [ 0 .. texsize] ?

i'm tired of "/ texW" and "/ texH"

waran
16-07-2008, 08:58 PM
Yes, you can!
However it won't work until you scale the texture matrix correctly:

glMatrixMode(GL_TEXTURE);
glScalef(1/texsize, 1/texsize, 1);


I don't know if texsize or 1/texsize will work. Just try out :D

tpascal
16-07-2008, 09:43 PM
why glTexCoord2f works with positions [0 .. 1] and glVertex2f works with [0 .. screensize]
Remember, when they say you use float values from 0..1 that dosent mean you can just use value 0 or 1, it mean you can use any values from 0.0001 goint to 0.99999 until 1.0, that is a lot of number.

With glTexCoord2f you define your % position of the current texture mapping, so you dont have to define absolute texture positions, that way you can load different textures dimensions and apply them to the same defined rectangle or triangle. for example if you do




//rectangle in clockwise order, 6 units size (0..5)
GlBegin(GL_Quads);
//1th vertex.
glTexCoord2f(0,0);
glVertex2f(0, 0);


//2th vertex.
glTexCoord2f(1,0);
glVertex2f(5, 0);


//3th vertex.
glTexCoord2f(1,1);
glVertex2f(5, -5);


//4th vertex.
glTexCoord2f(0,1);
glVertex2f(0, -5);


glend;



That will show a rectangle with the whole current texture mapped to it; it doset matter the texture size. Change value "1" by "0.5" for example for map just the half of the texture whatever the texture dimension is.



but if i load a texture i can't see the lines i render with this code anymore Confused , why?
Even when you are drawing lines a texture is still mapped to the lines vertex if you have glEnable(GL_TEXTURE_2D); when you have not defined any glTexCoord2f it apply as current 0,0; when not texture is loadedd in current active texture then 0,0 is color white; but when you load the texture then most likely a dark color texture is get into 0,0 so your line looks invisible (becouse you have a dark background). Use glDissable(GL_TEXTURE_2D); before rendering the lines.

good luck

Brainer
16-07-2008, 10:51 PM
why glTexCoord2f works with positions [0 .. 1] and glVertex2f works with [0 .. screensize]


Remember, when they say you use float values from 0..1 that dosent mean you can just use value 0 or 1, it mean you can use any values from 0.0001 goint to 0.99999 until 1.0, that is a lot of number.

For all I know, you can use values higher than 1.0 so that you can repeat the texture on the object.

arthurprs
16-07-2008, 11:42 PM
but if i load a texture i can't see the lines i render with this code anymore Confused , why?


Even when you are drawing lines a texture is still mapped to the lines vertex if you have glEnable(GL_TEXTURE_2D); when you have not defined any glTexCoord2f it apply as current 0,0; when not texture is loadedd in current active texture then 0,0 is color white; but when you load the texture then most likely a dark color texture is get into 0,0 so your line looks invisible (becouse you have a dark background). Use glDissable(GL_TEXTURE_2D); before rendering the lines.

good luck

thanks, it worked nice now!

Thanks for the attention guys, all doubts solved. :)