OpenGL calls

Previous topic - Next topic

Kitty Hello

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.

Code (glbasic) Select
// --------------------------------- //
// 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

bigsofty

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 ;)
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Kitty Hello

Here's an update how to diretcly write to texture pixels:
Code (glbasic) Select
// --------------------------------- //
// 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

bigsofty

Again, very handy info.

GLBasic can really do a lot more than its command set initially would reveal.
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Minion

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.

Kitty Hello

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.

bigsofty

Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Kitty Hello


bigsofty

Ah thank you, so any up to date OpenGL API Header will do?
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Kitty Hello

Any ancient one will do. Newer function are available though wglGetProcAddress, only.

bigsofty

:(

Code (glbasic) Select
_______________________________________
*** 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?
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Kitty Hello

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.

bigsofty

Ah, I see. OK, not a biggie then, thank you Gernot.
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

bigsofty

Just had a wee play with this... this is incredibly powerful for adding your own OpenGL features that GLBasic does not natively support...! :D
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Kitty Hello

Code (glbasic) Select
// --------------------------------- //
// 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.