Re: [Plib-users] Drawing a simple triangle
Brought to you by:
sjbaker
From: Peter P. <pet...@st...> - 2004-03-08 15:05:21
|
"Paolo Leoncini" <p.l...@ci...> writes: > Peter, > > on my opinion you just missed to set up a camera for looking at scene > objects. It can be done this way: > > #include <plib/sg.h> > > sgCoord camera; > SGfloat pos_x, pos_y, pos_z, yaw, pitch, roll; > > // set pos_x, pos_y, pos_z, yaw, pitch and roll to some suitable > values > // then, before ssgCullAndDraw, actually set the SSG camera > sgSetCoord( camera, pos_x, pos_y, pos_z, yaw, pitch, roll ); > ssgSetCamera( &camera ); > > SG is the prefix for the Plib math/geometry library stuffs. > > Finding a plausible position for the init view (given a scene, in > general, you could not know its extent along the three axes) it's a > question of taste - I could suggest this: > > sgSphere *sp = scene->getBSphere(); > SGfloat radius = sp->getRadius(); > SGfloat EyeDist = float( radius * 1.f / tan( float( fov_x/2 * > SG_DEGREES_TO_RADIANS ) ) ); > SGfloat Ex, Ey, Ez; > > pos_x = sp->getCenter()[0]; > pos_y = sp->getCenter()[1] + EyeDist; > pos_z = sp->getCenter()[2]; > > Then, moving in 3D above or around the scene is a matter of changing > those six parameters to the sgSetCoord/ssgSetCamera (yaw is compass > heading, pitch rotates around the screen x-axis, roll around the axis > norma to the screen). > Thanks for your contribution. Unfortunately it still does not work :-( It may of course be that I have misunderstood your suggestion, in particular I am not sure that the yaw is correct. --- ssg_triangle.cc --- #define USE_SSG 1 #include <GL/gl.h> #include <GL/glu.h> #include <plib/ssg.h> #include <SDL/SDL.h> #include <iostream> using namespace std; ssgRoot* scene; sgCoord camera; SGfloat pos_x, pos_y, pos_z, yaw = 0.0f, pitch = 0.0f, roll = 0.0f; #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 240 #define SCREEN_BPP 24 void init() { int videoFlags; SDL_Surface* surface; if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { fprintf( stderr, "Video initialization failed: %s\n", SDL_GetError( ) ); exit(0); } videoFlags = SDL_OPENGL; videoFlags |= SDL_GL_DOUBLEBUFFER; videoFlags |= SDL_HWPALETTE; videoFlags |= SDL_HWSURFACE; surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags ); if ( !surface ) { fprintf( stderr, "Video mode set failed: %s\n", SDL_GetError( ) ); exit(0); } #ifdef USE_SSG ssgInit(); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glEnable(GL_DEPTH_TEST); ssgVertexArray* vertices = new ssgVertexArray; sgVec3 x = { 0.5f, 2.5f , -0.25f }; sgVec3 y = { -0.5f, 2.5f , -0.25f }; sgVec3 z = { 0.0f, 1.5f , 0.75f }; vertices->add(x); vertices->add(y); vertices->add(z); sgVec4 white = { 1.0f, 1.0f, 1.0f, 0.5f }; ssgColourArray* colours = new ssgColourArray; colours->add(white); colours->add(white); colours->add(white); ssgNormalArray* normals = new ssgNormalArray(3); sgVec3 normal = { 0, -1.0f, 0 }; normals->add(normal); normals->add(normal); normals->add(normal); ssgTexCoordArray* texs = new ssgTexCoordArray(3); sgVec2 tex_vec = {0,0}; texs->add(tex_vec); texs->add(tex_vec); texs->add(tex_vec); ssgVtxTable* n = new ssgVtxTable(GL_TRIANGLES, vertices, normals, texs, colours); ssgTransform* trans = new ssgTransform; trans->addKid(n); scene = new ssgRoot; scene->addKid(trans); sgSphere *sp = scene->getBSphere(); SGfloat radius = sp->getRadius(); SGfloat EyeDist = float( radius * 1.f / tan( float( SCREEN_WIDTH/2 * SG_DEGREES_TO_RADIANS ) ) ); SGfloat Ex, Ey, Ez; pos_x = sp->getCenter()[0]; pos_y = sp->getCenter()[1] + EyeDist; pos_z = sp->getCenter()[2]; #else glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glEnable(GL_DEPTH_TEST); #endif } void update() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ; #ifdef USE_SSG sgSetCoord( &camera, pos_x, pos_y, pos_z, yaw, pitch, roll ); ssgSetCamera( &camera ); ssgCullAndDraw(scene); #else glLoadIdentity(); 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( ); } int main(int argc, char *argv[]) { init(); for(unsigned int i = 0; i < 1000; i++) { update(); } return 0; } > Hoping you'll also love Plib for its simplicity not payed with > completness - Me too :-) If somebody could write a small program that shows a triangle I would really be happy. It does not have to be using SDL, as I have a decent understanding of both glut and OpenGL (it is just ssg that is giving me problems). <snip> -- Yours Peter Poulsen |