Recent

Author Topic: OpenGL problem  (Read 898 times)

lazboy10

  • New member
  • *
  • Posts: 7
OpenGL problem
« on: August 18, 2022, 04:27:00 pm »
Hi!

Im trying to build a top down editor where you draw 2d lines with two points... Point(LineStart.x, LineStart.y), Point(LineEnd.x, LineEnd.y) . i then connect the lines together to form a polygon shape.

The problem is displaying the lines as walls in opengl... i need the walls to draw in 3d as quads with a fixed height and that the 3d walls positions are correct based on the 2d lines starting and ending points so i can connect the walls together in 3d as well.

Im also using old opengl i know its deprecated but that doesnt matter to me.
So if someone could help be with a peace of old opengl code for the 2d wall to 3d wall conversion/drawing i would be a happy camper!

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: OpenGL problem
« Reply #1 on: August 18, 2022, 06:32:09 pm »
Sorry, I can't fully understand your explanation. Can you provide some pictures, or sketches?

And can you show us what code you already have?

lazboy10

  • New member
  • *
  • Posts: 7
Re: OpenGL problem
« Reply #2 on: August 18, 2022, 06:50:49 pm »
Hi,

What i mean is that lets say i draw a regular line on a tcanvas, it has a starting point and an ending point. i then want that line to be drawn in opengl as a 3d line but it has a height so it would look like a wall in 3d. like a quad or 2 triangles to form the wall.

here is some code ive been messing around with trying  to get it to work but it doesnt work.

procedure DrawWallSlice1(x,y,z,size:glfloat;Texture:gluint);
begin
  glDisable(GL_LIGHTING); // disable lighting
  glBindTexture(GL_TEXTURE_2D, Texture);

  // enable blend settings
  glEnable(GL_BLEND);
  // blend settings
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

  glBegin(GL_QUADS);
  glTexCoord2d(1, 1); glVertex3d(size-x, z, size-y);
  glTexCoord2d(0, 1); glVertex3d(-size-x, z, size-y);
  glTexCoord2d(0, 0); glVertex3d(-size-x, 0, size-y);
  glTexCoord2d(1, 0); glVertex3d(size-x, 0, size-y);
  glEnd();

  // disable blend settings
  glDisable(GL_BLEND);
  glEnable(GL_LIGHTING); // enable lighting
end;

im only getting the wall to render with out an angle. i want it to have the same angle and position as the tcanvas line but in 3d

the attached image shows what i mean in a better way.

sysrpl

  • Sr. Member
  • ****
  • Posts: 315
    • Get Lazarus
Re: OpenGL problem
« Reply #3 on: August 19, 2022, 07:29:21 am »
It would be best if you learned linear algebra. The correct way to draw a line as a vector is to find the vector, normalize it, multiply it by the magnitude, then draw two triangles to compose a line. Learn what is "vector", "vector normalize", and "vector magnitude".

This might help:

https://www.khanacademy.org/computing/computer-programming/programming-natural-simulations/programming-vectors/a/vector-magnitude-normalization


Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: OpenGL problem
« Reply #4 on: August 20, 2022, 07:45:25 am »
Turning 2D polygon into 3D is as easy, as adding Z coordinate to X and Y. I.e. your polygon is stored as series of points (X1, Y1), (X2, Y2), (X3, Y3). They're drawn as lines, i.e. 1-2, 2-3, etc. Turning them into quads is as simple, as adding Z coordinate, that is either 0 or "height". Like (X1, Y1, 0), (X1, Y1, height), (X2, Y2, height), (X2, Y2, 0). Now all you need - is to set proper transformation from local coordinate system into device coordinate system. ModelView matrix is used to transform your wall itself. Rotate it for example. Please remember, that in 3D Z coordinate is usually perpendicular to screen. The most important part - is projection matrix. It's used to convert coordinates from world space into device space. It's important, because if you need your 3D wall to look similar to 2D one, you need proper scale. I don't remember exact device coordinate system boundaries. It's (-1, -1, -1) - (1, 1, 1) or it's (-1, -1, 0) - (1, 1, 1). Something like that. So, if your 2D coordinate system is, let's say, (0, 0) - (100, 100), then it should be converted into (-1, -1, -1) - (1, 1, 1) via simple (X - 50) / 50 operation, that should be performed by projection matrix. See glOrtho for reference.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

dje

  • Full Member
  • ***
  • Posts: 134
Re: OpenGL problem
« Reply #5 on: August 20, 2022, 08:49:50 am »
I'm unsure what exactly you want. You say you have "2d lines with two points". Yet, your DrawWallSlice1() function has x, y, z & size?
Anyway, To draw extrude a line into quad, you would need something like:
Code: Pascal  [Select][+][-]
  1. procedure DrawWallSlice(x1, y1, x2, y2: double; acolor: tcolor);
  2. begin
  3.   glBegin(GL_QUADS);
  4.   glColor3d(Red(AColor) / 255, Green(AColor) / 255, Blue(AColor) / 255);
  5.   glVertex3d(x1, 0.0, y1);
  6.   glVertex3d(x2, 0.0, y2);
  7.   glVertex3d(x2, 0.5, y2);
  8.   glVertex3d(x1, 0.5, y1);
  9.   glEnd;
  10. end;  
It also sounds like you are wanting isometric projection? (That's just a feeling I get from your diagrams)
https://stackoverflow.com/questions/1059200/true-isometric-projection-with-opengl

So, if I take the isometric projection example, and add in my DrawWallSlice, I get:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.OpenGLControl1Paint(Sender: TObject);
  2. var
  3.   Dist: double;
  4. begin
  5.   glDisable(GL_CULL_FACE);
  6.   glClearColor(0.0, 0.0, 0.0, 1.0);
  7.   glClear(GL_COLOR_BUFFER_BIT);
  8.  
  9.   glMatrixMode(GL_PROJECTION);
  10.   glLoadIdentity;
  11.  
  12.   // use this length so that camera is 1 unit away from origin
  13.   Dist := sqrt(1 / 3.0);
  14.  
  15.   gluLookAt(Dist, Dist, Dist,  // position of camera
  16.     0.0, 0.0, 0.0,   // where camera is pointing at
  17.     0.0, 1.0, 0.0);  // which direction is up
  18.   glMatrixMode(GL_MODELVIEW);
  19.  
  20.   glBegin(GL_LINES);
  21.   glColor3d(1.0, 0.0, 0.0);
  22.   glVertex3d(0.0, 0.0, 0.0);
  23.   glVertex3d(1.0, 0.0, 0.0);
  24.   glColor3d(0.0, 1.0, 0.0);
  25.   glVertex3d(0.0, 0.0, 0.0);
  26.   glVertex3d(0.0, 1.0, 0.0);
  27.   glColor3d(0.0, 0.0, 1.0);
  28.   glVertex3d(0.0, 0.0, 0.0);
  29.   glVertex3d(0.0, 0.0, 1.0);
  30.   glEnd();
  31.  
  32.   DrawWallSlice(0, 0, 0.5, 0, clYellow);
  33.   DrawWallSlice(0, 0.5, 0, 0, clGreen);
  34.   glFlush;
  35.   OpenGLControl1.SwapBuffers;
  36. end;  

Attached is a screen shot. You are going to run into issues with depth buffering using isometric projections. Some people say "it can be done". Other people say, "you need to sort your walls". Who knows!

Good luck with your work.

EDIT: Just a thought. The isometric example I got online seems to require the y axis to be put into the z coordinates. This might be why I didnt get depth buffering working. I would look for similar code and then adjust DrawWallSlice as needed. I guess the first step is to find out if depth buffers are valid for isometric views.
« Last Edit: August 20, 2022, 09:09:46 am by derek.john.evans »

lazboy10

  • New member
  • *
  • Posts: 7
Re: OpenGL problem
« Reply #6 on: August 20, 2022, 04:56:43 pm »
Thank you dje! problem solved!

this is exactly what i was looking for!  :D


 

TinyPortal © 2005-2018