R: [Plib-users] Drawing a simple triangle
Brought to you by:
sjbaker
From: Paolo L. <p.l...@ci...> - 2004-03-08 15:44:46
|
Peter, Ok, one step forward - let's set a state for the geometry (leaf) node. For a leaf (ssgVTable, ssgVtxTable, or ssgVtxArray) a state tells OpenGL about object material (properties, texture) as well as modes to enable/disable. The following code generates and returns a state with safe defaults: ssgSimpleState *gen_state( char *tex_name ) { ssgSimpleState *st =3D new ssgSimpleState () ; // st -> setName( mat -> name ); st -> setMaterial ( GL_AMBIENT, 1.0f, 1.0f, 1.0f, 1.0f ); st -> setMaterial ( GL_DIFFUSE, 1.0f, 1.0f, 1.0f, 1.0f ); st -> setMaterial ( GL_SPECULAR, 0, 0, 0, 1 ); st -> setMaterial ( GL_EMISSION, 0, 0, 0, 1 ); st -> setShadeModel( GL_SMOOTH ); st -> disable( GL_BLEND ); st -> setOpaque(); if ( tex_name !=3D NULL ) { ssgTexture *tex =3D new ssgTexture(tex_name, 0, 0, 1); if ( tex ) st -> setTexture( tex ); else fprintf( stderr, "can't load texture from image file %s\n", tex_name ); st -> enable( GL_TEXTURE_2D ); } else st -> disable( GL_TEXTURE_2D ); return st ; } Such a state is ready to be set for the leaf n you generated: ssgSimpleState *s =3D gen_state( NULL /* tex_name */ ); // you have set null tex coords, so it's better to not map any texture n -> setState( s ); s -> enable( GL_LIGHTING ); // or could disable it if you supply good normals s -> disable( GL_COLOR_MATERIAL ); // or could enable it if you supply per-vertex colors Since the default in SSG is to enable back face culling, at our stage is safer to disable it for our unknown-winding triangle: n -> setCullFace( FALSE ); Note that face culling is set through a leaf method, not a state one. Looking forward to hear about a "triangle born" - Paolo > -----Messaggio originale----- > Da: pli...@li...=20 > [mailto:pli...@li...] Per conto di=20 > Peter Poulsen > Inviato: luned=EC 8 marzo 2004 15.56 > A: pli...@li... > Oggetto: Re: [Plib-users] Drawing a simple triangle >=20 >=20 > "Paolo Leoncini" <p.l...@ci...> writes: >=20 > > Peter, > >=20 > > on my opinion you just missed to set up a camera for=20 > looking at scene=20 > > objects. It can be done this way: > >=20 > > #include <plib/sg.h> > >=20 > > sgCoord camera; > > SGfloat pos_x, pos_y, pos_z, yaw, pitch, roll; > >=20 > > // set pos_x, pos_y, pos_z, yaw, pitch and roll to some=20 > suitable=20 > > values > > // then, before ssgCullAndDraw, actually set the SSG camera > > sgSetCoord( camera, pos_x, pos_y, pos_z, yaw, pitch, roll ); > > ssgSetCamera( &camera ); > >=20 > > SG is the prefix for the Plib math/geometry library stuffs. > >=20 > > Finding a plausible position for the init view (given a scene, in=20 > > general, you could not know its extent along the three axes) it's a=20 > > question of taste - I could suggest this: > >=20 > > sgSphere *sp =3D scene->getBSphere(); > > SGfloat radius =3D sp->getRadius(); > > SGfloat EyeDist =3D float( radius * 1.f / tan( float( fov_x/2 *=20 > > SG_DEGREES_TO_RADIANS ) ) ); > > SGfloat Ex, Ey, Ez; > >=20 > > pos_x =3D sp->getCenter()[0]; > > pos_y =3D sp->getCenter()[1] + EyeDist; > > pos_z =3D sp->getCenter()[2]; > >=20 > > Then, moving in 3D above or around the scene is a matter of=20 > changing=20 > > those six parameters to the sgSetCoord/ssgSetCamera (yaw is compass=20 > > heading, pitch rotates around the screen x-axis, roll=20 > around the axis=20 > > norma to the screen). > >=20 > Thanks for your contribution. Unfortunately it still does not=20 > work :-( It may of course be that I have misunderstood your=20 > suggestion, in particular I am not sure that the yaw is correct. >=20 > --- ssg_triangle.cc --- >=20 > #define USE_SSG 1 >=20 > #include <GL/gl.h> > #include <GL/glu.h> >=20 > #include <plib/ssg.h> > #include <SDL/SDL.h> > #include <iostream> >=20 > using namespace std; >=20 > ssgRoot* scene; > sgCoord camera; > SGfloat pos_x, pos_y, pos_z, yaw =3D 0.0f, pitch =3D 0.0f, roll =3D = 0.0f; >=20 >=20 > #define SCREEN_WIDTH 320 > #define SCREEN_HEIGHT 240 > #define SCREEN_BPP 24 >=20 > void init() > { > int videoFlags; > SDL_Surface* surface; >=20 > if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { > fprintf( stderr, "Video initialization failed:=20 > %s\n", SDL_GetError( ) ); > exit(0); > } >=20 > videoFlags =3D SDL_OPENGL; > videoFlags |=3D SDL_GL_DOUBLEBUFFER; > videoFlags |=3D SDL_HWPALETTE; =20 > videoFlags |=3D SDL_HWSURFACE; > surface =3D SDL_SetVideoMode( SCREEN_WIDTH,=20 > SCREEN_HEIGHT, SCREEN_BPP, videoFlags ); >=20 > if ( !surface ) { > fprintf( stderr, "Video mode set failed:=20 > %s\n", SDL_GetError( ) ); > exit(0); > } >=20 > #ifdef USE_SSG > ssgInit(); > =09 > glClearColor(0.0f, 0.0f, 0.0f, 0.0f); > glEnable(GL_DEPTH_TEST); >=20 > ssgVertexArray* vertices =3D new ssgVertexArray; > sgVec3 x =3D { 0.5f, 2.5f , -0.25f }; > sgVec3 y =3D { -0.5f, 2.5f , -0.25f }; > sgVec3 z =3D { 0.0f, 1.5f , 0.75f };=20 > vertices->add(x); > vertices->add(y); > vertices->add(z); >=20 > sgVec4 white =3D { 1.0f, 1.0f, 1.0f, 0.5f }; > ssgColourArray* colours =3D new ssgColourArray; > colours->add(white); > colours->add(white); > colours->add(white); > ssgNormalArray* normals =3D new ssgNormalArray(3); > sgVec3 normal =3D { 0, -1.0f, 0 }; > normals->add(normal); > normals->add(normal); > normals->add(normal); > ssgTexCoordArray* texs =3D new ssgTexCoordArray(3); > sgVec2 tex_vec =3D {0,0}; > texs->add(tex_vec); > texs->add(tex_vec); > texs->add(tex_vec); > =09 > ssgVtxTable* n =3D new ssgVtxTable(GL_TRIANGLES,=20 > vertices, normals, texs, colours); > ssgTransform* trans =3D new ssgTransform; > trans->addKid(n); >=20 > scene =3D new ssgRoot; > scene->addKid(trans); >=20 > sgSphere *sp =3D scene->getBSphere(); > SGfloat radius =3D sp->getRadius(); > SGfloat EyeDist =3D float( radius * 1.f / tan( float(=20 > SCREEN_WIDTH/2 * SG_DEGREES_TO_RADIANS ) ) ); > SGfloat Ex, Ey, Ez; >=20 > pos_x =3D sp->getCenter()[0]; > pos_y =3D sp->getCenter()[1] + EyeDist; > pos_z =3D sp->getCenter()[2]; >=20 > #else > glClearColor(0.0f, 0.0f, 0.0f, 0.0f); > glEnable(GL_DEPTH_TEST); > #endif > } >=20 > void update() > { > glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ; >=20 > #ifdef USE_SSG > sgSetCoord( &camera, pos_x, pos_y, pos_z, yaw, pitch, roll ); > ssgSetCamera( &camera ); > ssgCullAndDraw(scene); > #else > glLoadIdentity(); > =09 > glColor3f(1.0f, 1.0f, 1.0f); > glBegin(GL_TRIANGLES); > glVertex2f(0.5f, -0.25f); > glVertex2f(-0.5f, -0.25f); > glVertex2f(0.0f, 0.75f); > glEnd(); > #endif > SDL_GL_SwapBuffers( ); > } >=20 > int main(int argc, char *argv[]) > { > init(); > for(unsigned int i =3D 0; i < 1000; i++) { > update(); > } > return 0; > } >=20 >=20 > > Hoping you'll also love Plib for its simplicity not payed with=20 > > completness - >=20 > Me too :-) >=20 > If somebody could write a small program that shows a triangle=20 > I would really be happy. It does not have to be using SDL, as=20 > I have a decent understanding of both glut and OpenGL (it is=20 > just ssg that is giving me problems). >=20 > <snip> >=20 > --=20 > Yours=20 > Peter Poulsen >=20 >=20 >=20 > ------------------------------------------------------- > This SF.Net email is sponsored by: IBM Linux Tutorials > Free Linux tutorial presented by Daniel Robbins, President=20 > and CEO of GenToo technologies. Learn everything from=20 > fundamentals to system=20 > = administration.http://ads.osdn.com/?ad_id=3D1470&alloc_id=3D3638&op=3Dcli= ck > _______________________________________________ > plib-users mailing list > pli...@li...=20 > https://lists.sourceforge.net/lists/listinfo/p> lib-users >=20 |