Sandundhammika's Blog











hi today I’m going to demonstrate how to write a simple win32 program using C++

to display screen resolution values using a simple Message Box.

Here is the source code.


#include <windows.h>
#include <tchar.h>


TCHAR  gResultOutBuffer [80];

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow)
{
 // get the vertical resolution.
 int vert_res = GetSystemMetrics(SM_CYSCREEN);
 // get the horizontal resolution.
 int hori_res = GetSystemMetrics(SM_CXSCREEN);

 // then create use wsprintf to print it to an
 // appropriate buffer.
 wsprintf(gResultOutBuffer,_T("Resolution is : %dx%d \n" ),hori_res,vert_res );

 // Then Create a MessageBox and show it to the user.
 MessageBox(NULL,gResultOutBuffer,_T("screen resolution"),MB_OK);

 return 0;
}

And here is how you should compile this,

and for more information head to the http://msdn.microsoft.com/en-us/library/ms724385%28VS.85%29.aspx

–Happy Coding–



{April 12, 2010}   OpenGL depth buffer

welll what is the opengl depth buffer

Opengl is supporting the drawing 3D objects. So there is something called depth in the 3rd dimension.

Therefore OpenGL uses this depth buffer to represent the depth information about the each pixel.For

a example consider the bellow code and it’s drawing two triangles with different depths. One is at depth

1.0(red one)  and one is at depth 0.0(green one).So the blue one is on the red one.


glBegin(GL_TRIANGLES);
// set hte color
glColor3f(1.0,0.0,0.0);

// the triangle with the depth 1
glVertex3f(0.6,0.6,1.0);
glVertex3f(0.2,-0.2,1.0);
glVertex3f(-0.8,0.2,1);

// set the second color
glColor3f(0.0,1.0,0.0);
// traingle with the depth 0
glVertex3f(0.2,1.0,0.0);
glVertex3f(0.8,0.0,0.0);
glVertex3f(-0.8,0.0,0.0);
glEnd();

So when OpenGL is drawing each triangle it will compare the current depth buffer value and update if it is occlude

with the new value.So simply it contains the depth value of the pixel from the triangle that is visible.Simply that’s

how it’s working.

To enable this depth buffer you should enable two properties  form the windowing system, and the OpenGL.

If you are using GLUT(GL utility toolkit) then use this function to enable that.


glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA);

and to enable the property on OpenGL you should invoke the bellow line.


glEnable(GL_DEPTH_TEST);

so after that you can use the depth buffer.

here is a simple program that just draw two triangles and demonstrates the depth buffer.


#include <GL/glut.h>
#include <GL/gl.h>
#include <stdio.h>
#include <stdlib.h>


void init()
{
/* performs the init routines for the
* GLUT window
*/
// set the clear color
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
}

void draw_loop()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
// set hte color
glColor3f(1.0,0.0,0.0);

// the triangle with the depth 1
glVertex3f(0.6,0.6,1.0);
glVertex3f(0.2,-0.2,1.0);
glVertex3f(-0.8,0.2,1);

// set the second color
glColor3f(0.0,1.0,0.0);
// traingle with the depth 0
glVertex3f(0.2,1.0,0.0);
glVertex3f(0.8,0.0,0.0);
glVertex3f(-0.8,0.0,0.0);
glEnd();
}

void keyboard_loop(unsigned char key, int x,int y)
{
if(key=='q')
{
printf("Exiting.......\n");
exit(0);
}
}

int main( int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA);
glutInitWindowSize(300,300);
glutInitWindowPosition(100,100);
glutCreateWindow("gl depth buffer testing");
glutDisplayFunc(draw_loop);
glutKeyboardFunc(keyboard_loop);
glutMainLoop();

return 0;
}

To compile this under linux just save the source code as main.cc and invoke these commands on the bash shell.


$gcc -c main.cc

$gcc -o main main.o -lglut

And heres how it works.

Do IT YOURSELF.

and turn off the depth buffer and see what will happens.

–HAPPY CODING–



{April 6, 2010}  

TEXTAREA_ID



et cetera
Follow

Get every new post delivered to your Inbox.