2017-04-25 62 views
1

我有一個代碼,找出在國際象棋棋盤單擊單元格鼠標,那麼二維三角形去細胞位置,現在我想2D三角變爲3D立方體,但我不知道怎麼做。變化2D物體的3D對象的OpenGl

這是我的代碼

#include <string> 
#include <stdlib.h> 
#ifdef __APPLE__ 
#include <OpenGL/OpenGL.h> 
#include <GLUT/glut.h> 
#else 
#include <GL/glut.h> 
#endif 
#include <iostream> 
using namespace std; 

void init() 
{ 
    glClearColor (1.0, 0.0, 1.0, 0.0); 
    glShadeModel (GL_FLAT); 
} 
const int scl = 80 ; 
const int STEP_SIZE = 80; 
const int WIN_WIDTH = 800; 
const int WIN_HEIGHT = 800; 
int LeftRight = 0 ; 
int UpDown = 0; 

void moveRight() 
{ 
    LeftRight+=STEP_SIZE; 
} 
void moveLeft() 
{ 
    LeftRight-=STEP_SIZE; 
} 
void moveUp() 
{ 
    UpDown+=STEP_SIZE; 
} 
void moveDown() 
{ 
    UpDown-=STEP_SIZE; 
} 
void DrawBoard() 
{ 
    int x , y , color = 0; 
    glClear (GL_COLOR_BUFFER_BIT); 
    for(x=1; x<=8; x++) 
    { 
     if(color==0) 
      glColor3f (1.0, 0.0, 0.0), color++; 

     else 
      glColor3f (1.0, 1.0, 1.0),color=0; 

     for(y=1; y<=8; y++) 
     { 
      if(color==0) 
       glColor3f (0.0, 0.0, 0.0),color++; 
      else 
       glColor3f (1.0, 1.0, 1.0),color=0; 

      glBegin(GL_QUADS); 
      glVertex2f(scl+scl*x, scl+scl*y); 
      glVertex2f(scl*x, scl+scl*y); 
      glVertex2f(scl*x, scl*y); 
      glVertex2f(scl+scl*x, scl*y); 
      glEnd(); 
     } 
    } 
    // This is the Triangle that will change to cube 
    glBegin(GL_QUADS); 
    glColor3f (1.0, 0.5, 0.0); 
    glVertex2f(90+LeftRight,100+UpDown); 
    glVertex2f(90+LeftRight,100+UpDown); 
    glVertex2f(120+LeftRight,140+UpDown); 
    glVertex2f(150+LeftRight,100+UpDown); 
    glEnd(); 
//-------------------------------------- 
    glFlush(); 


} 
int mouse_x = -1 ; 
int mouse_y = -1 ; 
int object_x = -1 ; 
int object_y = -1 ; 

void moveObjectToFrom() 
{ 
    if (mouse_x > 8 || mouse_x < 1 || mouse_y > 8 || mouse_y < 1) 
     return ; 
    if(object_x > mouse_x) 
    { 
     object_x-- ; 
     moveUp(); 
    } 
    if(object_x < mouse_x) 
    { 
     object_x++; 
     moveDown(); 

    } 
    if(object_y > mouse_y) 
    { 
     object_y-- ; 
     moveLeft(); 

    } 
    if(object_y < mouse_y) 
    { 
     object_y++ ; 
     moveRight(); 
    } 

} 

void mouseClicks(int button , int key, int x, int y) 
{ 
    // this variables related to chessboard 
    mouse_x = y/scl ; 
    mouse_y = 9 - (WIN_HEIGHT - x)/scl ; 
    object_x = 9 - (100 + UpDown)/ scl ; 
    object_y = 9 - (WIN_HEIGHT - (LeftRight+90))/scl ; 

    if (button !=0 || mouse_x > 8 || mouse_x < 1 || mouse_y > 8 || mouse_y < 1) 
     return ; 

    glutPostRedisplay(); 
} 


void reshape (int w, int h) 
{ 
    glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h); 
} 

void update(int value) 
{ 

    moveObjectToFrom(); 
    glutTimerFunc(50, update, 0); 
    glutPostRedisplay(); 
} 

int main(int argc, char** argv) 
{ 
    glutInit(&argc, argv); 
    // glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
    glEnable(GL_DEPTH_TEST); 
    glutInitWindowSize (WIN_WIDTH, WIN_HEIGHT); 
    glutInitWindowPosition (100,100); 
    glutCreateWindow (argv[0]); 
    glutTimerFunc(25, update, 0); 
    glutMouseFunc(mouseClicks); 
    init(); 
    glutDisplayFunc(DrawBoard); 
    glutReshapeFunc(reshape); 
    glutMainLoop(); 
    return 0; 
} 
+0

所以要映射在3D場景中點擊鼠標?如果是這樣,你可以在https://www.opengl.org/discussion_boards/showthread.php/175699-How-to-click-on-3D-objects-with-mouse看一看,看看這些答案的人幫助您。 – Aumnayan

回答

1

你的主要問題是,所以當你渲染軸對齊立方體,它會永遠只生產方的GL_PROJECTION設置爲gluOrtho2D。爲了解決這個問題,你應該使用gluProjection。但是,這將使你周圍景觀0.0,0.0,代替目前的屏幕中心爲中心座標(0.5*w,0.5*h)所以你需要也通過它翻譯GL_MODELVIEW

這裏例如如何做到這一點:

我們呈現一個立方體,你可以這樣做:

void render_cube() 
    { 
    const GLfloat a=20.0; // cube half size 
    const GLfloat pos[]= // glVertex cube centered at (0,0,0) of size 2*a 
     { 
    // x y z 
     -a,+a,-a, 
     +a,+a,-a, 
     +a,-a,-a, 
     -a,-a,-a, 

     -a,-a,+a, 
     +a,-a,+a, 
     +a,+a,+a, 
     -a,+a,+a, 

     -a,-a,-a, 
     +a,-a,-a, 
     +a,-a,+a, 
     -a,-a,+a, 

     +a,-a,-a, 
     +a,+a,-a, 
     +a,+a,+a, 
     +a,-a,+a, 

     +a,+a,-a, 
     -a,+a,-a, 
     -a,+a,+a, 
     +a,+a,+a, 

     -a,+a,-a, 
     -a,-a,-a, 
     -a,-a,+a, 
     -a,+a,+a, 
     }; 
    const GLfloat col[]= 
     { 
    // r g b  
     1.0,0.0,0.0, 
     1.0,0.0,0.0, 
     1.0,0.0,0.0, 
     1.0,0.0,0.0, 

     1.0,1.0,0.0, 
     1.0,1.0,0.0, 
     1.0,1.0,0.0, 
     1.0,1.0,0.0, 

     0.0,1.0,0.0, 
     0.0,1.0,0.0, 
     0.0,1.0,0.0, 
     0.0,1.0,0.0, 

     0.0,1.0,1.0, 
     0.0,1.0,1.0, 
     0.0,1.0,1.0, 
     0.0,1.0,1.0, 

     0.0,0.0,1.0, 
     0.0,0.0,1.0, 
     0.0,0.0,1.0, 
     0.0,0.0,1.0, 

     1.0,1.0,1.0, 
     1.0,1.0,1.0, 
     1.0,1.0,1.0, 
     1.0,1.0,1.0, 
     }; 
    const GLfloat nor[]= // glNormal 
     { 
    // nx ny nz 
     0.0, 0.0,-1.0, 
     0.0, 0.0,-1.0, 
     0.0, 0.0,-1.0, 
     0.0, 0.0,-1.0, 

     0.0, 0.0,+1.0, 
     0.0, 0.0,+1.0, 
     0.0, 0.0,+1.0, 
     0.0, 0.0,+1.0, 

     0.0,-1.0, 0.0, 
     0.0,-1.0, 0.0, 
     0.0,-1.0, 0.0, 
     0.0,-1.0, 0.0, 

     +1.0, 0.0, 0.0, 
     +1.0, 0.0, 0.0, 
     +1.0, 0.0, 0.0, 
     +1.0, 0.0, 0.0, 

     0.0,+1.0, 0.0, 
     0.0,+1.0, 0.0, 
     0.0,+1.0, 0.0, 
     0.0,+1.0, 0.0, 

     -1.0, 0.0, 0.0, 
     -1.0, 0.0, 0.0, 
     -1.0, 0.0, 0.0, 
     -1.0, 0.0, 0.0, 
     }; 
    int i,i3; 
    glBegin(GL_QUADS); 
    for (i=0,i3=0;i<24;i++,i3+=3) 
     { 
     glColor3fv (col+i3); 
     glNormal3fv(nor+i3); 
     glVertex3fv(pos+i3); 
     } 
    glEnd(); 
    } 

這是爲了使這樣的:

glEnable(GL_LIGHTING); 
glEnable(GL_LIGHT0); 
glEnable(GL_COLOR_MATERIAL); 
glEnable(GL_CULL_FACE); 
glFrontFace(GL_CCW); 
render_cube(); 

只設置a常數,你需要什麼都和翻譯模型觀察到其設置位置...

這裏預覽:

cube

每一方都有自己的顏色進行調試...看看這裏的3D視圖例如:

[EDIT1]我調整你的代碼有點

有多邊形一些不一致的地方繞組。嘗試用這些來交換你的函數(他們的工作對我來說),並留下您的代碼的其餘部分是:

//--------------------------------------------------------------------------- 
void render_cube(GLfloat a) // cube half size 
    { 
    GLfloat pos[]= // glVertex 
     { 
    // x y z 
     -a,+a,-a, 
     +a,+a,-a, 
     +a,-a,-a, 
     -a,-a,-a, 

     -a,-a,+a, 
     +a,-a,+a, 
     +a,+a,+a, 
     -a,+a,+a, 

     -a,-a,-a, 
     +a,-a,-a, 
     +a,-a,+a, 
     -a,-a,+a, 

     +a,-a,-a, 
     +a,+a,-a, 
     +a,+a,+a, 
     +a,-a,+a, 

     +a,+a,-a, 
     -a,+a,-a, 
     -a,+a,+a, 
     +a,+a,+a, 

     -a,+a,-a, 
     -a,-a,-a, 
     -a,-a,+a, 
     -a,+a,+a, 
     }; 
    const GLfloat col[]= // glColor 
     { 
    // r g b 
     1.0,0.0,0.0, 
     1.0,0.0,0.0, 
     1.0,0.0,0.0, 
     1.0,0.0,0.0, 

     1.0,1.0,0.0, 
     1.0,1.0,0.0, 
     1.0,1.0,0.0, 
     1.0,1.0,0.0, 

     0.0,1.0,0.0, 
     0.0,1.0,0.0, 
     0.0,1.0,0.0, 
     0.0,1.0,0.0, 

     0.0,1.0,1.0, 
     0.0,1.0,1.0, 
     0.0,1.0,1.0, 
     0.0,1.0,1.0, 

     0.0,0.0,1.0, 
     0.0,0.0,1.0, 
     0.0,0.0,1.0, 
     0.0,0.0,1.0, 

     1.0,1.0,1.0, 
     1.0,1.0,1.0, 
     1.0,1.0,1.0, 
     1.0,1.0,1.0, 
     }; 
    const GLfloat nor[]= // glNormal 
     { 
    // nx ny nz 
     0.0, 0.0,-1.0, 
     0.0, 0.0,-1.0, 
     0.0, 0.0,-1.0, 
     0.0, 0.0,-1.0, 

     0.0, 0.0,+1.0, 
     0.0, 0.0,+1.0, 
     0.0, 0.0,+1.0, 
     0.0, 0.0,+1.0, 

     0.0,-1.0, 0.0, 
     0.0,-1.0, 0.0, 
     0.0,-1.0, 0.0, 
     0.0,-1.0, 0.0, 

     +1.0, 0.0, 0.0, 
     +1.0, 0.0, 0.0, 
     +1.0, 0.0, 0.0, 
     +1.0, 0.0, 0.0, 

     0.0,+1.0, 0.0, 
     0.0,+1.0, 0.0, 
     0.0,+1.0, 0.0, 
     0.0,+1.0, 0.0, 

     -1.0, 0.0, 0.0, 
     -1.0, 0.0, 0.0, 
     -1.0, 0.0, 0.0, 
     -1.0, 0.0, 0.0, 
     }; 
    int i,i3; 
    glBegin(GL_QUADS); 
    for (i=0,i3=0;i<24;i++,i3+=3) 
     { 
     glColor3fv (col+i3); 
     glNormal3fv(nor+i3); 
     glVertex3fv(pos+i3); 
     } 
    glEnd(); 
    } 
//--------------------------------------------------------------------------- 
void init() 
    { 
    glClearColor(1.0, 0.0, 1.0, 0.0); 
    glShadeModel(GL_FLAT); 
    glEnable(GL_DEPTH_TEST); 
    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
    glEnable(GL_COLOR_MATERIAL); 
    glEnable(GL_CULL_FACE); 
    glFrontFace(GL_CCW); 
    } 
//--------------------------------------------------------------------------- 
const int scl = 80 ; 
int LeftRight = 0 ; 
int UpDown = 0; 
void DrawBoard() 
    { 
    int x , y , color = 0; 
    glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // do not forget also to clear depth buffer 
    for(x=1;x<=8;x++) 
     { 
     if (color) glColor3f (0.0, 0.0, 0.0);   // I like this a bit better then your approach 
     else  glColor3f (1.0, 1.0, 1.0); color^=1; 
     for(y=1; y<=8; y++) 
      { 
      if (color) glColor3f (0.0, 0.0, 0.0); 
      else  glColor3f (1.0, 1.0, 1.0); color^=1; 
      glBegin(GL_QUADS); 
      glNormal3f(0.0,0.0,+1.0); 
      glVertex3f(scl+scl*x, scl+scl*y,0.0); 
      glVertex3f(scl*x, scl+scl*y,0.0); 
      glVertex3f(scl*x, scl*y,0.0); 
      glVertex3f(scl+scl*x, scl*y,0.0); 
      glEnd(); 
      } 
     } 
    // your triangle just above cube (and reversed order to match the rest of rendering) 
    glBegin(GL_QUADS); 
    glColor3f (1.0, 0.5, 0.0); 
    glNormal3f(0.0,0.0,+1.0); 
    glVertex3f(150+LeftRight,100+UpDown,scl+1.0); 
    glVertex3f(120+LeftRight,140+UpDown,scl+1.0); 
    glVertex3f(90+LeftRight,100+UpDown,scl+1.0); 
    glVertex3f(90+LeftRight,100+UpDown,scl+1.0); 
    glEnd(); 
    // cube 
    glMatrixMode(GL_MODELVIEW); 
    glPushMatrix();        // remember modelview 
    glTranslatef(1.5*scl+LeftRight,1.5*scl+UpDown,+0.5*scl); // set cube position 
    render_cube(0.5*scl); 
    glPopMatrix();        // restore modelview 
    glFlush(); 
    } 
//--------------------------------------------------------------------------- 
void reshape (int w, int h) 
    { 
    glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(60,float(w)/float(h),10.0,10000.0); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glTranslatef(-w/2,-h/2,-1000.0); 
    } 
//--------------------------------------------------------------------------- 

使用爲800x800窗口中的結果應該是這樣的:

preview

順便說一句。這些包括已過時:

#include <string> 
#include <stdlib.h> 
#include <iostream> 
using namespace std; 

因爲你是不是在你的代碼中使用從他們什麼。

+0

我試圖做到這一點,但沒有,我只是一個初學者,所以你會幫我合併兩個代碼請! – Amr

+0

@Amr 1。如果你的問題還沒有解決,那麼不要接受它作爲答案(因爲接受標誌,你的問題被解決,這意味着有很低的機會,其他人會增加新的答案)。 2.您首先需要更改投影矩陣,以便它具有透視投影,並且您可以像使用正交投影一樣查看視圖。因此,您需要試驗'znear,zfar'棋盤放置位置('vertex3f' z座標)並將棋盤放置得更深,以便在它之前仍然有立方體的位置。只有這樣才能繼續下去。 – Spektre

+0

@Amr不要忘記翻譯模型視圖,或者圍繞「(0,0)」將網格居中,否則你會看到你的棋盤。 3.你是否改變了立方體大小的「a」常數,以便它真正適合視圖內部?我預計這個尺寸將會是棋盤格的一半。 4.看到我的答案中的鏈接,你的設置應該是相似的,如果不相同。 5.「什麼都沒有」意味着什麼?你沒有看到任何東西,或只是立方體沒有呈現或什麼?在更新後查看代碼會很高興。重要的東西是渲染循環和相機的設置 – Spektre