Link to home
Start Free TrialLog in
Avatar of bloodbird
bloodbird

asked on

OpenGl , how to map texture while drawing concave polygon? (about OpenGL Tesselation)

i want to draw a concave polygon ,and map texture to it. my code does not work , could you help me ?

this is my delphi code, this code just draw a polygon but not map texture.

procedure TForm2.glDraw();
var
  i:integer;
  count :integer;

  T: PGLUtesselator;
  V: array of TGLArrayd3;  
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);    
  glLoadIdentity();                                      

  glTranslatef(0, 0, zPos);
  gluOrtho2D(-512, 512, -512, 512);

  glRotatef(angle, 0, 1, 0);
  glBindTexture(GL_TEXTURE_2D, Texture1);

  T := gluNewTess();

  gluTessCallback(T, GLU_TESS_BEGIN, @glBegin);
  gluTessCallback(T, GLU_TESS_VERTEX, @glVertex3dv);
  gluTessCallback(T, GLU_TESS_END, @glEnd);

  Count:=Length(Poly);
  SetLength(v,Count);
  for i:=0 to Count-1 do
  begin
    v[i][0]:=GlCordX(Poly[i].X,1024);  
    v[i][1]:=GlCordY(Poly[i].Y,1024);  
    v[i][2]:=GlCordZ(TheHeight,1024);  
  end;

  gluBeginPolygon(T);
      for i:= 0 to count - 1 do
      begin
         glTexCoord2f(Poly[i].X, Poly[i].Y); //nothing to do with this code????
         gluTessVertex(T,V[i],@V[i]);
      end;
   gluEndPolygon(T);
   gluDeleteTess(T);
end;

you can use some other progaming language to show me how to code it.

thank you.



Avatar of bloodbird
bloodbird

ASKER

this code still does not work
procedure myCallback(x,y,z:Integer);
begin
  glTexCoord2f(X, Y);
  glVertex3d(x,y,z );
end;

procedure TForm2.glDraw();
var
  i:integer;
  count :integer;

  T: PGLUtesselator;
  V: array of TGLArrayd3;  
  V1:TGLArrayd3;
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);    
  glLoadIdentity();                                      

  glTranslatef(0, 0, zPos);
  gluOrtho2D(-512, 512, -512, 512);

  glRotatef(angle, 0, 1, 0);
  glBindTexture(GL_TEXTURE_2D, Texture1);

  T := gluNewTess();

  gluTessCallback(T, GLU_TESS_BEGIN, @glBegin);
  gluTessCallback(T, GLU_TESS_VERTEX, @MyCallBack);
  gluTessCallback(T, GLU_TESS_END, @glEnd);

  Count:=Length(Poly);
  SetLength(v,Count);
  for i:=0 to Count-1 do
  begin
    v[i][0]:=GlCordX(Poly[i].X,1024);  
    v[i][1]:=GlCordY(Poly[i].Y,1024);  
    v[i][2]:=GlCordZ(TheHeight,1024);  
  end;

  gluBeginPolygon(T);
      for i:= 0 to count - 1 do
      begin
         v1[0]:=Poly[i].X;
         v1[1]:=Poly[i].Y;
         v1[2]:=TheHeight;      
         gluTessVertex(T,V[i],@V1);
      end;
   gluEndPolygon(T);
   gluDeleteTess(T);
end;


this code does not work yet  , it raise a exception of access violation  


Function GlCordX(x:integer;TheWidth:integer):GlFloat;  //windows coordinate to Opengl coordinate
begin
   Result:=(X-(theWidth/2)) / theWidth ;
end;

Function GlCordY(y:integer;TheHeight:integer):GlFloat;
begin
  Result:=((TheHeight/2)- Y)/TheHeight;
end;

Function GlCordZ(z:integer;TheWidth:integer):GlFloat;
begin
   Result:= z / theWidth ;
end;

Function GlCordTX(x:integer;TheWidth:integer):GlFloat;   //texture coordinate
begin
   Result:=X / theWidth ;
end;

Function GlCordTY(y:integer;TheHeight:integer):GlFloat;
begin
  Result:=(TheHeight- Y)/TheHeight;
end;

procedure myCallback(x,y,z:Integer);
begin
  glTexCoord2f(X, Y);
  glVertex3d(x,y,z );
end;

procedure TForm2.glDraw();
var
  i:integer;
  count :integer;

  T: PGLUtesselator;
  V: array of TGLArrayd3;  
  V1: array[0..2] of integer;   //different from last code
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);    
  glLoadIdentity();                                      

  glTranslatef(0, 0, zPos);
  gluOrtho2D(-512, 512, -512, 512);

  glRotatef(angle, 0, 1, 0);
  glBindTexture(GL_TEXTURE_2D, Texture1);

  T := gluNewTess();

  gluTessCallback(T, GLU_TESS_BEGIN, @glBegin);
  gluTessCallback(T, GLU_TESS_VERTEX, @MyCallBack);
  gluTessCallback(T, GLU_TESS_END, @glEnd);

  Count:=Length(Poly);
  SetLength(v,Count);
  for i:=0 to Count-1 do
  begin
    v[i][0]:=GlCordX(Poly[i].X,1024);  
    v[i][1]:=GlCordY(Poly[i].Y,1024);  
    v[i][2]:=GlCordZ(TheHeight,1024);  
  end;

  gluBeginPolygon(T);
      gluTessBeginContour(T);     //different from last code
      for i:= 0 to count - 1 do
      begin
         v1[0]:=Poly[i].X;
         v1[1]:=Poly[i].Y;
         v1[2]:=TheHeight;      
         gluTessVertex(T,V[i],@V1);    
      end;
      gluTessEndContour(T);    //different from last code
   gluEndTressPolygon(T);     //different from last code
   gluDeleteTess(T);
end;
oh, i give a wrong  mycallback function code , it should be this in my delphi project

procedure myCallback(x,y,z:Integer);
begin
  glTexCoord2f(GLCordTX(X,1024), GlCordTY(Y,1024));
  glVertex3d(GlCordX(X,1024),GlCordY(Y,1024),GlCordZ(Z,1024) );
end;
i modified the code , add  stdcall after  myCallBack function   and  make  gluTessBeginPolygon(T, nil) instead of  gluBeginPolygon(T)  ,  

it does not work yet, access vialotion .....
Avatar of ikework
Hi bloodbird,

You said in the beginning of the thread that it was working, except the texture was not rendered.

It will be easier to help you, starting with that code, that did not throw the access-violation-exception.

Please go back to that code and post it here. Btw you can use the Code-Snippet-Box for the code.

ike
SOLUTION
Avatar of developmentguru
developmentguru
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi, thanks all , especially  developmentguru,    i have solved this problem.  
the  param of the function "mycallback" must be a pointer. this is the code. the code works good

Function GlCordX(x:integer;TheWidth:integer):GlDouble;  //windows coordinate to Opengl coordinate
begin
   Result:=(X-(theWidth/2)) / theWidth ;
end;

Function GlCordY(y:integer;TheHeight:integer):GlDouble;
begin
  Result:=((TheHeight/2)- Y)/TheHeight;
end;

Function GlCordZ(z:integer;TheWidth:integer):GlDouble;
begin
   Result:= z / theWidth ;
end;

Function GlCordTX(x:integer;TheWidth:integer):GlDouble;   //texture coordinate
begin
   Result:=X / theWidth ;
end;

Function GlCordTY(y:integer;TheHeight:integer):GlDouble;
begin
  Result:=(TheHeight- Y)/TheHeight;
end;

procedure myCallback(a:pointer);  stdcall;
var
  b:PGLDouble;
begin
  b:=a;
  inc(b,3);
  glTexCoord2fv(b);
  glVertex3dv(a);
end;

procedure TForm2.glDraw();
var
  i:integer;
  count :integer;

  T: PGLUtesselator;
  V: array of TGLArrayd3;  
  V1: array[0..4] of GLDouble;
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);    
  glLoadIdentity();                                      
  glTranslatef(0, 0, zPos);
  glRotatef(angle, 0, 1, 0);
  glBindTexture(GL_TEXTURE_2D, Texture1);

  T := gluNewTess();
  gluTessCallback(T, GLU_TESS_BEGIN, @glBegin);
  gluTessCallback(T, GLU_TESS_VERTEX, @MyCallBack);
  gluTessCallback(T, GLU_TESS_END, @glEnd);

  Count:=Length(Poly);
  SetLength(v,Count);
  for i:=0 to Count-1 do
  begin
    v[i][0]:=GlCordX(Poly[i].X,1024);  
    v[i][1]:=GlCordY(Poly[i].Y,1024);  
    v[i][2]:=GlCordZ(TheHeight,1024);  
  end;

  gluBeginTessPolygon(T,nil);
      gluTessBeginContour(T);    
      for i:= 0 to count - 1 do
      begin
         v1[0]:=v[i,0];
         v1[1]:=v[i,1];
         v1[2]:=v[i,2]t;    
         v1[3]:=GlCordTX(Poly[i].X,1024);
         v14]:=GlCordTY(Poly[i].Y,1024);
         gluTessVertex(T,V[i],@V1);    
      end;
      gluTessEndContour(T);  
   gluEndTessPolygon(T);    
   gluDeleteTess(T);
end;

thanks , i should accept it,haha , give points to ikework.   and...

i have another question "How to save opengl scene to a bitmap?"  i'll ask it in a new thread.  if you interested in it. take a look. thank you very much.
i solved the problem myself, but they gave me a good advice, let me know the"normal" things.
You are, of course, right about the order of the vertexes being the issue ikework.  I made the mistake of giving the right advice using the wrong terms.  I appreciate the correction.