开发者

Draw two crossed triangles in OpenGL

开发者 https://www.devze.com 2023-01-24 15:21 出处:网络
I\'m doing my first steps with OpenGL in processing.org. I\'d like to draw two crossed triangles, but don\'t really get how to rotate the triangles to cross them.

I'm doing my first steps with OpenGL in processing.org. I'd like to draw two crossed triangles, but don't really get how to rotate the triangles to cross them.

PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
GL gl = pgl.beginGL();

gl.glTranslatef(width/2, height/2, 0);
gl.glRotatef(a, 0, 0, 0);

gl.glBegin(GL.GL_TRIANGLES);
gl.glColor4f(0.7, 0.1, 0.7, 0.8);
gl.glVertex3f(0, 0, 0);
gl.glVertex3f(0, 50, 0);
gl.glVertex3f(25, 0, 25);
gl.glEnd();

gl.glRo开发者_如何学Ctatef(90, 1, 0, 0);
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor4f(0.1, 0.9, 0.7, 0.8);
gl.glVertex3f(0, 0, 0);
gl.glVertex3f(0, 50, 0);
gl.glVertex3f(25, 0, 25);
gl.glEnd();

pgl.endGL();

The triangles should be crossed like these old 3D models of trees. They should rotate and move as one object in later use, which I figured works with pop and push around both vertices, I just can't figure out the rotation to get these two triangles together.


If I understand you correctly you are trying to rotate the triangles independently of eachother? In that case you would need to use gl.PushMatrix() before the triangle and gl.PopMatrix(); after the triangle. Ex:

gl.PushMatrix();
{
  gl.glTranslatef(width/2, height/2, 0); 
  gl.glRotatef(a, 0, 0, 0); 

  gl.glBegin(GL.GL_TRIANGLES); 
  gl.glColor4f(0.7, 0.1, 0.7, 0.8); 
  gl.glVertex3f(0, 0, 0); 
  gl.glVertex3f(0, 50, 0); 
  gl.glVertex3f(25, 0, 25); 
  gl.glEnd(); 
}
gl.PopMatrix();

gl.PushMatrix();
{
  gl.glRotatef(90, 1, 0, 0); 
  gl.glBegin(GL.GL_TRIANGLES); 
  gl.glColor4f(0.1, 0.9, 0.7, 0.8); 
  gl.glVertex3f(0, 0, 0); 
  gl.glVertex3f(0, 50, 0); 
  gl.glVertex3f(25, 0, 25); 
  gl.glEnd(); 
}
gl.PopMatrix();

other wise the top rotation will be applied to both triangles.

Also, I noticed you said you need two crossed "rectangles". If that is the case you will need 4 triangles or one quad for each. So one Quad, rectangle, would be:

gl.PushMatrix();
{

  gl.glTranslatef(width/2, height/2, 0);       
  gl.glRotatef(a, 0, 0, 0);       

  gl.glBegin(GL.GL_TRIANGLES);       
  gl.glColor4f(0.7, 0.1, 0.7, 0.8);       
  gl.glVertex3f(0, 0, 0);       
  gl.glVertex3f(0, 50, 0);       
  gl.glVertex3f(25, 0, 25);
  gl.glVertex3f(25, 0, 25);
  gl.glVertex3f(0, 50, 0);
  gl.glVertex3f(25, 50, 25);     
  gl.glEnd();

}
gl.PopMatrix();

or even better

gl.PushMatrix();
{    
  gl.glTranslatef(width/2, height/2, 0);       
  gl.glRotatef(a, 0, 0, 0);       

  gl.glBegin(GL.GL_QUADS);
  gl.glColor4f(0.7, 0.1, 0.7, 0.8);       
  gl.glVertex3f(0, 0, 0);       
  gl.glVertex3f(0, 50, 0); 
  gl.glVertex3f(25, 50, 25);      
  gl.glVertex3f(25, 0, 25);         
  gl.glEnd();      
}
gl.PopMatrix();

Hope this helps.

Ahh, now we are getting somewhere! Ok this is way simple. As far as the rotation goes you can avoid that and go directly to drawing the quads on top of each other. Going based on your original values of 25 and 50 here is an Example with triangles:

gl.PushMatrix();
{
  gl.glBegin(GL.GL_TRIANGLES);       
  gl.glColor4f(0.7, 0.1, 0.7, 0.8);  

  gl.glVertex3f(-12.5, -25, -12.5);       
  gl.glVertex3f(-12.5, 25, -12.5);       
  gl.glVertex3f(12.5, -25, -12.5);
  gl.glVertex3f(12.5, -25, -12.5);
  gl.glVertex3f(-12.5, 25, -12.5);
  gl.glVertex3f(12.5, 25, -12.5); 

  gl.glVertex3f(0, -25, 0);       
  gl.glVertex3f(0, 25, 0);       
  gl.glVertex3f(0, -25, -25);
  gl.glVertex3f(0, -25, -25);
  gl.glVertex3f(0, 25, 0);
  gl.glVertex3f(0, 25, -25); 

  gl.glEnd();

}
gl.PopMatrix();

Example with Quads:

gl.PushMatrix();
{
  gl.glBegin(GL.GL_QUADS);
  gl.glColor4f(0.7, 0.1, 0.7, 0.8); 

  gl.glVertex3f(-12.5, -25, -12.5);       
  gl.glVertex3f(-12.5, 25, -12.5);  
  gl.glVertex3f(12.5, 25, -12.5);      
  gl.glVertex3f(12.5, -25, -12.5);


  gl.glVertex3f(0, -25, 0);       
  gl.glVertex3f(0, 25, 0);   
  gl.glVertex3f(0, 25, -25);      
  gl.glVertex3f(0, -25, -25);
  gl.glEnd();     
}
gl.PopMatrix();

If this is just for an example then this code should be fine. However, if your going to be rendering multiples of these then you are going to want to store the quad render code into a vertex buffer object and then render multiple of the vertex buffer objects.


I'll try to answer your question in several parts.

1) This code will draw a single rectangle, but you mention 2 rectangle and 2 triangles and it's fairly confusing what you are trying to do.

2) "Both rectangles should act as one object later". This really isn't possible via OpenGL. In opengl (at least at beginner levels), entire objects are created by chaining together single commands. So you don't say "here is an object, now draw it here, here and here". Instead you say "draw this triangle, now draw this rectangle, now rotate, now draw this triangel". It's a step-by-step process.

OpenGL knows nothing about objects or models, it's up to you to define that in your code, then tell OpenGL how to draw each object.

One of the best sources for learning OpenGL is the NeHe tutorials found here http://nehe.gamedev.net/ They should have java versions of the tutorials, otherwise not hard to understand.


Thanks to your code and some geometry on paper, I finally found the solution(s) for my problem:

Crossed-triangle solution:

gl.glBegin(GL.GL_TRIANGLES);
  gl.glColor4f(0.1, 0.9, 0.7, 0.8);
  gl.glVertex3f(-25, 0, 0);    // lower left vertex
  gl.glVertex3f( 25, 0, 0);    // lower right vertex
  gl.glVertex3f( 0,  50, 0);    // upper vertex
gl.glEnd();

gl.glBegin(GL.GL_TRIANGLES);
  gl.glColor4f(0.9, 0.1, 0.7, 0.8);
  gl.glVertex3f(0, 0, 25);    // lower left vertex
  gl.glVertex3f( 0, 0, -25);    // lower right vertex
  gl.glVertex3f( 0,  50, 0);    // upper vertex
gl.glEnd();

Or the same with four triangles:

gl.glBegin(GL.GL_TRIANGLES);
  gl.glColor4f(0.7, 0.1, 0.7, 0.8);
  gl.glVertex3f(0, 0, 0);
  gl.glVertex3f(0, 50, 0);
  gl.glVertex3f(25, 0, 25);
gl.glEnd();

gl.glBegin(GL.GL_TRIANGLES);
  gl.glColor4f(0.1, 0.9, 0.7, 0.8);
  gl.glVertex3f(0, 0, 0);
  gl.glVertex3f(-25, 0, -25);
  gl.glVertex3f(0, 50, 0);
gl.glEnd();

gl.glBegin(GL.GL_TRIANGLES);
  gl.glColor4f(0.1, 0.1, 0.9, 0.8);
  gl.glVertex3f(0, 0, 0);
  gl.glVertex3f(25, 0, -25);
  gl.glVertex3f(0, 50, 0);
gl.glEnd();

gl.glBegin(GL.GL_TRIANGLES);
  gl.glColor4f(0.1, 0.9, 0.7, 0.8);
  gl.glVertex3f(0, 0, 0);
  gl.glVertex3f(-25, 0, 25);
  gl.glVertex3f(0, 50, 0);
gl.glEnd();
0

精彩评论

暂无评论...
验证码 换一张
取 消