Uhmmm... I see some problems with the code above, but I'm too lazy to try to understand all the push/pop involved
Anyway I see some push/pop unbalanced, unless I'm mistaken. About the blades, probably the problem is you didn't set the normals ?
For the other objects using the glu_ library I believe the normals are created automatically, even if using the scaling distort them (so you have to use #GL_NORMALIZE).
I have enabled color tracking to simplify things and I made this quick hack (horrible as it is):
Code: Select all
IncludeFile #PB_Compiler_Home + "Examples\Sources - Advanced\OpenGL Cube\OpenGL.pbi"
InitKeyboard()
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
CompilerIf Subsystem("OpenGL") = #False
MessageRequester("Error", "Please set the subsystem to OpenGL")
End
CompilerEndIf
CompilerEndIf
If InitSprite() = 0
MessageRequester("Error", "Can't open screen & sprite environment!")
End
EndIf
OpenWindow(0, 0, 0, 640, 480, "press space key to switch On/Off the fan rotation", #PB_Window_SystemMenu | #PB_Window_Invisible)
SetWindowColor(0, 0)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)
HideWindow(0, #False)
Global running.b = 0;
Global angle_step.f = 0.3
Global angle_direction.f
lngh.f : width.f : depth.f
Global Dim LightPos.f(4) ;Light Position
LightPos(0)= 0.0 : LightPos(1)= 5.0 : LightPos(2)=-4.0 : LightPos(3)= 1.0
Global Dim LightAmb.f(4) ;Ambient Light Values
LightAmb(0)= 0.2 : LightAmb(1)= 0.2 : LightAmb(2)= 0.2 : LightAmb(3)= 1.0
Global Dim LightDif.f(4) ;Diffuse Light Values
LightDif(0)= 0.6 : LightDif(1)= 0.6 : LightDif(2)= 0.6 : LightDif(3)= 1.0
Global Dim LightSpc.f(4) ;Specular Light Values
LightSpc(0)=-0.2 : LightSpc(1)=-0.2 : LightSpc(2)=-0.2 : LightSpc(3)= 1.0
Global Dim MatAmb.f(4) ;Material - Ambient Values
MatAmb(0)= 0.4 : MatAmb(1)= 0.4 : MatAmb(2)= 0.4 : MatAmb(3)= 1.0
Global Dim MatDif.f(4) ;Material - Diffuse Values
MatDif(0)= 1.0 : MatDif(1)= 0.6 : MatDif(2)= 0.0 : MatDif(3)= 1.0
Global Dim MatSpc.f(4) ;Material - Specular Values
MatSpc(0)= 0.0 : MatSpc(1)= 0.0 : MatSpc(2)= 0.0 : MatSpc(3)= 1.0
Global Dim MatShn.f(1) ;Material - Shininess
MatShn(0)= 0.0
glClearColor_ (0.0, 0.0, 0.0, 0.0);
glShadeModel_ (#GL_SMOOTH)
glEnable_(#GL_LIGHTING);
glEnable_(#GL_LIGHT0);
glEnable_(#GL_DEPTH_TEST);
glLightfv_(#GL_LIGHT1,#GL_POSITION,LightPos()) ;Set Light1 Position
glLightfv_(#GL_LIGHT1,#GL_AMBIENT,LightAmb()) ;Set Light1 Ambience
glLightfv_(#GL_LIGHT1,#GL_DIFFUSE,LightDif()) ;Set Light1 Diffuse
glLightfv_(#GL_LIGHT1,#GL_SPECULAR,LightSpc()) ;Set Light1 Specular
;glEnable_(#GL_LIGHT1) ;Enable Light1
glEnable_(#GL_LIGHTING) ;Enable Lighting
; glMaterialfv_(#GL_FRONT,#GL_AMBIENT,MatAmb()) ;Set Material Ambience
; glMaterialfv_(#GL_FRONT,#GL_DIFFUSE,MatDif()) ;Set Material Diffuse
; glMaterialfv_(#GL_FRONT,#GL_SPECULAR,MatSpc()) ;Set Material Specular
; glMaterialfv_(#GL_FRONT,#GL_SHININESS,MatShn()) ;Set Material Shininess
glColorMaterial_(#GL_FRONT, #GL_AMBIENT_AND_DIFFUSE)
glEnable_(#GL_COLOR_MATERIAL) ; color tracking
glEnable_(#GL_NORMALIZE)
Procedure.l blade(lngh.f, width.f, depth.f)
a.f = lngh: b.f = width: c.f = depth
glBegin_(#GL_QUADS);
; Top face of box
glColor3f_(1.0, 0.0, 0.0) ; red blades !
glNormal3f_(0.0, 1.0, 0.0)
glVertex3f_(a, b, -c); // Top right vertex (Top of cube)
glVertex3f_(-a, b, -c); //Top left vertex (Top of cube)
glVertex3f_(-a, b, c); // Bottom left vertex (Top of cube)
glVertex3f_(a, b, c); // Bottom right vertex (Top of cube)
;Bottom face of box
glNormal3f_(0.0, -1.0, 0.0)
glVertex3f_(a, -b, -c); // Top right vertex (Bottom of cube)
glVertex3f_(-a, -b, -c); // Top left vertex (Bottom of cube)
glVertex3f_(-a, -b, c); // Bottom left vertex (Bottom of cube)
glVertex3f_( a, -b, c); // Bottom right vertex (Bottom of cube)
;Front of box
glNormal3f_(0.0, 0.0, 1.0)
glVertex3f_(a, b, c); // Top right vertex (Front)
glVertex3f_(-a, b, c); // Top left vertex (Front)
glVertex3f_(-a, -b, c); // Bottom left vertex (Front)
glVertex3f_(a, -b, c); // Bottom right vertex (Front)
;Back of box
glNormal3f_(0.0, 0.0, -1.0)
glVertex3f_(a, -b, -c); // Bottom right vertex (Back)
glVertex3f_(-a, -b, -c); // Bottom left vertex (Back)
glVertex3f_(-a, b, -c); // top left vertex (Back)
glVertex3f_(a, b, -c); // Top right vertex (Back)
;Left of box
glNormal3f_(-1.0, 0.0, 0.0)
glVertex3f_(-a, b, c); // Top right vertex (Left)
glVertex3f_(-a, b, -c); // Top left vertex (Left)
glVertex3f_(-a, -b, -c); // Bottom left vertex (Left)
glVertex3f_(-a, -b, c); // Bottom vertex (Left)
;Right of box
glNormal3f_(1.0, 0.0, 0.0)
glVertex3f_(a, b, -c); // Top right vertex (Right)
glVertex3f_(a, b, c); // Top left vertex (Right)
glVertex3f_(a, -b, c); // Bottom left vertex (Right)
glVertex3f_(a, -b, -c); // Bottom right vertex (Right)
;End drawing the box
glEnd_();
;Return TRUE;
EndProcedure
Procedure.l Fan_Render(spin.f)
glPushMatrix_();
glColor3f_(1.0, 1.0, 0.0)
;Fan
glPushMatrix_();
;Fan Base:
qobj = gluNewQuadric_();
gluQuadricDrawStyle_(qobj, #GL_FILL); /* smooth shaded */
gluQuadricNormals_(qobj, #GL_SMOOTH);
glScalef_( 4 , 0.5 , 4 );
;glEnable_(#GL_NORMALIZE);
;Sphere With radius 0.25 then scaled
gluSphere_(qobj, 0.25, 20, 20);
glPopMatrix_();
;Fan stand:
glPushMatrix_();
gluQuadricDrawStyle_(qobj, #GL_FILL); /* flat shaded */
gluQuadricNormals_(qobj, #GL_FLAT);
glRotatef_(-90, 1.0, 0.0, 0.0);
gluCylinder_(qobj, 0.125, 0.125, 3, 16, 10);
glPopMatrix_();
;Fan Motor:
glRotatef_(angle_direction, 0.0, 1.0, 0.0)
glPushMatrix_();
gluQuadricDrawStyle_(qobj, #GL_FILL); /* smooth shaded */
gluQuadricNormals_(qobj, #GL_SMOOTH);
glTranslatef_(0.0, 2.5, 0.0);
glScalef_( 0.5, 0.5, 1);
;glEnable_(#GL_NORMALIZE);
gluSphere_(qobj, 1 , 20 , 20 );
glPopMatrix_();
;Blades construction
glTranslatef_(0.0, 2.5, 0.5)
;glClear_ (#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glRotatef_(spin, 0.0, 0.0, 1.0 );
For i = 1 To 360 Step 60
glPushMatrix_();
glRotatef_( i, 0.0, 0.0, 1.0 );
glTranslatef_(1.2, 0.0, 0.0);
glRotatef_( -45, 1.0, 0.0, 0.0 );
glShadeModel_(#GL_FLAT);
glEnable_(#GL_DEPTH_TEST);
; glEnable_(#GL_LIGHTING);
; glEnable_(#GL_LIGHT0);
;glMaterialfv_p(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff_color);
glPushMatrix_();
;calling blade ie: drawing the Blade of the fan*/
;glClear_ (#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
blade(0.8,0.3,0.01);
glPopMatrix_();
glPopMatrix_();
Next
; **********************************************
glPopMatrix_();
;glPopMatrix_();
;glPopMatrix_();
EndProcedure
Procedure.f Fan_Physics()
Static spin.f
Static speed.f
If running = 1
speed = speed + 1
angle_direction = angle_direction + angle_step
If angle_direction > 110
angle_step = -0.3
ElseIf angle_direction < 0
angle_step = 0.3
EndIf
EndIf
If speed > 360.0: speed = 360.0: EndIf
If running = 0 :speed = speed - 1.8: EndIf
If speed < 0: speed = 0: EndIf
spin = spin + speed/100
ProcedureReturn spin
EndProcedure
HideWindow(0, #False)
Procedure.l Fan_Run()
glViewport_(0, 0, WindowWidth(0), WindowHeight(0))
glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_()
gluPerspective_(30.0, Abs(WindowWidth(0) / WindowHeight(0)), 0.1, 500.0)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
glLoadIdentity_ ()
; viewing transformation
glTranslatef_(0.0, 0.0, -6.0);
gluLookAt_(5,5,5,0,1.5,0,0,1,0);
spin.f = Fan_Physics();
Fan_Render(spin);
EndProcedure
While Quit.b = 0
Repeat
EventID = WindowEvent()
Select EventID
Case #PB_Event_CloseWindow
Quit = 1
EndSelect
ExamineKeyboard()
If KeyboardReleased(#PB_Key_Space)
running ! 1
EndIf
Fan_Run()
FlipBuffers()
Delay(1)
Until EventID = 0
Wend
End
BTW: NOT is an unary operator and I believe that code should be refused by the compiler, I changed that line with a bitwise XOR that should work better.
Nice demo, thanks for the PB conversion, I like the simulated inertia of the blades !
Maybe I'll adapt it and steal it as a demo to be included in the lib I'm writing eh eh eh.