Showing posts with label opengl es. Show all posts
Showing posts with label opengl es. Show all posts

Monday, July 4, 2011

3D shapes and Rotation in Opengl ES 2.0

It will be helpful to go through Previous tutorial before reading this one. LINK

Now, we will change the structure of Vertex a little. Position array is now of size 3 to handle x, y as well as z.


struct Vertex {
    float Position[3];
    float Color[4];
};



Our rotation matrix
void ApplyRotationX(float degrees)
{
    float radians = degrees * 3.14159f / 180.0f;
    float s = std::sin(radians);
    float c = std::cos(radians);
    float zRotation[16] = {
        1, 0, 0, 0,
        0, c, s, 0,
        0, -s, c, 0,
        0, 0, 0, 1
    };
    
    GLint modelviewUniform = glGetUniformLocation(buildProgram, "Modelview");
    glUniformMatrix4fv(modelviewUniform, 1, 0, &zRotation[0]);
}

void ApplyRotationY(float degrees)
{
    float radians = degrees * 3.14159f / 180.0f;
    float s = std::sin(radians);
    float c = std::cos(radians);
    float zRotation[16] = {
        s, 0, c, 0,
        0, 1, 0, 0,
        c, 0, -s, 0,
        0, 0, 0, 1
    };
    
    GLint modelviewUniform = glGetUniformLocation(buildProgram, "Modelview");
    glUniformMatrix4fv(modelviewUniform, 1, 0, &zRotation[0]);
}

void ApplyRotationXY(float degrees)
{
    float radians = degrees * 3.14159f / 180.0f;
    float s = std::sin(radians);
    float c = std::cos(radians);
    float zRotation[16] = {
        s, 0, c, 0,
        s*c, c, -s* s, 0,
        c*c, -s, -c * s, 0,
        0, 0, 0, 1
    };
    
    GLint modelviewUniform = glGetUniformLocation(buildProgram, "Modelview");
    glUniformMatrix4fv(modelviewUniform, 1, 0, &zRotation[0]);
}




Arrays of vertex and indices

// Define the positions and colors(R,G,B,A) of 8 vertices of square.
const Vertex Vertices[] = {
    {{0, 0, 0}, {1, 0, 0, 1}},
    {{0, 0.5, 0},  {0, 1, 0, 1}},
    {{0.5, 0.5, 0},  {1, 1, 0, 1}},
    {{0.5, 0, 0}, {1, 0, 1, 1}},
    {{0, 0, 0.5}, {0, 1, 1, 1}},
    {{0, 0.5, 0.5},  {0, 0.5, 0, 1}},
    {{0.5, 0.5, 0.5},  {0.5, 0.5, 0, 1}},
    {{0.5, 0, 0.5}, {0.5, 0, 0.5, 1}},
  
};

//Define the order of vertices for 12 triangles/
//0,1,2 forms first triangle
//2,3,0 form second triangle.
const GLubyte Indices[] = {
    0, 1, 2,
    2, 3, 0,
    4,5,6,
    6,7,4,
    0,1,4,
    1,4,5,
    1,2,6,
    1,5,6,
    2,3,6,
    3,6,7,
    0,3,4,
    3,4,7,
    
};


And finally drawing

       // Drawing code
    glClearColor(1.0f, 1.0f, 1.0f, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    
    switch (rotateAlong) {
        case 1:
            ApplyRotationX(degreesX);
            break;
        case 2:ApplyRotationY(degreesY);
            break;
        default:ApplyRotation(degreesZ);
            break;
    }
    
    
    
    
    glEnableVertexAttribArray(_positionSlot);
    glEnableVertexAttribArray(_colorSlot);
    
    
    //Lets give these functions pointer to head of vertex array.
    GLsizei stride = sizeof(Vertex);
    const GLvoid* pCoords = &Vertices[0].Position[0];
    const GLvoid* pColors = &Vertices[0].Color[0];
    
   //Attribute changed from 2 (when we had drawn square) to 3 (dealing with cube x,y and z)
    glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, stride, pCoords);
    glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, stride, pColors);
    
    //Draw the 12 triangles and (12*3=)36 indices.
    const GLvoid* bodyIndices = &Indices[0];
    glDrawElements(GL_TRIANGLES,12* 3, GL_UNSIGNED_BYTE, bodyIndices);
    
    glDisableVertexAttribArray(_positionSlot);
    glDisableVertexAttribArray(_colorSlot);    
    
    [mimContext presentRenderbuffer:GL_RENDERBUFFER];


In order to know about rotation matrix, read this LINK
It tells you about matrix used for rotation around x, y and z axis. For rotation around X as well Y (mixed rotation) we just multiply rotation matrix of X and Y axis.

There are 3 sliders with which  you can rotate the cube along X, Y and Z axis.

     //Solo Rotation along either of X,Y or Z.
    switch (rotateAlong) {
        case 1:
            ApplyRotationX(degreesX);
            break;
        case 2:ApplyRotationY(degreesY);
            break;
        default:ApplyRotation(degreesZ);
            break;
    }


    //Comment above lines in switch in code
    //Uncomment following lines to see mixed rotation.
    //Mixed Rotation along X and Y
    ApplyRotationXY(40);
    

Check out screenshot:
















You can download the code HERE

Wednesday, June 29, 2011

Integrating C++ files with Objective-C (tips)

This trick will be helpful for people who are trying to embed Opengl ES code in their iPhone project.
One way to let your compiler know that you will be using mix of C++ and objective-C will be to convert the extension of all .m files to .mm as discussed in one the previous post(LINK) but there is another way if you don't want to change all the extensions  !


In your project Settings, Under Build Settings > GCC 4.2- Language
Change the "Compile Source As" value to "Objective-C++"
































It will let you compile the code without changing the extensions of all the files.

I found this wonderful tip from Pete Cole blog. LINK

Sunday, June 26, 2011

Installing Github on MacOSX

Ok. After delaying the process for so many days, I decided to just do this task pending on my to-do list !
Now on, all my opensource projects/codes will be available on github repositories !
I have been constantly  getting hit by new ideas to do and my to-do list is just getting bigger day by day ! :)
So more articles will  be arriving on blog :)

Anyways. If you want to set up your github too. Here is the link to do so which is its official page. I followed the same :)

LINK


Note: After you follow steps on link above , if you are new then following are 2 topics are not specified in the link above. You can find them in following articles:

ARTICLE 1: How to upload fresh project on github?


ARTICLE 2: How to commit changes to your project on github?



There is a good news. I am trying to work on this opengl ES  tutorial series which is loosely based on the legendary NeHe opengl tutorial series. I will be following kind of same topics but in opengl ES :) Also going to put all the tutorial source code on the github. I have been getting constant emails about how the basic codes in my articles which I have uploaded so far have been helping people in getting things going. I am really really glad that I could be useful for the coder community.



Wednesday, June 8, 2011

New Frameworks in iOS 5

Here are few new frameworks added in  iOS 5.


1.Core Image Framework - I thank Apple for bringing in this framework.. with this framework come huge possibility of image processing which is possible on iPhone OS 5 without having to deal with OPENGL now unless it is some something heavy ! Till now there was only Quartz available on iOS... but arrival of CoreImage as well its great !


There is face feature detection available as well in CoreImage. LINK


2.Twitter Framework- Thats convenient ! Knowing twitter has become a part of everyone's life.. and its expected to be there in every app if you want to share things in your app with world out there ! So great work.


3.Accounts Framework - It  can be used to manage account authentication process  in your application. It is used in with Twitter framework as well.

4.Generic Security Services Framework.






5. GLKit - Now this is a great framework which has been included to ease the use of OPENGL ES in the application by developers. It looks great with the options available to get it started easily.






  • The GLKView and GLKViewController  - view related classes which let you draw !
  • The GLKTextureLoader class provides image conversion and loading routines to your application, allowing it to automatically load texture images into your context. 
  • The GLKit framework provides implementations of vector, matrix, and quaternions as well as a matrix stack operation to provides the same functionality found in OpenGL ES 1.1.
  • The GLKBaseEffectGLKSkyboxEffect, and GLKReflectionMapEffect classes provide precreated, configurable graphics shaders that implement commonly used graphics operations.