【问题标题】:See the same color for different points OpenGL不同点OpenGL看到相同的颜色
【发布时间】:2016-05-30 09:00:28
【问题描述】:

我试图展示一个由 7600700 个点组成的 3D 矩阵,我想获得一个表面为黑色而内部为红色的平行六面体。每个点对应一个不同的能量值,我只使用不同的红色来做到这一点。我有平行六面体,它的表面是黑色的,里面是红色的,但我只能看到一种红色而不是不同的红色。 这是我的部分代码:

#define MATRIX_FILE "dose_file.dat"

struct Point
{
    GLfloat x, y, z;
    GLfloat r, g, b, a;
};
static vector< Point > points; //Vettore points di elementi Point

void fill_data() {

    g_vertex_buffer_data = new GLfloat[3*p_mat_size_tot];
    int i=0;
    int k=0;

    for(int z=0; z<263; z++)
        for(int y=0; y<170; y++)
            for(int x=0; x<170; x++) {
                g_vertex_buffer_data[i]=(GLfloat)x+0.5;
                g_vertex_buffer_data[i+1]=(GLfloat)y+0.5;
                g_vertex_buffer_data[i+2]=(GLfloat)z+0.5;
                i+=3;
            }

    g_color_buffer_data=new GLfloat[3*p_mat_size_tot];

    for(int j=0;j<(3*p_mat_size_tot);j+=3) {
        if(p_val[k]!=0.0) {
            g_color_buffer_data[j]=(GLfloat)(255-13*fabs(logf(p_val[k])));//g_color_buffer_data[j]=(GLfloat)(255-30*fabs(log10f(p_val[k])));
        } else {
            g_color_buffer_data[j]=0.0;} 
            g_color_buffer_data[j+1]=0.0;
            g_color_buffer_data[j+2]=0.0;
            k++;
        }

    for( size_t i = 0; i < (3*p_mat_size_tot); i+=3 )
    {
        Point pt;
        pt.x = g_vertex_buffer_data[i];
        pt.y = g_vertex_buffer_data[i+1];
        pt.z = g_vertex_buffer_data[i+2];
        pt.r = g_color_buffer_data[i];
        pt.g = g_color_buffer_data[i+1];
        pt.b = g_color_buffer_data[i+2];
        pt.a = 255.0;
        points.push_back(pt);
    }    
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();

    gluLookAt (85.0, 85.0, 526.0, 85.0, 85.0, 131.5, 0.0, 1.0, 0.0);
    glPushMatrix();
    glRotatef(step1, 1.0f, 0.0f, 0.0f);//Rotazione attorno all'asse x
    glRotatef(step2, 0.0f, 1.0f, 0.0f);//Rotazione attorno all'asse y
    glScalef(step3, step3, step3);//Zoom
    glTranslatef(0.0f, 0.0f, step4);//Traslazione lungo z
    glTranslatef(step5, 0.0f, 0.0f);//Traslazione lungo x
    glTranslatef(0.0f, step6, 0.0f);//Traslazione lungo y

    glMatrixMode(GL_MODELVIEW); //Lavoriamo sulla matrice del modello

    // draw
    //glColor3ub( 255, 255, 255 ); //Setta il colore corrente
    glEnableClientState( GL_VERTEX_ARRAY ); //Abilita il l'array di vertici
    glEnableClientState( GL_COLOR_ARRAY );  //Abilita l'array di colori 
    glVertexPointer( 3, GL_FLOAT, sizeof(Point), &points[0].x ); //Definisce un array di dati di vertici (numero di coordinate, tipo, offset nelle coordinate tra punti consecutivi, puntatore alla prima coordinata del primo vertice nell'array)
    glColorPointer( 4, GL_FLOAT, sizeof(Point), &points[0].r ); //Come sopra, ma con i colori
    glPointSize( 3.0 );
    glDrawArrays( GL_POINTS, 0, points.size()); //Disegna points.size() punti partendo dal primo
    glDisableClientState( GL_VERTEX_ARRAY );
    glDisableClientState( GL_COLOR_ARRAY );

    glFlush(); //Forza l'esecuzione dei comandi GL in un tempo finito
    glPopMatrix();
    glutSwapBuffers(); //Swap dei buffers front e back della finestra (riserva memoria per inserire i comandi dati dall'utente)
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glFrustum (-0.5, 0.5, -0.5, 0.5, 1.0, 1000.0); //glFrustum (-0.5, 0.5, -0.5, 0.5, 1.0, 264.0);
    glMatrixMode (GL_MODELVIEW);
}

void keyPressed(unsigned char key, int x, int y)
{
    switch(key)
    {
    .
    .
    .
    glutPostRedisplay();
}


int main(int argc, char** argv)
{
    .
    .
    .
    glutInit(&argc, argv); //Inizializza glut
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); //Display mode iniziale

    glutInitWindowSize(800,800);
    glutInitWindowPosition (100, 100);
    glutCreateWindow("Test");

    glClearColor (255.0, 255.0, 255.0, 0.0);
    glEnable(GL_DEPTH_TEST);

    fill_data();

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyPressed);

    glutMainLoop();
    return 0;
}

我有浮点值(类似于 10^(-7), 10^(-6), ..., 10^(-1) ),所以我使用对数将这些小值转换成一些东西更大,我从 255 中减去这些新值以获得适合我的 R 值的值(我想要红色阴影,所以我设置 G = 0 和 B = 0)。
我应该添加法线吗? 感谢您的帮助。

【问题讨论】:

  • 您可能应该将代码缩减为一个最小但完整的示例。
  • 好的,谢谢,我做到了
  • 还没有,抱歉…… (-:
  • 我应该添加法线吗?
  • 是的,您真的应该减少代码大小或以某种方式对其进行注释(英文),它既不短也不易于阅读。在这种情况下,不太可能有人会调查它..

标签: c++ opengl graphics


【解决方案1】:

首先,我看到了一些令人困惑的计算,混合了 ints floats 和 doubles。由于您的代码不够完整,因此我可以编译和重现,它可能是有效的,但我发现它很难阅读。我会使用fabsf():

g_color_buffer_data[j]=(GLfloat)(255.-13*fabs(logf(p_val[k])));

第二,这可能是您的问题的原因(隐藏在难以阅读的行中),颜色缓冲区数据类型在哪里以及如何定义? AFAIK,OpenGL 中的float 颜色被限制在0.0 和1.0 之间,而您将它们介于0. 和255 之间。这可能是你只得到红色的一种细微差别的原因。尝试规范化以上行中0. 和1. 之间的所有颜色值,看看你会得到什么。

希望这能有所帮助。

【讨论】:

  • 非常感谢你!!!你是对的!如果我正常化,我可以看到红色阴影:) !!
  • 只有一个经验法则:尽可能简单编写代码是成功的一半以上。祝你好运! (-:
  • 我会努力做到的!谢谢:-)
猜你喜欢
  • 2011-06-12
  • 2020-12-16
  • 1970-01-01
  • 2015-08-31
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多