java - 处理中带有纹理的三角形

标签 java processing

我正在处理一个代码,其中包含随机移动的点。这些点寻找球并开始三角测量(塑造三角形)。我想在它们上显示一个不透明的序列图像文件夹。因此,每次形成三角形时都会使用图像进行纹理化。 我尝试过使用texture和loadimage函数,但问题似乎是三角形用各种点快速渲染,并且无法正确看到纹理。

import processing.opengl.*;
float r1, r2;
void setup()
{
  size(800, 800, OPENGL);
 for (int j=0; j<numAgents; j++)
    agents[j]=new agent();
  for (int i=0; i<numMovers; i++)
    movers[i]=new Mover();
  smooth();
}
void draw()
{
  background(0);

  for (int i=0; i<numMovers; i++)
  {
    movers[i].update();
    movers[i].checkEdges();
    movers[i].display();
    int closestAgentNumber=-1;
    for (int j=0; j<numAgents; j++)
    {
      agents[j].checkEdges();
      agents[j].display();

      agents[j].update();
      agents[j].repel();

      float d = dist(agents[j].location.x, agents[j].location.y, movers[i].location.x, movers[i].location.y );

      if (d < 100) {
        closestAgentNumber=j; 
      }
      if (d<200)
      {
        agents[j].behaviour=1;
        agents[j].follow(movers[i].location.x, movers[i].location.y);
        movers[i].hit = true;
      }
      else if (d>100) {
        movers[i].hit = false;

        agents[j].behaviour=0;
      }

    }
  }
}
int numAgents= 100;
agent[]agents =new agent[numAgents];

class agent
{
  PVector location;
  PVector velocity;
  PVector acceleration;
  float topspeed;
  float r1, r2;
  // boolean connect=false;
  int behaviour;
  boolean follow=false;
  //boolean follow= false;
  agent()
  {
    float speed=800;

    if (behaviour==0)
    {

      location= new PVector(random(0, speed), random(0, speed));
      velocity= new PVector(1, 1);
      acceleration= new PVector( random(-0.01, 0.01), random(-0.01, 0.01));
      acceleration.normalize();
      topspeed=5;
    }
    else if (behaviour==1)
    {


      // connect=true;
    }
    // Set to acceleration
  }
  void follow(float x, float y)
  {
    if (follow==true)
    { 

      // follow==true;
      // Our algorithm for calculating acceleration:
      PVector moverPos = new PVector(x, y);
      PVector dir = PVector.sub(moverPos, location);  // Find vector pointing towards mouse
      dir.normalize();     // Normalize
      dir.mult(4);       // Scale 
      acceleration = dir;
    }
  }
  void update()
  {
    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity);
  }

  void display()
  {
    pushMatrix();
    translate(location.x, location.y);
    if (behaviour==1)
    {
      fill(255, 0, 0);
    }
    else if ( behaviour==0)
    {
      fill(255, 255, 255);
    }
    ellipse(0, 0, 4, 4);
    strokeWeight(1);

    popMatrix();
  }

  void checkEdges() 
  {

    if (location.x < 0) {
      location.x = 0;
      velocity.x *= -1;
      acceleration.mult(0.001);
    }
    if (location.x > 800) {
      location.x = 800;
      velocity.x *= -1;
      acceleration.mult(0.001);
    }
    if (location.y < 0) {
      location.y = 0;
      velocity.y *= -1;
      acceleration.mult(0.001);
    }
    if (location.y > 800) {
      location.y = 800;
      velocity.y *= -1;
      acceleration.mult(0.001);
    }
  }
  void repel()
  {

    for (int i=0; i<numAgents; i++) {

      if ((agents[i].behaviour==1))

      {
        int k;

        k=1;
        for (int j=0; j<numAgents; j++)
        {


          //--------------
          // float dm = agents[i].location.dist(agents[k].location);
          float dm = dist(agents[j].location.x, agents[j].location.y, agents[i].location.x, agents[i].location.y);
          if (dm < 50) {
            agents[i].velocity.mult(-1);
            agents[i].acceleration.mult(0.5);
            agents[j].velocity.mult(-1);
            agents[j].acceleration.mult(0.5);
          }
          else if (behaviour==0)
          {
            agents[i].velocity.mult(1);
            agents[j].velocity.mult(1);
          }



          //float dm = dist(agents[i].location.x, agents[i].location.y, agents[j].location.x, agents[j].location.y);
          // float dm = agents[i].location.dist(agents[j].location);
          // int clr=(int) map (dm,0,100,200,0);
          if (dm<180 && dm>100)


          {
            if ( k<2)
            {
              //println(dm);
              //stroke(255,0,0);
              r1 = agents[j].location.x;
              r2 = agents[j].location.y;

              k=k+1;
            }
            else
            {
              //fill(random(255),random(255),random(255),random(0,20));

              //stroke(0,50);
              // texture(A);

              //fill(random(0,255),random(0,255),random(0,255),random(0, 40));

              // stroke(140, 50);  
              fill(random(0, 255), random(0, 255), random(0, 255), random(0, 10));

              stroke(140, 50);
              beginShape();


              //texture();
              //  image(A,agents[i].location.x, agents[i].location.y);
              vertex(agents[i].location.x, agents[i].location.y);
              vertex(r1, r2);
              vertex(agents[j].location.x, agents[j].location.y);
              endShape(CLOSE);
              j=300;
            }
          }
        }
        /*void triangulate()
         {

         fill(random(0, 255), random(0, 255), random(0, 255), random(0, 40));

         stroke(140, 50);
         for (int i=0; i<numAgents; i++)
         {
         //gens[i].connect==true)
         //if(! agents[i].connect)

         if (agents[i].behaviour==1)

         {
         //println(agents[i]);
         int k;

         k=1;

         for (int j=0; j<numAgents; j++)
         {
         float dm = dist(agents[i].location.x, agents[i].location.y, agents[j].location.x, agents[j].location.y);
         // float dm = agents[i].location.dist(agents[j].location);
         // int clr=(int) map (dm,0,100,200,0);
         if (dm<180 && dm>100)

         {
         if ( k<2)
         {
         //println(dm);
         //stroke(255,0,0);
         }
         }
         }
         }*/
      }
    }
  }
}
int numMovers= 1;
Mover[]movers= new Mover[numMovers];

class Mover
{

  PVector location;
  PVector velocity;
  PVector acceleration;
  float topspeed;
  boolean hit = false;

  Mover() 
  {
    float spead = 800;
    location = new PVector(random(0,spead),random(0,spead));
    velocity = new PVector(0,0);
    acceleration = new PVector(random(-1,1),random(-1,1));//random initial acceleration
    topspeed = 4;
  }



  void update() 
  {
    // Motion 101!  Velocity changes by acceleration.  Location changes by velocity.
    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity);
  }


  void display()
  {
    pushMatrix();
  translate(location.x,location.y);
  //fill(102,0,155,random(120,160));
  //fill(32,225,245,127 + sin(frameCount*.01) * 127);
  if(hit) fill(255,10,96,120);
  else   
  fill(3,225,190,random(50,127));
  ellipse(0,0,30,30);
  noStroke();
  popMatrix();
  }


  void checkEdges() 
  {

    if(location.x < 0){
      location.x = 0;
      velocity.x *= -1;
      acceleration.mult(0.001);
    }
    if(location.x > 800){
      location.x = 800;
      velocity.x *= -1;
      acceleration.mult(0.001);

    }
    if(location.y < 0){
      location.y = 0;
      velocity.y *=- 1;
      acceleration.mult(0.001);

    }
    if(location.y > 800){
      location.y = 800;
      velocity.y *= -1;
      acceleration.mult(0.001);

    }




  }

}

最佳答案

为此,您需要使用 3 个处理函数:

  1. tint() - 您将用于透明度(例如,tint(255,127);将使图像/纹理 50% 透明(127 of 255))
  2. texture() - 这将纹理设置为形状(beginShape(),endShape() block )。您可以在 beginShape() 之后调用它,然后在调用 vertex() 时传递两个附加参数,它们是纹理坐标
  3. textureMode() - 如果您始终知道纹理图像的大小,则这是可选的,但了解这一点很方便。默认情况下,纹理模式设置为 IMAGE,这意味着对于要映射到 300 单位长四边形的 100x100 像素图像,您将使用纹理坐标映射顶点角位置,如下例所示。

默认纹理模式(textureMode(IMAGE))

//assumes tex is already initialized/loaded PImage
beginShape();
texture(tex);
vertex(-150,-150,0,    0,  0);//top-left
vertex( 150,-150,0,  100,  0);//top-right
vertex( 150, 150,0,  100,100);//bottom-right
vertex(-150, 150,0,    0,100);//bottom-left
endShape();

另一个选项是使用 NORMALIZED 模式,该模式采用标准化值(0.0 到 1.0 之间)而不是实际像素值,因此上面 NORMALIZED 模式下的示例为:

beginShape();
texture(tex);
vertex(-150,-150,0,    0.0,0.0);//top-left
vertex( 150,-150,0,    1.0,0.0);//top-right
vertex( 150, 150,0,    1.0,1.0);//bottom-right
vertex(-150, 150,0,    0.0,1.0);//bottom-left
endShape();

textureMode() 通常在 setup() 中调用一次,优点是您不必担心了解所有纹理的所有尺寸,尤其是当您在草图中使用多个纹理时。

所以,回到你的代码,你需要像这样初始化纹理:

tex = loadImage("yourTextureImage.png");
  textureMode(NORMALIZED);

稍后在 repel() 函数中绘制形状时,您将执行以下操作:

beginShape();
              tint(255,127);  
              texture(tex);
              vertex(agents[i].location.x, agents[i].location.y,0.0,0.0);//aditional u,v coordinates 0,0 - top left of texture
              vertex(r1, r2,1.0,0.0);//top right texture coordinate
              vertex(agents[j].location.x, agents[j].location.y,1.0,1.0);//bottom right texture coordinat
              endShape(CLOSE);

所以完整的代码如下所示:

import processing.opengl.*;
float r1, r2;
PImage tex;
void setup()
{
  size(800, 800, OPENGL);
 for (int j=0; j<numAgents; j++)
    agents[j]=new agent();
  for (int i=0; i<numMovers; i++)
    movers[i]=new Mover();
  tex = loadImage("yourImage.png");
  textureMode(NORMALIZED);
  smooth();
}
void draw()
{
  background(0);

  for (int i=0; i<numMovers; i++)
  {
    movers[i].update();
    movers[i].checkEdges();
    movers[i].display();
    int closestAgentNumber=-1;
    for (int j=0; j<numAgents; j++)
    {
      agents[j].checkEdges();
      agents[j].display();

      agents[j].update();
      agents[j].repel();

      float d = dist(agents[j].location.x, agents[j].location.y, movers[i].location.x, movers[i].location.y );

      if (d < 100) {
        closestAgentNumber=j; 
      }
      if (d<200)
      {
        agents[j].behaviour=1;
        agents[j].follow(movers[i].location.x, movers[i].location.y);
        movers[i].hit = true;
      }
      else if (d>100) {
        movers[i].hit = false;

        agents[j].behaviour=0;
      }

    }
  }
}
int numAgents= 100;
agent[]agents =new agent[numAgents];

class agent
{
  PVector location;
  PVector velocity;
  PVector acceleration;
  float topspeed;
  float r1, r2;
  // boolean connect=false;
  int behaviour;
  boolean follow=false;
  //boolean follow= false;
  agent()
  {
    float speed=800;

    if (behaviour==0)
    {

      location= new PVector(random(0, speed), random(0, speed));
      velocity= new PVector(1, 1);
      acceleration= new PVector( random(-0.01, 0.01), random(-0.01, 0.01));
      acceleration.normalize();
      topspeed=5;
    }
    else if (behaviour==1)
    {


      // connect=true;
    }
    // Set to acceleration
  }
  void follow(float x, float y)
  {
    if (follow==true)
    { 

      // follow==true;
      // Our algorithm for calculating acceleration:
      PVector moverPos = new PVector(x, y);
      PVector dir = PVector.sub(moverPos, location);  // Find vector pointing towards mouse
      dir.normalize();     // Normalize
      dir.mult(4);       // Scale 
      acceleration = dir;
    }
  }
  void update()
  {
    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity);
  }

  void display()
  {
    pushMatrix();
    translate(location.x, location.y);
    if (behaviour==1)
    {
      fill(255, 0, 0);
    }
    else if ( behaviour==0)
    {
      fill(255, 255, 255);
    }
    ellipse(0, 0, 4, 4);
    strokeWeight(1);

    popMatrix();
  }

  void checkEdges() 
  {

    if (location.x < 0) {
      location.x = 0;
      velocity.x *= -1;
      acceleration.mult(0.001);
    }
    if (location.x > 800) {
      location.x = 800;
      velocity.x *= -1;
      acceleration.mult(0.001);
    }
    if (location.y < 0) {
      location.y = 0;
      velocity.y *= -1;
      acceleration.mult(0.001);
    }
    if (location.y > 800) {
      location.y = 800;
      velocity.y *= -1;
      acceleration.mult(0.001);
    }
  }
  void repel()
  {

    for (int i=0; i<numAgents; i++) {

      if ((agents[i].behaviour==1))

      {
        int k;

        k=1;
        for (int j=0; j<numAgents; j++)
        {


          //--------------
          // float dm = agents[i].location.dist(agents[k].location);
          float dm = dist(agents[j].location.x, agents[j].location.y, agents[i].location.x, agents[i].location.y);
          if (dm < 50) {
            agents[i].velocity.mult(-1);
            agents[i].acceleration.mult(0.5);
            agents[j].velocity.mult(-1);
            agents[j].acceleration.mult(0.5);
          }
          else if (behaviour==0)
          {
            agents[i].velocity.mult(1);
            agents[j].velocity.mult(1);
          }



          //float dm = dist(agents[i].location.x, agents[i].location.y, agents[j].location.x, agents[j].location.y);
          // float dm = agents[i].location.dist(agents[j].location);
          // int clr=(int) map (dm,0,100,200,0);
          if (dm<180 && dm>100)


          {
            if ( k<2)
            {
              //println(dm);
              //stroke(255,0,0);
              r1 = agents[j].location.x;
              r2 = agents[j].location.y;

              k=k+1;
            }
            else
            {
              //fill(random(255),random(255),random(255),random(0,20));

              //stroke(0,50);
              // texture(A);

              //fill(random(0,255),random(0,255),random(0,255),random(0, 40));

              // stroke(140, 50);  
              fill(random(0, 255), random(0, 255), random(0, 255), random(0, 10));

              stroke(140, 50);
              beginShape();
              tint(255,127);  
              texture(tex);
              vertex(agents[i].location.x, agents[i].location.y,0.0,0.0);//aditional u,v coordinates 0,0 - top left of texture
              vertex(r1, r2,1.0,0.0);//top right texture coordinate
              vertex(agents[j].location.x, agents[j].location.y,1.0,1.0);//bottom right texture coordinat
              endShape(CLOSE);
              j=300;
            }
          }
        }
        /*void triangulate()
         {

         fill(random(0, 255), random(0, 255), random(0, 255), random(0, 40));

         stroke(140, 50);
         for (int i=0; i<numAgents; i++)
         {
         //gens[i].connect==true)
         //if(! agents[i].connect)

         if (agents[i].behaviour==1)

         {
         //println(agents[i]);
         int k;

         k=1;

         for (int j=0; j<numAgents; j++)
         {
         float dm = dist(agents[i].location.x, agents[i].location.y, agents[j].location.x, agents[j].location.y);
         // float dm = agents[i].location.dist(agents[j].location);
         // int clr=(int) map (dm,0,100,200,0);
         if (dm<180 && dm>100)

         {
         if ( k<2)
         {
         //println(dm);
         //stroke(255,0,0);
         }
         }
         }
         }*/
      }
    }
  }
}
int numMovers= 1;
Mover[]movers= new Mover[numMovers];

class Mover
{

  PVector location;
  PVector velocity;
  PVector acceleration;
  float topspeed;
  boolean hit = false;

  Mover() 
  {
    float spead = 800;
    location = new PVector(random(0,spead),random(0,spead));
    velocity = new PVector(0,0);
    acceleration = new PVector(random(-1,1),random(-1,1));//random initial acceleration
    topspeed = 4;
  }



  void update() 
  {
    // Motion 101!  Velocity changes by acceleration.  Location changes by velocity.
    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity);
  }


  void display()
  {
    pushMatrix();
  translate(location.x,location.y);
  //fill(102,0,155,random(120,160));
  //fill(32,225,245,127 + sin(frameCount*.01) * 127);
  if(hit) fill(255,10,96,120);
  else   
  fill(3,225,190,random(50,127));
  ellipse(0,0,30,30);
  noStroke();
  popMatrix();
  }


  void checkEdges() 
  {

    if(location.x < 0){
      location.x = 0;
      velocity.x *= -1;
      acceleration.mult(0.001);
    }
    if(location.x > 800){
      location.x = 800;
      velocity.x *= -1;
      acceleration.mult(0.001);

    }
    if(location.y < 0){
      location.y = 0;
      velocity.y *=- 1;
      acceleration.mult(0.001);

    }
    if(location.y > 800){
      location.y = 800;
      velocity.y *= -1;
      acceleration.mult(0.001);

    }




  }

}

关于java - 处理中带有纹理的三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8325603/

相关文章:

java - 如何在 Travis-CI 中跳过 Java Maven 项目的 gpg 签名

将负数变为0的java函数

performance - 处理帧率非常低且无法更改

java - 自动 Hibernate 索引创建时间太长

java - Groovy 比较除第一项之外的两个集合

java - 使用 HttpURLConnection 发送 UTF-8 字符串

javascript - 处理 JS svg 加载 - Google Appengine

java - 使用 PGraphic 对象为线条着色

java - 在 HashMap Java 中存储数据

java 。文件处理。 For循环错误