GLBasic forum
Codesnippets => Code Snippets => Topic started by: Kitty Hello on 2006-Oct-12
-
Well, here's a small snippet that shows how to use all the OpenGL calls (in case you're missing something) from GLBasic. It uses the INLINE command, however.
// --------------------------------- //
// 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
BLACKSCREEN
// create 3D viewport + set texture
X_MAKE3D 1,10,45
X_CAMERA 0,0,5, 0,0,-1
X_SETTEXTURE 1,-1
// start OpenGL calls
TryGL()
SHOWSCREEN
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://www.css.taylor.edu/~btoll/resources/graphics/opengl/xp/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();
}
// reasuring the namespace
namespace __GLBASIC__
{
ENDINLINE
FUNCTION TryGL:
INLINE
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);
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
ENDFUNCTION
-
Aha, so thats how its done!
finally a way into the rendering pipeline, cool :D
Thanks Gernot, this little old GLBasic program sure is flexible ;)
-
Here's an update how to diretcly write to texture pixels:
// --------------------------------- //
// 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
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*);
}
// reasuring the namespace
namespace __GLBASIC__
{
ENDINLINE
FUNCTION TryGL:
STATIC tex_id=0
// create a new texture, but only once
IF tex_id = 0 THEN tex_id = glbBuildTexture(256,256);
// do something directly to the texture
glbAlterTexture(tex_id, 256, 256)
INLINE
// use GLBasic built-in texture
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
ENDFUNCTION
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
// 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
-
Again, very handy info.
GLBasic can really do a lot more than its command set initially would reveal.
-
Genot,
Ive had a look at this and im not quite sure i understand whats going on, but hey ho, i`ll carry on racking my brains until i wouk out what is actually happening.
I`ll ask now before I contiue tho. Is this actually creating/modifing a bmp that can be used as a sprite etc, or is it soley a texture for 3d ? (am trying to build bmps on the fly so to speak). And how would I go about thowing variables in and out of the inline code ? ( basically arrays )
Sorry if this is a noddy question, but ive pondered this code for 3 days now and if im not running on the right trcks I`d sooner get off this train and get on the right one.
-
The code above creates an OpenGL texture and binds it to the current state. It does not matter if you use it for 3D or 2D, however the SPRITE command will bind another texture to the state, so, you might be out of luck here. I'll put in some way to give acccess to internal numbers in next update.
For variables, you can use the external names. $ converts to '_Str'. An array index is (a,b) instead of [a] within INLINE.
-
This URL... "http://www.css.taylor.edu/~btoll/resources/graphics/opengl/xp/gl.h" is no longer available.
-
http://mvb.saic.com/freeware/freewarev40/mesa/include/gl/gl.h
-
Ah thank you, so any up to date OpenGL API Header will do?
-
Any ancient one will do. Newer function are available though wglGetProcAddress, only.
-
:(
_______________________________________
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.2007.050 - 3D, NET
Wordcount:18 commands
compiling:
C:/DOCUME~1/Dad/LOCALS~1/Temp/glbasic/gpc_temp0.cpp: In function `DGInt __GLBASIC__::glbBuildTexture(DGInt, DGInt)':
C:/DOCUME~1/Dad/LOCALS~1/Temp/glbasic/gpc_temp0.cpp:221: error: expected primary-expression before "int"
C:/DOCUME~1/Dad/LOCALS~1/Temp/glbasic/gpc_temp0.cpp:221: error: `i' undeclared (first use this function)
C:/DOCUME~1/Dad/LOCALS~1/Temp/glbasic/gpc_temp0.cpp:221: error: (Each undeclared identifier is reported only once for each function it appears in.)
C:/DOCUME~1/Dad/LOCALS~1/Temp/glbasic/gpc_temp0.cpp:221: error: expected `;' before ')' token
C:/DOCUME~1/Dad/LOCALS~1/Temp/glbasic/gpc_temp0.cpp:239: error: `RETURN' undeclared (first use this function)
C:/DOCUME~1/Dad/LOCALS~1/Temp/glbasic/gpc_temp0.cpp:239: error: expected `;' before "TextureID"
*** FATAL ERROR - Please post this output in the forum
_______________________________________
*** Finished ***
Time: 0.8 sec
Build: 0 succeeded
*** 1 FAILED ***
When I try and compile the top demo?
-
oh dear. When copy/pasting, some IF and stuff in the inline section got spelled with capital letters. It's a bug. Better copy the file in notepad and save as .gbas. I'll fix it next release.
-
Ah, I see. OK, not a biggie then, thank you Gernot.
-
Just had a wee play with this... this is incredibly powerful for adding your own OpenGL features that GLBasic does not natively support...! :D
-
// --------------------------------- //
// 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.
-
Hehe, this is an old thread, just trying to get back into GLB... =D
My main worry is cross-platform compatibility, when using inline opengl calls.
If this is an issue what guidelines are there for keeping to the correct calls?
Should I just stick to the OpenGL ES docs as my main source of reference when using inline GL calls? :whistle:
-
My main worry is cross-platform compatibility, when using inline opengl calls.
Thats no problem... ... ?
-
So GLB uses Open ES?, if so, which version is supported? :doubt:
I am not using my own rendering context, I am using GLBasics, its quite important to stick within the bounds of GLBasic here.
-
for iPhone I use EGL, yes. I try to stick with V 1.1.
-
Excellent, thanks Gernot, I'll dig up the 1.1 docs now...
OK, that supports the extension I am after. I will detect the platform and Import the relevant header, it may be beneficial to publish a list of supported GL headers, for in-lining GL on various platforms ;)
-
Yes a nice glb file which inlines ogles would be awesome.
-
In a sense, there should be no need, I would imagine that GLB compiler already links to one, if you could IMPORT (with GLBs default lib paths) the commands that your program required, that would be ideal for me, no worrying about version etc... no real idea if this is really possible though? :P
-
?
http://www.glbasic.com/files/gl.gbas
-
that file errors all over the place
compiling:
D:\temp\glbasic\gpc_temp0.cpp: In function `DGInt __GLBASIC__::glPolygonStipple(__GLBASIC__::DGIntArray&)':
D:\temp\glbasic\gpc_temp0.cpp:442: error: no match for call to `(__GLBASIC__::DGIntArray) (int&, int&)'
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:355: note: candidates are: DGInt& __GLBASIC__::DGIntArray::operator()(int)
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:394: note: __GLBASIC__::DGIntArray& __GLBASIC__::DGIntArray::operator()()
D:\temp\glbasic\gpc_temp0.cpp: In function `DGInt __GLBASIC__::glGetPolygonStipple(__GLBASIC__::DGIntArray&)':
D:\temp\glbasic\gpc_temp0.cpp:470: error: no match for call to `(__GLBASIC__::DGIntArray) (int&, int&)'
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:355: note: candidates are: DGInt& __GLBASIC__::DGIntArray::operator()(int)
C:/Program Files/GLBasic/Compiler/platform/Include/glb.h:394: note: __GLBASIC__::DGIntArray& __GLBASIC__::DGIntArray::operator()()
-
Oooops....
Look at Samples/Common/gl.gbas
works fine!
-
Oooops....
Look at Samples/Common/gl.gbas
works fine!
Did you also test it by creating an IPhone project?
-
That wont work, EGL is not the same as OpenGL, EGL, put simply, allows OpenGLES access to the rendering surfaces of the iPhone... the nearest I can think of a similar library is something like GLUT, in that its there to assist the underlying rendering API, rather than do the actual rendering... the main thing to remember though is that the iphone is using OpenGLES not openGL, two different APIs.
...then again, this is only what I've looked up, I've yet to dabble in the iPhone as I cant afford one yet... :P
-
OPENGLES is a cut down version of OPENGL specifically designed for mobile/handheld devices. http://www.khronos.org/opengles/
A noticeable difference is that you dont have to use glbegin() glend()
lots of other differences of course :)
-
Quite a few things don't work on android because of GLES. Gernot describes that nicely in a topic about deploying to iOS. The same problems happen because of GLES restrictions. I remember that grabsprite, texture bump, shadows, etc. don't work. I found it useful to build all the 3D demo programs to (your device)/my tablet to see what works and what doesn't.
However everything works on a computer thanks to openGL.