2016-11-04 36 views
-1

固定顏色:需要一個glShadeModel(GL_SMOOTH);呈現在Java中的OpenGL彩虹魔影並沒有融入

當前的結果:http://prntscr.com/d3ev6j

public static void drawBlendRectangle(double x, double y, double x1, double y1, int... colors) { 
    glPushMatrix(); 
    glDisable(GL_TEXTURE_2D); 
    glEnable(GL_BLEND); 
    glShadeModel(GL_SMOOTH); 
    glBegin(GL_QUADS); 

    double height = (y1 - y)/colors.length; 
    for (int i = 0; i < colors.length - 1; i++) {   
     float cTop[] = RenderUtils.getRGBA(colors[i]);  
     float cBottom[] = RenderUtils.getRGBA(colors[i + 1]);  
     glColor4f(cTop[0], cTop[1], cTop[2], cTop[3]); 
     glVertex2d(x, y + i * height); // top-left 
     glVertex2d(x1, y + i * height); // top-right 
     glColor4f(cBottom[0], cBottom[1], cBottom[2], cBottom[3]); 
     glVertex2d(x1, y + i * height + height); // bottom-right 
     glVertex2d(x, y + i * height + height); // bottom-left 
    } 
    glEnd(); 
    glDisable(GL_BLEND); 
    glEnable(GL_TEXTURE_2D); 
    glPopMatrix(); 
} 

我有一個問題,有讓我矩形混合所有的顏色以一種奇特的方式,但不能得到它的工作 這裏有一張圖片:http://prntscr.com/d3767e

這裏是我創建了功能,我不是很熟悉的OpenGL,所以我真的不知道,如果我在混合過程中丟失的部分或我做這完全錯了

public static void drawBlendRectangle(double x, double y, double x1, double y1, int... colors) { 
    glPushMatrix(); 
    glDisable(GL_TEXTURE_2D); 
    glEnable(GL_BLEND); 
    glBegin(GL_QUADS); 
    for (int i = 0; i < colors.length; i++) { 
     float c[] = RenderUtils.getRGBA(colors[i]); 
     double height = (y1 - y)/colors.length; 

     glColor4d(c[0], c[1], c[2], 0.5f); 
     glVertex2d(x, y + i * height); // top-left 
     glVertex2d(x1, y + i * height); // top-right 
     glVertex2d(x1, y + i * height + height); // bottom-right 
     glVertex2d(x, y + i * height + height); // bottom-left 
    } 
    glEnd(); 
    glDisable(GL_BLEND); 
    glEnable(GL_TEXTURE_2D); 
    glPopMatrix(); 
} 

getRGBA( ...)只給出了從整數顏色值的顏色float數組,這不是一個問題,提前

+0

你的顏色正在尋找錯誤看看[RGB可見光譜的值(http://stackoverflow.com/a/22681410/2521214) – Spektre

回答

0

感謝您在四邊形的四角需要不同的顏色。

double height = (y1 - y)/colors.length; 
for (int i = 0; i < colors.length - 1; i++) {   
    float cTop[] = RenderUtils.getRGBA(colors[i]);  
    float cBottom[] = RenderUtils.getRGBA(colors[i + 1]);  
    glColor4f(cTop[0], cTop[1], cTop[2], 0.5f); 
    glVertex2d(x, y + i * height); // top-left 
    glVertex2d(x1, y + i * height); // top-right 
    glColor4f(cBottom[0], cBottom[1], cBottom[2], 0.5f); 
    glVertex2d(x1, y + i * height + height); // bottom-right 
    glVertex2d(x, y + i * height + height); // bottom-left 
} 

請注意,這隻會內插RGB值。如果你想要物理合理的插值,你需要一個自定義的着色器。

您可能還想考慮使用三角形條而不是四元列表。這將如下所示:

glBegin(GL_TRIANGLE_STRIP); 
double height = (y1 - y)/colors.length; 
for (int i = 0; i < colors.length; i++) {   
    float c[] = RenderUtils.getRGBA(colors[i]);  
    glColor4f(c[0], c[1], c[2], 0.5f); 
    glVertex2d(x, y + i * height); // left 
    glVertex2d(x1, y + i * height); // right 
} 
glEnd(); 
+0

需要glShadeModel(GL_SMOOTH) ; 感謝無論如何,我需要使用第一個解決方案來爲它們着色:) – NetherSoul