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.



bloodbirdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bloodbirdAuthor Commented:
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;


0
bloodbirdAuthor Commented:
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;
0
bloodbirdAuthor Commented:
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;
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

bloodbirdAuthor Commented:
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 .....
0
ikeworkCommented:
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
0
developmentguruPresidentCommented:
I am confused by the wording of your question.  You stated that you, "want to draw a concave polygon".  The term "polygon" when used in 3D programming refers to a planar set of coordinates (normally 3) and therefore could not be concave.  I assume you meant that you want to construct a concave surface and paint it with your texture... is this correct?  I mention this only so the terminology does not confuse other experts.

I also question if you gave all of the source related to the OpenGL.  I have not done a lot with OpenGL, but it seems that some initialization code is missing.  If you would provide all of the code it would help us to help you.

The first thought I have on your concave rendering is that the normal for the polygons in your render should be reversed.  If you do not reverse the normal then you it will draw nothing since the normal indicates that the polygon should be drawn if seen from one side, and hidden if seen from the other.
0
ikeworkCommented:
>> If you do not reverse the normal then you it will draw nothing since the normal indicates that the polygon should be drawn if seen from one side, and hidden if seen from the other.

No, the normal only affects the lighting.

Whether the polygon is drawn or not depends on the order of the vertices, the order defines the Front-Faces and Back-Faces.
Depending on your configuration its either clockwise or counterclockwise. By default culling is disabled, you must enable it with:

  glEnable(GL_CULLFACE);

to activate it. Then you specify the front-face with either:

  glFrontFace(GL_CW);

for clockwise or

  glFrontFace(GL_CCW);

for counterclockwise.

But as said above by default the culling is disabled, so it doesnt matter what order your polygons have, if you dont activate it.

References:

  - "10.090 How does face culling work? Why doesn't it use the surface normal?":

        http://www.opengl.org/resources/faq/technical/clipping.htm

  - "glFrontFace"

        http://www.opengl.org/sdk/docs/man/xhtml/glFrontFace.xml


ike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bloodbirdAuthor Commented:
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;

0
bloodbirdAuthor Commented:
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.
0
bloodbirdAuthor Commented:
i solved the problem myself, but they gave me a good advice, let me know the"normal" things.
0
developmentguruPresidentCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.