nicogno
nicogno

Reputation: 57

See the same color for different points OpenGL

I'm trying to show a 3D matrix made up of 7600700 points and I want to obtain a parallelepiped that is black on the surface and red inside. Every point correspond to a different energy value and i use only different shades of red to do that. I have the parallelepiped and it's black on the surface and red inside, but I can see only one shade of red and not different shades of it. Here's part of my code:

#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;
}

I've got float values (something like 10^(-7), 10^(-6), ..., 10^(-1) ) and so I used a logarithm to transform those small values into something bigger and that I subtracted those new values from 255 to obtain a proper value for my R value (I want to have red shades so I set G = 0 and B = 0).
Should I add normals? Thank you for your help.

Upvotes: 1

Views: 105

Answers (1)

user3078414
user3078414

Reputation: 1937

First, I see some confusing calculation, mixing ints floats and doubles. As your code isn't complete enough so I can compile and reproduce, it's probably valid, but I just find it difficult to read. I'd use fabsf():

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

Second, and this may be the cause of your problem (hiding in the difficult-to-read line), where and how are the color buffer data types defined? AFAIK, float colors in OpenGL get clamped between 0.0 and 1.0, while you have them between 0. and 255. and this may be the reason that you get only one nuance of red. Try to normalize all the color values in the above line between 0. and 1. and see what you'll get.

Hope this can help.

Upvotes: 1

Related Questions