DGL
https://delphigl.com/forum/

drawing 'basic' geometric shapes without glu
https://delphigl.com/forum/viewtopic.php?f=19&t=9691
Seite 1 von 1

Autor:  noeska [ Mo Jan 10, 2011 15:57 ]
Betreff des Beitrags:  drawing 'basic' geometric shapes without glu

Is there easy to use code (under mpl ) to generate/draw basic shapes like cube/sphere/cylinder/cone etc?
Shapes should be drawable by GL_TRIANGLES.

My results so sofar:
cube ok
sphere ok
cylinder fail/ok (closed/open)

I have this feeling it should not be to hard , just draw some stacked circles, but ...

At this moment i feel like giving up with my 4th attempt from scratch making a cylinder.
I post my (broken) code here for you to give comments on:
Code:
function createCylinder(radius: GLFloat; height: GLFloat; var vertices: GLBufferf;var normals: GLbufferf; var texCoords: GLbufferf; var indices: GLBufferi;var numberVertices: GLuint): GLuint;
var
  i,j: GLuint;
  CIRCLE_RESOLUTION : integer;
  WIDTH_RESOLUTION : integer;
  WIDTH : glFloat;
  totalElements : integer;
  MAX_VERTICIES: integer;
  curidx: integer;
  anglestep: GLFloat;
begin

//stacking is wrong


RADIUS := RADIUS /2; //why div 2 is that because i define with instead of radious

  CIRCLE_RESOLUTION := 35;
  WIDTH_RESOLUTION := 10;
  WIDTH := height/2;


  MAX_VERTICIES := (CIRCLE_RESOLUTION * WIDTH_RESOLUTION);
  numberVertices := MAX_VERTICIES;

      angleStep := (2.0 * PI) / CIRCLE_RESOLUTION;


  // Vertices
  setlength(vertices, (CIRCLE_RESOLUTION * WIDTH_RESOLUTION * 4) + (2*4));

  curidx := 0;




  for j:=0 to WIDTH_RESOLUTION-1 do
  begin
    for i:=0 to CIRCLE_RESOLUTION-1 do
    begin

      vertices[curidx + 0] := cos( ( anglestep * i )) * RADIUS;
      vertices[curidx + 1] := (WIDTH*j) / (WIDTH_RESOLUTION);
      vertices[curidx + 2] := sin( ( anglestep * i )) * RADIUS;
      vertices[curidx + 3] := 1.0;
      curidx := curidx + 4;
    end;
  end;

  vertices[curidx + 0] := 0.0;
  vertices[curidx + 1] := -1.0 * width;
  vertices[curidx + 2] := 0.0;
  vertices[curidx + 3] := 1.0;
  curidx := curidx + 4;

  vertices[curidx + 0] := 1.0;
  vertices[curidx + 1] := 1.0 ;//* width;
  vertices[curidx + 2] := 1.0;
  vertices[curidx + 3] := 1.0;

  curidx := curidx + 4;

  // Indices
  totalElements := (CIRCLE_RESOLUTION * WIDTH_RESOLUTION * 6) + (CIRCLE_RESOLUTION * 3 * 2);
  setlength(indices, totalElements);

  curidx :=0;
  for i:=0 to CIRCLE_RESOLUTION-1 do
  begin
      for j:=0 to WIDTH_RESOLUTION-1 do
    begin

      if j= 0 then
      begin

      Indices[(curidx)+0]:=((CIRCLE_RESOLUTION * WIDTH_RESOLUTION * 4) + (2*4))-8;
      Indices[(curidx)+1]:=(i*j)+1;
      if ((i*j) = CIRCLE_RESOLUTION-1) then
        Indices[(curidx)+2]:=1
      else
        Indices[(curidx)+2]:=(i*j)+2;

      curidx := curidx + 3;
    end;
  end;
  end;


    for i:=0 to CIRCLE_RESOLUTION-1 do
  begin
      for j:=0 to WIDTH_RESOLUTION-1 do
    begin


      indices[curidx+0]:=((i*WIDTH_RESOLUTION + j + 1) mod MAX_VERTICIES);
      indices[curidx+1]:=((i*WIDTH_RESOLUTION + j) mod MAX_VERTICIES);
      indices[curidx+2]:=(((i+1) * WIDTH_RESOLUTION + j) mod MAX_VERTICIES);

      indices[curidx+3]:=((i*WIDTH_RESOLUTION + j + 1) mod MAX_VERTICIES);
      indices[curidx+4]:=(((i+1) * WIDTH_RESOLUTION + j) mod MAX_VERTICIES);
      indices[curidx+5]:=(((i+1) * WIDTH_RESOLUTION + j + 1) mod MAX_VERTICIES);

      curidx := curidx +6;
    end;
  end;



  // Normals
  setlength(normals, (CIRCLE_RESOLUTION * WIDTH_RESOLUTION * 3) + (2*3) );

  curidx := 0;

  normals[curidx + 0] := 0.0;
  normals[curidx + 1] := 1.0;
  normals[curidx + 2] := 0.0;

  curidx := curidx + 3;

  for i:=0 to CIRCLE_RESOLUTION-1 do
  begin
    for j:=0 to WIDTH_RESOLUTION-1 do
    begin
      normals[curidx + 0] := cos( ( anglestep * i ));//cos( degtorad( (360.0 / CIRCLE_RESOLUTION) * i ) );// * RADIUS) / radius;
      normals[curidx + 1] := ((j*WIDTH) / WIDTH_RESOLUTION) /radius;
      normals[curidx + 2] := sin( ( anglestep * i ));//sin( degtorad( (360.0 / CIRCLE_RESOLUTION) * i ) );// * RADIUS) / radius;
      curidx := curidx + 3;
    end;
  end;

  normals[curidx + 0] := 0.0;
  normals[curidx + 1] := -1.0;
  normals[curidx + 2] := 0.0;

  curidx := curidx + 3;

  // TexCoords
  setlength(texcoords, CIRCLE_RESOLUTION * WIDTH_RESOLUTION * 2 );
  curidx := 0;
  for i:=0 to CIRCLE_RESOLUTION-1 do
  begin
    for j:=0 to WIDTH_RESOLUTION-1 do
    begin
      texcoords[curidx + 0] := 0;
      texcoords[curidx + 1] := 0;
      curidx := curidx + 2;
    end;
  end;

  result := totalElements;

end;

Autor:  Flash [ Mo Jan 10, 2011 18:19 ]
Betreff des Beitrags:  Re: drawing 'basic' geometric shapes without glu

We have some code in the wiki for this

see: http://wiki.delphigl.com/index.php/Techniken_und_Algorithmen#Geometrie_Objekte

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/