Friday, August 24, 2012

OpenGL :: Fundamentals :: Material 2


OpenGL :: Fundamentals :: Material 2

It includes the following topics 
  • Color interpolation
  • Vertices and polygons
  • Functions and types

Color interpolation


In the final screenshot of the previous chapter (colored triangle) you could admire the purity and beauty of the colors in OpenGL.To set the color of a vertex is used:Code: C + +


Select
1
glColor3ub ( red ,  green ,  blue );


Values ​​of red, green and blue are integers between 0 and 255. 
We could both use glColor3f (therefore with color components between 0 and 1), but the value range [0 .. 255] is easier to understand because heavily used in software design. Based on a specific color, to know its RGB components (RGB or English) just use the Windows color palette (eg using Paint).



User Image


A call to glColor affects the current color, which will be used for vertices to be defined later. It can change color at each vertex or just occasionally.

And for the points which are not vertices, how color is defined?

The colors of the points which constitute a face are calculated by interpolation of vertex colors.Quote: Dictionary

Interpolation: assessing the value of a function between two known values.


This therefore gives rise to beautiful gradients between the peaks.

Exercise



Change the code in the previous chapter to draw a rectangle with a gradient blue / red.

Corrected



Code: C + +- Select
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<SDL/SDL.h> # Include 
# include <GL/gl.h> 
# include <GL/glu.h>
 
int  main ( int  argc ,  char  * argv []) 
{ 
    SDL_Init ( SDL_INIT_VIDEO ) 
    SDL_WM_SetCaption ( "nice \ " square \ u00E9 \ " " , NULL ); 
    SDL_SetVideoMode ( 640 ,  480 ,  32 ,  SDL_OPENGL );
 
    bool  continue  =  true ; 
    SDL_Event  event ;
 
    while  ( continue ) 
    { 
        SDL_WaitEvent ( & event ) 
        switch ( event . kind ) 
        { 
            case  SDL_Quit: 
                continue  =  false ; 
        }
 
         glClear ( GL_COLOR_BUFFER_BIT );
 
        glBegin(GL_QUADS);
            glColor3ub(0,0,255);
            glVertex2d(-0.75,-0.75);
            glVertex2d(-0.75,0.75);
            glColor3ub(255,0,0);
            glVertex2d(0.75,0.75);
            glVertex2d(0.75,-0.75);
        glEnd();
 
        glFlush (); 
        SDL_GL_SwapBuffers (); 
    }
 
    SDL_Quit ();
 
    return  0 ; 
}


User Image
Gradient very easily with OpenGL

download the required files ---->>  Click Here




Ah yes but your square is not really square one!?


Indeed default coordinates range from -1 to 1 in both X and Y on whatever the ratio (width / height) of the window. We will see later how to take into account the ratio of the window and change the coordinates min / max. now you can have fun drawing 2D shapes in this range of coordinates. Play with colors, shapes and gradients.

Vertices and polygons

OpenGL is all based on polygons, the basic polygon being used as the triangle.
Even a 3D model is represented with polygons which compose the sides.

User Image
Land wireframe *


* We will see in Part II of this tutorial how to generate a terrain in OpenGL.

To draw a polygon must:
  • define all vertices that compose it;
  • tell OpenGL how it should use its vertices.


Summit says vertex in English, the plural vertices. To stay close to the language and OpenGL 3D world, we will keep the English name. It is more pro, is not it? : Sun:


Let the 1 st line of code barbaric previous chapter:Code: C + +
Select
1
glBegin ( GL_TRIANGLES );


With GL_TRIANGLES tells OpenGL that we must make triangles with vertices that will be reported. Listed below are ways that we can use:



modedetail
GL_POINTSeach vertex is represented as a point
GL_LINESvertices are connected 2-2 to make lines. If we define 4 vertices, 1 st will be connected with 2 nd , 3 e 4 with e , but not the 2 e 3 will be connected to e
GL_LINE_STRIPvertices are connected by lines of the 1 st to the last. If we define 3 vertices, 1 st will be connected with 2 nd and 2 nd with 3 e
GL_LINE_LOOPsame GL_LINE_STRIP , but the last vertex is connected to 1 st (it makes a loop: loop)
GL_TRIANGLEStriples of vertices are used to form triangles. This mode is most often used when we represent complex objects based triangular faces
GL_TRIANGLE_STRIPTriangles touch, that is to say it takes 3 vertices to make a first triangle, and a 4 th vertex is sufficient to define another because the last 2 vertices will also be used
GL_TRIANGLE_FANtriangles touch and share the 1 st vertex set. If we define 4 vertices, 1 st 2 e and 3 e form a triangle, 1 ste and 4 e form a second triangle.
GL_QUADSquadruplets of vertices form quadrilaterals full
GL_QUAD_STRIPas previously, the quadrilaterals are connected, just two new vertices to define a new one, the last 2 previous vertices of the quadrilateral being used
GL_POLYGONall vertices forming a convex polygon (such as a pentagon, or any other polygon having more than four vertices)


Once the drawing mode is set, it must be declared with the vertices glVertex :Code: C + +-Select
1
2
3
glVertex2d ( - 0.75 , - 0.75 ) 
glVertex2d ( 0 , 0.75 ); 
glVertex2d ( 0.75 , - 0.75 );


We earlier, the same OpenGL function here glVertex can be called with a variable number of arguments. Here we use two (hence the 2d), so we define the X and Y from the top and the Z in question is automatically set to 0. Needless to say that the order of defining vertices is important. Must follow the contour of the polygon if you do not want to end up with aberrations. Finally we must close the open block by glBegin with glEnd :Code: C + +-Select




1
glEnd ();


Forget to close a block opened with glBegin by glEnd will block invalid and nothing will be drawn.


Some options



If you have to work with points or lines, you'll want to change the size ... OpenGL has thought of you:Code: C + +-Select
1
2
glPointSize (  size  ); 
glLineWidth (  width  );


size and width are real worth 1.0 by default.

Comparative



To understand the influence of the mode of rendering, here's an animation rendering vertices with six different modes:

User Image


Some methods were not used because they had little interest in this example.
In the mode GL_QUADS as we have 6 vertices, the first 4 form a rectangle, but the other 2 are unused.

Functions and types

In the previous chapter we encountered our first OpenGL functions.
example we had:Code: C + +-Select
1
2
glBegin ( GL_TRIANGLES ) 
glColor3ub ( 255 , 0 , 0 );     glVertex2d ( - 0.75 , - 0.75 );


To differentiate them from other functions, the OpenGL functions have a special syntax in their name:

glNom [NbType] ([Settings]);


I suppose a little explanation of the formula barbaric above is not denial. ;)

  • gl or glu : common prefixes to all OpenGL functions;
  • Name : this is the name of the function as Begin or Vertex ;
  • Nb : for functions with variable number of parameters defines the number of parameters that follow (eg to Begin no forVertex 2);
  • Type : for functions with variable type defines the type of parameters used. We will use include:
    • i for integer (integer);
    • f for float (real);
    • d for double (real precise);
    • ub for unsigned byte (integer between 0 and 255).
  • Parameters : Finally if the function requires parameters they must return.


With this formalism you now understand that glVertex2i is a function that requires two integer parameters. In our code we will encounter on OpenGL constants. They are easily recognized because their name is fully capitalized. Ex: GL_TRUE , GL_FALSEwhich are equivalent to true and false (true and false). If you recall, we have already passed a constant without realizing it :Code: C + +-Select





1
glClear ( GL_COLOR_BUFFER_BIT );


We said here OpenGL to clear the display buffer by passing a parameter to glClear a constant that allows him to know how delete buffer. At any time, do not hesitate to consult the documentation for more details on the use of a function. documention is available on the site of OpenGL

No comments:

Post a Comment

Slider

Image Slider By engineerportal.blogspot.in The slide is a linking image  Welcome to Engineer Portal... #htmlcaption

Tamil Short Film Laptaap

Tamil Short Film Laptaap
Laptapp

Labels

About Blogging (1) Advance Data Structure (2) ADVANCED COMPUTER ARCHITECTURE (4) Advanced Database (4) ADVANCED DATABASE TECHNOLOGY (4) ADVANCED JAVA PROGRAMMING (1) ADVANCED OPERATING SYSTEMS (3) ADVANCED OPERATING SYSTEMS LAB (2) Agriculture and Technology (1) Analag and Digital Communication (1) Android (1) Applet (1) ARTIFICIAL INTELLIGENCE (3) aspiration 2020 (3) assignment cse (12) AT (1) AT - key (1) Attacker World (6) Basic Electrical Engineering (1) C (1) C Aptitude (20) C Program (87) C# AND .NET FRAMEWORK (11) C++ (1) Calculator (1) Chemistry (1) Cloud Computing Lab (1) Compiler Design (8) Computer Graphics Lab (31) COMPUTER GRAPHICS LABORATORY (1) COMPUTER GRAPHICS Theory (1) COMPUTER NETWORKS (3) computer organisation and architecture (1) Course Plan (2) Cricket (1) cryptography and network security (3) CS 810 (2) cse syllabus (29) Cyberoam (1) Data Mining Techniques (5) Data structures (3) DATA WAREHOUSING AND DATA MINING (4) DATABASE MANAGEMENT SYSTEMS (8) DBMS Lab (11) Design and Analysis Algorithm CS 41 (1) Design and Management of Computer Networks (2) Development in Transportation (1) Digital Principles and System Design (1) Digital Signal Processing (15) DISCRETE MATHEMATICS (1) dos box (1) Download (1) ebooks (11) electronic circuits and electron devices (1) Embedded Software Development (4) Embedded systems lab (4) Embedded systems theory (1) Engineer Portal (1) ENGINEERING ECONOMICS AND FINANCIAL ACCOUNTING (5) ENGINEERING PHYSICS (1) english lab (7) Entertainment (1) Facebook (2) fact (31) FUNDAMENTALS OF COMPUTING AND PROGRAMMING (3) Gate (3) General (3) gitlab (1) Global warming (1) GRAPH THEORY (1) Grid Computing (11) hacking (4) HIGH SPEED NETWORKS (1) Horizon (1) III year (1) INFORMATION SECURITY (1) Installation (1) INTELLECTUAL PROPERTY RIGHTS (IPR) (1) Internal Test (13) internet programming lab (20) IPL (1) Java (38) java lab (1) Java Programs (28) jdbc (1) jsp (1) KNOWLEDGE MANAGEMENT (1) lab syllabus (4) MATHEMATICS (3) Mechanical Engineering (1) Microprocessor and Microcontroller (1) Microprocessor and Microcontroller lab (11) migration (1) Mini Projects (1) MOBILE AND PERVASIVE COMPUTING (15) MOBILE COMPUTING (1) Multicore Architecute (1) MULTICORE PROGRAMMING (2) Multiprocessor Programming (2) NANOTECHNOLOGY (1) NATURAL LANGUAGE PROCESSING (1) NETWORK PROGRAMMING AND MANAGEMENT (1) NETWORKPROGNMGMNT (1) networks lab (16) News (14) Nova (1) NUMERICAL METHODS (2) Object Oriented Programming (1) ooad lab (6) ooad theory (9) OPEN SOURCE LAB (22) openGL (10) Openstack (1) Operating System CS45 (2) operating systems lab (20) other (4) parallel computing (1) parallel processing (1) PARALLEL PROGRAMMING (1) Parallel Programming Paradigms (4) Perl (1) Placement (3) Placement - Interview Questions (64) PRINCIPLES OF COMMUNICATION (1) PROBABILITY AND QUEUING THEORY (3) PROGRAMMING PARADIGMS (1) Python (3) Question Bank (1) question of the day (8) Question Paper (13) Question Paper and Answer Key (3) Railway Airport and Harbor (1) REAL TIME SYSTEMS (1) RESOURCE MANAGEMENT TECHNIQUES (1) results (3) semester 4 (5) semester 5 (1) Semester 6 (5) SERVICE ORIENTED ARCHITECTURE (1) Skill Test (1) software (1) Software Engineering (4) SOFTWARE TESTING (1) Structural Analysis (1) syllabus (34) SYSTEM SOFTWARE (1) system software lab (2) SYSTEMS MODELING AND SIMULATION (1) Tansat (2) Tansat 2011 (1) Tansat 2013 (1) TCP/IP DESIGN AND IMPLEMENTATION (1) TECHNICAL ENGLISH (7) Technology and National Security (1) Theory of Computation (3) Thought for the Day (1) Timetable (4) tips (4) Topic Notes (7) tot (1) TOTAL QUALITY MANAGEMENT (4) tutorial (8) Ubuntu LTS 12.04 (1) Unit Wise Notes (1) University Question Paper (1) UNIX INTERNALS (1) UNIX Lab (21) USER INTERFACE DESIGN (3) VIDEO TUTORIALS (1) Virtual Instrumentation Lab (1) Visual Programming (2) Web Technology (11) WIRELESS NETWORKS (1)

LinkWithin