Welcome to Timothy's Glide 3.x Tutorial number 2

Q:Where do I get Timothy's Glide 3.x Tutorials ?
A:This document and others can be obtained via the web at http://www.icon.co.za/~tsb

Q:Where can I mail any comments/flames/etc ?
A:All mail can be sent to tsb@icon.co.za

Q:Why did you write these tutorials?
A:Because I couldn't find any descent source or textfiles that worked correctly. And this makes everything very confusing for coders new to glide. Even the Glide3.x SDK has code snippets that don't compile :(

Compile and run the code first and then I'll explain a few things.

    ------------- CUT HERE ----------------

    #include [windows.h]
    #include [conio.h]
    #include [stdio.h]
    #include [glide.h]

    void drawscene2()
    {
    	typedef struct { float x, y, r, g, b, a; } myVertex;
    	myVertex p1,p2,p3;

    	grVertexLayout(GR_PARAM_XY, 0, GR_PARAM_ENABLE);
    	grVertexLayout(GR_PARAM_RGB, 8, GR_PARAM_ENABLE);
    	grVertexLayout(GR_PARAM_A, 12+8, GR_PARAM_ENABLE);

    	p1.x = 0;	p1.y = 0;	p1.r = 255;	p1.g = 0;	p1.b = 0;	p1.a = 255;
    	p2.x = 639;	p2.y = 0;	p2.r = 0;	p2.g = 255;	p2.b = 0;	p2.a = 255;
    	p3.x = 319;	p3.y = 479;	p3.r = 0;	p3.g = 0;	p3.b = 255;	p3.a = 255;
    	grDrawTriangle(&p1, &p2, &p3);

    	p1.x = 0;	p1.y = 0;	p1.r = 255;	p1.g = 0;	p1.b = 0;	p1.a = 255;
    	p2.x = 319;	p2.y = 479;	p2.r = 0;	p2.g = 0;	p2.b = 255;	p2.a = 255;
    	p3.x = 0;	p3.y = 479;	p3.r = 255;	p3.g = 255;	p3.b = 0;	p3.a = 255;
    	grDrawTriangle(&p1, &p2, &p3);

    	p1.x = 639;	p1.y = 0;	p1.r = 0;	p1.g = 255;	p1.b = 0;	p1.a = 255;
    	p2.x = 319;	p2.y = 479;	p2.r = 0;	p2.g = 0;	p2.b = 255;	p2.a = 255;
    	p3.x = 639;	p3.y = 479;	p3.r = 255;	p3.g = 0;	p3.b = 255;	p3.a = 255;
    	grDrawTriangle(&p1, &p2, &p3);
    }

    void drawscene1()
    {
    	typedef struct { float x, y, r, g, b, a; } myVertex;
    	myVertex p1,p2,p3;

    	grVertexLayout(GR_PARAM_XY, 0, GR_PARAM_ENABLE);
    	grVertexLayout(GR_PARAM_RGB, 8, GR_PARAM_ENABLE);
    	grVertexLayout(GR_PARAM_A, 12+8, GR_PARAM_ENABLE);

    	p1.x = 1;	p1.y = 1;	p1.r = 255;	p1.g = 0;	p1.b = 0;	p1.a = 255;
    	p2.x = 640;	p2.y = 1;	p2.r = 0;	p2.g = 255;	p2.b = 0;	p2.a = 255;
    	p3.x = 320;	p3.y = 480;	p3.r = 0;	p3.g = 0;	p3.b = 255;	p3.a = 255;
    	grDrawLine(&p1, &p2);
    	grDrawLine(&p2, &p3);
    	grDrawLine(&p3, &p1);

    	p1.x = 1;	p1.y = 1;	p1.r = 255;	p1.g = 0;	p1.b = 0;	p1.a = 255;
    	p2.x = 320;	p2.y = 480;	p2.r = 0;	p2.g = 0;	p2.b = 255;	p2.a = 255;
    	p3.x = 1;	p3.y = 480;	p3.r = 255;	p3.g = 255;	p3.b = 0;	p3.a = 255;
    	grDrawLine(&p1, &p2);
    	grDrawLine(&p2, &p3);
    	grDrawLine(&p3, &p1);

    	p1.x = 640;	p1.y = 1;	p1.r = 0;	p1.g = 255;	p1.b = 0;	p1.a = 255;
    	p2.x = 320;	p2.y = 480;	p2.r = 0;	p2.g = 0;	p2.b = 255;	p2.a = 255;
    	p3.x = 640;	p3.y = 480;	p3.r = 255;	p3.g = 0;	p3.b = 255;	p3.a = 255;
    	grDrawLine(&p1, &p2);
    	grDrawLine(&p2, &p3);
    	grDrawLine(&p3, &p1);
    }

    int n;
    void main()
    {
    	GrContext_t context;
    	grGlideInit();
    	if (! grGet(GR_NUM_BOARDS, sizeof(n), &n))
    		printf("ERROR: no 3Dfx Interactive Graphics Accelerator!\n");
    	grSstSelect(0);
    	context = grSstWinOpen( 0, GR_RESOLUTION_640x480, GR_REFRESH_85Hz, GR_COLORFORMAT_RGBA, GR_ORIGIN_LOWER_LEFT, 2, 0);
    	//Engine drawing Settings
    	grColorCombine(GR_COMBINE_FUNCTION_LOCAL, GR_COMBINE_FACTOR_NONE, GR_COMBINE_LOCAL_ITERATED, GR_COMBINE_OTHER_NONE, FXFALSE ); //How colors are combined
    	//Clear the back buffer to black */
    	grBufferClear(0, 0, 0);
    	//Program starts here

    	drawscene1(); grBufferSwap( 1 );	getch();
    	drawscene2(); grBufferSwap( 1 );

    	//Program ends here
    	//Wait for keyboard hit to end
    	while ( !kbhit() );
    	grSstWinClose(context);
    	grGlideShutdown();
    }

    ------------- CUT HERE ----------------

grSstSelect(0): Selects which 3DFX card to use. (0 is first one.)
grVertexLayout: This defines how your "vertex" structure looks like. I used x,y,r,g,b,a = r=red, g=green, b=blue, a=alpha Alpha spescifies how transparent something is.

grDrawLine: This draws a line, duh ;)
grDrawTriangle: This draws a triangle =)
grColorCombine: Defines how the "lines", "points" and "triangles" are drawn. Some types are ConstantColor, Iterated, Texture, Texture+Fog, etc Iterated is Gourade Shading ofcourse :)

I used Gourade(Iterated) shading for the 3 lines/triangles I did in DrawScene1/2 You need to specify a color for each vertex you use in your triangles.

grBufferSwap( 1 ): flips the "virtual screen" to your monitor.

Mail me if you want me to explain something in more detail.
But remember the trick is to assume everything until you understand the basics.

-Timothy S. Bowers
tsb@icon.co.za
http://www.icon.co.za/~tsb