1

Render triangle:

private void OpenGLControl_OpenGLDraw(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
{
    OpenGL gL = GLControl.OpenGL;

    gL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); 
    gL.LoadIdentity();

    maxX = Math.Max(AX, Math.Max(BX, CX));
    maxY = Math.Max(AY, Math.Max(BY, CY));
    minX = Math.Min(AX, Math.Min(BX, CX));
    minY = Math.Min(AX, Math.Min(BX, CX));
    maxZ = Math.Max(AZ, Math.Max(BZ, CZ));
    minZ = Math.Min(AZ, Math.Min(BZ, CZ));

    double figureWidht = maxX - minX;
    double figureHeight = maxY - minY;
    double figureSquare = figureWidht * figureHeight;


    double viewPortSquare = GLControl.Width * GLControl.Height;

    gL.Translate(-figureWidht / 2, -figureHeight / 2, -6);

    gL.Begin(OpenGL.GL_LINES);
    gL.Color(1.0F, 1.0F, 1.0F);
    gL.Vertex((float)AX, (float)AY, (float)AZ);
    gL.Vertex((float)BX, (float)BY, (float)BZ);
    gL.Vertex((float)BX, (float)BY, (float)BZ);
    gL.Vertex((float)CX, (float)CY, (float)CZ);
    gL.Vertex((float)CX, (float)CY, (float)CZ);
    gL.Vertex((float)AX, (float)AY, (float)AZ);
    gL.End();

}

How to make a figure always fit under the viewport of OpenGLControl? That is, if the figure is larger than the opengl window, then it is adjusted to this window so that all the axes have approximately the same spacing, and if less, it becomes larger accordingly.

I explain on the picture: explain in

xomem
  • 147
  • 7

2 Answers2

0

Under OpengL 2.1 you can use glViewport(...) to define a viewport. As a next step you define a perspective. Under OpengL 2.1 you can use glFrustum(...) or glOrtho(...). In this context you should also take a look on glloadidentity(), glMatrixMode(), glPushMatrix() and glPopMatrix()

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/

Botlin
  • 36
  • 2
0

A mental lapse

I would create a bounding box (calculate the minX, maxX, minY, maxY, minZ, maxZ of the vertices)

Then check the maximum delta for each axis Max(maxX-minX, maxY-minY, maxZ- minZ)

Use this maximum delta to scale the object down with the same delta on all axis (create a scale matrix) scale(1/maxDelta, 1/maxDelta, 1/maxDelta)

Now you object is inside the 1, 1, 1 range. Now you can scale it to the current viewport size.

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • `delta = Math.Max(maxX - minX, Math.Max(maxY -minY, maxZ - minZ)); gL.Translate(-figureWidht / 2, -figureHeight / 2, 0); gL.Scale(1 / delta, 1 / delta, 1 / delta);` All right? I just do not see a triangle, no matter what coordinates I enter. – xomem Aug 02 '18 at 18:27
  • @xomem try: `1.0 / delta`. It's probably the integer division. The code was pseudo... – Jeroen van Langen Aug 03 '18 at 07:57