Codesnippets > Code Snippets

OpenGL calls

(1/6) > >>

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 ---// --------------------------------- //
// 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 ;)

Kitty Hello:
Here's an update how to diretcly write to texture pixels:

--- Code: GLBasic ---// --------------------------------- //
// 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.

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.

Navigation

[0] Message Index

[#] Next page

Go to full version