Skip to content Skip to sidebar Skip to footer

draw line 3d wits point init and point finish opengl

Tutorial 04 :
Describe simple rotating shapes - Return

  •  Tutorial

Now, we can begin the OpenGl tutorials. All the tutorials will runs eiher with Gl4java, Jogl and Jogl JSR231.

This first tutorials will learn you how to draw simple shapes similar triangles, quads.
By association of unproblematic 2d shapes, we can creates 3D shapes like a pyramid (equanimous of four triangles and one quad) and a cube (composed of 6 quads).
We will as well view how to add colors and see the differents return : point, linear and fill.

  • Introduction

To draw a simple shape like a triangle, we need the coordinates his 3 vertices.
These vertices are marked in white in the following picture :

In a 3D world, coordinates of a vertex are represented by 3 values (10, y, z) in the axis system :

x is the position in the Ten axis, y is the position on the Y axis ...
The origin (the center) of the centrality system is the betoken (0, 0, 0). This point is in the center of the scene (this is done when reseting transformation matrix with glLoadIdentity). Nosotros can apply transformations such every bit translation, rotation and scaling to the axis organisation.

By combining tringles, nosotros can creates the pyramid shown on the screen shot of the tutorial.
For the cube, we volition utilize a combination of quads.

  • The Pyramid

Here is the pyramid represented with its axis organization, vertices are marked with white points :

To describe this pyramid, nosotros will draw each of its iv triangles successively.
The pyramid have five differents vertices that will be chosen V0, V1, V2 ...

  • The Cartoon

The showtime triangle is equanimous of the vertex V0, V1 and V2, which have the coordinates :
V0 = ( 0,  1, 0)
V1 = (-one, -ane, 1)
V2 = ( 0, -1, one)

Each vertices accept a color. The color used by OpenGL is in RGB mode. The color is besides composed of three components : (r, one thousand, b).
Each component defines the percentage of carmine(r), dark-green(thousand) and blueish (b). The value of each component must exist include in the interval [0.0f, 1.0f]
V0 is affected past the color cherry-red   = (1.f, 0.f, 0.f)
V1 is affected past the color greenish = (0.f, 1.f, 0.f)
V2 is affected by the color blueish  = (0.f, 0.f, one.f)

Here is some examples forOther colors :
whith = (1.f, 1.f, one.f)
black = (0.f, 0.f, 0.f)

To draw a vertex (x, y, z), we utilise the OpenGl method :
gl .glVertex3f( x , y , z )
The color affected to this vertex is the electric current color defined. To affect a colour on a vertex, we simply gear up the current color earlier calling glVertex. This is done with :
gl .glColor3f( r , g , b )

The calls glVertex must be placed into a bloc glBegin / glEnd. With this block, we tells OpenGl that want to describe archaic shape.
The kind of shape drawn is specified with the parameter of glBegin. The following picture, taken from The Cherry-red Book, shows all primitive shape with their corresponding parameter :


Motion-picture show Copyrigth @ Addison-Wesley Publishing Company

For drawing a pyramid, we can either apply :

  • GL_TRIANGLE     Draw sucessively 4 independant triangles, this will result on cartoon 12 vertices.

  • GL_TRIANGLE_FAN Take the advantage of common vertices of the pyramid, this volition result on drawing half dozen vertices.

Here is two rendering fashion for displaying a pyramid :

Drawing of the pyramid
Method one :

using GL_TRIANGLES
gl.glBegin(GL_TRIANGLES);

gl.glColor3f(i.0f,0.0f,0.0f); gl.glVertex3f( 0.0f, 1.0f, 0.0f);
gl.glColor3f(0.0f,1.0f,0.0f); gl.glVertex3f(-one.0f,-1.0f, ane.0f);
gl.glColor3f(0.0f,0.0f,i.0f); gl.glVertex3f( 1.0f,-1.0f, 1.0f);

gl.glColor3f(ane.0f,0.0f,0.0f); gl.glVertex3f( 0.0f, 1.0f, 0.0f);
gl.glColor3f(0.0f,0.0f,1.0f); gl.glVertex3f( 1.0f,-1.0f, 1.0f);
gl.glColor3f(0.0f,1.0f,0.0f); gl.glVertex3f( 1.0f,-1.0f,-1.0f);

gl.glColor3f(1.0f,0.0f,0.0f); gl.glVertex3f( 0.0f, 1.0f, 0.0f);
gl.glColor3f(0.0f,1.0f,0.0f); gl.glVertex3f( ane.0f,-1.0f,-1.0f);
gl.glColor3f(0.0f,0.0f,one.0f); gl.glVertex3f(-1.0f,-1.0f,-1.0f);

gl.glColor3f(1.0f,0.0f,0.0f); gl.glVertex3f( 0.0f, 1.0f, 0.0f);
gl.glColor3f(0.0f,0.0f,i.0f); gl.glVertex3f(-1.0f,-1.0f,-one.0f);
gl.glColor3f(0.0f,1.0f,0.0f); gl.glVertex3f(-i.0f,-1.0f, 1.0f);
gl.glEnd();

Method two :

using GL_TRIANGLE_FAN
gl.glBegin(GL_TRIANGLE_FAN);
gl.glColor3f(1.0f,0.0f,0.0f); gl.glVertex3f( 0.0f, 1.0f, 0.0f);
gl.glColor3f(0.0f,1.0f,0.0f); gl.glVertex3f(-1.0f,-i.0f, 1.0f);
gl.glColor3f(0.0f,0.0f,i.0f); gl.glVertex3f( 1.0f,-1.0f, i.0f);
gl.glColor3f(0.0f,1.0f,0.0f); gl.glVertex3f( one.0f,-1.0f,-ane.0f);
gl.glColor3f(0.0f,0.0f,one.0f); gl.glVertex3f(-one.0f,-one.0f,-1.0f);
gl.glColor3f(0.0f,one.0f,0.0f); gl.glVertex3f(-i.0f,-ane.0f, ane.0f);
gl.glEnd();

  •  The Cube

At present the cube, here is the description of the cube that will be fatigued :

Cube rendered with GL_FILL

Cube rendered with a GL_LINE


To draw this shape, you tin can use GL_QUADS or GL_QUAD_STRIP.
You can remarks that I carry of the order of the glVertex calls. The guild defines the face orientation, see Tutorial 5 : Confront orientation for more information on this. One side of the face up is forepart (GL_FRONT), the other is back (GL_BACK).

Cartoon of the cube
Method 1 :

using GL_QUADS
gl.glBegin(GL_QUADS);

gl.glVertex3f( 1.0f, 1.0f, 1.0f);
gl.glVertex3f( one.0f,-ane.0f, 1.0f);
gl.glVertex3f( one.0f,-1.0f,-one.0f);
gl.glVertex3f( 1.0f, 1.0f,-1.0f);

gl.glVertex3f( 1.0f, 1.0f,-ane.0f);
gl.glVertex3f( 1.0f,-1.0f,-1.0f);
gl.glVertex3f(-1.0f,-one.0f,-1.0f);
gl.glVertex3f(-ane.0f, ane.0f,-i.0f);

gl.glVertex3f(-i.0f, 1.0f,-1.0f);
gl.glVertex3f(-ane.0f,-ane.0f,-i.0f);
gl.glVertex3f(-1.0f,-one.0f, one.0f);
gl.glVertex3f(-1.0f, i.0f, 1.0f);

gl.glVertex3f(-one.0f, 1.0f, i.0f);
gl.glVertex3f(-1.0f,-1.0f, one.0f);
gl.glVertex3f( 1.0f,-1.0f, 1.0f);
gl.glVertex3f( 1.0f, 1.0f, 1.0f);

gl.glVertex3f(-1.0f, 1.0f,-1.0f);
gl.glVertex3f(-1.0f, ane.0f, 1.0f);
gl.glVertex3f( 1.0f, 1.0f, i.0f);
gl.glVertex3f( 1.0f, 1.0f,-1.0f);

gl.glVertex3f(-i.0f,-1.0f, ane.0f);
gl.glVertex3f(-1.0f,-ane.0f,-1.0f);
gl.glVertex3f( 1.0f,-1.0f,-one.0f);
gl.glVertex3f( i.0f,-1.0f, 1.0f);
gl.glEnd();

Method 2 :

using GL_QUAD_STRIP
gl.glBegin(GL.GL_QUAD_STRIP);

gl.glVertex3f( one.0f, one.0f, one.0f);
gl.glVertex3f( 1.0f,-1.0f, 1.0f);
gl.glVertex3f( one.0f, 1.0f,-1.0f);
gl.glVertex3f( one.0f,-i.0f,-i.0f);
gl.glVertex3f(-1.0f, one.0f,-1.0f);
gl.glVertex3f(-i.0f,-1.0f,-1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f,-one.0f, one.0f);
gl.glVertex3f( 1.0f, 1.0f, 1.0f);
gl.glVertex3f( 1.0f,-ane.0f, 1.0f);
gl.glEnd();
gl.glBegin(GL.GL_QUADS);

gl.glVertex3f(-i.0f, 1.0f,-1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f( ane.0f, 1.0f, one.0f);
gl.glVertex3f( one.0f, 1.0f,-1.0f);

gl.glVertex3f(-1.0f,-i.0f, 1.0f);
gl.glVertex3f(-1.0f,-i.0f,-one.0f);
gl.glVertex3f( 1.0f,-ane.0f,-1.0f);
gl.glVertex3f( one.0f,-ane.0f, 1.0f);
gl.glEnd();

Remarks :
GL. in forepart of the OpenGl constant is for Jogl (don't demand with Gl4java because GLAnimCanvas extends GLEnum).

  •  Transformations

A transformations is either a translation, a rotation or a scale. The drawing is performed in the transform coordinate system.

We utilise a translation to motion the pyramid to the left and the cube to the right. Without transformatrion, the cube and the pyramid volition be superimposed.
Read Lesson i & Lesson 2 for more informations about transformations.

  •  Translation

Before drawing the objects, we begin to translation the eye of the coordinate organization where the pyramid volition be fatigued.
To position the pyramid to the left, we just take to interpret the axis organization like this :
gl .glTranslatef(xTranslation , yTranslation , zTranslation )
For the pyramid, we translate from these values (-1.5f, 0.0f, -viii.0f)
The coordinate organization eye will exist translated to 1.v units to the left and 8 units backwards. The raison why nosotros translate along z axis is that initialy the coordinate system is on the most aeroplane of the viewing volume. This is better explained in the Lesson 1.

To position the cube, there are differents solutions :

  •  Method ane : translate to the right, symmetrically to the pyramid
    gl.glTranslatef(3.0f, 0.0f, 0.0f)
  •  Method 2 : reset the view and move to the position of the cube
    gl .glLoadIdentity()
    gl.glTranslatef(1.5f, 0.0f, zOffset)

glLoadIdentity reset transformations applyed to the coordinate system.

  •  Rotation

To rotate the system axis around its center, nosotros apply the command :
gl .glRotatef( angle , xAxis , yAxis , zAxis )
The *Centrality parameters is a unit vector defining the axis of rotation (ê on the picture). It represents a unit vector. The angle parameter is the rotation angle in degree (θ on the picture).


Axis/Angle rotation, picture from Wikipedia

Here, we want the shape to rotate around the vertical axis.
Vertical axis is y, and so the direction is (0, one, 0), rotPyramid represents the rotation bending is degree :
gl.glRotatef(rotPyramid, 0.0f, 1.0f, 0.0f)

  •  Advanced Part

  • Render

We have seen 2 differents render in the previous pictures ie confront filled or render as line.

The rendering style is defined with :
gl .glPolygonMode( face, mode )
face parameter is either GL_FRONT_AND_BACK, GL_FRONT or GL_BACK
style parameter is either GL_POINT, GL_LINE or GL_FILL

In this tutorial, shapes are render so :

  • GL_FRONT face seen we are in the outside of the shape.

  • GL_BACK  face seen we are in the  within of the shape.

Nosotros will meet in the Tutorial five : Face Orientation how OpenGl determine the front and the back confront of an object.

  • Hibernate a Face

We can hide a face up (GL_FRONT or GL_BACK) to optimize drawing performance.
For this, enable alternative mode represented equally GL_CULL_FACE. To activate information technology, apply the control glEnable. To face culled is specified with glCullFace.
gl .glCullFace( face )
gl .glEnable( GL_CULL_FACE )

Here is an exemple of a hidding the back face of the previous screen shots :


Scene using glCullFace(GL_BACK)

  • Shading model

This mode defines how the color is propagated around a vertex. To select a mode, nosotros apply :
gl .glShadeModel( mode )
fashion is i of these 2 values :
GL_FLAT   : aforementioned color to all the face
GL_SMOOTH : gradation of colors around the vertex (colors is mixed between 2 vertices)

All the screen shots are taken with a smoothen shading model. The post-obit film uses a apartment rendering :


Pyramid with a GL_FLAT ShadingModel

  •  Line width

We tin can defines the width of the lines with :
gl .glLineWidth( width )

Width is a float that defines the width of the line. The default value is 1.f.

Exemple of a line width equal to 5.f :


Pyramid with a LineWidth equal to v.f

  •  Keys Summary

C : hide/view back face
R : switch between GL_LINE & GL_FILL render
P : pause the rotation of the shapes

  •  Download Department

Think to download the GraphicEngine-1.1.2 to run this tutorial !
  •  Tutorial 4 src (8 ko)

  •  Tutorial 4 jar (6 ko)


If you've got any remarks on this tutorial, delight let me know to improve it.
Cheers for your feedback.

Copyright © 2004-2012 Jérôme Jouvie - All rights reserved. http://jerome.jouvie.gratuitous.fr/

taggartacte1998.blogspot.com

Source: http://jerome.jouvie.free.fr/opengl-tutorials/Tutorial4.php

Post a Comment for "draw line 3d wits point init and point finish opengl"