In this demo we have a cube in the scene. If you click on the cube, the cube changes its color to yellow:
the cube is now selected. Starting from a cube selected, a click on the plane moves the cube in the position where we have clicked. This video shows some examples:
The demo uses the unProject method as in the Picking post . You will find in the code some modification for drawing the cube in a different position on the plane.
In this post I want to share an easy implementation of picking through unProject method. The demo is realized in C, so we have only the main.cpp file and vector3.h file. In the demo we have a cube in the center of the scene, when we click on the cube, the cube changes its color.
If you open the main.cpp source file you can note the following lines of code:
The variables posX,posY and posZ give us the unproject mouse click coordinates.
The variables cubeX,cubeY and cubeZ save the cube position.
The variable cubSize set the size of the cube;
The boolean variable cubePick is true when the cube is clicked;
When we click in the scene, the unProject method is called:
void unProject(){ GLint viewport[4]; // Where The Viewport Values Will Be Stored glGetIntegerv(GL_VIEWPORT, viewport); // Retrieves The Viewport Values (X, Y, Width, Height) GLdouble modelview[16]; // Where The 16 Doubles Of The Modelview Matrix Are To Be Stored glGetDoublev(GL_MODELVIEW_MATRIX, modelview); // Retrieve The Modelview Matrix GLdouble projection[16]; // Where The 16 Doubles Of The Projection Matrix Are To Be Stored glGetDoublev(GL_PROJECTION_MATRIX, projection); // Retrieve The Projection Matrix GLfloat winX, winY, winZ; // Holds Our X, Y and Z Coordinates winX = (float)mouse2D.x; // Holds The Mouse X Coordinate winY = (float)mouse2D.y; // Holds The Mouse Y Coordinate glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ); gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ); //ROUND 2 DECIMAL posX= ((int)(posX * 10 + .5) / 10.0); posY= ((int)(posY * 10+ .5) / 10.0); posZ= ((int)(posZ * 10 + .5) / 10.0); }
the method saves posX,posY and posZ variables. After the pickingCheck method is called:
Hi, let's see a more complex texture mapping. In this demo I have written in the Geometry class a torus rendering method :
void torus(float out_radius, float int_radius, int textureId);
The method takes as input the tube radius ,the big radius from the center of the torus and a texture id. For a good quality rendering is important to set the normal on each vertex of the torus mesh, so for this reason we have the following private method used by torus:
In this post I decided to get some Quake texture and making a simple but interesting 3D scene. As the other OpenGL post, we have a project with some C++ files and classes like Geometry,Scene,Main but in this demo I have used an external class for texture loading, TextureLoader inside the project. The first thing you have to control before to worry about compilation error is the following setting in your Visual C++ project:
Check Character Set = Not Set. Let's see the video demo before to start:
TextureLoader is the class that loads the textures. We have three different textures, one for floor and two for boxes. The initGL() method inside the Scene class has now the code to initialize the textures:
The texture files must reside in your project directory. In my case I made a directory called texture and I put all files here. With Geometry class in the draw() method of the Scene class, we find the code to draw all boxes using textures:
In many applications the parts of a model depend on one another: if we move one part, it causes other parts to move. We represent such models using trasformations. OpenGL trasformations are applied to the existing model-view matrix. Each trasformation represents a relative change from one scaling,position and orientation to another. In this post I show you a demo with a lamp. You can move some parts of the lamp using the keys 'a','s','d','f','g','h' from your keyboard. The lamp uses also a diffuse light with an emissive component. Here it is the video demo:
In this post I want to show 3D affine trasformations using OpenGL functions like glTranslate(), glRotate() ,glPushMatrix and glPopMatrix(). Here the final result:
You can download the c++ sources from the url:OpenGL DemoUse left mouse button to rotate the scene and middle plus right button for zoom. In the sources you will find some classes:
Geometry class allows you boxes and spheres rendering with different colors
Scene class allows you to make a complete scene using Geometry
Main class starts the application
You can use Visual C++ Express Edition to build the demo but it's important to have OpenGL well configured in your environment, you can use this url to see how configure OpenGL:OpenGL configuration
Now open the sources and the Scene class. In this class we have several methods, for now we want to focus on draw() method. This method draws the scene and here we find the OpenGL code trasformations:
Geometry geo;
int colors1[]={ 255,153,51, 0,127,255, 255,153,51,
0,127,255, 0,127,255, 0,127,255};
int colors2[]={ 0,127,255, 102,255,0};
int colors3[]={ 255,0,0, 255,0,0, 255,0,0,
255,0,0, 255,0,0, 255,0,0};
int colors4[]={ 0,255,0, 0,255,0};
int colors5[]={ 255,255,0, 255,255,0};
/*Sphere in (0,1,0) 90 degree rotated around x axes
Note:First trasformation is the rotation
Second trasformation is the translation
So you have to read the trasformations
from down to up in your code
*/
glPushMatrix();
glTranslatef(0,1,0);//Second
glRotatef(90,1,0,0);//First
geo.sphere(1,20,20,colors2);
glPopMatrix();
/*Box in (2,0.75,2)*/
glPushMatrix();
glTranslatef(2,0.75,2);
geo.box(1.5,1.5,1.5,colors1);
glPopMatrix();
/*Box in (-2,0.75,-2)*/
glPushMatrix();
glTranslatef(-2,0.75,-2);
geo.box(1.5,1.5,1.5,colors3);
glPopMatrix();
/*Sphere in (3,1,-3) 90 degree rotated around x axes*/
glPushMatrix();
glTranslatef(3,1,-3);
glRotatef(90,1,0,0);
geo.sphere(1,20,20,colors4);
glPopMatrix();
/*Sphere in (-4,1,4) 90 degree rotated around x axes*/
glPushMatrix();
glTranslatef(-4,1,4);
glRotatef(90,1,0,0);
geo.sphere(1,20,20,colors5);
glPopMatrix();
/* */
The glPushMatrix() save the current ModelView matrix, the glPopMatrix() restore the matrix saved. So we have used this methods to save current trasformation matrix before to apply our trasformations for a box or a sphere and then restore the initial trasformation matrix. If you note the code for a sphere, you can see a trasformation composition. The sphere is first rotated and then translated. Use this fragment code to play with trasformations and understand how OpenGL works with them.Well I have finished for now. I hope this post is useful.