PDA

View Full Version : OpenGl Quickie



code_glitch
29-10-2010, 08:07 AM
Really stuck here; part because I never took OpenGl seriously enough, and part because this isnt all my code, but in a 2d environment, how to you convert the glvertex3f co-ordinates into pixels on a screen?

All my verticies are glvertex3f( x, y, 0) where 0 is a constant so it stays 3d...

Please help, drawing in Prometheus is a lot faster now, just that it doesnt draw where you want it, how you want it (yipee) I'd come across this problem before but the solution then was to sdl_blit it. Yea, its cheating but at the time performance wasnt priority 1.

cheers,
code_glitch.

Sascha Willems
29-10-2010, 08:37 AM
Doing 2D in OpenGL is pretty simple, and just using glVertex3f doesn't automatically mean it's 3D.

- Switch to orthogonal projection and use your screen size as the parameters (or a set virtual screen size if you want it to look the same everywhere)
- Call glVertex3f with (virtual) screen coordinates like you'd draw in 2D. Use the Z-Coordinate to get free sorting

Example :


glOrtho(0, 1280, 720, 0, -16, 16);
glBegin(GL_QUADS);
glVertex3f(100, 100, 0);
glVertex3f(100, 300, 0);
glVertex3f(300, 300, 0);
glVertex3f(300, 100, 0);
glEnd;
// Draw another quad ontop. OpenGL will automatically sort if depth test is enabled
glBegin(GL_QUADS);
glVertex3f(200, 200, 1);
glVertex3f(200, 500, 1);
glVertex3f(500, 500, 1);
glVertex3f(500, 200, 1);
glEnd;

Ñuño Martínez
29-10-2010, 08:38 AM
It was a long time since I used OpenGL but IIRC the trick is in the projection matrix. Can't remember how (actually I never mastered the projection matrix configuration) but I think the red or the blue book have an example.

[edit] Beaten...

code_glitch
29-10-2010, 08:59 AM
Hmm, I've managed to sort of fix the problem. But now I have a new problem... The image is, how to put it, not garbled but..... well. check the attachment :) Its better but not really practical.

To compare result is what the program looks like and Picture.png is what it should look like... Any ideas as to what I'm doing wrong? Here's my render code:



procedure Image.Draw(X,Y: Int64);
var
VxR: array [1..2] of LongInt;
VyR: array [1..2] of LongInt;

PxR: array [1..2] of Int64;
PyR: array [1..2] of Int64;

begin
VxR[1] := 0;
VxR[2] := Surface.W;
VyR[1] := 0;
VyR[2] := Surface.H;

PxR[1] := X;
PxR[2] := X + Surface.W;
PyR[1] := Y;
PyR[2] := Y + Surface.H;

glBindTexture( GL_TEXTURE_2D, texture );

glBegin( GL_QUADS );

glTexCoord2i( VxR[1], VyR[2] );
glVertex3f( PxR[1], PyR[2], 0. );

glTexCoord2i( VxR[2], VyR[2] );
glVertex3f( PxR[2], PyR[2], 0. );

glTexCoord2i( VxR[2], VyR[1] );
glVertex3f( PxR[2], PyR[1], 0. );

glTexCoord2i( VxR[1], VyR[1] );
glVertex3f( PxR[1], PyR[1], 0. );
glEnd();
end;


Note that these images were originally PNG before PGD converted them to jpg....

Edit: Almost forgot to mention: I am expecting it to go off the edge of the screen... I have a different scale procedure.

chronozphere
29-10-2010, 09:35 AM
Maybe this thread can help you a bit:

http://www.pascalgamedevelopment.com/showthread.php?6168-I-need-some-help-with-2D-drawing-libraries (http://www.pascalgamedevelopment.com/showthread.php?6168-I-need-some-help-with-2D-drawing-libraries)

The first part is about pixel-perfect drawing. :)

User137
29-10-2010, 02:48 PM
glTexCoord2i doesn't make your texture coordinates scale from 0..Width. They are still ranged from 0..1. Use glTexCoord2f with that proper range and it might solve it. Otherwise the error might be in texture loading.