function Main()
OpenGLTest()
return nil
REQUEST HB_GT_GUI_DEFAULT
PROCEDURE HB_GTSYS()
return
#pragma BEGINDUMP
#include <windows.h>
#include <GL/glut.h>
int hb_cmdargARGC( void );
char ** hb_cmdargARGV( void );
GLuint teapotList;
/*
* Initialize depth buffer, projection matrix, light source, and lighting
* model. Do not specify a material property here.
*/
void init(void)
{
GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat position[] = {0.0, 3.0, 3.0, 0.0};
GLfloat lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0};
GLfloat local_view[] = {0.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
glFrontFace(GL_CW);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
/* be efficient--make teapot display list */
teapotList = glGenLists(1);
glNewList (teapotList, GL_COMPILE);
glutSolidTeapot(1.0);
glEndList ();
}
/*
* Move object into position. Use 3rd through 12th
* parameters to specify the material property. Draw a teapot.
*/
void renderTeapot(GLfloat x, GLfloat y,
GLfloat ambr, GLfloat ambg, GLfloat ambb,
GLfloat difr, GLfloat difg, GLfloat difb,
GLfloat specr, GLfloat specg, GLfloat specb, GLfloat shine)
{
GLfloat mat[4];
glPushMatrix();
glTranslatef(x, y, 0.0);
mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] = 1.0;
glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
mat[0] = difr; mat[1] = difg; mat[2] = difb;
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
mat[0] = specr; mat[1] = specg; mat[2] = specb;
glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
glMaterialf(GL_FRONT, GL_SHININESS, shine * 128.0);
glCallList(teapotList);
glPopMatrix();
}
/**
* First column: emerald, jade, obsidian, pearl, ruby, turquoise
* 2nd column: brass, bronze, chrome, copper, gold, silver
* 3rd column: black, cyan, green, red, white, yellow plastic
* 4th column: black, cyan, green, red, white, yellow rubber
*/
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderTeapot(2.0, 17.0, 0.0215, 0.1745, 0.0215,
0.07568, 0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6);
renderTeapot(2.0, 14.0, 0.135, 0.2225, 0.1575,
0.54, 0.89, 0.63, 0.316228, 0.316228, 0.316228, 0.1);
renderTeapot(2.0, 11.0, 0.05375, 0.05, 0.06625,
0.18275, 0.17, 0.22525, 0.332741, 0.328634, 0.346435, 0.3);
renderTeapot(2.0, 8.0, 0.25, 0.20725, 0.20725,
1, 0.829, 0.829, 0.296648, 0.296648, 0.296648, 0.088);
renderTeapot(2.0, 5.0, 0.1745, 0.01175, 0.01175,
0.61424, 0.04136, 0.04136, 0.727811, 0.626959, 0.626959, 0.6);
renderTeapot(2.0, 2.0, 0.1, 0.18725, 0.1745,
0.396, 0.74151, 0.69102, 0.297254, 0.30829, 0.306678, 0.1);
renderTeapot(6.0, 17.0, 0.329412, 0.223529, 0.027451,
0.780392, 0.568627, 0.113725, 0.992157, 0.941176, 0.807843,
0.21794872);
renderTeapot(6.0, 14.0, 0.2125, 0.1275, 0.054,
0.714, 0.4284, 0.18144, 0.393548, 0.271906, 0.166721, 0.2);
renderTeapot(6.0, 11.0, 0.25, 0.25, 0.25,
0.4, 0.4, 0.4, 0.774597, 0.774597, 0.774597, 0.6);
renderTeapot(6.0, 8.0, 0.19125, 0.0735, 0.0225,
0.7038, 0.27048, 0.0828, 0.256777, 0.137622, 0.086014, 0.1);
renderTeapot(6.0, 5.0, 0.24725, 0.1995, 0.0745,
0.75164, 0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 0.4);
renderTeapot(6.0, 2.0, 0.19225, 0.19225, 0.19225,
0.50754, 0.50754, 0.50754, 0.508273, 0.508273, 0.508273, 0.4);
renderTeapot(10.0, 17.0, 0.0, 0.0, 0.0, 0.01, 0.01, 0.01,
0.50, 0.50, 0.50, .25);
renderTeapot(10.0, 14.0, 0.0, 0.1, 0.06, 0.0, 0.50980392, 0.50980392,
0.50196078, 0.50196078, 0.50196078, .25);
renderTeapot(10.0, 11.0, 0.0, 0.0, 0.0,
0.1, 0.35, 0.1, 0.45, 0.55, 0.45, .25);
renderTeapot(10.0, 8.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0,
0.7, 0.6, 0.6, .25);
renderTeapot(10.0, 5.0, 0.0, 0.0, 0.0, 0.55, 0.55, 0.55,
0.70, 0.70, 0.70, .25);
renderTeapot(10.0, 2.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0,
0.60, 0.60, 0.50, .25);
renderTeapot(14.0, 17.0, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01,
0.4, 0.4, 0.4, .078125);
renderTeapot(14.0, 14.0, 0.0, 0.05, 0.05, 0.4, 0.5, 0.5,
0.04, 0.7, 0.7, .078125);
renderTeapot(14.0, 11.0, 0.0, 0.05, 0.0, 0.4, 0.5, 0.4,
0.04, 0.7, 0.04, .078125);
renderTeapot(14.0, 8.0, 0.05, 0.0, 0.0, 0.5, 0.4, 0.4,
0.7, 0.04, 0.04, .078125);
renderTeapot(14.0, 5.0, 0.05, 0.05, 0.05, 0.5, 0.5, 0.5,
0.7, 0.7, 0.7, .078125);
renderTeapot(14.0, 2.0, 0.05, 0.05, 0.0, 0.5, 0.5, 0.4,
0.7, 0.7, 0.04, .078125);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(0.0, 16.0, 0.0, 16.0*(GLfloat)h/(GLfloat)w,
-10.0, 10.0);
else
glOrtho(0.0, 16.0*(GLfloat)w/(GLfloat)h, 0.0, 16.0,
-10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
/*
* Main Loop
*/
static int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 600);
glutInitWindowPosition(50,50);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}
HB_FUNC( OPENGLTEST )
{
main( hb_cmdargARGC(), hb_cmdargARGV() );
}
#pragma ENDDUMP
function Main()
OpenGLTest()
return nil
REQUEST HB_GT_GUI_DEFAULT
PROCEDURE HB_GTSYS()
return
#pragma BEGINDUMP
#include <windows.h>
#include <GL/glut.h>
int hb_cmdargARGC( void );
char ** hb_cmdargARGV( void );
#include <math.h>
GLfloat ctrlpoints[4][4][3] = {
{{ -1.5, -1.5, 4.0}, { -0.5, -1.5, 2.0},
{0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}},
{{ -1.5, -0.5, 1.0}, { -0.5, -0.5, 3.0},
{0.5, -0.5, 0.0}, {1.5, -0.5, -1.0}},
{{ -1.5, 0.5, 4.0}, { -0.5, 0.5, 0.0},
{0.5, 0.5, 3.0}, {1.5, 0.5, 4.0}},
{{ -1.5, 1.5, -2.0}, { -0.5, 1.5, -2.0},
{0.5, 1.5, 0.0}, {1.5, 1.5, -1.0}}
};
GLfloat texpts[2][2][2] = {{{0.0, 0.0}, {0.0, 1.0}},
{{1.0, 0.0}, {1.0, 1.0}}};
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glEvalMesh2(GL_FILL, 0, 20, 0, 20);
glFlush();
}
#define imageWidth 64
#define imageHeight 64
GLubyte image[3*imageWidth*imageHeight];
void makeImage(void)
{
int i, j;
float ti, tj;
for (i = 0; i < imageWidth; i++) {
ti = 2.0*3.14159265*i/imageWidth;
for (j = 0; j < imageHeight; j++) {
tj = 2.0*3.14159265*j/imageHeight;
image[3*(imageHeight*i+j)] = (GLubyte) 127*(1.0+sin(ti));
image[3*(imageHeight*i+j)+1] = (GLubyte) 127*(1.0+cos(2*tj));
image[3*(imageHeight*i+j)+2] = (GLubyte) 127*(1.0+cos(ti+tj));
}
}
}
void init(void)
{
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
0, 1, 12, 4, &ctrlpoints[0][0][0]);
glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2,
0, 1, 4, 2, &texpts[0][0][0]);
glEnable(GL_MAP2_TEXTURE_COORD_2);
glEnable(GL_MAP2_VERTEX_3);
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
makeImage();
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0,
GL_RGB, GL_UNSIGNED_BYTE, image);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glShadeModel (GL_FLAT);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-4.0, 4.0, -4.0*(GLfloat)h/(GLfloat)w,
4.0*(GLfloat)h/(GLfloat)w, -4.0, 4.0);
else
glOrtho(-4.0*(GLfloat)w/(GLfloat)h,
4.0*(GLfloat)w/(GLfloat)h, -4.0, 4.0, -4.0, 4.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(85.0, 1.0, 1.0, 1.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
static int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
HB_FUNC( OPENGLTEST )
{
main( hb_cmdargARGC(), hb_cmdargARGV() );
}
#pragma ENDDUMP
Configuring Borland C++ 5.5 for GLUT/OpenGL
( This page was formerly in Swedish, but since there also seems to be demand for this kind of information abroad I've decided to both update the information for Borland C++ version 5.5 and the new GLUT version 3.7.6 and translate it to English. )
Requirements:
Microsoft Windows platform
Borland C++ 5.5 , free compiler from Borland (registration required)
The OpenGL Utility Toolkit (GLUT), download the latest binary version.
OpenGL Software Runtime from Microsoft
Steps to install:
Run the OpenGL95.exe file which unzips a number of files in a directory
Copy the opengl32.dll file to \windows\system\ (on Win95/98/ME) or winnt\system32 (on WinNT/2000)
Copy GL.H, GLU.H and GLAUX.H to <borland55 install dir>\include\gl\> (create GL directory if not available)
You need to create libs from these Windows DLLs (libs also downloadable here):
opengl32.dll
glu32.dll
glut32.dll
glut.dll
winmm.dll
Libs are created with the Borland program implib (from the command prompt).
Usage: implib <lib_name>.lib <lib_name>.dll
Copy the libs to <borland55 install dir>\libs\
Now OpenGL/GLUT should work with the Borland C++ compiler!!
To Compile:
To compile a file you may need to include the glut32.lib library:
bcc32 -L <borland55 install dir>\lib\glut32.lib filename.c
Rember that you must add the line
#include <windows.h>
before
#include <GL\glut.h>
I hope these instructions make sense. If you have any questions or comments regarding the configuration (not about OpenGL programming!) please mail me: e95_tni@e.kth.se
function Main()
OpenGLTest()
return nil
REQUEST HB_GT_GUI_DEFAULT
PROCEDURE HB_GTSYS()
return
#pragma BEGINDUMP
#include <windows.h>
#include <GL/glut.h>
int hb_cmdargARGC( void );
char ** hb_cmdargARGV( void );
#include "c:\BITMAP.H"
#include "c:\BITMAP.C"
#define GL_BGR_EXT 0x80E0
BITMAPINFO *TexInfo; /* Texture bitmap information */
GLubyte *TexBits; /* Texture bitmap pixel bits */
void display(void)
{
glClearColor (1.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
TexBits = LoadDIBitmap( "..\\bitmaps\\visual.bmp", &TexInfo);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TexInfo->bmiHeader.biWidth,
TexInfo->bmiHeader.biHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, TexBits);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glColor3f(1.0, 1.0, 1.0);
//se activa el mapeado de texturas
glEnable(GL_TEXTURE_2D);
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0);
glVertex2f(-1.0, -1.0);
glTexCoord2f(1.0, 0.0);
glVertex2f(1.0, -1.0);
glTexCoord2f(1.0, 1.0);
glVertex2f(1.0, 1.0);
glTexCoord2f(0.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers() ;
}
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)height / (GLfloat)width, 1.0, 128.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 1.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
static int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow( "Using textures" );
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
HB_FUNC( OPENGLTEST )
{
main( hb_cmdargARGC(), hb_cmdargARGV() );
}
#pragma ENDDUMP
/*
* Windows BMP file functions for OpenGL.
*
* Written by Michael Sweet.
*/
#include "bitmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#ifdef WIN32
/*
* 'LoadDIBitmap()' - Load a DIB/BMP file from disk.
*
* Returns a pointer to the bitmap if successful, NULL otherwise...
*/
GLubyte * /* O - Bitmap data */
LoadDIBitmap(const char *filename, /* I - File to load */
BITMAPINFO **info) /* O - Bitmap information */
{
FILE *fp; /* Open file pointer */
GLubyte *bits; /* Bitmap pixel bits */
int bitsize; /* Size of bitmap */
int infosize; /* Size of header information */
BITMAPFILEHEADER header; /* File header */
/* Try opening the file; use "rb" mode to read this *binary* file. */
if ((fp = fopen(filename, "rb")) == NULL)
return (NULL);
/* Read the file header and any following bitmap information... */
if (fread(&header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)
{
/* Couldn't read the file header - return NULL... */
fclose(fp);
return (NULL);
}
if (header.bfType != 'MB') /* Check for BM reversed... */
{
/* Not a bitmap file - return NULL... */
fclose(fp);
return (NULL);
}
infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
{
/* Couldn't allocate memory for bitmap info - return NULL... */
fclose(fp);
return (NULL);
}
if (fread(*info, 1, infosize, fp) < infosize)
{
/* Couldn't read the bitmap header - return NULL... */
free(*info);
fclose(fp);
return (NULL);
}
/* Now that we have all the header info read in, allocate memory for *
* the bitmap and read *it* in... */
if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
bitsize = ((*info)->bmiHeader.biWidth *
(*info)->bmiHeader.biBitCount + 7) / 8 *
abs((*info)->bmiHeader.biHeight);
if ((bits = malloc(bitsize)) == NULL)
{
/* Couldn't allocate memory - return NULL! */
free(*info);
fclose(fp);
return (NULL);
}
if (fread(bits, 1, bitsize, fp) < bitsize)
{
/* Couldn't read bitmap - free memory and return NULL! */
free(*info);
free(bits);
fclose(fp);
return (NULL);
}
/* OK, everything went fine - return the allocated bitmap... */
fclose(fp);
return (bits);
}
/*
* 'SaveDIBitmap()' - Save a DIB/BMP file to disk.
*
* Returns 0 on success or -1 on failure...
*/
int /* O - 0 = success, -1 = failure */
SaveDIBitmap(const char *filename, /* I - File to load */
BITMAPINFO *info, /* I - Bitmap information */
GLubyte *bits) /* I - Bitmap data */
{
FILE *fp; /* Open file pointer */
int size, /* Size of file */
infosize, /* Size of bitmap info */
bitsize; /* Size of bitmap pixels */
BITMAPFILEHEADER header; /* File header */
/* Try opening the file; use "wb" mode to write this *binary* file. */
if ((fp = fopen(filename, "wb")) == NULL)
return (-1);
/* Figure out the bitmap size */
if (info->bmiHeader.biSizeImage == 0)
bitsize = (info->bmiHeader.biWidth *
info->bmiHeader.biBitCount + 7) / 8 *
abs(info->bmiHeader.biHeight);
else
bitsize = info->bmiHeader.biSizeImage;
/* Figure out the header size */
infosize = sizeof(BITMAPINFOHEADER);
switch (info->bmiHeader.biCompression)
{
case BI_BITFIELDS :
infosize += 12; /* Add 3 RGB doubleword masks */
if (info->bmiHeader.biClrUsed == 0)
break;
case BI_RGB :
if (info->bmiHeader.biBitCount > 8 &&
info->bmiHeader.biClrUsed == 0)
break;
case BI_RLE8 :
case BI_RLE4 :
if (info->bmiHeader.biClrUsed == 0)
infosize += (1 << info->bmiHeader.biBitCount) * 4;
else
infosize += info->bmiHeader.biClrUsed * 4;
break;
}
size = sizeof(BITMAPFILEHEADER) + infosize + bitsize;
/* Write the file header, bitmap information, and bitmap pixel data... */
header.bfType = 'MB'; /* Non-portable... sigh */
header.bfSize = size;
header.bfReserved1 = 0;
header.bfReserved2 = 0;
header.bfOffBits = sizeof(BITMAPFILEHEADER) + infosize;
if (fwrite(&header, 1, sizeof(BITMAPFILEHEADER), fp) < sizeof(BITMAPFILEHEADER))
{
/* Couldn't write the file header - return... */
fclose(fp);
return (-1);
}
if (fwrite(info, 1, infosize, fp) < infosize)
{
/* Couldn't write the bitmap header - return... */
fclose(fp);
return (-1);
}
if (fwrite(bits, 1, bitsize, fp) < bitsize)
{
/* Couldn't write the bitmap - return... */
fclose(fp);
return (-1);
}
/* OK, everything went fine - return... */
fclose(fp);
return (0);
}
#else /* !WIN32 */
/*
* Functions for reading and writing 16- and 32-bit little-endian integers.
*/
static unsigned short read_word(FILE *fp);
static unsigned int read_dword(FILE *fp);
static int read_long(FILE *fp);
static int write_word(FILE *fp, unsigned short w);
static int write_dword(FILE *fp, unsigned int dw);
static int write_long(FILE *fp, int l);
/*
* 'LoadDIBitmap()' - Load a DIB/BMP file from disk.
*
* Returns a pointer to the bitmap if successful, NULL otherwise...
*/
GLubyte * /* O - Bitmap data */
LoadDIBitmap(const char *filename, /* I - File to load */
BITMAPINFO **info) /* O - Bitmap information */
{
FILE *fp; /* Open file pointer */
GLubyte *bits; /* Bitmap pixel bits */
GLubyte *ptr; /* Pointer into bitmap */
GLubyte temp; /* Temporary variable to swap red and blue */
int x, y; /* X and Y position in image */
int length; /* Line length */
int bitsize; /* Size of bitmap */
int infosize; /* Size of header information */
BITMAPFILEHEADER header; /* File header */
/* Try opening the file; use "rb" mode to read this *binary* file. */
if ((fp = fopen(filename, "rb")) == NULL)
return (NULL);
/* Read the file header and any following bitmap information... */
header.bfType = read_word(fp);
header.bfSize = read_dword(fp);
header.bfReserved1 = read_word(fp);
header.bfReserved2 = read_word(fp);
header.bfOffBits = read_dword(fp);
if (header.bfType != BF_TYPE) /* Check for BM reversed... */
{
/* Not a bitmap file - return NULL... */
fclose(fp);
return (NULL);
}
infosize = header.bfOffBits - 18;
if ((*info = (BITMAPINFO *)malloc(sizeof(BITMAPINFO))) == NULL)
{
/* Couldn't allocate memory for bitmap info - return NULL... */
fclose(fp);
return (NULL);
}
(*info)->bmiHeader.biSize = read_dword(fp);
(*info)->bmiHeader.biWidth = read_long(fp);
(*info)->bmiHeader.biHeight = read_long(fp);
(*info)->bmiHeader.biPlanes = read_word(fp);
(*info)->bmiHeader.biBitCount = read_word(fp);
(*info)->bmiHeader.biCompression = read_dword(fp);
(*info)->bmiHeader.biSizeImage = read_dword(fp);
(*info)->bmiHeader.biXPelsPerMeter = read_long(fp);
(*info)->bmiHeader.biYPelsPerMeter = read_long(fp);
(*info)->bmiHeader.biClrUsed = read_dword(fp);
(*info)->bmiHeader.biClrImportant = read_dword(fp);
if (infosize > 40)
if (fread((*info)->bmiColors, infosize - 40, 1, fp) < 1)
{
/* Couldn't read the bitmap header - return NULL... */
free(*info);
fclose(fp);
return (NULL);
}
/* Now that we have all the header info read in, allocate memory for *
* the bitmap and read *it* in... */
if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
bitsize = ((*info)->bmiHeader.biWidth *
(*info)->bmiHeader.biBitCount + 7) / 8 *
abs((*info)->bmiHeader.biHeight);
if ((bits = malloc(bitsize)) == NULL)
{
/* Couldn't allocate memory - return NULL! */
free(*info);
fclose(fp);
return (NULL);
}
if (fread(bits, 1, bitsize, fp) < bitsize)
{
/* Couldn't read bitmap - free memory and return NULL! */
free(*info);
free(bits);
fclose(fp);
return (NULL);
}
/* Swap red and blue */
length = ((*info)->bmiHeader.biWidth * 3 + 3) & ~3;
for (y = 0; y < (*info)->bmiHeader.biHeight; y ++)
for (ptr = bits + y * length, x = (*info)->bmiHeader.biWidth;
x > 0;
x --, ptr += 3)
{
temp = ptr[0];
ptr[0] = ptr[2];
ptr[2] = temp;
}
/* OK, everything went fine - return the allocated bitmap... */
fclose(fp);
return (bits);
}
/*
* 'SaveDIBitmap()' - Save a DIB/BMP file to disk.
*
* Returns 0 on success or -1 on failure...
*/
int /* O - 0 = success, -1 = failure */
SaveDIBitmap(const char *filename, /* I - File to load */
BITMAPINFO *info, /* I - Bitmap information */
GLubyte *bits) /* I - Bitmap data */
{
FILE *fp; /* Open file pointer */
int size, /* Size of file */
infosize, /* Size of bitmap info */
bitsize; /* Size of bitmap pixels */
/* Try opening the file; use "wb" mode to write this *binary* file. */
if ((fp = fopen(filename, "wb")) == NULL)
return (-1);
/* Figure out the bitmap size */
if (info->bmiHeader.biSizeImage == 0)
bitsize = (info->bmiHeader.biWidth *
info->bmiHeader.biBitCount + 7) / 8 *
abs(info->bmiHeader.biHeight);
else
bitsize = info->bmiHeader.biSizeImage;
/* Figure out the header size */
infosize = sizeof(BITMAPINFOHEADER);
switch (info->bmiHeader.biCompression)
{
case BI_BITFIELDS :
infosize += 12; /* Add 3 RGB doubleword masks */
if (info->bmiHeader.biClrUsed == 0)
break;
case BI_RGB :
if (info->bmiHeader.biBitCount > 8 &&
info->bmiHeader.biClrUsed == 0)
break;
case BI_RLE8 :
case BI_RLE4 :
if (info->bmiHeader.biClrUsed == 0)
infosize += (1 << info->bmiHeader.biBitCount) * 4;
else
infosize += info->bmiHeader.biClrUsed * 4;
break;
}
size = sizeof(BITMAPFILEHEADER) + infosize + bitsize;
/* Write the file header, bitmap information, and bitmap pixel data... */
write_word(fp, BF_TYPE); /* bfType */
write_dword(fp, size); /* bfSize */
write_word(fp, 0); /* bfReserved1 */
write_word(fp, 0); /* bfReserved2 */
write_dword(fp, 18 + infosize); /* bfOffBits */
write_dword(fp, info->bmiHeader.biSize);
write_long(fp, info->bmiHeader.biWidth);
write_long(fp, info->bmiHeader.biHeight);
write_word(fp, info->bmiHeader.biPlanes);
write_word(fp, info->bmiHeader.biBitCount);
write_dword(fp, info->bmiHeader.biCompression);
write_dword(fp, info->bmiHeader.biSizeImage);
write_long(fp, info->bmiHeader.biXPelsPerMeter);
write_long(fp, info->bmiHeader.biYPelsPerMeter);
write_dword(fp, info->bmiHeader.biClrUsed);
write_dword(fp, info->bmiHeader.biClrImportant);
if (infosize > 40)
if (fwrite(info->bmiColors, infosize - 40, 1, fp) < 1)
{
/* Couldn't write the bitmap header - return... */
fclose(fp);
return (-1);
}
if (fwrite(bits, 1, bitsize, fp) < bitsize)
{
/* Couldn't write the bitmap - return... */
fclose(fp);
return (-1);
}
/* OK, everything went fine - return... */
fclose(fp);
return (0);
}
/*
* 'read_word()' - Read a 16-bit unsigned integer.
*/
static unsigned short /* O - 16-bit unsigned integer */
read_word(FILE *fp) /* I - File to read from */
{
unsigned char b0, b1; /* Bytes from file */
b0 = getc(fp);
b1 = getc(fp);
return ((b1 << 8) | b0);
}
/*
* 'read_dword()' - Read a 32-bit unsigned integer.
*/
static unsigned int /* O - 32-bit unsigned integer */
read_dword(FILE *fp) /* I - File to read from */
{
unsigned char b0, b1, b2, b3; /* Bytes from file */
b0 = getc(fp);
b1 = getc(fp);
b2 = getc(fp);
b3 = getc(fp);
return ((((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
}
/*
* 'read_long()' - Read a 32-bit signed integer.
*/
static int /* O - 32-bit signed integer */
read_long(FILE *fp) /* I - File to read from */
{
unsigned char b0, b1, b2, b3; /* Bytes from file */
b0 = getc(fp);
b1 = getc(fp);
b2 = getc(fp);
b3 = getc(fp);
return ((int)(((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
}
/*
* 'write_word()' - Write a 16-bit unsigned integer.
*/
static int /* O - 0 on success, -1 on error */
write_word(FILE *fp, /* I - File to write to */
unsigned short w) /* I - Integer to write */
{
putc(w, fp);
return (putc(w >> 8, fp));
}
/*
* 'write_dword()' - Write a 32-bit unsigned integer.
*/
static int /* O - 0 on success, -1 on error */
write_dword(FILE *fp, /* I - File to write to */
unsigned int dw) /* I - Integer to write */
{
putc(dw, fp);
putc(dw >> 8, fp);
putc(dw >> 16, fp);
return (putc(dw >> 24, fp));
}
/*
* 'write_long()' - Write a 32-bit signed integer.
*/
static int /* O - 0 on success, -1 on error */
write_long(FILE *fp, /* I - File to write to */
int l) /* I - Integer to write */
{
putc(l, fp);
putc(l >> 8, fp);
putc(l >> 16, fp);
return (putc(l >> 24, fp));
}
#endif /* WIN32 */
/*
* Windows BMP file definitions for OpenGL.
*
* Written by Michael Sweet.
*/
#ifndef _BITMAP_H_
# define _BITMAP_H_
/*
* Include necessary headers.
*/
# include <GL/glut.h>
# ifdef WIN32
# include <windows.h>
# include <wingdi.h>
# endif /* WIN32 */
/*
* Make this header file work with C and C++ source code...
*/
# ifdef __cplusplus
extern "C" {
# endif /* __cplusplus */
/*
* Bitmap file data structures (these are defined in <wingdi.h> under
* Windows...)
*
* Note that most Windows compilers will pack the following structures, so
* when reading them under MacOS or UNIX we need to read individual fields
* to avoid differences in alignment...
*/
# ifndef WIN32
typedef struct /**** BMP file header structure ****/
{
unsigned short bfType; /* Magic number for file */
unsigned int bfSize; /* Size of file */
unsigned short bfReserved1; /* Reserved */
unsigned short bfReserved2; /* ... */
unsigned int bfOffBits; /* Offset to bitmap data */
} BITMAPFILEHEADER;
# define BF_TYPE 0x4D42 /* "MB" */
typedef struct /**** BMP file info structure ****/
{
unsigned int biSize; /* Size of info header */
int biWidth; /* Width of image */
int biHeight; /* Height of image */
unsigned short biPlanes; /* Number of color planes */
unsigned short biBitCount; /* Number of bits per pixel */
unsigned int biCompression; /* Type of compression to use */
unsigned int biSizeImage; /* Size of image data */
int biXPelsPerMeter; /* X pixels per meter */
int biYPelsPerMeter; /* Y pixels per meter */
unsigned int biClrUsed; /* Number of colors used */
unsigned int biClrImportant; /* Number of important colors */
} BITMAPINFOHEADER;
/*
* Constants for the biCompression field...
*/
# define BI_RGB 0 /* No compression - straight BGR data */
# define BI_RLE8 1 /* 8-bit run-length compression */
# define BI_RLE4 2 /* 4-bit run-length compression */
# define BI_BITFIELDS 3 /* RGB bitmap with RGB masks */
typedef struct /**** Colormap entry structure ****/
{
unsigned char rgbBlue; /* Blue value */
unsigned char rgbGreen; /* Green value */
unsigned char rgbRed; /* Red value */
unsigned char rgbReserved; /* Reserved */
} RGBQUAD;
typedef struct /**** Bitmap information structure ****/
{
BITMAPINFOHEADER bmiHeader; /* Image header */
RGBQUAD bmiColors[256]; /* Image colormap */
} BITMAPINFO;
# endif /* !WIN32 */
/*
* Prototypes...
*/
extern GLubyte *LoadDIBitmap(const char *filename, BITMAPINFO **info);
extern int SaveDIBitmap(const char *filename, BITMAPINFO *info,
GLubyte *bits);
# ifdef __cplusplus
}
# endif /* __cplusplus */
#endif /* !_BITMAP_H_ */
El exito de un producto web es que permite llevar las applicaciones a la web, tal cual existen. Es algo muy ambicioso y posiblemente una utopia, pero de intentarlo, ese es el camino. Asi lo veo yo
function Find()
{
oDlg = new Dialog( "Find", 500, 200 );
Say( 60, 20, "Find what:", oDlg );
Get( 56, 110, "FiveWin Web Interface ", oDlg, 360, 30 );
Button( 140, 150, "Ok", oDlg, 100, 40, "oDlg.End()" );
Button( 140, 270, "Cancel", oDlg, 100, 40, "oDlg.End()" );
}
Return to FiveWin para Harbour/xHarbour
Users browsing this forum: No registered users and 142 guests