Document directory
- 1 initialize material, light, texture, and planet
- 2. Draw a planet
- 3. Use openfiledialog to replace the texture
- 4. Drag and Drop to replace the texture
Introduction to sharpgl (OpenGL)-texture planet
PS: sharpgl is the C # OpenGL encapsulation. If you are not familiar with sharpgl, you can search for sharpgl at www.codeproject.com.
In a small example, we can load various images to a spherical planet to show that the planet is automatically rotated. We can use wsad and the mouse to control the angle of view and use QE to control the upper and lower pages. The source code and the release program are provided at the end of this article.
A picture paints a thousand sentences.
PS: rotating Earth GIF here: http://images.cnblogs.com/cnblogs_com/bitzhuwei/482613/o_earth.gif
Rotating moon GIF here: http://images.cnblogs.com/cnblogs_com/bitzhuwei/482613/o_moon.gif
1 initialize material, light, texture, and planet
Private void openglcontrol_openglinitialized (Object sender, eventargs e) {OpenGL GL = openglcontrol. openGL; GL. clearcolor (0, 0, 0, 0); GL. shademodel (OpenGL. gl_smooth); // initialize the material var mat_specular = new float [] {1.0f, 1.0f, 1.0f, 1.0f}; var mat_ambient = new float [] {1.0f, 1.0f, 1.0f, 1.0f}; var mat_diffuse = new float [] {2.0f, 2.0f, 2.0f, 0.1f}; var mat_shininess = new float [] {100366f}; GL. material (OpenGL. gl_front, OpenGL. gl_specular, mat_specular); GL. material (OpenGL. gl_front, OpenGL. gl_ambient, mat_ambient); GL. material (OpenGL. gl_front, OpenGL. gl_diffuse, mat_diffuse); GL. material (OpenGL. gl_front, OpenGL. gl_shininess, mat_shininess); // initialize the illumination var ambientlight = new float [] {1.0f, 1.0f, 1.0f, 1.0f}; var diffuselight = new float [] {1.0f, 1.0f, 1.0f, 1.0f, 1.0f}; var poslight0 = new float [] {2.0f, 0.1f, 0.0f, 0.0f}; GL. light (OpenGL. gl_light0, OpenGL. gl_ambient, ambientlight); GL. light (OpenGL. gl_light0, OpenGL. gl_diffuse, diffuselight); GL. light (OpenGL. gl_light0, OpenGL. gl_position, poslight0); // initialize the texture = new sharpgl. scenegraph. assets. texture (); texture. create (GL, "Data/earth.bmp"); // initialize planet ptrball = gl. newquadric (); GL. quadricnormals (ptrball, OpenGL. gl_smooth); GL. quadrictexture (ptrball, (INT) (OpenGL. gl_true); GL. enable (OpenGL. gl_texture_2d); GL. enable (OpenGL. gl_lighting); GL. enable (OpenGL. gl_light0); GL. enable (OpenGL. gl_light1); GL. enable (OpenGL. gl_depth_test );}
2. Draw a planet
Private void openglcontrol_opengldraw (Object sender, painteventargs e) {If (this. Texture! = NULL) {OpenGL GL = openglcontrol. openGL; GL. clear (OpenGL. gl_color_buffer_bit | OpenGL. gl_depth_buffer_bit); GL. loadidentity (); GL. scale (3.6, 3.6, 3.6); // zoom in to 3.6 times GL. rotate (90, 1.0f, 0, 0); // Rotate 90 degrees GL around the X axis. rotate (-rotation, 0.0f, 0.0f, 1.0f); // rotate GL on the Rao Z axis. sphere (ptrball, 1.0f, 100,100); // draw the planet rotation + = 3.0f ;}}
3. Use openfiledialog to replace the texture
private void lblTextureImage_Click(object sender, EventArgs e) { if (openTextureImage.ShowDialog()== System.Windows.Forms.DialogResult.OK) { UpdateTextureImage(openTextureImage.FileName); } } private void UpdateTextureImage(string filename) { var gl = this.openGLControl.OpenGL; if (this.texture != null) { this.texture.Destroy(gl); this.texture.Create(gl, filename); this.lblTextureImage.Text = (new FileInfo(filename)).Name; } }
4. Replace texture 4.1 attribute settings by dragging
Set the allowdrop attribute of openglcontrol to true.
4.2 Event code
Add the dragenter and dragdrop events to openglcontrol.
The Code is as follows.
private void openGLControl_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.All; } else { e.Effect = DragDropEffects.None; } } private void openGLControl_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { var item = (string[])e.Data.GetData(DataFormats.FileDrop); UpdateTextureImage(item[0]); } }
Enjoy!
The complete source code is here: bitzhuwei.solarsystem.texturedearthur .zip
The release version is available here: tattoo -release.zip