2016-10-11 7 views
2

Я рисую Icosahedron, но я не могу покрасить каждое его лицо, так что это может быть что-то вроде «разноцветного» икосаэдра.Разноцветный икосаэдр в OpenGL с использованием Glut

Как я могу это достичь?

Вот код до сих пор:

#include <freeglut.h> 

void init(); 
void display(void); 
void centerOnScreen(); 
void drawObject(); 

// define the window position on screen 
int window_x; 
int window_y; 

// variables representing the window size 
int window_width = 480; 
int window_height = 480; 

// variable representing the window title 
char *window_title = "Sample OpenGL FreeGlut App"; 

bool mouseDown = false; 

float xrot = 0.0f; 
float yrot = 0.0f; 

float xdiff = 0.0f; 
float ydiff = 0.0f; 



static GLfloat spin = 0.0; 
#define X .525731112119133606 
#define Z .850650808352039932 

//----------------------------------------------------------------------- 

#define X .525731112119133606 
#define Z .850650808352039932 

static GLfloat vdata[12][3] = { 
    { -X, 0.0, Z },{ X, 0.0, Z },{ -X, 0.0, -Z },{ X, 0.0, -Z }, 
    { 0.0, Z, X },{ 0.0, Z, -X },{ 0.0, -Z, X },{ 0.0, -Z, -X }, 
    { Z, X, 0.0 },{ -Z, X, 0.0 },{ Z, -X, 0.0 },{ -Z, -X, 0.0 } 
}; 
static GLuint tindices[20][3] = { 
    { 0,4,1 },{ 0,9,4 },{ 9,5,4 },{ 4,5,8 },{ 4,8,1 }, 
    { 8,10,1 },{ 8,3,10 },{ 5,3,8 },{ 5,2,3 },{ 2,7,3 }, 
    { 7,10,3 },{ 7,6,10 },{ 7,11,6 },{ 11,0,6 },{ 0,1,6 }, 
    { 6,1,10 },{ 9,0,11 },{ 9,11,2 },{ 9,2,5 },{ 7,2,11 } }; 
int i; 



//------------------------------------------------------------------------- 
// Set OpenGL program initial state. 
//------------------------------------------------------------------------- 
void init() 
{ 
    // Set the frame buffer clear color to black. 
    glClearColor(0.2, 0.3, 0.4, 0.5); 
} 

//------------------------------------------------------------------------- 
// This function is passed to glutDisplayFunc in order to display 
// OpenGL contents on the window. 
//------------------------------------------------------------------------- 
void display(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 

    gluLookAt(
     0.0f, 0.0f, 3.0f, 
     0.0f, 0.0f, 0.0f, 
     0.0f, 1.0f, 0.0f); 

    glRotatef(xrot, 1.0f, 0.0f, 0.0f); 
    glRotatef(yrot, 0.0f, 1.0f, 0.0f); 

    drawObject(); 

    glFlush(); 
    glutSwapBuffers(); 
} 

//------------------------------------------------------------------------- 
// Draws our object. 
//------------------------------------------------------------------------- 
void drawObject() 
{ 
    // Draw Icosahedron 

glBegin(GL_TRIANGLES);  
for (i = 0; i < 20; i++) {  
    /* color information here */ 
    glVertex3fv(&vdata[tindices[i][0]][0]); 
    glVertex3fv(&vdata[tindices[i][1]][0]); 
    glVertex3fv(&vdata[tindices[i][2]][0]); 
} 
glEnd(); 
} 

//------------------------------------------------------------------------- 
// This function sets the window x and y coordinates 
// such that the window becomes centered 
//------------------------------------------------------------------------- 
void centerOnScreen() 
{ 
    window_x = (glutGet(GLUT_SCREEN_WIDTH) - window_width)/2; 
    window_y = (glutGet(GLUT_SCREEN_HEIGHT) - window_height)/2; 
} 

void resize(int w, int h) 
{ 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    glViewport(0, 0, w, h); 

    gluPerspective(45.0f, 1.0f * w/h, 1.0f, 100.0f); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
} 

void idle() 
{ 
    if (!mouseDown) 
    { 
     xrot += 0.3f; 
     yrot += 0.4f; 
    } 

    glutPostRedisplay(); 
} 

void mouse(int button, int state, int x, int y) 
{ 
    if (button == GLUT_LEFT_BUTTON & state == GLUT_DOWN) 
    { 
     mouseDown = true; 

     xdiff = x - yrot; 
     ydiff = -y + xrot; 
    } 
    else 
     mouseDown = false; 
} 

void mouseMotion(int x, int y) 
{ 
    if (mouseDown) 
    { 
     yrot = x - xdiff; 
     xrot = y + ydiff; 

     glutPostRedisplay(); 
    } 
} 

//------------------------------------------------------------------------- 
// Program Main method. 
//------------------------------------------------------------------------- 
void main(int argc, char **argv) 
{ 
    // Connect to the windowing system + create a window 
    // with the specified dimensions and position 
    // + set the display mode + specify the window title. 
    glutInit(&argc, argv); 
    centerOnScreen(); 
    glutInitWindowSize(window_width, window_height); 
    glutInitWindowPosition(window_x, window_y); 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); 
    glutCreateWindow(window_title); 

    // Set OpenGL program initial state. 
    init(); 

    // Set the callback functions 
    glutDisplayFunc(display); 
    glutMouseFunc(mouse); 
    glutMotionFunc(mouseMotion); 
    glutReshapeFunc(resize); 

    // Start GLUT event processing loop 
    glutMainLoop(); 
} 
+1

Не уничтожьте свой пост. – dorukayhan

ответ

1

Проблема заключается в том, что в вашей drawObject() функции, вы никогда не установить цвет вершин. Вы только рисуете их. Вы можете установить их с помощью glColor4f(red, green, blue, alpha);. Вам нужно будет иметь либо список цветов, которые вы хотите для сторон, или иметь способ их вычисления в цикле, где вы рисуете каждую вершину. Если вы вызываете glColor4f() в начале цикла, это будет использовать тот же цвет для всех трех вершин.

Например, вы можете обновить свой метод drawObject() выглядеть следующим образом:

void drawObject() 
{ 
    // Draw Icosahedron 
    glBegin(GL_TRIANGLES);  
    for (i = 0; i < 20; i++) { 
     // Set the color for this face 
     glColor4f(cdata[i].red, cdata[i].green, cdata[i].blue, cdata[i].alpha); 

     // Set the vertices of this face 
     glVertex3fv(&vdata[tindices[i][0]][0]); 
     glVertex3fv(&vdata[tindices[i][1]][0]); 
     glVertex3fv(&vdata[tindices[i][2]][0]); 
    } 
    glEnd(); 
} 

Вы должны будете иметь массив лица цветов, что-то вроде этого:

typedef struct RGBAColor { 
    float red; 
    float green; 
    float blue; 
    float alpha; 
} RGBAColor; 

RGBAColor cdata[] = { 
    { 1.0, 0.0, 0.0, 1.0 }, // Red 
    ... etc. for whatever colors you want the faces to be 
}; 
+0

Спасибо за ваше время! Можете ли вы дать мне рабочий пример о раскраске? – hopittyhop

+0

ОК, я добавил короткий примерный код, показывающий, что я имел в виду. – user1118321

 Смежные вопросы

  • Нет связанных вопросов^_^