Лаги OpenGL

Тема в разделе "WASM.OpenGL", создана пользователем skyproc, 26 фев 2012.

  1. skyproc

    skyproc New Member

    Публикаций:
    0
    Доброго всем времени суток.
    Есть такая проблема.
    Написал приложение OpenGL, ящик съезжает по наклонной плоскости.
    Но при анимации заметны подлагивания, т.е. на глаз видно что ящик съезжает не плавно равнозамедленно а рывками.
    Как исправить сию проблему?

    Код (Text):
    1. #include "stdafx.h"
    2. #include "windows.h"
    3.  
    4.  
    5.  
    6. #include    <math.h>
    7. #include    <glut.h>
    8. #include    <glext.h>
    9. #include    <stdio.h>
    10. #include    <stdlib.h>
    11. #include    <ctype.h>
    12. #include    <stdio.h>
    13. #include    <string.h>
    14.  
    15. #include    <lib3ds.h>
    16. #include    <lib3ds_impl.h>
    17. #pragma comment(lib, "lib3ds.lib")
    18.  
    19. #include    <Vector3D.h>
    20. #include    <Vector2D.h>
    21.  
    22. #include    <gl\gl.h>
    23. #include    <gl\glu.h>
    24. #include    <gl\glaux.h>
    25.  
    26. #pragma comment( lib, "opengl32.lib" )                          // Search For OpenGL32.lib While Linking
    27. #pragma comment( lib, "glu32.lib" )                             // Search For GLu32.lib While Linking
    28.  
    29. #pragma comment(lib, "glaux.lib")
    30.  
    31. Lib3dsMesh *testmesh;
    32. float GLTrivert[0xFFFF][3];
    33. float GLTrinorm[0xFFFF][3];
    34.  
    35. PFNGLACTIVETEXTUREARBPROC       glActiveTextureARB       = NULL;
    36.  
    37. FILE* hFile;
    38.  
    39.  
    40. Vector3D    eye   ( 2, 2, 2 );          // camera position
    41. Vector3D    light ( 0.5, 0, 1 );        // light position
    42. unsigned    normCubeMap;                // normalization cubemap id
    43. unsigned    bumpMap;                    // normal map
    44. unsigned    decalMap;                   // decal (diffuse) texture
    45. float       angle = 0;
    46.  
    47. Vector3D    v  [4];
    48. Vector2D    t0 [4];
    49. Vector3D    t1 [4];
    50.  
    51.  
    52. GLfloat rtri;               // Oaie aey ie?aieau
    53. GLfloat rquad;              // Oaie aey eaaa?aoa
    54. int nfaces_wedge, nfaces_box;
    55. int nfaces_vector_1_1, nfaces_vector_1_2, nfaces_vector_2_1, nfaces_vector_2_2;
    56. int nfaces_vector_3_1, nfaces_vector_3_2, nfaces_vector_4_1, nfaces_vector_4_2;
    57.  
    58.  
    59. float (*vertex_normal_wedge)[3];
    60. float (*vertex_normal_box)[3];
    61. float (*face_normal_wedge)[3];
    62. float (*face_normal_box)[3];
    63.  
    64. float (*vertex_normal_vector_1_1)[3];
    65. float (*face_normal_vector_1_1)[3];
    66. float (*vertex_normal_vector_1_2)[3];
    67. float (*face_normal_vector_1_2)[3];
    68.  
    69. float (*vertex_normal_vector_2_1)[3];
    70. float (*face_normal_vector_2_1)[3];
    71. float (*vertex_normal_vector_2_2)[3];
    72. float (*face_normal_vector_2_2)[3];
    73.  
    74. float (*vertex_normal_vector_3_1)[3];
    75. float (*face_normal_vector_3_1)[3];
    76. float (*vertex_normal_vector_3_2)[3];
    77. float (*face_normal_vector_3_2)[3];
    78.  
    79. float (*vertex_normal_vector_4_1)[3];
    80. float (*face_normal_vector_4_1)[3];
    81. float (*vertex_normal_vector_4_2)[3];
    82. float (*face_normal_vector_4_2)[3];
    83.  
    84. Lib3dsFile *Model_1,*Model_2, *Model_3, *Model_4, *Model_5, *Model_6, *Model_7, *Model_8, *Model_9, *Model_10;
    85.  
    86.  
    87.  
    88.  
    89.  
    90. //Lib3dsIo *testio;
    91.  
    92.  
    93. char TexName [1024];
    94. char exten [0xFFFF];
    95.  
    96. unsigned texture1;
    97.  
    98. GLuint  base;
    99. GLfloat  cnt1;  
    100. GLfloat  cnt2;  
    101. HDC         hDC=NULL;
    102. HWND        hWnd;
    103.  
    104. GLYPHMETRICSFLOAT gmf[256];                                     // Storage For Information About Our Outline Font Characters
    105.  
    106. GLuint textures[2];
    107.  
    108. float Scale_X, Scale_Y, Scale_Z;
    109. unsigned int wedge_tex, box_tex;
    110.  
    111.  
    112.  
    113. struct S_N3F_V3F
    114. {
    115. float vertex_normal [3];
    116. float vertices [3];
    117. };
    118.  
    119. struct S_T2F_N3F_V3F
    120. {
    121. float TexCoord [2];
    122. float vertex_normal [3];
    123. float vertices [3];
    124. };
    125.  
    126.  
    127. S_T2F_N3F_V3F *Model_1_array, *Model_2_array;
    128. S_N3F_V3F *Model_3_array, *Model_4_array, *Model_5_array, *Model_6_array, *Model_7_array, *Model_8_array, *Model_9_array, *Model_10_array;
    129.  
    130.  
    131.  
    132.  
    133.  
    134. Vector3D    box_vector   ( -1.0, 0.0, 0 );          
    135. Vector3D    box_coord   ( 0, 0, 0 );
    136. Vector3D    Translate   (0.0f,-10.0f,-30.0f);
    137. Vector3D    mg_vector   ( -0.9527, -0.304, 0 );
    138.  
    139.  
    140. //Преременные времени
    141. unsigned msec = 0;
    142. unsigned old_msec = 0;
    143. unsigned StartTime;
    144. float T, S, old_S, F_1;
    145.  
    146.  
    147. bool start_animation = FALSE;
    148. bool rotate = FALSE;
    149. bool transparency = FALSE;
    150.  
    151. float alpha, alpha_r, h, h_default, hc, g;
    152. float F_module, Ft_module, N_module, Ff_module, CoeffFriction, InitialSpeed;
    153.  
    154. void Load3Ds(const char *Filename, Lib3dsFile **Model_X, float (**vertex_normal)[3], float (**face_normal)[3], int *nfaces)
    155. {
    156.  
    157.  
    158. *Model_X = lib3ds_file_open(Filename);
    159.  
    160.  
    161.  
    162. *vertex_normal = (float (*)[3])malloc(sizeof(float) * 9 * (*Model_X)->meshes[0]->nfaces);
    163. *face_normal = (float (*)[3])malloc(sizeof(float) * 3 * (*Model_X)->meshes[0]->nfaces);
    164.  
    165. lib3ds_mesh_calculate_vertex_normals((*Model_X)->meshes[0], (float (*)[3])*vertex_normal);
    166. lib3ds_mesh_calculate_face_normals((*Model_X)->meshes[0], (float (*)[3])*face_normal);
    167.  
    168. *nfaces = (*Model_X)->meshes[0]->nfaces;
    169.  
    170. }
    171.  
    172.  
    173.  
    174.  
    175.  
    176.  
    177. void init ()
    178. {
    179.  
    180.  
    181.  
    182.     alpha_r = alpha*3.14159265f/180.0f;
    183.  
    184.     mg_vector.y = -1.0f*sin(alpha_r);
    185.     mg_vector.x = -1.0f*cos(alpha_r);
    186.  
    187.  
    188.  
    189.  
    190.  
    191.  
    192.    
    193.     Ft_module = 4.716f;
    194.     F_module = Ft_module * sin(alpha_r);
    195.     N_module = Ft_module * cos(alpha_r);
    196.     Ff_module = CoeffFriction * N_module;
    197.  
    198.     h=30.005f * tan(alpha_r);
    199.     h_default =30.005f * tan(17.73323859f*3.14159265f/180.0f);
    200.  
    201.     hc = 26.90f * tan(alpha_r) + 1.647f;
    202.  
    203.  
    204.  
    205.     F_1 = 1;
    206.     Scale_X = 1.0;
    207.  
    208.     Load3Ds("vector_1_1.3ds", &Model_3, &vertex_normal_vector_1_1, &face_normal_vector_1_1, &nfaces_vector_1_1);
    209.     Load3Ds("vector_1_2.3ds", &Model_4, &vertex_normal_vector_1_2, &face_normal_vector_1_2, &nfaces_vector_1_2);
    210.     Load3Ds("vector_2_1.3ds", &Model_5, &vertex_normal_vector_2_1, &face_normal_vector_2_1, &nfaces_vector_2_1);
    211.     Load3Ds("vector_2_2.3ds", &Model_6, &vertex_normal_vector_2_2, &face_normal_vector_2_2, &nfaces_vector_2_2);
    212.     Load3Ds("vector_3_1.3ds", &Model_7, &vertex_normal_vector_3_1, &face_normal_vector_3_1, &nfaces_vector_3_1);
    213.     Load3Ds("vector_3_2.3ds", &Model_8, &vertex_normal_vector_3_2, &face_normal_vector_3_2, &nfaces_vector_3_2);
    214.     Load3Ds("vector_4_1.3ds", &Model_9, &vertex_normal_vector_4_1, &face_normal_vector_4_1, &nfaces_vector_4_1);
    215.     Load3Ds("vector_4_2.3ds", &Model_10, &vertex_normal_vector_4_2, &face_normal_vector_4_2, &nfaces_vector_4_2);
    216.  
    217.  
    218.     Load3Ds("wedge.3ds", &Model_1, &vertex_normal_wedge, &face_normal_wedge, &nfaces_wedge);
    219.     printf("Load Texture - %s\n",Model_1->materials[0]->texture1_map.name);
    220.     AUX_RGBImageRec *texture1;
    221.     texture1 = auxDIBImageLoadA(Model_1->materials[0]->texture1_map.name);
    222.  
    223.    
    224.     Load3Ds("box.3ds", &Model_2, &vertex_normal_box, &face_normal_box, &nfaces_box);
    225.     printf("Load Texture - %s\n",Model_2->materials[0]->texture1_map.name);
    226.     AUX_RGBImageRec *texture2;
    227.     texture2 = auxDIBImageLoadA(Model_2->materials[0]->texture1_map.name);
    228.    
    229.  
    230.     glGenTextures(1, &wedge_tex);
    231.     glGenTextures(1, &box_tex);
    232.  
    233.  
    234.  
    235.  
    236.  
    237.     glClearColor ( 0, 0, 0, 1 );
    238.     glEnable     ( GL_DEPTH_TEST );
    239.  
    240.  
    241.     glEnable(GL_LIGHT0);
    242.     glEnable(GL_COLOR_MATERIAL);
    243.     glEnable(GL_LIGHTING);
    244.     glEnable(GL_NORMALIZE);
    245.    
    246.    
    247.     glDepthFunc  ( GL_LEQUAL );
    248.     glHint ( GL_POLYGON_SMOOTH_HINT,         GL_NICEST );
    249.     glHint ( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
    250.  
    251.  
    252.  
    253.    
    254.     glBindTexture(GL_TEXTURE_2D,wedge_tex);
    255.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    256.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    257.     glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);  
    258.     glEnable(GL_TEXTURE_2D);
    259.  
    260.  
    261.     glBindTexture(GL_TEXTURE_2D,box_tex);
    262.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    263.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    264.     glTexImage2D(GL_TEXTURE_2D, 0, 3, texture2->sizeX, texture2->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture2->data);  
    265.     glEnable(GL_TEXTURE_2D);
    266.  
    267.  
    268.     hWnd = FindWindowA("GLUT", "Visual Source V1.0.0");
    269.     hDC=GetWindowDC(hWnd);
    270. //  BuildFont();            
    271.  
    272.     HFONT   font;                                               // Windows Font ID
    273.  
    274.     base = glGenLists(256);                                     // Storage For 256 Characters
    275.  
    276.     font = CreateFont(  -12,                                    // Height Of Font
    277.                         0,                                      // Width Of Font
    278.                         0,                                      // Angle Of Escapement
    279.                         0,                                      // Orientation Angle
    280.                         FW_BOLD,                                // Font Weight
    281.                         FALSE,                                  // Italic
    282.                         FALSE,                                  // Underline
    283.                         FALSE,                                  // Strikeout
    284.                         ANSI_CHARSET,                           // Character Set Identifier
    285.                         OUT_TT_PRECIS,                          // Output Precision
    286.                         CLIP_DEFAULT_PRECIS,                    // Clipping Precision
    287.                         ANTIALIASED_QUALITY,                    // Output Quality
    288.                         FF_DONTCARE|DEFAULT_PITCH,              // Family And Pitch
    289.                         NULL);                                  // Font Name
    290.    
    291.    
    292.     SelectObject(hDC, font);                                    // Selects The Font We Created
    293.  
    294.     wglUseFontOutlines( hDC,                                    // Select The Current DC
    295.                         0,                                      // Starting Character
    296.                         255,                                    // Number Of Display Lists To Build
    297.                         base,                                   // Starting Display Lists
    298.                         0.0f,                                   // Deviation From The True Outlines
    299.                         0.0f,                                   // Font Thickness In The Z Direction
    300.                         WGL_FONT_POLYGONS,                      // Use Polygons, Not Lines
    301.                         gmf);                                   // Address Of Buffer To Recieve Data
    302.  
    303. }
    304.  
    305. GLvoid glPrint(float x, float y, float z, const char *fmt, ...) // Custom GL "Print" Routine
    306. {
    307.     float       length=0;                                       // Used To Find The Length Of The Text
    308.     char        text[256];                                      // Holds Our String
    309.     va_list     ap;                                             // Pointer To List Of Arguments
    310.  
    311.     if (fmt == NULL)                                            // If There's No Text
    312.         return;                                                 // Do Nothing
    313.  
    314.     va_start(ap, fmt);                                          // Parses The String For Variables
    315.         vsprintf(text, fmt, ap);                                // And Converts Symbols To Actual Numbers
    316.     va_end(ap);                                                 // Results Are Stored In Text
    317.  
    318.     for (unsigned int loop=0;loop<(strlen(text));loop++)        // Loop To Find Text Length
    319.     {
    320.         length+=gmf[text[loop]].gmfCellIncX;                    // Increase Length By Each Characters Width
    321.     }
    322.  
    323.     glTranslatef(x - length, y, z);                             // Position Text On The Screen
    324.  
    325.     glPushAttrib(GL_LIST_BIT);                                  // Pushes The Display List Bits
    326.     glListBase(base);                                           // Sets The Base Character to 0
    327.     glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);          // Draws The Display List Text
    328.     glPopAttrib();                                              // Pops The Display List Bits
    329.  
    330.     glTranslatef(-x, -y, -z);                                   // Position Text On The Screen
    331. }
    332.  
    333.  
    334.  
    335. void display ()
    336. {
    337.    
    338. }
    339.  
    340. void reshape ( int w, int h )
    341. {
    342.    glViewport     ( 0, 0, (GLsizei)w, (GLsizei)h );
    343.    glMatrixMode   ( GL_PROJECTION );
    344.    glLoadIdentity ();
    345.    gluPerspective ( 60.0, (GLfloat)w/(GLfloat)h, 1.0, 60.0 );
    346.    glMatrixMode   ( GL_MODELVIEW );
    347.    glLoadIdentity ();
    348.    gluLookAt      ( eye.x, eye.y, eye.z,    // eye
    349.                     0, 0, 0,                // center
    350.                     0.0, 0.0, 1.0 );        // up
    351. }
    352.  
    353.  
    354. void key ( unsigned char key, int x, int y )
    355. {
    356.     if ( key == 27 || key == 'q' || key == 'Q' )        // quit requested
    357.         exit ( 0 );
    358.     if ( key == 'w' )        // quit requested
    359.         rtri+=1.2f;
    360.     if ( key == 0x0D )        // quit requested
    361.     {
    362.         start_animation = TRUE;
    363.         StartTime = (unsigned)GetTickCount();
    364.     }
    365.     if ( key == 'r' )        // quit requested
    366.     {
    367.         start_animation = TRUE;
    368.         old_S = 0.0;
    369.     }
    370.     if ( key == 'e' )        // quit requested
    371.     {
    372.        if (rotate == FALSE)
    373.             rotate = TRUE;
    374.        else
    375.             rotate = FALSE;
    376.     }
    377.     if ( key == 't' )        // quit requested
    378.     {
    379.        if (transparency == FALSE)
    380.             transparency = TRUE;
    381.        else
    382.             transparency = FALSE;
    383.     }
    384.  
    385.  
    386.  
    387.  
    388.  
    389. }
    390.  
    391. void    animate ()
    392. {
    393.  
    394.  
    395. msec = (unsigned)GetTickCount();
    396. if (old_msec == msec)
    397.         return;
    398.  
    399. old_msec = msec;
    400.  
    401.  
    402.  
    403.  
    404. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    405.  
    406.  
    407.  
    408.  
    409.        
    410.  
    411.  
    412.  
    413.    
    414.  
    415.  
    416.  
    417. glLoadIdentity();               // Na?in i?iniio?a
    418. glTranslatef(Translate.x, Translate.y, Translate.z);            // Naaea aeaai e aaeoau ye?aia
    419.  
    420. glMatrixMode(GL_MODELVIEW);
    421. glPushMatrix();
    422.  
    423. glRotatef(rtri,0.0f,1.0f,0.0f);         // A?auaiea ie?aieau ii ine Y
    424.  
    425.  
    426.  
    427. //**************************************************
    428. //Анимация ящика
    429. //**************************************************
    430. if (start_animation == TRUE)
    431. {
    432.     T = (msec - StartTime) / 1000.0f; //вычислим время анимации
    433.     S = (InitialSpeed*T - (g*T*T/2.0f) * (CoeffFriction * cos(alpha_r) - sin(alpha_r))); //Вычислим путь
    434.     F_1 = -2.0f*((S*(-1.0f)) - InitialSpeed*T)/(T*T);
    435.  
    436.     if (old_S > S)
    437.     {
    438.         S = old_S;
    439.         F_1 = 1.0f;
    440.     }
    441.     old_S = S;
    442. }
    443. //**************************************************
    444.  
    445. //**************************************************
    446. //PUSH 1
    447. //**************************************************
    448. glMatrixMode(GL_MODELVIEW);
    449. glPushMatrix();
    450.  
    451.  
    452. //if (F_1 != 1.0f)
    453.     //printf("S=%f, T=%f, F=%f\n", S, T, F_1);
    454.  
    455.  
    456.  
    457. glTranslatef(11.0f, hc, 0.0);
    458.  
    459. if (start_animation == TRUE)
    460. {
    461.     box_coord = mg_vector * S;
    462.     glTranslatef(box_coord.x, box_coord.y, box_coord.z);
    463. }
    464.  
    465.  
    466.  
    467.  
    468. //**************************************************
    469. //PUSH 4
    470. //**************************************************
    471. glMatrixMode(GL_MODELVIEW);
    472. glPushMatrix();
    473. glRotatef(alpha,0.0f,0.0f,1.0f);
    474.  
    475.  
    476. //**************************************************
    477. //PUSH 2
    478. //**************************************************
    479. glMatrixMode(GL_MODELVIEW);
    480. glPushMatrix();
    481.  
    482. glTranslatef(Ft_module - F_module , 0.0f, 0.0f);
    483. glTranslatef((-F_1 * F_module) +  F_module, 0.0f, 0.0f);
    484.  
    485.  
    486. glBegin(GL_TRIANGLES);
    487. for(int i=0; i<nfaces_vector_1_1; i++){
    488.     glColor3f(1.0f,1.0f,0.0f);         
    489.     glNormal3fv(face_normal_vector_1_1[i]);
    490.     glNormal3fv(vertex_normal_vector_1_1[i*3]);
    491.     glVertex3fv(Model_3->meshes[0]->vertices[Model_3->meshes[0]->faces[i].index[0]]);
    492.     glNormal3fv(vertex_normal_vector_1_1[i*3+1]);
    493.     glVertex3fv(Model_3->meshes[0]->vertices[Model_3->meshes[0]->faces[i].index[1]]);
    494.     glNormal3fv(vertex_normal_vector_1_1[i*3+2]);
    495.     glVertex3fv(Model_3->meshes[0]->vertices[Model_3->meshes[0]->faces[i].index[2]]);
    496. }
    497. glEnd();
    498. //**************************************************
    499. //POP 2
    500. //**************************************************
    501. glMatrixMode(GL_MODELVIEW);
    502. glPopMatrix();
    503.  
    504.  
    505.  
    506. //**************************************************
    507. //PUSH 3
    508. //**************************************************
    509. glMatrixMode(GL_MODELVIEW);
    510. glPushMatrix();
    511. glScalef(F_1*(F_module/Ft_module),1,1);
    512.  
    513. glBegin(GL_TRIANGLES);
    514. for(int i=0; i<nfaces_vector_1_2; i++){
    515.     glColor3f(0.0f,1.0f,0.0f);         
    516.     glNormal3fv(face_normal_vector_1_2[i]);
    517.     glNormal3fv(vertex_normal_vector_1_2[i*3]);
    518.     glVertex3fv(Model_4->meshes[0]->vertices[Model_4->meshes[0]->faces[i].index[0]]);
    519.     glNormal3fv(vertex_normal_vector_1_2[i*3+1]);
    520.     glVertex3fv(Model_4->meshes[0]->vertices[Model_4->meshes[0]->faces[i].index[1]]);
    521.     glNormal3fv(vertex_normal_vector_1_2[i*3+2]);
    522.     glVertex3fv(Model_4->meshes[0]->vertices[Model_4->meshes[0]->faces[i].index[2]]);
    523. }
    524. glEnd();
    525. //**************************************************
    526. //POP 3
    527. //**************************************************
    528. glMatrixMode(GL_MODELVIEW);
    529. glPopMatrix();
    530.  
    531.  
    532.  
    533. //**************************************************
    534. //PUSH 5
    535. //**************************************************
    536. glMatrixMode(GL_MODELVIEW);
    537. glPushMatrix();
    538.  
    539.  
    540. glTranslatef(0.0f, N_module-Ft_module, 0.0f);
    541.  
    542.  
    543. glBegin(GL_TRIANGLES);
    544. for(int i=0; i<nfaces_vector_2_1; i++){
    545.     glColor3f(0.0f,0.0f,1.0f);         
    546.     glNormal3fv(face_normal_vector_2_1[i]);
    547.     glNormal3fv(vertex_normal_vector_2_1[i*3]);
    548.     glVertex3fv(Model_5->meshes[0]->vertices[Model_5->meshes[0]->faces[i].index[0]]);
    549.     glNormal3fv(vertex_normal_vector_2_1[i*3+1]);
    550.     glVertex3fv(Model_5->meshes[0]->vertices[Model_5->meshes[0]->faces[i].index[1]]);
    551.     glNormal3fv(vertex_normal_vector_2_1[i*3+2]);
    552.     glVertex3fv(Model_5->meshes[0]->vertices[Model_5->meshes[0]->faces[i].index[2]]);
    553. }
    554. glEnd();
    555. //**************************************************
    556. //POP 5
    557. //**************************************************
    558. glMatrixMode(GL_MODELVIEW);
    559. glPopMatrix();
    560.  
    561. //**************************************************
    562. //PUSH 6
    563. //**************************************************
    564. glMatrixMode(GL_MODELVIEW);
    565. glPushMatrix();
    566. glScalef(1,N_module/Ft_module,1);
    567.  
    568.  
    569. glBegin(GL_TRIANGLES);
    570. for(int i=0; i<nfaces_vector_2_2; i++){
    571.     glColor3f(0.0f,1.0f,0.0f);     
    572.     glNormal3fv(face_normal_vector_2_2[i]);
    573.     glNormal3fv(vertex_normal_vector_2_2[i*3]);
    574.     glVertex3fv(Model_6->meshes[0]->vertices[Model_6->meshes[0]->faces[i].index[0]]);
    575.     glNormal3fv(vertex_normal_vector_2_2[i*3+1]);
    576.     glVertex3fv(Model_6->meshes[0]->vertices[Model_6->meshes[0]->faces[i].index[1]]);
    577.     glNormal3fv(vertex_normal_vector_2_2[i*3+2]);
    578.     glVertex3fv(Model_6->meshes[0]->vertices[Model_6->meshes[0]->faces[i].index[2]]);
    579. }
    580. glEnd();
    581. //**************************************************
    582. //POP 6
    583. //**************************************************
    584. glMatrixMode(GL_MODELVIEW);
    585. glPopMatrix();
    586.  
    587.  
    588.  
    589.  
    590. //**************************************************
    591. //PUSH 7
    592. //**************************************************
    593. glMatrixMode(GL_MODELVIEW);
    594. glPushMatrix();
    595.  
    596. glTranslatef(Ff_module-Ft_module, 0.0f, 0.0f);
    597.  
    598.  
    599. glBegin(GL_TRIANGLES);
    600. for(int i=0; i<nfaces_vector_3_1; i++){
    601.     glColor3f(1.0f,0.0f,0.0f);     
    602.     glNormal3fv(face_normal_vector_3_1[i]);
    603.     glNormal3fv(vertex_normal_vector_3_1[i*3]);
    604.     glVertex3fv(Model_7->meshes[0]->vertices[Model_7->meshes[0]->faces[i].index[0]]);
    605.     glNormal3fv(vertex_normal_vector_3_1[i*3+1]);
    606.     glVertex3fv(Model_7->meshes[0]->vertices[Model_7->meshes[0]->faces[i].index[1]]);
    607.     glNormal3fv(vertex_normal_vector_3_1[i*3+2]);
    608.     glVertex3fv(Model_7->meshes[0]->vertices[Model_7->meshes[0]->faces[i].index[2]]);
    609. }
    610. glEnd();
    611.  
    612. //**************************************************
    613. //POP 7
    614. //**************************************************
    615. glMatrixMode(GL_MODELVIEW);
    616. glPopMatrix();
    617.  
    618.  
    619. //**************************************************
    620. //PUSH 8
    621. //**************************************************
    622. glMatrixMode(GL_MODELVIEW);
    623. glPushMatrix();
    624. glScalef(Ff_module/Ft_module,1.0f,1.0f);
    625.  
    626.  
    627. glBegin(GL_TRIANGLES);
    628. for(int i=0; i<nfaces_vector_3_2; i++){
    629.     glColor3f(0.0f,1.0f,0.0f);     
    630.     glNormal3fv(face_normal_vector_3_2[i]);
    631.     glNormal3fv(vertex_normal_vector_3_2[i*3]);
    632.     glVertex3fv(Model_8->meshes[0]->vertices[Model_8->meshes[0]->faces[i].index[0]]);
    633.     glNormal3fv(vertex_normal_vector_3_2[i*3+1]);
    634.     glVertex3fv(Model_8->meshes[0]->vertices[Model_8->meshes[0]->faces[i].index[1]]);
    635.     glNormal3fv(vertex_normal_vector_3_2[i*3+2]);
    636.     glVertex3fv(Model_8->meshes[0]->vertices[Model_8->meshes[0]->faces[i].index[2]]);
    637. }
    638. glEnd();
    639.  
    640. //**************************************************
    641. //POP 8
    642. //**************************************************
    643. glMatrixMode(GL_MODELVIEW);
    644. glPopMatrix();
    645.  
    646.  
    647.  
    648. //**************************************************
    649. //POP 4
    650. //**************************************************
    651. glMatrixMode(GL_MODELVIEW);
    652. glPopMatrix();
    653.  
    654.  
    655.  
    656. glBegin(GL_TRIANGLES);
    657. for(int i=0; i<nfaces_vector_4_1; i++){
    658.     glColor3f(0.5f,0.0f,1.0f); 
    659.     glNormal3fv(face_normal_vector_4_1[i]);
    660.     glNormal3fv(vertex_normal_vector_4_1[i*3]);
    661.     glVertex3fv(Model_9->meshes[0]->vertices[Model_9->meshes[0]->faces[i].index[0]]);
    662.     glNormal3fv(vertex_normal_vector_4_1[i*3+1]);
    663.     glVertex3fv(Model_9->meshes[0]->vertices[Model_9->meshes[0]->faces[i].index[1]]);
    664.     glNormal3fv(vertex_normal_vector_4_1[i*3+2]);
    665.     glVertex3fv(Model_9->meshes[0]->vertices[Model_9->meshes[0]->faces[i].index[2]]);
    666. }
    667. glEnd();
    668.  
    669.  
    670.  
    671.  
    672. glBegin(GL_TRIANGLES);
    673. for(int i=0; i<nfaces_vector_4_2; i++){
    674.     glColor3f(0.0f,1.0f,0.0f);     
    675.     glNormal3fv(face_normal_vector_4_2[i]);
    676.     glNormal3fv(vertex_normal_vector_4_2[i*3]);
    677.     glVertex3fv(Model_10->meshes[0]->vertices[Model_10->meshes[0]->faces[i].index[0]]);
    678.     glNormal3fv(vertex_normal_vector_4_2[i*3+1]);
    679.     glVertex3fv(Model_10->meshes[0]->vertices[Model_10->meshes[0]->faces[i].index[1]]);
    680.     glNormal3fv(vertex_normal_vector_4_2[i*3+2]);
    681.     glVertex3fv(Model_10->meshes[0]->vertices[Model_10->meshes[0]->faces[i].index[2]]);
    682. }
    683. glEnd();
    684.  
    685.  
    686.  
    687.  
    688.  
    689.  
    690. //**************************************************
    691. //POP 1
    692. //**************************************************
    693. glMatrixMode(GL_MODELVIEW);
    694. glPopMatrix();
    695.  
    696.  
    697. if (transparency == TRUE)
    698. {
    699.     glEnable(GL_ALPHA_TEST);
    700.     glEnable(GL_BLEND);
    701.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    702. }
    703.  
    704.  
    705.  
    706. //**************************************************
    707. //POP 9
    708. //**************************************************
    709. glMatrixMode(GL_MODELVIEW);
    710. glPushMatrix();
    711. glScalef(1.0f, h/h_default, 1.0);
    712.  
    713.  
    714.  
    715.  
    716. glEnable(GL_TEXTURE_2D);
    717. glBindTexture(GL_TEXTURE_2D, wedge_tex);  
    718. glBegin(GL_TRIANGLES);
    719. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);    //Возможные значения: GL_MODULATE  GL_BLEND GL_REPLACE
    720.  
    721.  
    722.  
    723.  
    724. for(int i=0; i<nfaces_wedge; i++){
    725.  
    726.     glColor4f(1.0f,1.0f,1.0f, 0.5f);   
    727.     glNormal3fv(face_normal_wedge[i]);
    728.     glNormal3fv(vertex_normal_wedge[i*3]);
    729.     glTexCoord2fv(Model_1->meshes[0]->texcos[Model_1->meshes[0]->faces[i].index[0]]);
    730.     glVertex3fv(Model_1->meshes[0]->vertices[Model_1->meshes[0]->faces[i].index[0]]);
    731.     glNormal3fv(vertex_normal_wedge[i*3+1]);
    732.     glTexCoord2fv(Model_1->meshes[0]->texcos[Model_1->meshes[0]->faces[i].index[1]]);
    733.     glVertex3fv(Model_1->meshes[0]->vertices[Model_1->meshes[0]->faces[i].index[1]]);
    734.     glNormal3fv(vertex_normal_wedge[i*3+2]);
    735.     glTexCoord2fv(Model_1->meshes[0]->texcos[Model_1->meshes[0]->faces[i].index[2]]);
    736.     glVertex3fv(Model_1->meshes[0]->vertices[Model_1->meshes[0]->faces[i].index[2]]);
    737.  
    738. }
    739. glEnd();
    740.  
    741.  
    742.  
    743. //**************************************************
    744. //POP 9
    745. //**************************************************
    746. glMatrixMode(GL_MODELVIEW);
    747. glPopMatrix();
    748.  
    749. glTranslatef(11.0f, hc, 0.0f);
    750.  
    751.  
    752. glMatrixMode(GL_MODELVIEW);
    753. glPushMatrix();
    754.  
    755. glRotatef(alpha,0.0f,0.0f,1.0f);
    756.  
    757. //**************************************************
    758. //Анимация ящика
    759. //**************************************************
    760. if (start_animation == TRUE)
    761. {
    762.     box_coord = box_vector * S;
    763.     glTranslatef(box_coord.x, box_coord.y, box_coord.z);
    764. }
    765. //**************************************************
    766.  
    767.  
    768.  
    769.  
    770.  
    771. glBindTexture(GL_TEXTURE_2D, box_tex);
    772. glBegin(GL_TRIANGLES);
    773. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);    //Возможные значения: GL_MODULATE  GL_BLEND GL_REPLACE
    774.  
    775.    
    776.  
    777. for(int i=0; i<nfaces_box; i++){
    778.  
    779.     glColor4f(1.0f,1.0f,1.0f, 0.5f);   
    780.     glNormal3fv(face_normal_box[i]);
    781.     glNormal3fv(vertex_normal_box[i*3]);
    782.     glTexCoord2fv(Model_2->meshes[0]->texcos[Model_2->meshes[0]->faces[i].index[0]]);
    783.     glVertex3fv(Model_2->meshes[0]->vertices[Model_2->meshes[0]->faces[i].index[0]]);
    784.     glNormal3fv(vertex_normal_box[i*3+1]);
    785.     glTexCoord2fv(Model_2->meshes[0]->texcos[Model_2->meshes[0]->faces[i].index[1]]);
    786.     glVertex3fv(Model_2->meshes[0]->vertices[Model_2->meshes[0]->faces[i].index[1]]);
    787.     glNormal3fv(vertex_normal_box[i*3+2]);
    788.     glTexCoord2fv(Model_2->meshes[0]->texcos[Model_2->meshes[0]->faces[i].index[2]]);
    789.     glVertex3fv(Model_2->meshes[0]->vertices[Model_2->meshes[0]->faces[i].index[2]]);
    790.  
    791. }
    792. glEnd();
    793.  
    794.  
    795. glDisable(GL_TEXTURE_2D);
    796.  
    797. if (transparency == TRUE)
    798. {
    799.     glDisable(GL_BLEND);
    800.     glDisable(GL_ALPHA_TEST);
    801. }
    802.  
    803. glMatrixMode(GL_MODELVIEW);
    804. glPopMatrix();
    805.  
    806.  
    807.  
    808.  
    809.  
    810. glMatrixMode(GL_MODELVIEW);
    811. glPopMatrix();
    812.  
    813.  
    814. glutSwapBuffers ();
    815.  
    816. if (rotate == TRUE)
    817.     rtri+=0.2f;
    818.  
    819.  
    820.  
    821.    
    822. }
    823.  
    824.  
    825.  
    826.  
    827.  
    828.  
    829.  
    830.  
    831. void    printExtList ( const char * extension )
    832. {
    833.     char    name [1024];                    // hope this will be enough
    834.     int i, j, k;
    835.  
    836.     k=0;
    837.     k = k + sprintf(exten + k, "\nSupported extensions:\n");
    838.     printf ( "Supported extensions:\n" );
    839.  
    840.  
    841.     for ( i = 0, j = 0; extension [i] != '\0'; i++ )
    842.         if ( !isspace ( extension [i] ) )
    843.             name [j++] = extension [i];
    844.         else                                // end of name
    845.         {
    846.             name [j] = '\0';                // terminate name
    847.  
    848.             printf ( "\t%s\n", name );
    849.             k = k + sprintf(exten + k, "%s\n", name);
    850.  
    851.             j = 0;
    852.         }
    853.  
    854.     if ( j > 0 )
    855.     {
    856.         name [j] = '\0';
    857.  
    858.         printf ( "\t%s\n", name );
    859.         k = k + sprintf(exten + k, "%s\n", name);
    860.     }
    861. }
    862.  
    863.  
    864.  
    865.  
    866.  
    867. int main (int argc, char **argv)
    868. {
    869.    
    870.     g = 9.8f;
    871.     alpha = 17.73323859f;
    872.     CoeffFriction = 0.68f;
    873.     InitialSpeed = 15.0f;
    874.  
    875.    
    876.  
    877.     if (argc >= 2)
    878.     {
    879.         g = atof(argv [1]);
    880.         printf("g = %f\n",g);
    881.     }
    882.            
    883.     if (argc >= 3)
    884.     {
    885.         alpha = atof(argv [2]);
    886.         printf("alpha = %f\n",alpha);
    887.     }
    888.  
    889.     if (argc >= 4)
    890.     {
    891.         CoeffFriction = atof(argv [3]);
    892.         printf("CoeffFriction = %f\n",CoeffFriction);
    893.     }
    894.  
    895.     if (argc >= 5)
    896.     {
    897.         InitialSpeed = atof(argv [4]);
    898.         printf("InitialSpeed = %f\n",InitialSpeed);
    899.     }
    900.  
    901.  
    902.  
    903.     printf("%d\n",LoadLibrary(TEXT("lib3ds-2_0.dll")));
    904.     printf("%d\n",GetLastError());
    905.  
    906.     // initialize glut
    907.     glutInit            ( &argc, argv );
    908.     glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
    909.     glutInitWindowSize  ( 1024, 786 );
    910.  
    911.  
    912.     //printf("\n%d\n", LoadLibrary(TEXT("opengl32.dll")));
    913.    
    914.     //glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC) GetProcAddress(GetModuleHandle(TEXT("opengl32.dll")), "glActiveTextureARB");
    915.    
    916.  
    917.    // if (glActiveTextureARB==0) {
    918.     //      MessageBoxA(0, "glActiveTextureARB=0", "glActiveTextureARB", MB_ICONERROR);
    919.     //      printf("\n%d\n", GetLastError());
    920.     //}
    921.  
    922.                                 // create window
    923. int    Wnd = glutCreateWindow ( "Visual Source V1.0.0" );
    924.  
    925.                                 // register handlers
    926.     glutDisplayFunc  ( display );
    927.     glutReshapeFunc  ( reshape );
    928.     glutKeyboardFunc ( key );
    929.     glutIdleFunc     ( animate );
    930.  
    931.  
    932.  
    933.     init           ();
    934.  
    935.                                            // setup vertices
    936.     //Lib3dsFile *file = lib3ds_file_open("box.3ds");
    937.  
    938.  
    939.  
    940.  
    941.  
    942.  
    943.  
    944.  
    945.  
    946.  
    947.     //lib3ds_file_free(file);
    948.    
    949.  
    950.     const char * vendor    = (const char *)glGetString ( GL_VENDOR     );
    951.     const char * renderer  = (const char *)glGetString ( GL_RENDERER   );
    952.     const char * version   = (const char *)glGetString ( GL_VERSION    );
    953.     const char * extension = (const char *)glGetString ( GL_EXTENSIONS );
    954.  
    955.     char  buffer [1024];
    956.  
    957.     sprintf(buffer, "Vendor: %s\nRenderer: %s\nVersion: %s\n", vendor, renderer, version);
    958.  
    959.  
    960.  
    961.  
    962.    
    963.     hFile = fopen("GL.log", "w");
    964.     fwrite(buffer, 1, strlen(buffer), hFile);
    965. ;
    966.  
    967.     printf ( "Vendor:   %s\nRenderer: %s\nVersion:  %s\n", vendor, renderer, version );
    968.  
    969.     printExtList ( extension );
    970.     fwrite(exten, 1, strlen(exten), hFile);
    971.     fclose(hFile);
    972.  
    973.      glutMainLoop ();
    974.      
    975.  
    976.  
    977.     return 0;
    978. }
    Вот проект целиком 10 Метров http://31.31.203.47/OpenGL_X.zip
     
  2. Araknu

    Araknu Member

    Публикаций:
    0
    У меня были рывки объектов в игре из-за того, что отрисовка идет в одном потоке, а изменение координат рисуемых объектов в другом потоке. Помогла синхронизация данных. Правда у меня java.
    У тебя как я понял все в одном потоке (в сях не шарю, грешен). Правильно будет выделить для изменения координат и других параметров, зависимых от времени, отдельный поток, то есть отрисовка отдельно, логика отдельно.
     
  3. skyproc

    skyproc New Member

    Публикаций:
    0
    Ну я об этом думал, хотя вот в уроке NEHE http://pmg.org.ru/nehe/nehe30.htm вычислений больше но лагов то нет как и многопоточности...
    Походу чтото в самой реализации...
     
  4. skyproc

    skyproc New Member

    Публикаций:
    0
    Все разобрался, частоты кадровой развертки маленькая была, дров криво поставлен и не настроена OpenGL. Буду ковырять API настройки OpenGL.