Recent Updates Page 3 Toggle Comment Threads | Keyboard Shortcuts

  • Ali BaderEddin 10:52 am on May 21, 2010 Permalink | Reply
    Tags: 3d, array, jagged, Multi-dimensional, Multidimensional Array   

    Dynamic Three Dimensional Arrays in C\C++\C#\Java 


    If you come from a Java or C# perspective and want to create a multi-dimensional array in C or C++, you’ll soon figure out that multi-dimensional array allocation in C\C++ is not as simple, plus you’ll have to worry about deallocation since there is no garbage collector to do the work for you. Below I’ll show four different sample codes showing how to work with a three dimensional array in Java, C#, C++ and C, respectively.

    Java 3D Array

    In Java, creating a 3-dimensional array is as simple as saying

    int[][][] array3D = new int[x][y][z];

    You can then access the elements of the 3-dimensional array at array3D[i][j][k].

    Sample Code

    public static void main(String[] args)
    {
        //  Array 3 Dimensions
        int x = 4, y = 5, z = 6;
    
        //  Array Iterators
        int i, j, k;
    
        //  Allocate 3D Array
        int[][][] array3D = new int[x][y][z];
    
        //  Access array elements
        for (i = 0; i < x; i++)
        {
            System.out.println(i);
    
            for (j = 0; j < y; j++)
            {
                System.out.println();
    
                for (k = 0; k < z; k++)
                {
                    array3D[i][j][k] = (i * y * z) + (j * z) + k;
                    System.out.print("\t" + array3D[i][j][k]);
                }
            }
    
            System.out.println('\n');
        }
    }

    C# 3D Array

    In C#, the concept is almost the same as in Java. However, C# makes the distinction between jagged and multi-dimensional arrays. Elements of a multi-dimensional array are stored in a contiguous block in memory while elements of a jagged array are not. Java arrays are actually jagged arrays, while C# supports both and allows you to choose which one you want based on the syntax of your code. Note that multi-dimensional arrays are better (in most cases) than jagged arrays, and that is considered a minus point for Java.

    Using jagged arrays in C# is not as simple as in Java. It’s almost like the way we would implement it in C++.

    int[][] jaggedArray = new int[2][];
    jaggedArray[0] = new int[4];
    jaggedArray[1] = new int[3];

    However, multi-dimensional arrays in C# are very simply to use. You can create a 3 dimensional array as follows

    int[,,] array3D = new int[x, y, z];

    then access its elements at array3D[i][j][k].

    Sample Code

    static void Main(string[] args)
    {
        //  Array 3 Dimensions
        int x = 4, y = 5, z = 6;
    
        //  Array Iterators
        int i, j, k;
    
        //  Allocate 3D Array
        int[,,] array3D = new int[x, y, z];
    
        //  Access array elements
        for (i = 0; i < x; i++)
        {
            Console.WriteLine(i);
    
            for (j = 0; j < y; j++)
            {
                Console.WriteLine();
    
                for (k = 0; k < z; k++)
                {
                    array3D[i, j, k] = (i * y * z) + (j * z) + k;
                    Console.Write("\t{0}", array3D[i, j, k]);
                }
            }
    
            Console.WriteLine('\n');
        }
    }

    C++ 3D Array

    To create a multi-dimensional array in C++, we should change perspective a little bit and think of creating arrays that point to other arrays, which point to other arrays, and so on. For example, to create a 2x3x4 array in C++, we should imagine the implementation as follows:

    For simplicity, we are doing the jagged implementation of the multi-dimensional array (address of array3d[0][1][0] is not directly after array3d[0][0][3] in memory representation above). In the next section, we will implement it in C the contiguous way. To allocate a jagged 2D array in C++, one can write the following (compare to C# jagged above):

    int** jaggedArray = new int*[2];
    jaggedArray[0] = new int[4];
    jaggedArray[1] = new int[3];

    The elements can be accessed as usual: jaggedArray[i][j]. The extra work we have to do in C++ is to explicitly deallocate the array.

    delete[] jaggedArray[0];
    delete[] jaggedArray[1];
    delete[] jaggedArray;

    See the code sample below to understand how we allocate and deallocate a 3 dimensional array in C++.

    Sample Code

    #include <iostream>
    
    using namespace std;
    
    void main()
    {
        //  Array 3 Dimensions
        int x = 4, y = 5, z = 6;
    
        //  Array Iterators
        int i, j, k;
    
        //  Allocate 3D Array
        int ***array3D = new int**[x];
    
        for(i = 0; i < x; i++)
        {
            array3D[i] = new int*[y];
    
            for(j = 0; j < y; j++)
            {
                array3D[i][j] = new int[z];
            }
        }
    
        //  Access array elements
        for(i = 0; i < x; i++)
        {
            cout << i << endl;
    
            for(j = 0; j < y; j++)
            {
                cout << endl;
    
                for(k = 0; k < z; k++)
                {
                    array3D[i][j][k] = (i * y * z) + (j * z) + k;
                    cout << '\t' << array3D[i][j][k];
                }
            }
    
            cout << endl << endl;
        }
    
        //  Deallocate 3D array
        for(i = 0; i < x; i++)
        {
            for(j = 0; j < y; j++)
            {
                delete[] array3D[i][j];
            }
    
            delete[] array3D[i];
        }
        delete[] array3D;
    }

    C 3D Array

    Implementing multi-dimensional arrays in C is very similar to C++, except that we use malloc()\free()  stdlib methods instead of the new\delete keywords. The memory representation below is the same, but we are going to focus in this section on making the elements of the 3 dimensional array contiguous.

    To do so, we start by allocating space for all array elements in one call to malloc.

    int *allElements = malloc(x * y * z * sizeof(int));

    Next, we create the arrays of pointers, and point to the contiguous elements we’ve already allocated.

    int ***array3D = malloc(x * sizeof(int **));
    for(i = 0; i < x; i++)
    {
        array3D[i] = malloc(y * sizeof(int *));
    
        for(j = 0; j < y; j++)
        {
            array3D[i][j] = allElements + (i * y * z) + (j * z);
        }
    }

    Note that if we wanted the same jagged implementation as in the C++ example above, we could ignore the allocation of allElements and change the line of code array3D[i][j] = allElements + (i * y * z) + (j * z); to array3D[i][j] = malloc(z * sizeof(int)). Below is a sample code for allocating, accessing and deallocating a 3 dimensional array in C.

    #include <stdio.h>
    #include <stdlib.h>
    
    void main()
    {
        //  Array 3 Dimensions
        int x = 4, y = 5, z = 6;
    
        //  Array Iterators
        int i, j, k;
    
        //  Allocate 3D Array
        int *allElements = malloc(x * y * z * sizeof(int));
        int ***array3D = malloc(x * sizeof(int **));
    
        for(i = 0; i < x; i++)
        {
            array3D[i] = malloc(y * sizeof(int *));
    
            for(j = 0; j < y; j++)
            {
                array3D[i][j] = allElements + (i * y * z) + (j * z);
            }
        }
    
        //  Access array elements
        for(i = 0; i < x; i++)
        {
            printf("%d\n", i);
    
            for(j = 0; j < y; j++)
            {
                printf("\n");
    
                for(k = 0; k < z; k++)
                {
                    array3D[i][j][k] = (i * y * z) + (j * z) + k;
                    printf("\t%d", array3D[i][j][k]);
                }
            }
    
            printf("\n\n");
        }
    
        //  Deallocate 3D array
        free(allElements);
        for(i = 0; i < x; i++)
        {
            free(array3D[i]);
        }
        free (array3D);
    }

    Source Code

    Full source code for the above 4 samples is available here.

     
    • ashish 5:48 am on September 14, 2010 Permalink | Reply

      #include
      #include

      void main()
      {
      // Array 3 Dimensions
      int x = 4, y = 5, z = 6;

      // Array Iterators
      int i, j, k;

      // Allocate 3D Array
      int *allElements = malloc(x * y * z * sizeof(int));
      int ***array3D = malloc(x * sizeof(int **));

      for(i = 0; i < x; i++)
      {
      array3D[i] = malloc(y * sizeof(int *));

      for(j = 0; j < y; j++)
      {
      array3D[i][j] = allElements + (i * y * z) + (j * z);
      }
      }

      // Access array elements
      for(i = 0; i < x; i++)
      {
      printf("%d\n", i);

      for(j = 0; j < y; j++)
      {
      printf("\n");

      for(k = 0; k < z; k++)
      {
      array3D[i][j][k] = (i * y * z) + (j * z) + k;
      printf("\t%d", array3D[i][j][k]);
      }
      }

      printf("\n\n");
      }

      // Deallocate 3D array
      free(allElements);
      for(i = 0; i < x; i++)
      {
      free(array3D[i]);
      }
      free (array3D);
      }

      """""how this aryya program exeute this not working

    • Ali BaderEddin 8:11 am on September 14, 2010 Permalink | Reply

      What exactly is not working?

    • chetna 2:00 am on November 30, 2011 Permalink | Reply

      plz given me program for three dimensional array

    • Addaboi 5:13 pm on March 28, 2012 Permalink | Reply

      Thanks. This worked great.

    • Phil 9:08 am on July 12, 2013 Permalink | Reply

      Doesn’t work… I get a segmentation fault :(

      • Phil 12:47 pm on July 12, 2013 Permalink | Reply

        I rescind that previous comment.. kind of
        It did not work when I placed the allocation code in a separate function (I was passing a pointer int*** to the function but things weren’t working.. this is likely because I have not developed in C for quite awhile and am a little rusty).

        The code did however work when I returned a pointer (int***) from the function back to main.

    • williamholdin 11:43 pm on April 23, 2014 Permalink | Reply

      More about multidimensional array Multi dimensional array

      william

  • Ali BaderEddin 7:52 pm on May 20, 2010 Permalink | Reply
    Tags: *ptr++, Array Indexing, Multi-dimensional Array, Pointer Arithmetics, pointers, Pointers to Arrays   

    C Pointer Tricks 


    I’ve been exploring\reading more about C pointers (mostly from this source) and I’ve found the following interesting tricks.

    Trick 1 – Pointer Arithmetics

    Question

    What is the result of running the following code?

    void trick1()
    {
        int arr[] = {1, 2, 3};
        int *ptr;
    
        ptr = arr;
    
        printf("arr[0] = %d, arr[1] = %d, arr[2] = %d, ptr = %p, *ptr = %d\n",
                arr[0], arr[1], arr[2], ptr, *ptr);
        *ptr++ = -1;
        printf("arr[0] = %d, arr[1] = %d, arr[2] = %d, ptr = %p, *ptr = %d\n",
                arr[0], arr[1], arr[2], ptr, *ptr);
        *++ptr = -2;
        printf("arr[0] = %d, arr[1] = %d, arr[2] = %d, ptr = %p, *ptr = %d\n",
                arr[0], arr[1], arr[2], ptr, *ptr);
        (*ptr)++;
        printf("arr[0] = %d, arr[1] = %d, arr[2] = %d, ptr = %p, *ptr = %d\n",
                arr[0], arr[1], arr[2], ptr, *ptr);
    }

    Solution

    • ptr = arr
      • This will make the pointer ptr point to the first element in the array arr.
    • *ptr++ = -1
      • There are two operators (* and ++) to be applied on a single operand (ptr). We have to know which one takes precedence. The answer is that the post-increment (++) will be applied first. Once it’s applied, it will not affect the value of ptr until the statement completes execution.  So as we are evaluating the current statement, ptr is still pointing to the first element of the array (arr[0]). Now, we apply the * operator to the ptr operand. This will dereference the pointer and will let us access the content it points to. Since it’s pointing to arr[0], *ptr = -1 will set the value of arr[0] to -1. After the statement completes execution, the pointer ptr is incremented and thus will point to the next element in the array (arr[1]).
    • *++ptr = -2
      • As we mentioned previously, the increment operator (++) takes precedence over the dereference operator (*). Since this is a pre-increment operator, ++ptr will make the pointer point to the next element in the array arr[2]. *ptr = -2 will then set the value of arr[2] to -2.
    • (*ptr)++
      • This one should be clear by now. *ptr will point us to arr[2] and thus running (*ptr)++ will increment the value of the integer that ptr is pointing to. So the value of arr[2] will  be incremented by 1 once the statement completes execution and the pointer ptr will still point to arr[2].
      • If we try to compile (*ptr)++ = 0 we will get the following compilation error:  “error C2106: ‘=’ : left operand must be l-value”.

    The result would look like this:

    arr[0] = 1, arr[1] = 2, arr[2] = 3, ptr = 0043FE44, *ptr = 1
    arr[0] = -1, arr[1] = 2, arr[2] = 3, ptr = 0043FE48, *ptr = 2
    arr[0] = -1, arr[1] = 2, arr[2] = -2, ptr = 0043FE4C, *ptr = -2
    arr[0] = -1, arr[1] = 2, arr[2] = -1, ptr = 0043FE4C, *ptr = -1

    Trick 2 – Array Indexing

    Question

    What is the result of running the following code?

    void trick2()
    {
        int arr[] = {1, 2, 3};
    
        *arr = 5;
        printf("arr[0] = %d, arr[1] = %d, arr[2] = %d\n",
               arr[0], arr[1], arr[2]);
        *(arr + 1) = 10;
        printf("arr[0] = %d, arr[1] = %d, arr[2] = %d\n",
               arr[0], arr[1], arr[2]);
        2[arr] = 15;
        printf("0[arr] = %d, 1[arr] = %d, 2[arr] = %d\n",
               0[arr], 1[arr], 2[arr]);
    }

    Solution

    • *arr = 5
      • An array variable can be used as a pointer, but its value can’t be changed. Think of it as a constant pointer, though they’re not quite the same. Throughout the array’s life cycle on the stack, it will always point to the first element it pointed to when it was initialized. If we try to compile arr = &i or arr = arr2 or arr = ptr, we will get a compilation error: “error C2106: ‘=’ : left operand must be l-value”.
      • So back to the statement we are trying to evaluate, this will simply set the value of the first element in the array to 5. So now arr[0] = 5.
    • *(arr + 1) = 10
      • arr is a pointer to a value of type int. Running arr + 1 would actually result in arr + 1 * sizeof(int). Assuming that arr represents address 2500 and sizeof(int) is 4, arr + 1 will actually represent address 2504 and not 2501. This is called pointer arithmetics, where the type of the object the pointer points to is taken into consideration when incrementing or decrementing the pointer.
      • *(arr + 1) is like saying arr[1] so it will let as access the second element of the array. Thus, the statement will set arr[1] to 10.
    • 2[arr] = 15
      • This is the weirdest thing I’ve learned in C, but it does make sense after all. At first you’d think this will result in a compilation error, but when compiling 2[arr], C compiler will convert it to *(2 + arr), which is the same as *(arr + 2) since addition is commutative.
      • So 2[arr] = 15 is like saying arr[2] = 15 and this the third element in the array will be set to 15.

    The result would look like this:

    arr[0] = 5, arr[1] = 2, arr[2] = 3
    arr[0] = 5, arr[1] = 10, arr[2] = 3
    0[arr] = 5, 1[arr] = 10, 2[arr] = 15

    Trick 3 – Pointers to Arrays

    Question

    What’s the difference between str1, str2str3 and cPtr? What is the difference between sArr, sArr2DsPtr1, sPtr2 and SPtr3? What will be the result of running the code below?

    void trick3()
    {
        char str1[] = { 'A', 'B', 'C', 'D', 'E' };
        char str2[] = "ABCDE";
        char *str3 = "ABCDE";
        char  *cPtr = str1;
    
        short sArr[] = {1, 2, 3, 4, 5};
        short sArr2D[][5] = { {1, 2, 3, 4, 5},
                              {6, 7, 8, 9, 10} };
        short *sPtr1 = sArr;
        short (*sPtr2)[5] = sArr2D;
        short *sPtr3[5];
    
        printf("sizeof(str1) = %u\n", sizeof(str1));
        printf("sizeof(str2) = %u\n", sizeof(str2));
        printf("sizeof(str3) = %u\n", sizeof(str3));
        printf("sizeof(cPtr) = %u\n", sizeof(cPtr));
        printf("\n");
    
        printf("sizeof(sArr) = %u\n", sizeof(sArr));
        printf("sizeof(sPtr1) = %u\n", sizeof(sPtr1));
        printf("sizeof(sArr2D) = %u\n", sizeof(sArr2D));
        printf("sizeof(sPtr2) = %u\n", sizeof(sPtr2));
        printf("sizeof(*sPtr2) = %u\n", sizeof(*sPtr2));
        printf("sizeof(sPtr3) = %u\n", sizeof(sPtr3));
        printf("\n");
    
        printf("sArr2D[1][2] = %d\n", sArr2D[1][2]);
        printf("sPtr2[0][0] = %d, sPtr2[1][2] = %d\n", sPtr2[0][0], sPtr2[1][2]);
        printf("*(* (sArr2D + 1) + 2) = %d\n", *(* (sArr2D + 1) + 2));
        printf("*(*(sArr2D) + 1*5 + 2) = %d\n\n", *(*(sArr2D) + 1*5 + 2));
    }

    Solution

    • str1 is a char array with 5 elements: ‘A’, ‘B’, ‘C’, ‘D’, ‘E’. str2 is a char array with 6 elements ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ and ”. str3 is a pointer to an array of 6 elements ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ and ”. str2 and str3 seem to mean the same thing, but actually they are different. str2 is not stored on the stack (from symbol table, it directly refers to the first element in the array) while str3 is stored on the stack and contains the value of the address of the first element of the array. In other words, str3 is storing an extra pointer of size sizeof(char *), which is 4 bytes on a 32-bit system and 8 bytes on a 64-bit system. cPtr and str3 are the same in that they are pointers that point to a char array, though they are not pointing to the same array.
    • There is one additional difference between pointers and arrays. If we run sizeof on an array, we’ll get the full size of the array. However, if we run sizeof on a pointer, it will return the size of the pointer only and not the content it refers to.
      • So sizeof(str1) is 5 * sizeof(char), which is 5 * 1 = 5. sizeof(str2) is 6.
      • sizeof(str3) is the size of the pointer sizeof(char *), which is 4 bytes. sizeof(cPtr) is also 4 bytes.
    • sArr is an array of 5 short numbers. Thus, its size is 5 * sizeof(short) = 10. sPtr is a pointer that points to sArr[0]. sizeof(sPtr) is 4 bytes. sArr2D is a 2×5 2-dimensional array with size = 2 * 5 * sizeof(short) = 20. The short (*sPtr2)[5] = sArr2D will declare a pointer to an array of 2 dimensions where dimension 2 has a size of 5 then make the pointer point to the 2-dimensional array sArr2D. It does not do any allocation. So sizeof(sPtr2) is 4 bytes since it’s a pointer. The sizeof(*sPtr2) is the 5 * sizeof(short) = 10. short *sPtr3[5] creates an array of 5 pointers to short values. The array contains pointers and not short values. So sizeof(sPtr3) = 5 * sizeof(short *) = 20.
    • Multi-dimensional Array
      • sArr2D[1][2] simply tries to access the third element of the second array in the 2-dimensional array sArr2D. Think of sArr2D as an array of 2 arrays of 5 int values. So sArr2D[1][2] = 8.
      • sPtr2 is a pointer to the 2-dimensional array sArr2D and thus can be used in the same way as variable sArr2D. So sPtr2[1][2] is the same as sArr2D[1][2].
      • * (sArr2D + 1) is like sArr2D[1] which points us to the first element in the 2nd array sArr2D[1][0]. *(* (sArr2D + 1) + 2) is like *(sArr2D[1] + 2), which is like *(&sArr2D[1][0] + 2), which is like *(&sArr2D[1][1]), which is sArr2D[1][2].
      • *(sArr2D) + 1 * (column count) is the same as sArr2D[1]. So *(*(sArr2D) + 1*5 + 2) is *(&sArr2D[1][0] + 2), which is sArr2D[1][2].

    The full result of the above code would look like this:

    sizeof(str1) = 5
    sizeof(str2) = 6
    sizeof(str3) = 4
    sizeof(cPtr) = 4

    sizeof(sArr) = 10
    sizeof(sPtr1) = 4
    sizeof(sArr2D) = 20
    sizeof(sPtr2) = 4
    sizeof(*sPtr2) = 10
    sizeof(sPtr3) = 20

    sArr2D[1][2] = 8
    sPtr2[0][0] = 1, sPtr2[1][2] = 8
    *(* (sArr2D + 1) + 2) = 8
    *(*(sArr2D) + 1*5 + 2) = 8

    Source Code

    Full source code for the above tricks is available here.

     
    • John 7:02 am on June 6, 2010 Permalink | Reply

      Thanks man …. it helped me a lot :)

    • Ali BaderEddin 1:42 pm on June 6, 2010 Permalink | Reply

      I’m glad it did.. Thanks.

    • KevinC 3:51 am on August 31, 2010 Permalink | Reply

      The str3 in trick3 is a pointer to an array of 5 elements ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ only.

      while(*str3){
      printf(“(%c)”,*str3);
      str3++;
      }

      output:(A)(B)(C)(D)(E)

    • KevinC 6:13 pm on August 31, 2010 Permalink | Reply

      oops, I’m fault, and char * has ” indeed.

  • Ali BaderEddin 11:52 pm on May 16, 2010 Permalink | Reply  

    OpenGL Color Interpolation 


    Using the GL_LINES mode, OpenGL will draw for us a line for every two vertices we specify.

    glBegin (GL_LINES);
    
        glColor3f  (1, 0, 0);
        glVertex2f (x0, y0);
        glColor3f  (0, 0, 1);
        glVertex2f (x1, y1);
    
    glEnd ();

    Behind the scenes, OpenGL is using interpolation to draw the line. That is, given 2 points, OpenGL will determine the set of pixels that the line would cross through and then change their color based on the color of the line. Bresenham‘s algorithm is a famous algorithm for drawing lines, and it (or some extension of it) is probably the one used internally by OpenGL.

    Using the basic linear equation we all learned in school (y = ax + b), we should be able to get to an algorithm that draws the points across the lines. We are drawing a line segment from vertex v0 (x0, y0) to vertex v1 (x1, y1). Let’s denote any point across the line as vertex v with coordinates (x, y). Given those three vertices, we can build the following 3 independent equations.

    Now, we should try to create some relationship between those 3 independent equations and try to get rid of the unknowns a and b. Let’s first try to get rid of b.

    Let’s try y – y0 and y1 – y0, we will get the following two independent equations.

    Now, let’s try to create a relationship between those 2 independent equations and at the same time try to get rid of a.

    We can now sample some values of x between x0 and x1 and get the value of y using the above equation. This is the basic idea behind drawing a line. In addition to drawing the line, OpenGL is also doing something else behind the scenes: Color Interpolation. If you run the above code sample in OpenGL, you’ll see the following line:

    As points are moving from vertex v0 to vertex v1, their color is also transitioning from v0 color to v1 color. This is handled by the OpenGL Smooth shading model. OpenGL supports two shading models: Smooth and Flat. Flat shading means that no shading calculations are performed on the interior of the primitives. The color of the interiors is the color of the last vertex specified. You can set it by calling glShadeModel (GL_FLAT). Smooth shading is thedefault shading model in OpenGL and you can explicitly set it by calling glShadeModel (GL_SMOOTH).

    Even though OpenGL automatically does color interpolation (in case of smooth shading), it would be interesting if we try to implement it and experiment a little bit with it.

    To implement the linear interpolation of the points and their colors, we are going to use the below generic linear interpolation method, where u is any value we select from 0 to 1.

    First, let’s define the point data structure.

    //  Define the point data structure
    typedef struct Point
    {
        float x;                  //  x position of the point
        float y;                  //  y position of the point
        float z;                  //  z position of the point
        float r;                  //  red color component of the point
        float g;                  //  green color component of the point
        float b;                  //  blue color component of the point
        float a;                  //  alpha color component of the point
    } Point;

    Next we set the number N of how many points we want to sample on the line, and initialize the array that will hold the points.

    //  represents the number of points the line consists of
    #define N 250
    
    //  Declare the array of points
    Point linePoints[N];

    Now, we generate the location and color components for the points on the line using the above linear interpolation formula.

    //  Location and color interpolation
    for (i = 0; i < N; i++)
    {
        u = (float)i/(N - 1);
    
        linePoints[i].x = v0.x * (1.0 - u) + v1.x * u;
        linePoints[i].y = v0.y * (1.0 - u) + v1.y * u;
        linePoints[i].z = v0.z * (1.0 - u) + v1.z * u;
        linePoints[i].r = v0.r * (1.0 - u) + v1.r * u;
        linePoints[i].g = v0.g * (1.0 - u) + v1.g * u;
        linePoints[i].b = v0.b * (1.0 - u) + v1.b * u;
        linePoints[i].a = v0.a * (1.0 - u) + v1.a * u;
    }

    Finally, we draw this set of points.

    glBegin (GL_POINTS);
    
    for (i = 0; i < N; i++)
    {
        glColor4f  (linePoints[i].r, linePoints[i].g, linePoints[i].b, linePoints[i].a);
        glVertex3f (linePoints[i].x, linePoints[i].y, linePoints[i].z);
    }
    
    glEnd ();

    More implementation details are available in the full source code of this OpenGL app. You can use this app for experimenting with color interpolation and you might even notice how the OpenGL interpolation method is way faster than our simplistic implementation (if you have a machine with a relatively slow processor). If you have any issues compiling or running the app, check out this section for details about compiling and running an OpenGL app that uses the GLUT library.

     
  • Ali BaderEddin 11:48 am on May 16, 2010 Permalink | Reply  

    OpenGL Resolution 


    This is a simple OpenGL app that runs in full screen (game mode), shows every other pixel in alternating color, and displays the screen resolution (m x n grid of pixels) and the screen dimension in millimeters.


    Note: The pixels are actually black and white, but because this screenshot is reduced in size, you see a gray background. Click on this image to see the full sized one.

    The implementation is very simple. We use glutGet (GLUT_SCREEN_WIDTH) to get the screen width in pixels and glutGet (GLUT_SCREEN_HEIGHT) to get the screen height. To get the width and height in millimeters, we use glutGet (GLUT_SCREEN_WIDTH_MM) and glutGet (GLUT_SCREEN_HEIGHT_MM) consecutively. This would even tell us the width and height of a single pixel in millimeters. For example, pixel width in millimeters is equal to screen width in millimeters divided by the screen width in pixels. Same can be done for the height.

    //-------------------------------------------------------------------------
    //  Draw string representing the resolution of your screen
    //-------------------------------------------------------------------------
    void drawResolutionStr ()
    {
        glColor3f (0, 0, 1);
    
        printw (20, 20, 0,
                "Resolution: %d x %d pixels, %d x %d mm",
                glutGet (GLUT_SCREEN_WIDTH), glutGet (GLUT_SCREEN_HEIGHT),
                glutGet (GLUT_SCREEN_WIDTH_MM), glutGet (GLUT_SCREEN_HEIGHT_MM));
    
        glColor3f (1, 0, 0);
    
        printw (20, 45, 0,
                "Pixel Dimensions: %.4f x %.4f mm",
                (float)glutGet (GLUT_SCREEN_WIDTH_MM) / glutGet (GLUT_SCREEN_WIDTH),
                (float)glutGet (GLUT_SCREEN_HEIGHT_MM) / glutGet (GLUT_SCREEN_HEIGHT));
    }

    You can find the full source code here. If you have any issues compiling or running the app, check out this section for details about compiling and running an OpenGL app that uses the GLUT library.

     
  • Ali BaderEddin 9:45 pm on May 15, 2010 Permalink | Reply  

    Download GL\GLUT\GLAUX\GLUI .lib, .h and .dll 


    Looking for the lib, header, or dll files for OpenGL, GLUT,  GLAUX or GLUI on Windows? I have collected them from different sources so they can be easily downloaded as a single package: http://cid-e38f9fc6490b29d9.skydrive.live.com/self.aspx/Public/GL%20Files.zip. It contains the following content:

    Folder Files
    dll
    • glu32.dll
    • glut32.dll
    • opengl32.dll
    include\GL
    • gl.h
    • glu.h
    • glaux.h
    • glut.h
    • glui.h
    Lib
    • opengl32.lib
    • glu32.lib
    • glaux.lib
    • glut.lib
    • glui.lib

    Disclaimer: I have not created or modified any of those files, nor do I claim any ownership of them. They were simply collected from different sources and redistributed.

     
    • rongguonyu 2:18 pm on February 17, 2011 Permalink | Reply

      It is a very useful link to me.

    • mohsen 3:39 am on August 6, 2011 Permalink | Reply

      link is broken :-(

      • Ali BaderEddin 7:58 pm on August 6, 2011 Permalink | Reply

        Are you sure? It works fine for me.

        • mohsen 3:31 am on August 7, 2011 Permalink

          yes. this link is work but when go to this link
          i can not download the GLFile.zip
          please send this file to my email.
          thank you

    • mohsen 4:01 am on August 7, 2011 Permalink | Reply

      oh excuse me i dont see the download linK in the top-right page .
      file downloaded
      thanks ali

    • Anon 10:25 am on October 13, 2011 Permalink | Reply

      Thanks very much for this!

    • shannon 1:41 am on October 15, 2011 Permalink | Reply

      I really appreciate for your help.

      Thanks so much!!! great thanks to you.

    • huge 5:20 am on February 25, 2012 Permalink | Reply

      like this post :D

    • myintmyintsein 9:51 pm on March 19, 2012 Permalink | Reply

      I very thanks to mycodelog.com

    • 0569123566 11:01 am on April 30, 2012 Permalink | Reply

      thank you ….:D

    • iBreeze 5:51 pm on May 1, 2012 Permalink | Reply

      thanx :-)

    • justin 2:21 am on June 13, 2012 Permalink | Reply

      Thank you thank you thank you.

    • lupi 6:34 pm on June 18, 2012 Permalink | Reply

      thank you sir..

    • rajendra 2:12 am on July 19, 2012 Permalink | Reply

      For linux????

    • rohan 7:59 am on July 31, 2012 Permalink | Reply

      thanks…. any idea which version this is?

    • halk 5:26 am on August 3, 2012 Permalink | Reply

      thanks

    • pankaj 4:50 am on September 29, 2012 Permalink | Reply

      thanks a lot !!!

    • Anon2 4:49 am on October 19, 2012 Permalink | Reply

      Thanks!!!!

    • Jabs 7:57 am on November 20, 2012 Permalink | Reply

      Is der glx header file plz upload or reply me back

    • toommeng 6:53 pm on November 29, 2012 Permalink | Reply

      thank you

    • sumit 8:37 pm on December 11, 2012 Permalink | Reply

      Thank you

    • anti 7:43 am on December 16, 2012 Permalink | Reply

      thanks a lot..

    • shabnam 11:56 pm on April 19, 2013 Permalink | Reply

      sorry i cant download it.it is necessary for me please send me.thanks alot.
      sha.safarzadeh@gmail.com

    • Mper 5:52 pm on June 2, 2013 Permalink | Reply

      the link is not work..can you send this to my email please?? ferawati.10109266@gmail.com..thank you..

    • gilgamesh 8:50 am on June 11, 2013 Permalink | Reply

      thank you so much yo kind soul :’)

      • gilgamesh 9:38 am on June 11, 2013 Permalink | Reply

        I reuploaded it to mediafire (because there are people said the link doesn’t work, eventhough it worked for me). I change nothing:

        http://www.mediafire.com/?8hbxdg50s6nndht

        if you do not like it, just remove this post :) or send me email, I’ll remove the file :)

        again, thank you so much!

    • Ferat 11:01 pm on June 23, 2013 Permalink | Reply

      Thank you very much.

    • Sebastian Abt 10:00 pm on October 7, 2013 Permalink | Reply

      Thank you so much for posting this.

    • Zeeshan Ali Basbasah 7:22 pm on December 10, 2013 Permalink | Reply

      whare is installation file ???????????

    • Ahmed Khaled 6:40 am on January 16, 2014 Permalink | Reply

      Can these files work on windows 7 64-bit ??

    • nischal 3:57 am on March 15, 2014 Permalink | Reply

      i am getting an error in gl file

    • bbraptor.lee 9:16 pm on March 31, 2014 Permalink | Reply

      thank you, i got it.

    • MCO 9:36 am on September 23, 2014 Permalink | Reply

      Thanks bro, it’s really useful!

    • glutax 11:37 pm on December 9, 2014 Permalink | Reply

      Everyone loves what you guys are up too.
      This kind of clever work and coverage! Keep up
      the terrific works guys I’ve added you guys to our blogroll.

    • streaming bokep 3:25 am on December 18, 2014 Permalink | Reply

      Good post. I learn something totally new and challenging on blogs I stumbleupon everyday.
      It will always be useful to read through content from other
      authors and practice something from their sites.

    • agen bola terbesar 11:01 pm on December 18, 2014 Permalink | Reply

      Thanks for sharing your thoughts. I truly appreciate
      your efforts and I am waiting for your further post thank you once again.

    • Cindy 2:36 pm on January 30, 2015 Permalink | Reply

      Very helpful. Thanks for sharing.

    • Md. Sazzadur Ahamed 11:05 pm on February 10, 2015 Permalink | Reply

      Thank you so much Mr. Ali BaderEddin. For a while i am searching for all the files, indifferent place. And it’s you who makes it so easy to have all the file in one place.

    • sugaff 6:15 am on February 20, 2015 Permalink | Reply

      m not getting the link….plz…give an another way….its vry imp 4 me….plz

    • kevin 2:30 am on March 15, 2015 Permalink | Reply

      worked like a charm… thanks :)

    • jigar 11:28 pm on April 10, 2015 Permalink | Reply

      Thanxx…..

    • heroro 2:01 pm on April 14, 2015 Permalink | Reply

      thank. Worked !!

    • Aditya 4:57 am on April 21, 2015 Permalink | Reply

      thanks a lot,was fed up of searching for each file ,finally got it here ,all at one place

    • deepika 9:40 am on May 8, 2015 Permalink | Reply

      Heyy….can u plzz help me in linking imagel loader.h n main source code.
      …n how to load images into it…i m very begginer for this opengl…so plzzz anyone let me knw…n its urgent

    • doels 7:20 pm on June 20, 2015 Permalink | Reply

      thaks it’s very helpful!

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: