Lets first discuss how OpenGL does. Describing co-ordinates system of OpenGL, center of co-ordinate is middle of widow, positive x-axis is pointed towards right and negative x-axis toward left ,positive y-axis is pointed towards up and negative y-axis towards bottom , positive z-axis is pointed towards outside of screen and negative z-axis towards inside the screen.
OpenGL draw object similar to our eye . By default eye is in origin and we are looking toward negative z-axis.
Basic function of GLFW is describe below.
Before using GLFW any function we have to initialize GLFW by calling
int glfwInit(void)
It initializes the parts of GLFW that are not dependent on a window, such as threading, timer and joystick input. glfwInit returns GL_TRUE if initialization succeeded, or GL_FALSE if it failed.
Opening an OpenGL window is done with the glfwOpenWindow function. The function takes nine
arguments, which are used to describe the following properties of the requested window:
It can be call like this
One of the simplest ways of checking for keyboard input is to use the function glfwGetKey:
c syntax is given below:
Geometric shape in OpenGL is drawn by providing vertices command between primitives.Primitives are specifies like this
glBegin( primitivesType );
…
glEnd();
The vertices command is shown below
PrimitivesType determines how vertices are combined.
Pairs of vertices interpreted as individual line segments
Can specify line width using:
glLineWidth (float width)
glBegin(GL_LINES);
glColor3fv( color );
glVertex2f( P0.x, P0.y );
glVertex2f( P1.x, P1.y );
glVertex2f( P2.x, P2.y );
glVertex2f( P3.x, P3.y );
glVertex2f( P4.x, P4.y );
glVertex2f( P5.x, P5.y );
glVertex2f( P6.x, P6.y );
glVertex2f( P7.x, P7.y );
glEnd();
series of connected line segments
Now lets take a look to the sample source code to draw Basic shapes.
The output of the program is show below:
You can also download the project file From here Download
OpenGL draw object similar to our eye . By default eye is in origin and we are looking toward negative z-axis.
Basic function of GLFW is describe below.
1.Including Header file:
At first we have to include header file of GLFW.//include header file for glfw library so that we can use OpenGL
#include <GL/glfw.h>
2.Initialize GLFW:
Before using GLFW any function we have to initialize GLFW by calling
int glfwInit(void)
It initializes the parts of GLFW that are not dependent on a window, such as threading, timer and joystick input. glfwInit returns GL_TRUE if initialization succeeded, or GL_FALSE if it failed.
3.Opening An OpenGLWindow:
Opening an OpenGL window is done with the glfwOpenWindow function. The function takes nine
arguments, which are used to describe the following properties of the requested window:
- Window dimensions (width and height) in pixels.
- Color and alpha buffer bit depth.
- Depth buffer (Z-buffer) bit depth.
- Stencil buffer bit depth.
- Whether to use fullscreen or windowed mode.
It can be call like this
int glfwOpenWindow( int width, int height,
int redbits, int greenbits, int bluebits,
int alphabits, int depthbits, int stencilbits,
int mode );
4.Using Keyboard Input
One of the simplest ways of checking for keyboard input is to use the function glfwGetKey:
c syntax is given below:
int glfwGetKey( int key )
5.Drawing Geometric shape
Geometric shape in OpenGL is drawn by providing vertices command between primitives.Primitives are specifies like this
glBegin( primitivesType );
…
glEnd();
The vertices command is shown below
PrimitivesType determines how vertices are combined.
6.Vertices and primitives
A.Lines, GL_LINESPairs of vertices interpreted as individual line segments
Can specify line width using:
glLineWidth (float width)
glBegin(GL_LINES);
glColor3fv( color );
glVertex2f( P0.x, P0.y );
glVertex2f( P1.x, P1.y );
glVertex2f( P2.x, P2.y );
glVertex2f( P3.x, P3.y );
glVertex2f( P4.x, P4.y );
glVertex2f( P5.x, P5.y );
glVertex2f( P6.x, P6.y );
glVertex2f( P7.x, P7.y );
glEnd();
B.Line Strip, GL_LINE_STRIP
series of connected line segments
C.Polygon,GL_POLYGON
D.Triangles,GL_TRIANGLES
E.Triangles strips,GL_TRIANGLE_STRIP
F.Triangles Fan,GL_TRIANGLE_STRIP
G.Quads,GL_QUADS
Now lets take a look to the sample source code to draw Basic shapes.
//include header file for glfw library so that we can use OpenGL
#include <GL/glfw.h>
#include <stdlib.h> //needed for exit function
#include <iostream>
using namespace std;
//Initializes 3D rendering
void initializeRendering()
{
glfwInit();
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
}
//Called when a key is pressed
void GLFWCALL handleKeypress(int key,int press) //The key that was pressed
{
switch (key) {
case GLFW_KEY_ESC: //Escape key
exit(0); //Exit the program
}
}
//Called when the window is resized
void GLFWCALL handleResize(int width,int height)
{
//Tell OpenGL how to convert from coordinates to pixel values
glViewport( 0, 0, width, height );
glMatrixMode( GL_PROJECTION ); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //reset the camera
gluPerspective( 45.0f, //camera angle
(GLfloat)width/(GLfloat)height, //The width to height ratio
1.0f, //The near z clipping coordinate
100.0f ); //The far z clipping coordinate
}
void display()
{
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); //clear background screen to black
//Clear information from last draw
glClear( GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glBegin(GL_QUADS); //Begin quadrilateral coordinates
//Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);
glEnd(); //End quadrilateral coordinates
glBegin(GL_TRIANGLES); //Begin triangle coordinates
//Pentagon
glVertex3f(0.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.5f, -5.0f);
//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);
glEnd(); //End triangle coordinates
glfwSwapBuffers();
}
int main()
{
// int width, height;
//int frame = 0;
bool running = true;
initializeRendering();
if( !glfwOpenWindow( 512, // width of window
512, //height of window
1, //redbits
0, //greenbits
0, //bluebits
0, //alphabits
0, //depthbits
0, //stencilbits
GLFW_WINDOW ) //mode
) //return false if window is not created
{
glfwTerminate(); //terminating glfw window
return 0;
}
glfwSetWindowTitle("codeincodeblock.blogspot.com - basic shape");
glfwSetWindowSizeCallback(handleResize); //callback function of GLFW to handle window resize
glfwSetKeyCallback(handleKeypress); //callback function to handle keypress
while(running) // infinite loop to draw object again and again
{ // because once object is draw then window is terminated
display();
running = glfwGetWindowParam( GLFW_OPENED ); //when glfw window is opened then it return true
//if closed then return false
}
return 0;
}
The output of the program is show below:
You can also download the project file From here Download
Hi,
ReplyDeleteInteresting subject.
I tried this code, just copied and pasted,
but I have some compilation errors.
I installed code:block also GLFW.
Can you change your way to share the source codes,
because actually it need to have an account, and I prefer not.
Have a nice day,
here is the compilation messages:
||Warning: .drectve `-aligncomm:"__glfwWin",5 ' unrecognized|
||Warning: .drectve `-aligncomm:"__glfwInput",5 ' unrecognized|
||Warning: .drectve `-aligncomm:"__glfwLibrary",5 ' unrecognized|
||Warning: .drectve `-aligncomm:"__glfwThrd",5' unrecognized|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_throw.o):eh_throw.cc|| undefined reference to `__w32_sharedptr_unexpected'|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_throw.o):eh_throw.cc|| undefined reference to `__w32_sharedptr_terminate'|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_globals.o):eh_globals.cc|| undefined reference to `__w32_sharedptr'|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_globals.o):eh_globals.cc|| undefined reference to `__w32_sharedptr'|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_globals.o):eh_globals.cc|| undefined reference to `__w32_sharedptr'|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_globals.o):eh_globals.cc|| undefined reference to `__w32_sharedptr'|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_globals.o):eh_globals.cc|| undefined reference to `__w32_sharedptr'|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_globals.o):eh_globals.cc|| more undefined references to `__w32_sharedptr' follow|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_terminate.o):eh_terminate.cc|| undefined reference to `__w32_sharedptr_terminate'|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_terminate.o):eh_terminate.cc|| undefined reference to `__w32_sharedptr_unexpected'|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_terminate.o):eh_terminate.cc|| undefined reference to `__w32_sharedptr_terminate'|
E:\CodeBlocks\MinGW\lib\libstdc++.a(eh_terminate.o):eh_terminate.cc|| undefined reference to `__w32_sharedptr_unexpected'|
||=== Build finished: 12 errors, 4 warnings ===|
I have shared the download link with codeblocks project file. Download it and try it if error occur them mail me at smokindinesh@gmail.com. Because I haven't familiar with such type of error. In my windows7 machine it works. fine.
ReplyDeleteHi,
ReplyDeleteAs I said, I saw the file shared,
but 4shared need to be registered to download.
I don't want to register.
Can you share the files on another free place?
It's the same for all lessons.
I'm also on Seven.
;)
I have changed download link form 4shared to Dropbox where you don't have to signup.
ReplyDelete