// --------------------------------- //
// Project: OpenGL - OpenGL calls through INLINE
// Start: Thursday, October 12, 2006
// IDE Version: 3.283
// Make a texture
FILLRECT 0,0,64,64,RGB(0x80, 0x80, 0xff)
PRINT "GLBasic", 1,1
GRABSPRITE 1, 0,0,64,32 // we use this later
GRABSPRITE 2, 0,0,64,32 // we overwrite this later
BLACKSCREEN
SHOWSCREEN
// create 3D viewport + set texture
// start OpenGL calls
WHILE TRUE
X_MAKE3D 1,10,45
X_CAMERA 0,0,5, 0,0,-1
TryGL()
SHOWSCREEN
WEND
MOUSEWAIT
// this function is required to close the MainGame function
@FUNCTION dummy:
ENDFUNCTION
// here we need an inline block _outside_ of functions, that
// gives us some prototypes for glX calls.
INLINE
} // end namespace __GLBASIC__ - see manual for INLINE
// some constants - for more see:
// http://mvb.saic.com/freeware/freewarev40/mesa/include/gl/gl.h
#define GL_QUADS 0x0007
// the functions are all >>extern "C" __stdcall<<
extern "C"
{
void __stdcall glVertex2f(float, float);
void __stdcall glVertex3f(float, float, float);
void __stdcall glColor3f(float, float, float);
void __stdcall glTexCoord2f(float, float);
void __stdcall glBegin(int);
void __stdcall glEnd();
void __stdcall glEnable(int);
void __stdcall glDisable(int);
// direct Texture access
#define GL_UNPACK_ROW_LENGTH 0x0CF2
#define GL_UNPACK_SKIP_ROWS 0x0CF3
#define GL_UNPACK_SKIP_PIXELS 0x0CF4
#define GL_UNPACK_ALIGNMENT 0x0CF5
#define GL_NEAREST 0x2600
#define GL_LINEAR 0x2601
#define GL_CLAMP 0x2900
#define GL_REPEAT 0x2901
#define GL_TEXTURE_MAG_FILTER 0x2800
#define GL_TEXTURE_MIN_FILTER 0x2801
#define GL_TEXTURE_WRAP_S 0x2802
#define GL_TEXTURE_WRAP_T 0x2803
#define GL_TEXTURE_2D 0x0DE1
#define GL_RGB 0x1907
#define GL_RGBA 0x1908
#define GL_RGBA8 0x8058
#define GL_BGRA_EXT 0x80E1
#define GL_UNSIGNED_BYTE 0x1401
void __stdcall glPixelStorei(int, int);
void __stdcall glTexParameterf(int, int, float);
void __stdcall glTexParameteri(int, int, int);
void __stdcall glBindTexture(int, int);
void __stdcall glGenTextures(int, int*);
void __stdcall glTexImage2D(int, int, int, int, int, int, int, int, const void*);
void __stdcall glTexSubImage2D(int, int, int, int, int, int, int, int, const void*);
}
// restore the namespace
namespace __GLBASIC__
{
ENDINLINE
FUNCTION TryGL:
STATIC tex_id=0
// create a new OpenGL texture, but only once
IF tex_id = 0 THEN tex_id = glbBuildTexture(256,256);
// do something directly to the texture-image
// pixels
glbAlterTexture(tex_id, 256, 256)
INLINE
// use GLBasic built-in texture
// we grabbed at the start of the program
X_SETTEXTURE (1,-1);
glBegin(GL_QUADS);
glColor3f(1,1,1);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
glEnd();
// Disable all GLBasic-Texture stuff
// We're walking alone in the woods now
X_SETTEXTURE (-1,-1);
// re-anable texturing
glEnable(GL_TEXTURE_2D);
// bind our own texture
glBindTexture(GL_TEXTURE_2D, tex_id);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
ENDINLINE
// Now access a GLBasic sprite's internal
LOCAL someid
someid = get_sprite_texture(2);
// if it were 0, then you would have to create it with GRABSPRITE first
IF someid THEN glbAlterTexture(someid, 64, 32)
X_MAKE2D
SPRITE 2, 0,0
ENDFUNCTION
// -------------------------------------------------
// Creates a new OpenGL Texture and returns its (internal) ID
// -------------------------------------------------
FUNCTION glbBuildTexture: width, height
INLINE
int TextureID=0;
unsigned char* ubTex=new unsigned char[(int)(width*height*4)];
for(int i=0; i<(int)(width*height*4); ++i) ubTex[i]=64;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, (int)width, (int)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, ubTex);
delete[] ubTex;
return TextureID;
ENDINLINE
ENDFUNCTION
// -------------------------------------------------
// Change the pixels of an !existing! OpenGL texture
// -------------------------------------------------
FUNCTION glbAlterTexture: id, width, height
INLINE
// make space for pixel data
unsigned char* ubTex=new unsigned char[(int)(width*height*4)];
// write some stuff to the texture
unsigned char* pT=ubTex;
int start = GETTIMERALL()/10;
int sc=width;
for(int i=0; i<(int)(width*height); ++i)
{
*pT++ = SIN(start+i/sc )*255;
*pT++ = SIN(start+i/sc+30)*255;
*pT++ = SIN(start+i/sc+60)*255;
*pT++ = 255;
}
// bind this texture to gl context
glBindTexture(GL_TEXTURE_2D, id);
// change the image pixel data for the bound image
glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0,(int)width, (int)height, GL_RGBA, GL_UNSIGNED_BYTE, ubTex);
// free up - needs optimization, of course
delete[] ubTex;
ENDINLINE
ENDFUNCTION
Just updated it for the search feature of the forum.