OpenGL: scale then translate? and how?

前端 未结 3 1567
野的像风
野的像风 2020-12-02 14:42

I\'ve got some 2D geometry. I want to take some bounding rect around my geometry, and then render a smaller version of it somewhere else on the plane. Here\'s more or less t

3条回答
  •  囚心锁ツ
    2020-12-02 15:29

    I haven't played with OpenGL ES, just a bit with OpenGL.

    It sounds like you want to transform from a different position as opposed to the origin, not sure, but can you try to do the transforms and draws that bit within glPushMatrix() and glPopMatrix() ?

    e.g.
    
    // source and dest are arbitrary rectangles.
    float scaleX = dest.width / source.width;
    float scaleY = dest.height / source.height;
    float translateX = dest.x - source.x;
    float translateY = dest.y - source.y;
    
    glPushMatrix();
      glScalef(scaleX, scaleY, 0.0);
      glTranslatef(translateX, translateY, 0.0);
      // Draw geometry in question with its normal verts.
      //as if it were drawn from 0,0
    glPopMatrix();
    

    Here's a simple Processing sketch I wrote to illustrate the point:

    import processing.opengl.*;
    import javax.media.opengl.*;
    
    
    void setup() {
      size(500, 400, OPENGL);
    }
    
    void draw() {
      background(255);
      PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;
      GL gl = pgl.beginGL();  
    
    
      gl.glPushMatrix();
        //transform the 'pivot'
        gl.glTranslatef(100,100,0);
        gl.glScalef(10,10,10);
        //draw something from the 'pivot'
        gl.glColor3f(0, 0.77, 0);
        drawTriangle(gl);
      gl.glPopMatrix();
      //matrix poped, we're back to orginin(0,0,0), continue as normal
      gl.glColor3f(0.77, 0, 0);
      drawTriangle(gl);
      pgl.endGL();
    }
    
    void drawTriangle(GL gl){
      gl.glBegin(GL.GL_TRIANGLES);
      gl.glVertex2i(10, 0);
      gl.glVertex2i(0, 20);
      gl.glVertex2i(20, 20);
      gl.glEnd();
    }
    

    Here is an image of the sketch running, the same green triangle is drawn, with translation and scale applied, then the red one, outsie the push/pop 'block', so it is not affected by the transform:

    alt text

    HTH, George

提交回复
热议问题