bookmark_borderMatrix in OpenGL

[0  4  8 12]
[1  5  9 13]
[2  6 10 14]
[3  7 11 15]

4x4 matrix in memory.

[R0 R3 R6 Tx]
[R1 R4 R7 Ty]
[R2 R5 R8 Tz]
[ 0  0  0  1]

‘R’ represents rotations and scaling (and shearing)
‘T’ translation

First, I read the matrix and print it.

float x[16];
int i;


//read the 4x4 matrix and store in x
glGetFloatv (GL_MODELVIEW_MATRIX, (float*)x);

//print the matrix
for(i=0; i

bookmark_borderSimple Solar System in OpenGL

I have created a simple Solar System using OpenGL

#include <stdlib.h>
#include <GLUT/glut.h>
#include <math.h>
#include <stdio.h>

static float Xvalue = 0.0, Yvalue = 0.0, Angle = 0.0;

int MoveX = 0;
int MoveY = 0;

void myInit(void) {
 glClearColor (0.0, 0.0, 0.0, 0.0);
}


static float x1[360][2];
static float x2[360][2];
static float x3[720][2];


void generateCircle()
{
 int i = 0;
 
  for(i=0; i <= 360; i++)
  {
   x1[i][0] = sin(i*3.1416/180)*3;
   x1[i][1] = cos(i*3.1416/180)*3;
  }
 
 for(i=0; i <= 360; i++)
 {
  x2[i][0] = sin(i*3.1416/180)*1;
  x2[i][1] = cos(i*3.1416/180)*1;
 }
 
 for(i=0; i <= 720; i++)
 {
  x3[i][0] = sin(i*3.1416/180)*5;
  x3[i][1] = cos(i*3.1416/180)*5;
 }
 
}




void myDisplay(void) {
 
 glClear (GL_COLOR_BUFFER_BIT);
 glColor3f (1.0, 1.0, 1.0);
 
 //sun
 glPushMatrix();
 gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
 glTranslatef(Xvalue, 0.0, Yvalue);
 glRotatef(Angle, 0.0, 0.0, 1.0);
 glutWireSphere (0.5, 15, 15);
 glPopMatrix();
 
 glPushMatrix();
 gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
 if(MoveX==360)
  MoveX = 0;
 glTranslatef(x1[MoveX][1], x1[MoveX][0], 0.0);
 glRotatef(Angle, 0.0, 0.0, 1.0);
 glutWireSphere (0.4, 15, 15);
 glTranslatef(x2[MoveX][0], x2[MoveX][1], 0.0);
 glutWireSphere (0.2, 15, 15);
 glPopMatrix();
 
 glPushMatrix();
 gluLookAt (0.0, 10.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
 if(MoveY==720)
  MoveY = 0;
 glTranslatef(x3[MoveY/2][1], x3[MoveY/2][0], 0.0);
 glRotatef(Angle, 0.0, 0.0, 1.0);
 glutWireSphere (0.4, 15, 15);
 int i = 0;
 //glBegin(GL_LINE_STRIP);
 glBegin(GL_QUAD_STRIP);
 for(i=0; i <= 360; i++)
 {
  glVertex3f(sin(i*3.1416/180)*0.5, cos(i*3.1416/180)*0.5, 0 );
  glVertex3f(sin(i*3.1416/180)*0.7, cos(i*3.1416/180)*0.7, 0 );
 }
 glEnd();
 glRotatef(Angle, 0.0, 0.0, 1.0);
 glPopMatrix();
 
 glFlush ();
}


void resize(int w, int h)
{
 glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
 glMatrixMode (GL_PROJECTION);
 glLoadIdentity ();
 glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
 glMatrixMode (GL_MODELVIEW);
 glLoadIdentity ();
}


void animation()
{
 Angle += 15.0;
 glutPostRedisplay();
 MoveX +=1;
 MoveY +=1;
 glutPostRedisplay();
 glutTimerFunc(100, animation, 0);
 
}


int main(int argc, char ** argv){
 
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
 glutInitWindowSize(1024, 768);
 glutInitWindowPosition(100, 150);
 glutCreateWindow("OpenGL");
 myInit();
 glutDisplayFunc(myDisplay);
 glutReshapeFunc(resize);  
 generateCircle();
 glutTimerFunc(100, animation, 0);
 glutMainLoop();
}

bookmark_borderputpixel in Linux

Some days ago I was trying to read a BMP format file and show it using putpixel.

I haved used qdbmp.sourceforge.net which is a minimalistic cross-platform C library for handling BMP image file.

The Allegro (www.allegro.cc) graphics library that provides many functionality.

putpixel
Writes a pixel into a bitmap.
Description void putpixel(BITMAP *bmp, int x, int y, int color);

Download qdbmp.c qdbmp.h from the qdbmp website

Sample code that reads a BMP file and show it using Allegro library

/*
* Example program for the Allegro library, by Shawn Hargreaves.
*
* This is a very simple program showing how to get into graphics
* mode and draw text onto the screen.
*/

#include <allegro.h>
/* Creates a negative image of the input bitmap file */
#include “qdbmp.h”
#include <stdio.h>

int main( int argc, char* argv[] )
{
BMP* bmp;
UCHAR r, g, b;
UINT width, height;
UINT x, y;

int color;
/* you should always do this at the start of Allegro programs */
if (allegro_init() != 0)
return 1;

/* set up the keyboard handler */
install_keyboard();

/* set a graphics mode sized 320×200 */
if (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0) {
if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message(“Unable to set any graphic moden%sn”, allegro_error);
return 1;
}
}

/* set the color palette */
set_palette(desktop_palette);

/* clear the screen to white */
clear_to_color(screen, makecol(255, 255, 255));

/* you don’t need to do this, but on some platforms (eg. Windows) things
* will be drawn more quickly if you always acquire the screen before
* trying to draw onto it.
*/
acquire_screen();

/* write some text to the screen with black letters and transparent background */
// textout_centre_ex(screen, font, “Hello, world!”, SCREEN_W/2, SCREEN_H/2, makecol(0,0,0), -1);

if ( argc != 3 )
{
fprintf( stderr, “Usage: %s <input file> <output file>n”, argv[ 0 ] );
return 0;
}

/* Read an image file */
bmp = BMP_ReadFile( argv[ 1 ] );
BMP_CHECK_ERROR( stderr, -1 ); /* If an error has occurred, notify and exit */

/* Get image’s dimensions */
width = BMP_GetWidth( bmp );
height = BMP_GetHeight( bmp );

/* Iterate through all the image’s pixels */
for ( x = 0 ; x < width ; ++x )
{
for ( y = 0 ; y < height ; ++y )
{
/* Get pixel’s RGB values */
BMP_GetPixelRGB( bmp, x, y, &r, &g, &b );

color = makecol(r,g,b);

putpixel(screen, x, y, color);

/* Invert RGB values */
BMP_SetPixelRGB( bmp, x, y, 255 – r, 255 – g, 255 – b );
}
}

// putpixel(screen, SCREEN_W/2, SCREEN_H/2, 50);

/* you must always release bitmaps before calling any input functions */
release_screen();

/* wait for a key press */
readkey();

/* Save result */
BMP_WriteFile( bmp, argv[ 2 ] );
BMP_CHECK_ERROR( stderr, -2 );

/* Free all memory allocated for the image */
BMP_Free( bmp );

return 0;
}

END_OF_MAIN()

Compile
$gcc -g -O2 -o name `allegro-config –libs` sample.c qdbmp.c

Resources:
http://qdbmp.sourceforge.net/
http://www.allegro.cc/manual/api/drawing-primitives/putpixel
http://www.allegro.cc/manual/api/truecolor-pixel-formats/makecol