Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: The very basics of shading, where do I start?

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2012
    Posts
    5

    The very basics of shading, where do I start?

    So I am incredibly new to OpenGL and have to learn for school, however, our teacher just hands out assignments, without any background on opengl whatsoever. So I'm stuck.

    I'm working on visualization and one of the ones I want to do is a a solid colored model. I already have code that makes a wireframe of the data I'm trying to visualize, using the code below:

    Code :
     
    /****************************************************************************
     * Get texture image from pgm- or ppm-file.
     * Format of input file:
     * - line containing ascii file type, e.g. strings 'P5' or 'P6'
     * - optional ascii comment lines starting with '#'
     * - ascii line containing horizontal and vertical size separated by space
     * - ascii line containing number '255'
     * - binary pixel values: single byte per pixel with grayscale if type P5 (pgm)
     * -                      triples of bytes with RGB values if type P6 (ppm)
     ***************************************************************************/
    void getImageFromFile (char filename[])
    {
    #define inputLineLength 81
     
        int i, j;
        int byteValue, intensityLevels;
        char inputLine[inputLineLength], typeOfFile[3];
        FILE *f = fopen (filename, "r");
     
        if (f == NULL)        /* check if file exists */
        { fprintf (stderr, "\nERROR: Failed to open file.\n");
            exit (1);
        }
     
        /* Get file type. */
        do  fgets (inputLine, inputLineLength-1, f);  while (inputLine[0] == '#');
        typeOfFile[0] = inputLine[0];
        typeOfFile[1] = inputLine[1];
        typeOfFile[2] = '\0';
        printf ("  file type:  %s\n", typeOfFile);
     
        /* Get horizontal en vertical size. */
        do  fgets (inputLine, inputLineLength-1, f);  while (inputLine[0] == '#');
        sscanf (inputLine, "%d %d", &texImageWidth, &texImageHeight);
        printf ("  horizontal: %d\n", texImageWidth);
        printf ("  vertical:   %d\n", texImageHeight);
        /* Allocate array that will contain the texture image. */
        texImage1 =
        (GLubyte *) malloc (texImageWidth*texImageHeight*4*sizeof(GLubyte));
     
        /* Get number with maximum number of shades. */
         do  fgets (inputLine, inputLineLength-1, f);  while (inputLine[0] == '#');
         sscanf (inputLine, "%d", &intensityLevels);
         printf ("  int levels: %d\n", intensityLevels); 
     
        /* Copy binary pixel values into texture image array. */
        for (i = texImageHeight-1; i >= 0; i--)
        { for (j = 0; j < texImageWidth; j++)
        { int position = (i*texImageWidth+j)*4;
            /* read and store RGB or grayscale values*/
            byteValue = getc (f);     /* R-value or grayscale value */
            texImage1[position] = (GLubyte) byteValue;
            if (typeOfFile[1] == '6')
                byteValue = getc (f);   /* G-value if file type = R6 */
            texImage1[position+1] = (GLubyte) byteValue;
            if (typeOfFile[1] == '6')
                byteValue = getc (f);   /* B-value if file type = R6 */
            texImage1[position+2] = (GLubyte) byteValue;
            /*  alpha-value */
            texImage1[position+3] = (GLubyte) 255;
        }
        }
    }
     
     
    void init(void)
    {
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glMatrixMode(GL_MODELVIEW);
     
        getImageFromFile ("ltgraf22.pgm");
     
    }
     
     
     
    void display3(void)
    {
     
        glClear (GL_COLOR_BUFFER_BIT);
        glColor3f(1, 1, 1);
     
        glRotated(5, 1, 0, 0); // x
        glRotated(0, 0, 1, 0); // y
        glRotated(-45, 0, 0, 1); // z
        glTranslated(0, -10, 0);
     
     
     
        for (int i = texImageHeight-1; i >= 0; i--)
     
        {
            for (int j = 0; j < texImageWidth; j++)
            {
                int p = (i*texImageWidth+j)*4;
                int pi1 = ((i+1)*texImageWidth+(j))*4;
                int pj1 = ((i)*texImageWidth+(j+1))*4;
     
     
                glLineWidth(1);
                glBegin(GL_LINES);
                glVertex3d(j-50, i-50, -texImage1[p]+110);
                glVertex3d(j+1-50, i-50, -texImage1[pj1]+110);
                glEnd();
     
                glBegin(GL_LINES);
                glVertex3d(j-50, i-50, -texImage1[p]+110);
                glVertex3d(j-50, i-50+1, -texImage1[pi1]+110);
                glEnd();
     
     
            }
        }
     
     
     
        glutSwapBuffers();
    }
     
    /****************************************************************************
     * Reshape 3D windows
     ****************************************************************************/
    void resize3D (int w, int h)
    {
        int w2 = w / 100;
        int h2 = h / 100;
     
        glViewport (0, 0, w, h);
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
     
        /* Original size of objects will be kept. */
        glOrtho(-80, 80, -80, 80, 1, 146);
     
     
     
        if ( w2 == h2)
            pointsize = w2;
        glFlush ();
    }
     
     
    /*  Window 3d wire */
        glutInitWindowSize(WINSIZE_X*2, WINSIZE_Y*2);
        glutInitWindowPosition(WINPOS_X-WINSIZE_X*2-30, WINPOS_Y+WINSIZE_Y+30);
        glutCreateWindow("3D gray");
        init2();
        glutDisplayFunc(display3);
        glutKeyboardFunc(keyboard3D);
        glutReshapeFunc(resize3D);


    So I think to turn this wireframe into a solid colored model I should use, perhaps, vertex shading, or flat shading? Since I build my wireframe out of all separate vertices. But I can't find anything on google on just the very basics of shading. I can give each vertex a different colour, but then it's still just wireframe. I've seen countless examples on what a shader does, and how to write your own "void shader", but can anyone give me any insight on where to start? How do I get something like this shaded? Do I really need to write that much more code like google seems to tell me (or the websites I found anyway) or is there a built in function that can help me out?

    I honestly don't really care about lightning and everything, i just want to turn this into a solid model ....

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,194
    Read up on OpenGL primitive types in the OpenGL Programming Guide.

  3. #3
    Intern Newbie
    Join Date
    Jan 2013
    Posts
    42
    I really thought this tutorial was useful when I started without any knowledge of shaders:

    http://www.lighthouse3d.com/opengl/glsl/

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    There is a good e-book "OpenGL 4.0 Shading Language Cookbook"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •