opengl - Entire object rotates -
the idea code let windmill structure rotate, problem entire object rotates instead of windmill fan (not red triangles only). here code (i use keys control speed)
#include <windows.h> // ms windows #include <gl/glut.h> // glut, include glu.h , gl.h float angle = 0.00002f; int refreshmills = 30; void initgl() { glclearcolor(0.0f, 0.0f, 0.0f, 1.0f); // black , opaque } void timer(int value) { glutpostredisplay(); // post re-paint request activate display() gluttimerfunc(refreshmills, timer, 0); // next timer call milliseconds later } void display() { glclear(gl_color_buffer_bit); glbegin(gl_triangles); glcolor3f(1.0f, 0.0f, 0.0f) glvertex2f(0.0f, 0.0f); glvertex2f(-0.4f, 0.2f); glvertex2f(-0.2f, 0.4f); glcolor3f(1.0f, 0.0f, 0.0f); glvertex2f(0.0f, 0.0f); glvertex2f(0.4f, -0.2f); glvertex2f(0.2f, -0.4f); glcolor3f(1.0f, 0.0f, 0.0f); glvertex2f(0.0f, 0.0f); glvertex2f(-0.4f, -0.2f) glvertex2f(-0.2f, -0.4f); glcolor3f(1.0f, 0.0f, 0.0f); glvertex2f(0.0f, 0.0f); glvertex2f(0.4f, 0.2f); glvertex2f(0.2f, 0.4f); glend(); glrotatef(angle, 0.0f, 0.0f, 1.0f); angle=angle+0.000002f; glutpostredisplay(); glbegin(gl_triangles); glcolor3f(1.0f, 1.0f, 1.0f); glvertex2f(0.0f, 0.0f); glvertex2f(-0.4f, -0.6f); glvertex2f(0.4f, -0.6f); glend(); glflush(); glutswapbuffers(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'a':{ angle+=1; glutpostredisplay(); } case 's': angle+=2; glutpostredisplay(); case 'd': angle+=3 glutpostredisplay(); case 'f': angle=0; } } } int main(int argc, char** argv) { glutinit(&argc, argv); // initialize glutx glutcreatewindow("windmill"); // create window given title glutinitwindowsize(320, 320); // set window's initial width & height glutinitwindowposition(50, 50); // position window's initial top-left corner glutdisplayfunc(display); gluttimerfunc(0, timer, 0); glutspecialfunc(specialkeys); glutkeyboardfunc(keyboard); initgl(); // our own opengl initialization glutmainloop(); // enter event-processing loop return 0; }
you need implement sort of hierarchy(commonly scene graph) here uses transformation matrixes transformations.
basically, create "windmill" object has own transformation matrix. create "windmill fan" object has own. make fan child of parent. transformations propagate down. transform windmill, transform windmill fan.
you may want check out game development section of stackoverflow. these questions not met open arms here.
Comments
Post a Comment