How can I fix "The function glBegin(int) does not exist" error in processing2 ?

edited September 2014 in How To...

Hi,

Currently, I downloaded some code from OpenProcessing but I cannot run this code because of this error, "The function glBegin(int) does not exist" ....

I bet this code was written by processing 1 and at that time opengl was not included in processing 2 so that I need to edit a bit to run in processing 2.0.... Unfortunately I am a beginner for opengl and I can not fix this error! Please help me...

The following is the code I would like to work....

/** OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/15938*@* */
/** !do not delete the line above, required for linking your tweak if you upload again */
import controlP5.*;

import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;

import processing.opengl.*;
import javax.media.opengl.*  ;

import processing.video.*;

import toxi.geom.*;
import toxi.geom.mesh.*;
import toxi.volume.*;

//simulation size
int w=50;
int h=50;
int d=50;

GL gl;

MovieMaker mm;

//our chemical concentrations
float gridU[][][] = new float[w][h][d];
float gridV[][][] = new float[w][h][d];
//a temporary grid to hold values
float gridNext[][][] = new float[w][h][d];
//how fast the reaction progresses
//you can use this to change the scale (larger = more detailed
float reactionRate = .5;

//All these different ways of storing the diffusion rates are redundant
//only one at a time is necessary
//diffusion rate for chemical U
float diffusionRateU = .04;
//separate diffusion rates for the x and y direction for chemical U
float diffusionRateUX = diffusionRateU;
float diffusionRateUY = diffusionRateU;
//an array to hold a variable Y diffusion rate that changes through space
float diffRateUYarr[][] = new float[w][h];
//an array to hold fully general anisotropic diffusion that varies in direction and rate
float diffRateAnisoU[][][] = new float[w][h][3];
//diffusion rate for V
float diffusionRateV = .01;

//controls the speed of the simulation
//higher is faster but can cause error
float deltaT = 1.0;
//the grid space
//changes how fast diffusion occurs. smaller = larger scale pattern
float deltaXSq = .6*.6;

//reaction parameters
float F = .04;
float k = .06;
//reaction parameters changing through space, which override the previous parameters
//float[][] Farr = new float[w][h];
//float[][] karr = new float[w][h];

//make a movie
boolean movieOn = false;

float minU, maxU;

//Toxi's volume stuff
VolumetricSpace volume;
IsoSurface surface;
TriangleMesh mesh = new WETriangleMesh("meshy");
float threshold = .6;

//toggle for 2d vs 3d view
boolean drawMode = true;

//our camera, works automatically after initializing
PeasyCam cam;

void setup() {
  size(500,500,OPENGL);
  setupGUI();
  cam = new PeasyCam(this, 0,0,0,300);

  gl = ((PGraphicsOpenGL) this.g).beginGL();
  if(movieOn) mm = new MovieMaker(this,width,height,"reaction.mov",30, MovieMaker.H263, MovieMaker.HIGH);

  //initialize the chemicals, be creative
  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      for(int k=0;k<d;++k) {
        gridU[i][j][k] = .5 + random(-.01,.01);
        gridV[i][j][k] = .25 + random(-.01,.01);
      }
    }
  }
  for(int i=w/2-10;i<w/2+10;++i) {
    for(int j=h/2-10;j<h/2+10;++j) {
      for(int k=d/2-10;k<d/2+10;++k) {
        gridU[i][j][k] = 1.0 + random(-.01,.01);
        gridV[i][j][k] = 0 + random(-.01,.01);
      }
    }
  }
  //initialize volume and surface for volutils
  setupVolume();

  glSetup();

  //setup arrays for various parameters
  //setupF();

  //setupK();
  //setupDiffRates();
  //setupAnisoDiffusionU();
}

void draw() {
  //simulation step
  //you can run ten steps without slowing down
  for(int i=0;i<10;++i) {
    diffusionU();
    diffusionV();
    reaction();
  }

  if(drawMode) {
    drawPoints();
  } else {
    draw3D();
  }

  if(movieOn) mm.addFrame();
}

//draw 3d extruded shape
void draw3D() {
  glStart();
  glSetLights();
  glSetMaterials();

  //creates mesh from the volume data
  //buildVolume();
  //buildSurface();

  //number of triangle
  int num=mesh.getNumFaces();
  Vertex v1, v2, v3;
  gl.glBegin(GL.GL_TRIANGLES);
  for(int i=0; i<num; i++) {
    //loop through all faces
    Face f = mesh.faces.get(i);
    //get triangle vertices
    v1 = f.a;
    v2 = f.b;
    v3 = f.c;
    //call normal direction and vertex position
    gl.glNormal3f(-v1.normal.x,-v1.normal.y,-v1.normal.z);
    gl.glVertex3f(v1.x,v1.y,v1.z);

    gl.glNormal3f(-v2.normal.x,-v2.normal.y,-v2.normal.z);
    gl.glVertex3f(v2.x,v2.y,v2.z);

    gl.glNormal3f(-v3.normal.x,-v3.normal.y,-v3.normal.z);
    gl.glVertex3f(v3.x,v3.y,v3.z);
  }
  gl.glEnd();
  ((PGraphicsOpenGL) g).endGL();
}

void buildVolume() {
  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      for(int k=0;k<d;++k) {
        volume.setVoxelAt(i,j,k,1-gridU[i][j][k]);
      }
    }
  }
}

//simple drawing making a point for each voxel
void drawPoints() {
  translate(-w/2,-h/2,-d/2);
  colorMode(HSB,1.0);
  glStart();
  gl.glPointSize(6.0);
  gl.glBegin(GL.GL_POINTS);
  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      for(int k=0;k<d;++k) {
        if(1-gridU[i][j][k] > threshold) {
          color currColor = color((gridU[i][j][k]-minU)/(maxU-minU),1.0,1.0);
          gl.glColor4f(red(currColor),green(currColor),blue(currColor),.2);
          gl.glVertex3f(i,j,k);
        }
      }
    }
  }

  gl.glEnd();
  ((PGraphicsOpenGL) g).endGL();  
}

//diffusion for chemical U
//currently uses the general anisotropic diffusion (stored in diffRateAnisoU)
void diffusionU() {

  /** 2D ANISOTROPIC DIFFUSION
  float[] anisotropic;
  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      anisotropic = diffRateAnisoU[i][j];
      gridNext[i][j] = gridU[i][j]+deltaT/deltaXSq*(
      anisotropic[0]*(gridU[(i-1+w)%w][j]+gridU[(i+1)%w][j])+ //X direction
      anisotropic[2]*(gridU[i][(j-1+h)%h]+gridU[i][(j+1)%h])+ //Y direction
      anisotropic[1]*(gridU[(i+1)%w][(j-1+h)%h]+gridU[(i-1+w)%w][(j+1)%h] //diagonals
      -gridU[(i-1+w)%w][(j-1+h)%h]-gridU[(i+1)%w][(j+1)%h])- //diagonals
      2*gridU[i][j]*(anisotropic[0]+anisotropic[2]));
    }
  }
  float temp[][] = gridU;
  gridU = gridNext;
  gridNext = temp;
  */

  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      for(int k=0;k<d;++k) {

        gridNext[i][j][k] = gridU[i][j][k]+diffusionRateU*deltaT/deltaXSq*
          (gridU[(i-1+w)%w][j][k]
          +gridU[(i+1)%w][j][k]
          +gridU[i][(j-1+h)%h][k]
          +gridU[i][(j+1)%h][k]
          //z direction
          +gridU[i][j][(k-1+d)%d]
          +gridU[i][j][(k+1)%d]
          -6*gridU[i][j][k]
          );
      }
    }
  }
  float temp[][][] = gridU;
  gridU = gridNext;
  gridNext = temp;

}

//diffusion for chemical V
//uses a constant rate diffusionRateV
void diffusionV() {
  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      for(int k=0;k<d;++k) {

        gridNext[i][j][k] = gridV[i][j][k]+diffusionRateV*deltaT/deltaXSq*
          (gridV[(i-1+w)%w][j][k]
          +gridV[(i+1)%w][j][k]
          +gridV[i][(j-1+h)%h][k]
          +gridV[i][(j+1)%h][k]
          //z direction
          +gridV[i][j][(k-1+d)%d]
          +gridV[i][j][(k+1)%d]
          -6*gridV[i][j][k]
          );
      }
    }
  }
  float temp[][][] = gridV;
  gridV = gridNext;
  gridNext = temp;
}

//our reaction step
void reaction() {
  minU = 999;
  maxU = -999;
  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      for(int k=0;k<d;++k) {
        float  uVal = gridU[i][j][k];
        gridU[i][j][k] = gridU[i][j][k] + deltaT*(reactionU(gridU[i][j][k],gridV[i][j][k], F, k));
        gridV[i][j][k] = gridV[i][j][k] + deltaT*(reactionV(uVal,gridV[i][j][k], F, k));
        //keeping track of smallest and largest values of U
        //using these values to draw colors that are normalized to the current U range
        if(gridU[i][j][k] > maxU) maxU = gridU[i][j][k];
        if(gridU[i][j][k] < minU) minU = gridU[i][j][k];
      }
    }
  }
}

//Grey-Scott
float reactionU(float aVal, float bVal, float FVal, float kVal) {
  return reactionRate*(-aVal*bVal*bVal+F*(1-aVal));
}

float reactionV(float aVal, float bVal, float FVal, float kVal) {
  return reactionRate*(aVal*bVal*bVal-(F+k)*bVal);
}

//setup parameters varying in space
/**
void setupF() {
  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      Farr[i][j] = 10;
    }
  }
}

void setupK() {
  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      karr[i][j] = -.05;
    }
  }
}

//not being used
void setupDiffRates() {
  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      diffRateUYarr[i][j] = diffusionRateUY;
    }
  }
}

//changing diffusion through space
void setupAnisoDiffusionU() {
  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      //angle changes from left to right.
      float angle = PI*i/w;
      float diffusionRate1 = diffusionRateU;
      float diffusionRate2 = diffusionRateU;
      diffRateAnisoU[i][j] = anisotropy(angle,diffusionRate1,diffusionRate2);
    }
  }
}
*/

float[] anisotropy(float angle, float r1, float r2) {
  float cosA = cos(angle);
  float sinA = sin(angle);
  float out[] = new float[3];
  out[0] = r1*cosA*cosA+r2*sinA*sinA;
  out[1] = (r2-r1)*cosA*sinA;
  out[2] =  r2*cosA*cosA+r1*sinA*sinA;
  return out;
}

void setArrayByImage(PImage im, float[][] arr, float minVal, float maxVal) {
  for(int i=0;i<w;++i) {
    for(int j=0;j<h;++j) {
      arr[i][j] = brightness(im.pixels[j*w+i])/255.0*(maxVal-minVal)+minVal;
    }
  }
}

//standard toxiclibs volume setup
void setupVolume() {
  volume = new VolumetricSpaceArray(new Vec3D(w,h,d),w,h,d);
  surface = new ArrayIsoSurface(volume);
}

//save our mesh surface to STL
void saveSurface() {
  surface.reset();
  surface.computeSurfaceMesh(mesh,threshold);
  mesh.saveAsSTL(sketchPath("reaction_"+day()+"_"+hour()+"_"+minute()+".stl"));
}

//build the mesh and compute the normals
void buildSurface() {
  //volume.closeSides();
  surface.reset();
  //create mesh
  mesh = new WETriangleMesh("mesh");
  surface.computeSurfaceMesh(mesh,threshold);
  //compute normals
  mesh.computeFaceNormals();
  mesh.computeVertexNormals();
  mesh = thickenMesh(mesh,.5);
  mesh.computeFaceNormals();
  mesh.computeVertexNormals();  
}

//thicken the faces of our mesh
TriangleMesh thickenMesh(TriangleMesh tMesh, float thickness) {
  TriangleMesh thickMesh = new TriangleMesh("thick");
  int numFaces = tMesh.getNumFaces();
  Vec3D aUp, aDown, bUp, bDown, cUp,cDown;
  for(int i=0;i<numFaces;++i) {
    WEFace f = (WEFace) tMesh.faces.get(i);
    //offset one way along normals
    aUp = f.a.add(f.a.normal.scale(thickness));
    bUp = f.b.add(f.b.normal.scale(thickness));
    cUp = f.c.add(f.c.normal.scale(thickness));
    aDown = f.a.sub(f.a.normal.scale(thickness));
    bDown = f.b.sub(f.b.normal.scale(thickness));
    cDown = f.c.sub(f.c.normal.scale(thickness));

    thickMesh.addFace(cUp,bUp,aUp);

    //offset the other way
    thickMesh.addFace(aDown,bDown,cDown);

    //deal with edges
    WingedEdge we = f.edges.get(0);
    //if edge is on the boundary
    if(we.faces.size() == 1) {
      thickMesh.addFace(aUp,bUp,bDown);
      thickMesh.addFace(bDown,aDown, aUp);
    }
    we = f.edges.get(1);
    //if edge is on the boundary
    if(we.faces.size() == 1) {
      thickMesh.addFace(bUp,cUp,cDown);
      thickMesh.addFace(cDown,bDown, bUp);
    }    
    we = f.edges.get(2);
    //if edge is on the boundary
    if(we.faces.size() == 1) {
      thickMesh.addFace(cUp,aUp,aDown);
      thickMesh.addFace(aDown,cDown, cUp);
    }    

  }
  return thickMesh;
}

void keyPressed() {
  if(key == 'f' && movieOn) {
    mm.finish();
  }
  else if(key == 's') {
    saveSurface();
    mm.finish();
  } else if(key == 'v') {
    drawMode = !drawMode;
    if(!drawMode) {
      buildVolume();
      buildSurface();
    }
  }
}


void setupGUI() {
  ControlP5 cp5 = new ControlP5(this);
  ControlWindow cwindow = cp5.addControlWindow("window",0,0,400,200);
  Slider threshSlide = cp5.addSlider("threshold", 0.0,1.0,10,10,300,20);
  threshSlide.setWindow(cwindow);
}

void glSetup() {
  gl = ((PGraphicsOpenGL) g).beginGL();
  //glut = new GLUT();
  gl.glShadeModel ( GL.GL_SMOOTH );
  gl.glClearColor (1.0,1.0,1.0,1.0);
  gl.glEnable(GL.GL_DEPTH_TEST);
  gl.glEnable(GL.GL_BLEND);
  gl.glEnable(GL.GL_COLOR_MATERIAL);
  gl.glEnable(GL.GL_POINT_SMOOTH); 
  gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
  gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); 
    //glu.gluPerspective(50.0, 1.0, 5.0, 1500.0); //50
  //((PGraphicsOpenGL) g).endGL();
  //gl.glEnable(GL.GL_CULL_FACE);
  //gl.glCullFace(GL.GL_BACK);
  gl.glLightModeli(GL.GL_LIGHT_MODEL_TWO_SIDE, GL.GL_TRUE);
}


void glStart() {
  gl = ((PGraphicsOpenGL) g).beginGL();
  gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
}

void glSetMaterials() {
  gl.glDisable(GL.GL_COLOR_MATERIAL);  
  //gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE);
  gl.glColor3f(.6,.6,.6);

  float mat_specular[] = {1,1,1,1.0};
  gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, mat_specular,0); //specular reflection, defines effect mat has on the reflected light

  float mat_shininess[] = {20};
  gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, mat_shininess,0);  //specular exponent (shininess), control the size and brightness of the specular reflection

  float mat_diffuseb[] = {.6,.6,.6,1.0};
  gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_diffuseb,0);

  float mat_diffuse[] = {1,1,0,1.0};
  gl.glMaterialfv(GL.GL_BACK, GL.GL_DIFFUSE, mat_diffuse,0);

}

void glSetLights() {
  gl.glEnable ( GL.GL_LIGHTING ); 
  float light0_diffuse[] = {.7,.7,.7,1};
  gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE,  light0_diffuse,0);
  gl.glEnable ( GL.GL_LIGHT0 );
}
Tagged:

Answers

  • _vk_vk
    edited September 2014

    First try formatting your code in the forum, this way is unreadable. Select it after pasted and hit ctr k, to do so.

    I'm also a noob in OpenGL but had a similar error recently. My fuction was glViewport()that I found became viewport() I found that in the javadoc, you might look there to see if it helps you:

    http://processing.org/reference/javadoc/core/

    I think what you are after should be in

    PGL

    or

    PGraphicsOpenGL

    There is a beginPGL(), but it takes no arguments… so without reading the code I can't say… Probably after that there will be some more to update…

Sign In or Register to comment.