From cb34a7b71aa5ba3f981c56b5cf85c1d59a7ed63c Mon Sep 17 00:00:00 2001 From: Dario Sassi Date: Fri, 4 Apr 2014 09:40:34 +0000 Subject: [PATCH] EgtGraphics 1.5d2 : - prima implementazione dello shading. --- EgtGraphics.rc | Bin 11390 -> 11390 bytes GraphObjs.h | 3 +++ ObjNewGraphics.cpp | 11 ++++++++--- ObjOldGraphics.cpp | 5 +++++ ObjOldGraphics.h | 7 ++++++- Scene.h | 18 ++++++++++++++--- SceneBasic.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++--- 7 files changed, 81 insertions(+), 10 deletions(-) diff --git a/EgtGraphics.rc b/EgtGraphics.rc index c1992fb298ce6089ce3af9e46387d6a86700996d..fb6a5f03f421c0f07af105f47ba1f1e4e65d7896 100644 GIT binary patch delta 81 zcmewt@h@V-H#SD2&G%&mnSs F+yK2>8_xg$ delta 81 zcmewt@h@V-H#SDY&G%&mnSs F+yJ~O8^-_u diff --git a/GraphObjs.h b/GraphObjs.h index b08debd..79761fc 100644 --- a/GraphObjs.h +++ b/GraphObjs.h @@ -21,9 +21,12 @@ // Definizione di Vert3f class Vert3f { public : + Vert3f( const Vector3d& vtV) : x( (float)vtV.x), y( (float)vtV.y), z( (float)vtV.z) {} Vert3f( const Point3d& ptP) : x( (float)ptP.x), y( (float)ptP.y), z( (float)ptP.z) {} Vert3f( double dX, double dY, double dZ) : x( (float)dX), y( (float)dY), z( (float)dZ) {} Vert3f( void) : x( 0), y( 0), z( 0) {} + void Set( const Vector3d& vtV) + { x = (float)vtV.x ; y = (float)vtV.y ; z = (float)vtV.z ; } void Set( const Point3d& ptP) { x = (float)ptP.x ; y = (float)ptP.y ; z = (float)ptP.z ; } void Set( double dX, double dY, double dZ) diff --git a/ObjNewGraphics.cpp b/ObjNewGraphics.cpp index 1ebbfec..973d94a 100644 --- a/ObjNewGraphics.cpp +++ b/ObjNewGraphics.cpp @@ -155,7 +155,8 @@ ObjNewGraphics::StartTriangles( int nNum) return false ; } glBindBuffer( GL_ARRAY_BUFFER, nVboId) ; - glBufferData( GL_ARRAY_BUFFER, 3 * nNum * SIZEV3F, NULL, GL_STATIC_DRAW) ; + // 3 float per le coordinate e 3 per la normale + glBufferData( GL_ARRAY_BUFFER, 6 * nNum * SIZEV3F, NULL, GL_STATIC_DRAW) ; // aggiungo i dati in lista AddVerts( GL_TRIANGLES, 0, nVaoId, nVboId) ; // dichiaro modo triangoli @@ -177,7 +178,9 @@ ObjNewGraphics::AddTriangle( const Triangle3d& Tria) // emetto i vertici del triangolo for ( int i = 0 ; i < 3 ; ++ i) { Vert3f v3V( Tria.GetP( i)) ; - glBufferSubData( GL_ARRAY_BUFFER, nCount * SIZEV3F, SIZEV3F, &v3V) ; + glBufferSubData( GL_ARRAY_BUFFER, 2 * nCount * SIZEV3F, SIZEV3F, &v3V) ; + Vert3f v3N( Tria.GetN()) ; + glBufferSubData( GL_ARRAY_BUFFER, ( 2 * nCount + 1) * SIZEV3F, SIZEV3F, &v3N) ; ++ nCount ; } // aggiorno il conteggio @@ -195,8 +198,10 @@ ObjNewGraphics::EndTriangles( void) if ( m_nCurrMode != GL_TRIANGLES) return false ; // termino il modo triangoli - glVertexPointer( 3, GL_FLOAT, 0, ((void*)(0))) ; + glVertexPointer( 3, GL_FLOAT, 2 * SIZEV3F, ((void*)(0))) ; glEnableClientState( GL_VERTEX_ARRAY) ; + glNormalPointer( GL_FLOAT, 2 * SIZEV3F, ((void*)( 1 * SIZEV3F))) ; + glEnableClientState( GL_NORMAL_ARRAY) ; glBindVertexArray( 0) ; // dichiaro modalità standard m_nCurrMode = GL_NONE ; diff --git a/ObjOldGraphics.cpp b/ObjOldGraphics.cpp index 4d9ee4f..d7d74e7 100644 --- a/ObjOldGraphics.cpp +++ b/ObjOldGraphics.cpp @@ -110,6 +110,8 @@ ObjOldGraphics::AddTriangle( const Triangle3d& Tria) // modo corrente deve essere triangoli if ( m_nCurrMode != GL_TRIANGLES) return false ; + // emetto la normale del triangolo + AddNormal3f( Tria.GetN()) ; // emetto i vertici del triangolo AddVert3f( Tria.GetP( 0)) ; AddVert3f( Tria.GetP( 1)) ; @@ -156,6 +158,9 @@ ObjOldGraphics::Draw( int nStat, int nMark) case OldGrAtom::START : glBegin( iIter->m_nMode) ; break ; + case OldGrAtom::NORMAL : + glNormal3f( iIter->m_fX, iIter->m_fY, iIter->m_fZ) ; + break ; case OldGrAtom::VERT : glVertex3f( iIter->m_fX, iIter->m_fY, iIter->m_fZ) ; break ; diff --git a/ObjOldGraphics.h b/ObjOldGraphics.h index 0e7187c..de3ae73 100644 --- a/ObjOldGraphics.h +++ b/ObjOldGraphics.h @@ -27,7 +27,7 @@ class OldGrAtom { OldGrAtom( void) : m_nType( NONE), m_fX( 0), m_fY( 0), m_fZ( 0), m_fW( 1) {} public : - enum GrType { NONE = 0, START = 1, VERT = 2, COLOR = 3, END = 4} ; + enum GrType { NONE = 0, START = 1, VERT = 2, NORMAL = 3, COLOR = 4, END = 5} ; public : GrType m_nType ; @@ -95,6 +95,11 @@ class ObjOldGraphics : public ObjEGrGraphics oga.m_nType = OldGrAtom::VERT ; oga.m_fX = float( ptP.x) ; oga.m_fY = float( ptP.y) ; oga.m_fZ = float( ptP.z) ; return AddOldGrAtom( oga) ; } + bool AddNormal3f( const Vector3d& vtN) + { OldGrAtom oga ; + oga.m_nType = OldGrAtom::NORMAL ; + oga.m_fX = float( vtN.x) ; oga.m_fY = float( vtN.y) ; oga.m_fZ = float( vtN.z) ; + return AddOldGrAtom( oga) ; } bool AddColor( float fRed, float fGreen, float fBlue, float fAlpha = 1) { OldGrAtom oga ; oga.m_nType = OldGrAtom::COLOR ; diff --git a/Scene.h b/Scene.h index 4184563..0742d1c 100644 --- a/Scene.h +++ b/Scene.h @@ -55,6 +55,16 @@ class Scene : public IEGrScene virtual bool ZoomChange( double dCoeff) ; virtual bool ZoomOnPoint( const Point3d& ptWin, double dCoeff) ; virtual bool ZoomWin( const Point3d& ptWin1, const Point3d& ptWin2) ; + // ShowMode + virtual bool SetShowMode( int nShowMode) + { if ( nShowMode != SM_WIREFRAME && + nShowMode != SM_HIDDENLINE && + nShowMode != SM_SHADING) + return false ; + m_nShowMode = nShowMode ; + return true ; } + virtual int GetShowMode( void) + { return m_nShowMode ;} // Geometry virtual bool SetExtension( const BBox3d& b3Ext) ; virtual bool UpdateExtension( void) ; @@ -97,11 +107,11 @@ class Scene : public IEGrScene bool DrawWinRect( void) ; private : - HDC m_hDC ; // Device Context - HGLRC m_hRC ; // OpenGL Rendering Context + HDC m_hDC ; // Device Context + HGLRC m_hRC ; // OpenGL Rendering Context WGLEWContext m_wglewc ; // WGLEW Context GLEWContext m_glewc ; // GLEW Context - bool m_bNewWay ; // flag che indica nuova modalità (OpenGL 3.0 in poi) + bool m_bNewWay ; // flag che indica nuova modalità (OpenGL 3.0 in poi) Point3d m_ptCenter ; // centro verso cui è rivolta la camera Vector3d m_vtDirCamera ; // versore direzione dal centro alla camera @@ -114,6 +124,8 @@ class Scene : public IEGrScene Color m_colBackTop ; // colore di sfondo in alto Color m_colBackBottom ; // colore di sfondo in basso + int m_nShowMode ; // modo di visualizzazione (wireframe, hiddenline, shading) + IGeomDB* m_pGeomDB ; // puntatore al DB geometrico Color m_colDef ; // colore di default Color m_colMark ; // colore per marcatura diff --git a/SceneBasic.cpp b/SceneBasic.cpp index 5257ad5..8956ee2 100644 --- a/SceneBasic.cpp +++ b/SceneBasic.cpp @@ -53,6 +53,8 @@ Scene::Scene( void) // Sfondo m_colBackTop.Set( 128, 128, 128) ; m_colBackBottom.Set( 128, 128, 128) ; + // Modalità di visualizzazione + m_nShowMode = SM_WIREFRAME ; // GeomData m_pGeomDB = nullptr ; m_colDef.Set( 0, 0, 0) ; @@ -512,6 +514,9 @@ Scene::Background( void) // disabilito lo Zdepth glDisable( GL_DEPTH_TEST) ; + // disabilito illuminazione + glDisable( GL_LIGHTING) ; + // imposto riempimento poligoni glPolygonMode( GL_FRONT_AND_BACK, GL_FILL) ; @@ -632,14 +637,50 @@ Scene::Draw( void) // imposto la dimensione standard dei punti glPointSize( 3) ; - // imposto disegno wireframe anche per poligoni - glPolygonMode( GL_FRONT_AND_BACK, GL_LINE) ; + // impostazioni dipendenti dalla modalità di visualizzazione + switch ( m_nShowMode) { + case SM_WIREFRAME : + // imposto disegno wireframe anche per poligoni + glPolygonMode( GL_FRONT_AND_BACK, GL_LINE) ; + // disabilito illuminazione + glDisable( GL_LIGHTING) ; + break ; + case SM_HIDDENLINE : + break ; + case SM_SHADING : + // imposto disegno shading per poligoni + glPolygonMode( GL_FRONT_AND_BACK, GL_FILL) ; + // imposto tipo shading + glShadeModel( GL_SMOOTH) ; + // imposto illuminazione + float LightAmbient[] = { 0.25f, 0.25f, 0.25f, 1.0f} ; + float LightStd[] = { 0.8f, 0.8f, 0.8f, 0.8f} ; + float Light0Pos[] = { 0.8f, 0.9f, 1.2f, 0.0f} ; + float Light1Pos[] = { -0.9f, -0.7f, 0.6f, 0.0f} ; + glLightfv( GL_LIGHT0, GL_AMBIENT, LightAmbient) ; + glLightfv( GL_LIGHT0, GL_DIFFUSE, LightStd) ; + glLightfv( GL_LIGHT0, GL_SPECULAR, LightStd) ; + glLightfv( GL_LIGHT0, GL_POSITION, Light0Pos) ; + glEnable( GL_LIGHT0) ; + glLightfv( GL_LIGHT1, GL_AMBIENT, LightAmbient) ; + glLightfv( GL_LIGHT1, GL_DIFFUSE, LightStd) ; + glLightfv( GL_LIGHT1, GL_SPECULAR, LightStd) ; + glLightfv( GL_LIGHT1, GL_POSITION, Light1Pos) ; + glEnable( GL_LIGHT1) ; + glEnable( GL_LIGHTING) ; + // provvisorio + glEnable( GL_COLOR_MATERIAL) ; + glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE) ; + break ; + } // disegno le geometrie del DB DrawGroup( GDB_ID_ROOT, GDB_MD_STD, GDB_ST_ON, GDB_MK_OFF) ; - // aggiungo disegni diretti (senza test Zbuffer) + // aggiungo disegni diretti (senza test Zbuffer e con poligoni riempiti) glDisable( GL_DEPTH_TEST) ; + glDisable( GL_LIGHTING) ; + glPolygonMode( GL_FRONT_AND_BACK, GL_FILL) ; DrawWinRect() ; // aggiorno