Example #1
0
void GWindow::redrawRectangle(const R2Rectangle& r) {
    I2Point leftTop = map(R2Point(r.left(), r.top()));
    I2Point rightBottom = map(R2Point(r.right(), r.bottom()));
    redrawRectangle(
        I2Rectangle(
            leftTop,
            rightBottom.x - leftTop.x,
            rightBottom.y - leftTop.y
        )
    );
}
Example #2
0
void GWindow::fillRectangle(const R2Rectangle& r) {
    I2Point leftTop = map(R2Point(r.left(), r.top()));
    I2Point rightBottom = map(R2Point(r.right(), r.bottom()));

    //+++ printf("leftTop = (%d, %d), rightBottom = (%d, %d)\n",
    //+++     leftTop.x, leftTop.y, rightBottom.x, rightBottom.y);

    ::XFillRectangle(
        m_Display,
        m_Window,
        m_GC,
        leftTop.x, leftTop.y,
        rightBottom.x - leftTop.x, rightBottom.y - leftTop.y
    );
}
Example #3
0
GWindow::GWindow(
    const I2Rectangle& frameRect,
    const char* title /* = 0 */
):
    m_Window(0),
    m_GC(0),
    m_WindowPosition(frameRect.left(), frameRect.top()),
    m_IWinRect(I2Point(0, 0), frameRect.width(), frameRect.height()),
    m_RWinRect(),
    m_ICurPos(0, 0),
    m_RCurPos(0., 0.),
    m_xcoeff(1.),
    m_ycoeff(1.),
    m_WindowCreated(false),
    m_bgPixel(0),
    m_fgPixel(0),
    m_bgColorName(0),
    m_fgColorName(0),
    m_BorderWidth(DEFAULT_BORDER_WIDTH)
{
    GWindow(            // Call another constructor
        frameRect,
        R2Rectangle(
            R2Point(0., 0.),
            (double) frameRect.width(), (double) frameRect.height()
        ),
        title
    );
}
Example #4
0
/////////////////////////////////////////////////////////////
// Main: initialize X, create an instance of MyWindow class,
//       and start the message loop
int main() {
    // Initialize X stuff
    if (!GWindow::initX()) {
        printf("Could not connect to X-server.\n");
        exit(1);
    }

    MyWindow w;
    w.createWindow(
        I2Rectangle(                    // Window frame rectangle:
            I2Point(10, 10),            //     left-top corner
            GWindow::screenMaxX()/2,    //     width
            GWindow::screenMaxY()/2     //     height
        ),
        R2Rectangle(                    // Coordinate rectangle:
            R2Point(-12., -9.),         //     bottom-right corner
            24., 18.                    //     width, height
         ),
        "Graph of Function"             // Window title
    );
    w.setBackground("lightGray");

    GWindow::messageLoop();

    GWindow::closeX();
    return 0;
}
Example #5
0
R2Point 
operator/(const R2Point& point, double a)
{
    assert(a != 0);
    return R2Point(point.X() / a, 
		   point.Y() / a);
}
Example #6
0
void Terrain::Create(void)
{
    node = new R3Node();
    node->transformation = R3identity_matrix;

    R3Material::Params matParams;
    matParams.textureName = "mars.jpg";
    matParams.kd = R3Rgb(0.6, 0.6, 0.6, 1);
    matParams.ks = R3Rgb(0.3, 0.3, 0.3, 1);
    R3Material *mat = new R3Material(matParams);

    node->AddChild(new R3Node(Patch(R3Point(0,-3, 0),
                    R2Point(TERRAIN_SIZE,TERRAIN_SIZE), R2Point(TERRAIN_DPS,TERRAIN_DPS)),
                mat, R3identity_matrix));
    //node->AddChild(new R3Node(Patch(R3Point(10,-3, 0), R2Point(10,10), R2Point(60,60)), NULL, R3identity_matrix));
    globals.scene->root->AddChild(node);
}
Example #7
0
R2Point 
operator*(const R3Matrix& a, const R2Point& p)
{
    // Multiply matrix by point
    RNCoord x = a.m[0][0] * p.X() + a.m[0][1] * p.Y() + a.m[0][2];
    RNCoord y = a.m[1][0] * p.X() + a.m[1][1] * p.Y() + a.m[1][2];
    return R2Point(x, y);
}
Example #8
0
void R3Rectangle::
Draw(const R3DrawFlags draw_flags) const
{
    // Check if box is empty
    if (IsEmpty()) return;

    /* Get texture coordinates */
    static R2Point texcoords[4] = {
	R2Point(0.0, 0.0),
	R2Point(1.0, 0.0),
	R2Point(1.0, 1.0),
	R2Point(0.0, 1.0)
    };

    /* Get box corner points */
    R3Point corners[4];
    corners[0] = Corner(RN_NN_QUADRANT);
    corners[1] = Corner(RN_PN_QUADRANT);
    corners[2] = Corner(RN_PP_QUADRANT);
    corners[3] = Corner(RN_NP_QUADRANT);

    // Draw surface
    if (draw_flags[R3_SURFACES_DRAW_FLAG]) {
        if (draw_flags[R3_SURFACE_NORMALS_DRAW_FLAG]) 
            R3LoadNormal(Normal());

        R3BeginPolygon();
        for (int j = 0; j < 4; j++) {
            if (draw_flags[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG])
                R3LoadTextureCoords(texcoords[j]);
	    R3LoadPoint(corners[j]);
        }
        R3EndPolygon();
    }

    // Draw edges
    if (draw_flags[R3_EDGES_DRAW_FLAG]) {
	R3BeginLoop();
	for (int i = 0; i < 4; i++)
	    R3LoadPoint(corners[i]);
	R3EndLoop();
    }    
}
Example #9
0
R2Point GWindow::invMap(const I2Point& p) const {
    return R2Point(
        double(p.x - m_IWinRect.left()) *
        m_RWinRect.width() / m_IWinRect.width()
        + m_RWinRect.left(),

        double(m_IWinRect.bottom() - p.y) *
        m_RWinRect.height() / m_IWinRect.height()
        + m_RWinRect.bottom()
    );
}
Example #10
0
int 
R3InitCircle()
{
    // Initialize circle angles and points 
    for (int i = 0; i < R3circle_npoints; i++) {
        R3circle_angles[i] = ((RNScalar) i * RN_TWO_PI) / (RNScalar) R3circle_npoints;
        R3circle_points[i] = R3Point(cos(R3circle_angles[i]), sin(R3circle_angles[i]), 0.0);
        R3circle_texcoords[i] = R2Point(0.5 * (R3circle_points[i].X() + 1.0), 0.5 * (R3circle_points[i].Y() + 1.0));
    }

    /* Return success */
    return TRUE;
}
Example #11
0
R3Mesh* Terrain::Patch(R3Point center, R2Point size, R2Point dps) { // dps: dots per side
  R3Mesh *patch = new R3Mesh();
  for (unsigned int r = 0; r < dps.Y(); r++) {
    for (unsigned int c = 0; c < dps.X(); c++) {
      double dx = (double)c / dps.X() * size.X() - size.X() / 2;
      double dy = (r + c) % 2 * Util::UnitRandom(); // default

      if (params.heightMap) {
        int x = ((double)c / dps.X()) * params.heightMap->Width();
        int y = ((double)r / dps.Y()) * params.heightMap->Height();
        dy = 25 * params.heightMap->Pixel(x,y).Luminance();
      }

      double dz = (double)r / dps.Y() * size.Y() - size.Y() / 2;
      R3Point position(center.X() + dx, center.Y() + dy, center.Z() + dz);

      double tx = position.X() / 80;
      double ty = position.Z() / 80;

      //R2Point texcoord = R2Point(tx - ((int) tx), ty - ((int) ty));
      //if (texcoord.X() < 0)
          //texcoord.SetX(1 + texcoord.X());
      //if (texcoord.Y() < 0)
          //texcoord.SetY(1 + texcoord.Y());

      patch->CreateVertex(position, R3Vector(0,1,0), R2Point(tx, ty)); // id's go up along x axis, then along y
    }
  }
  
  for (unsigned int r = 0; r < dps.Y() - 1; r++) {
    for (unsigned int c = 0; c < dps.X() - 1; c++) {
      unsigned int cols = (unsigned int)dps.X();
      unsigned int k = c + r * cols;
      vector<R3MeshVertex*> vertices;
      vertices.push_back(patch->Vertex(k+cols));
      vertices.push_back(patch->Vertex(k+1));
      vertices.push_back(patch->Vertex(k));
      patch->CreateFace(vertices);

      vector<R3MeshVertex*> vertices2;
      vertices2.push_back(patch->Vertex(k+cols));
      vertices2.push_back(patch->Vertex(k+1+cols));
      vertices2.push_back(patch->Vertex(k+1));
      patch->CreateFace(vertices2);
    }
  }

  patch->Update();

  return patch;
}
Example #12
0
void GWindow::drawLine(const I2Point& p1, const I2Point& p2) {
    //... drawLine(invMap(p1), invMap(p2));
    if (
        abs(p1.x) < SHRT_MAX && abs(p1.y) < SHRT_MAX &&
        abs(p2.x) < SHRT_MAX && abs(p2.y) < SHRT_MAX
    )
    {
        ::XDrawLine(
            m_Display,
            m_Window,
            m_GC,
            p1.x, p1.y,
            p2.x, p2.y
        );
    } else {
        R2Point c1, c2;
        if (
            R2Rectangle(
                m_IWinRect.left(), m_IWinRect.top(),
                m_IWinRect.width(), m_IWinRect.height()
            ).clip(
                R2Point(p1.x, p1.y), R2Point(p1.x, p1.y),
                c1, c2
            )
        ) {
            ::XDrawLine(
                m_Display,
                m_Window,
                m_GC,
                (int)(c1.x + 0.5), (int)(c1.y + 0.5),
                (int)(c2.x + 0.5), (int)(c2.y + 0.5)
            );
        }
    }
    moveTo(invMap(p2));
}
Example #13
0
GWindow::GWindow():
    m_Window(0),
    m_GC(0),
    m_WindowPosition(0, 0),
    m_IWinRect(I2Point(0, 0), 300, 200),    // Some arbitrary values
    m_RWinRect(
        R2Point(0., 0.), 
        300., 200.
    ),
    m_ICurPos(0, 0),
    m_RCurPos(0., 0.),
    m_xcoeff(1.),
    m_ycoeff(1.),
    m_WindowCreated(false),
    m_bgPixel(0),
    m_fgPixel(0),
    m_bgColorName(0),
    m_fgColorName(0),
    m_BorderWidth(DEFAULT_BORDER_WIDTH),
    m_BeginExposeSeries(true)
{
    strcpy(m_WindowTitle, "Graphic Window");
}
Example #14
0
void Rails::Create(void)
{

    for (int i = 0; i < params.railMap->Width(); i++) {
        for (int j = 0; j < params.railMap->Height(); j++) {
            if (params.railMap->Pixel(i,j).Luminance() > .7)
                currentLocation = R2Point(i, j);
        }
    }
    /*
    for (int i = 0; i < params.railMap->Width()-1; i++) {
        for (int j = 0; j < params.railMap->Height()-1; j++) {
            if (params.railMap->Pixel(i,j)[0] > .5) {
                if (params.railMap->Pixel(i,j+1)[0] > .5 && params.railMap->Pixel(i+1,j+1)[0] > .5) {
                    params.railMap->Pixel(i,j+1)[0] = 0;//, R2black_pixel);
                }
                if (params.railMap->Pixel(i+1,j)[0] > .5 && params.railMap->Pixel(i+1,j+1)[0] > .5) {
                    params.railMap->Pixel(i+1,j)[0] = 0;//, R2black_pixel);
                }
            }
            
        }
    }
    */
    oldLocation = currentLocation;
    oldoldLocation = currentLocation;
    targetDirection = 3.14159;
    currentDirection = 3.14159;

    time = 1.0/MOVE_SPEED*TERRAIN_DPS/(double)TERRAIN_SIZE;

    node = new R3Node();
    node->transformation = R3identity_matrix;

    globals.scene->root->AddChild(node);

}
Example #15
0
R2Point 
operator-(const R2Point& point, const R2Vector& vector)
{
    return R2Point(point.X() - vector.X(), 
		   point.Y() - vector.Y());
}
Example #16
0
R2Point 
operator+(const R2Point& point1, const R2Point& point2)
{
    return R2Point(point1.v[0] + point2.v[0], 
		   point1.v[1] + point2.v[1]);
}
Example #17
0
R2Point 
operator-(const R2Point& point)
{
    return R2Point(-point.X(), 
		   -point.Y());
}
Example #18
0
void R3OrientedBox::
Draw(const R3DrawFlags draw_flags) const
{
    // Check if box is empty
    if (IsEmpty()) return;

    static R2Point texcoords[4] = {
	R2Point(0.0, 0.0),
	R2Point(1.0, 0.0),
	R2Point(1.0, 1.0),
	R2Point(0.0, 1.0)
    };
    static int surface_paths[6][4] = {
	{ 3, 0, 1, 2 },
	{ 4, 7, 6, 5 },
	{ 0, 4, 5, 1 },
	{ 7, 3, 2, 6 },
	{ 3, 7, 4, 0 },
        { 1, 5, 6, 2 }
    };
    static int outline_path[16] = {
	0, 1, 2, 3,
	0, 4, 5, 6,
	7, 4, 5, 1,
	2, 6, 7, 3
    };

    /* Get box corner points */
    R3Point corners[8];
    corners[0] = Corner(RN_NNN_OCTANT);
    corners[1] = Corner(RN_NNP_OCTANT);
    corners[2] = Corner(RN_NPP_OCTANT);
    corners[3] = Corner(RN_NPN_OCTANT);
    corners[4] = Corner(RN_PNN_OCTANT);
    corners[5] = Corner(RN_PNP_OCTANT);
    corners[6] = Corner(RN_PPP_OCTANT);
    corners[7] = Corner(RN_PPN_OCTANT);

    // Draw surface
    if (draw_flags[R3_SURFACES_DRAW_FLAG]) {
	for (int i = 0; i < 6; i++) {
	    R3BeginPolygon();
	    if (draw_flags[R3_SURFACE_NORMALS_DRAW_FLAG]) {
                RNScalar sign = (i < 3) ? 1.0 : -1.0;
                R3LoadNormal(sign * Axis(i%3));
            }
	    for (int j = 0; j < 4; j++) {
		if (draw_flags[R3_VERTEX_TEXTURE_COORDS_DRAW_FLAG]) 
		    R3LoadTextureCoords(texcoords[j]);
		R3LoadPoint(corners[surface_paths[i][j]]);
	    }
	    R3EndPolygon();
	}
    }

    // Draw edges
    if (draw_flags[R3_EDGES_DRAW_FLAG]) {
	R3BeginLine();
	for (int i = 0; i < 16; i++)
	    R3LoadPoint(corners[outline_path[i]]);
	R3EndLine();
    }    
}
Example #19
0
void Rails::Update(double dt)
{
    time -= dt;
    if (time < 0) {
        int i = -1;
        int j = -1;
        time += 1.0/MOVE_SPEED*(double)GetWidth()/TERRAIN_DPS;//*GetWidth()/(double)TERRAIN_SIZE;//TERRAIN_SIZE/(double)TERRAIN_DPS;//*coeff;//(double)GetWidth()*coeff;

        float greatest = 0;
        R2Point previousLocation = oldLocation;
        R2Point firstLocation = currentLocation;
        R2Point secondLocation = currentLocation;
        for (int k = 0; k < 5; k++) {
            float greatest = 0;
            for (i = -1; i <= 1; i++) {
                for (j = -1; j <= 1; j++) {
                    if (i == 0 && j == 0)
                        continue;
                    if (params.railMap->Pixel(currentLocation.X()+i,currentLocation.Y()+j)[0] > greatest) {
                        R2Point tempLocation = R2Point(currentLocation.X()+i,currentLocation.Y()+j);
                        if (tempLocation.X() != oldLocation.X() || tempLocation.Y() != oldLocation.Y()) {
                            if (tempLocation.X() != oldoldLocation.X() || tempLocation.Y() != oldoldLocation.Y()) {
                                greatest = params.railMap->Pixel(currentLocation.X()+i,currentLocation.Y()+j)[0];
                                nextLocation = tempLocation;
                            }
                        }
                    }
                }
            } 

            if (k==0)
                secondLocation = nextLocation;
            oldoldLocation = oldLocation;
            oldLocation = currentLocation;
            currentLocation = nextLocation;

        }
        for (i = -1; i <= 1; i++) {
            for (j = -1; j <= 1; j++) {
                if (i == 0 && j == 0)
                    continue;
                if (params.railMap->Pixel(currentLocation.X()+i,currentLocation.Y()+j)[2] > .5) {
                    globals.levelStatus = 1;
                    globals.gsmgr->Stop();
                }
            }
        } 



        R3Vector v = R3Vector(-(currentLocation.X()-firstLocation.X()), 0, currentLocation.Y()-firstLocation.Y());
        targetDirection = acos(-R3zaxis_vector.Dot(v)/v.Length());
        targetDirection = targetDirection - (int)(targetDirection/6.28);
        if (v.X() < 0) {
            targetDirection = 6.28-targetDirection;
        }
        oldoldLocation = previousLocation;
        oldLocation = firstLocation;
        currentLocation = secondLocation;

    }
    //double coeff = (sqrt(2) - 1)*abs((firstLocation.X()-currentLocation.X())*(currentLocation.Y()-firstLocation.Y())) + 1;
    //printf("coeff %f\n", coeff);
    float closeness = 0;
    if (targetDirection < 0)
        closeness = (-currentDirection + targetDirection);
    else
        closeness = (-currentDirection + targetDirection);
    currentDirection += ROLL_SPEED*dt * closeness;//abs(closeness)*closeness;
    if (abs(currentDirection - targetDirection) > .05) {
        /*
           if (currentDirection < targetDirection)
           currentDirection += ROLL_SPEED*dt;
           if (currentDirection > targetDirection)
           currentDirection -= ROLL_SPEED*dt;
           */
    }
    //currentDirection = targetDirection;//abs(closeness)*closeness;
    globals.player->SetDirection(currentDirection);
}
Example #20
0
void GWindow::drawLine(double x1, double y1, double x2, double y2) {
    drawLine(R2Point(x1, y1), R2Point(x2, y2));
}
Example #21
0
void GWindow::drawLineTo(double x, double y) {
    drawLineTo(R2Point(x, y));
}
Example #22
0
R2Point 
operator*(const R2Point& point, double a)
{
    return R2Point(point.X() * a, 
		   point.Y() * a);
}
Example #23
0
void GWindow::moveTo(double x, double y) {
    moveTo(R2Point(x, y));
}
Example #24
0
void GWindow::drawString(double x, double y, const char* str, int len) {
    drawString(map(R2Point(x, y)), str, len);
}
Example #25
0
void GLUTRedraw(void)
{
  // Check scene
  if (!scene) return;

  // Set viewing transformation
  viewer->Camera().Load();

  // Clear window
  RNRgb background = scene->Background();
  glClearColor(background.R(), background.G(), background.B(), 1.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // Load lights
  LoadLights(scene);

  // Draw camera
  if (show_camera) {
    glDisable(GL_LIGHTING);
    glColor3d(1.0, 1.0, 1.0);
    glLineWidth(5);
    DrawCamera(scene);
    glLineWidth(1);
  }

  // Draw lights
  if (show_lights) {
    glDisable(GL_LIGHTING);
    glColor3d(1.0, 1.0, 1.0);
    glLineWidth(5);
    DrawLights(scene);
    glLineWidth(1);
  }

  // Draw rays
  if (show_rays) {
    glDisable(GL_LIGHTING);
    glColor3d(0.0, 1.0, 0.0);
    glLineWidth(3);
    DrawRays(scene);
    glLineWidth(1);
  }

  // Draw rays
  if (show_photons) {
    glDisable(GL_LIGHTING);
    glColor3d(1.0, 1.0, 1.0);
    glLineWidth(1);
    DrawPhotonPaths(scene);
    glLineWidth(1);
  }

  // Draw rays
  if (show_global_samples) {
    glDisable(GL_LIGHTING);
    glColor3d(1.0, 1.0, 1.0);
    glLineWidth(1);
    DrawGlobalSamples(scene);
    glLineWidth(1);
  }

  // Draw rays
  if (show_caustic_samples) {
    glDisable(GL_LIGHTING);
    glColor3d(1.0, 1.0, 1.0);
    glLineWidth(1);
    DrawCausticSamples(scene);
    glLineWidth(1);
  }

  // Draw scene nodes
  if (show_shapes) {
    glEnable(GL_LIGHTING);
    R3null_material.Draw();
    DrawShapes(scene, scene->Root());
    R3null_material.Draw();
  }

  // Draw bboxes
  if (show_bboxes) {
    glDisable(GL_LIGHTING);
    glColor3d(1.0, 0.0, 0.0);
    DrawBBoxes(scene, scene->Root());
  }

  // Draw frame time
  if (show_frame_rate) {
    char buffer[128];
    static RNTime last_time;
    double frame_time = last_time.Elapsed();
    last_time.Read();
    if ((frame_time > 0) && (frame_time < 10)) {
      glDisable(GL_LIGHTING);
      glColor3d(1.0, 1.0, 1.0);
      sprintf(buffer, "%.1f fps", 1.0 / frame_time);
      DrawText(R2Point(100, 100), buffer);
    }
  }

  // Capture screenshot image
  if (screenshot_image_name) {
    if (print_verbose) printf("Creating image %s\n", screenshot_image_name);
    R2Image image(GLUTwindow_width, GLUTwindow_height, 3);
    image.Capture();
    image.Write(screenshot_image_name);
    screenshot_image_name = NULL;
  }

  // Swap buffers
  glutSwapBuffers();
}
Example #26
0
void GWindow::drawAxes(
    const char* axesColor /* = 0 */,
    bool drawGrid         /* = false */,
    const char* gridColor /* = 0 */
) {
    // Af first, draw a grid
    if (drawGrid) {
        if (gridColor != 0)
            setForeground(gridColor);
        // Vertical grid
        int xmin = (int) ceil(m_RWinRect.left());
        int xmax = (int) floor(m_RWinRect.right());
        int i;
        for (i = xmin; i <= xmax; i++) {
            if (i == 0)
                continue;
            drawLine(
                R2Point((double) i, m_RWinRect.bottom()),
                R2Point((double) i, m_RWinRect.top())
            );
        }
        int ymin = (int) ceil(m_RWinRect.bottom());
        int ymax = (int) floor(m_RWinRect.top());
        for (i = ymin; i <= ymax; i++) {
            if (i == 0)
                continue;
            drawLine(
                R2Point(m_RWinRect.left(), (double) i),
                R2Point(m_RWinRect.right(), (double) i)
            );
        }
    }

    if (axesColor != 0)
        setForeground(axesColor);
    drawLine(R2Point(getXMin(), 0.), R2Point(getXMax(), 0.));
    drawLine(R2Point(0., getYMin()), R2Point(0., getYMax()));

    // Scale
    drawLine(R2Point(1., -0.1), R2Point(1., 0.1));
    drawLine(R2Point(-0.1, 1.), R2Point(0.1, 1.));

    double w = m_RWinRect.width();
    double h = m_RWinRect.height();
    drawString(R2Point(getXMin() + w * 0.9, -h*0.06), "x", 1);
    drawString(R2Point(w * 0.03, getYMin() + h*0.9), "y", 1);
}