Two days ago, I introduced how to build the QT environment and debug simple programs, which may not be very detailed (no picture, it is not intuitive to read the text,
But now I still don't understand how csdn can be used as a method). I will introduce how to program OpenGL in Qt in a few days.
Because OpenGL has been integrated in QT, you can use it directly, but I also encountered a lot of trouble during the debugging process.
Link error, so I added the OpenGL header file and link library in vs2005 again. This compilation was successful, which is troublesome ~
How to configure OpenGL in vs2005:
Glut in Windows: (the size is about 150 K)
Http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
If you cannot download from the above address, use the following connection:
Http://upload.programfan.com/upfile/200607311626279.zip
To install glut in Windows:
1. Unpack the downloaded package and obtain 5 files.
2. Take my installation directory as an example:
(1) "D:/software/vs2005 team suite/VC/include/GL folder ". Put the extracted glut. h In this gl file.
Folder. You can create one by yourself without the GL folder.
(2) "D:/software/vs2005 team suite/VC/lib folder "). Put the extracted glut. lib and glu32.lib
The folder where the static library is located, that is, the Lib folder.
(3) Place the extracted glut. dll and glu32.dll In the System32 folder under the operating system directory. (The typical location is
: C:/Windows/system32) This is an important dynamic link library setting!
After all, I found a. BMP image. My OpenGL program is from
Http://www.chinagcn.com/bbs/viewthread.php? Tid = 2487 articles in 7th-the example on the eighth floor, but slightly changed
The Code is as follows:
Nehewidget. h file
/*
Qt OpenGL tutorial-Lesson 07
Nehewidget. h
V 1.00
2002/12/19
Copyright (c) 2002 carendish
Cavendish@qiliang.net
Http://www.qiliang.net/nehe_qt
This program is free software; you can redistribute it and/or modify
It under the terms of the GNU General Public License as published
The Free Software Foundation; either version 2 of the license, or
(At Your option) any later version.
*/
# Ifndef nehewidget_h
# Define nehewidget_h
# Include <QT/qgl. h>
Class nehewidget: Public qglwidget
{
Q_object
Public:
Nehewidget (qwidget * parent = 0, const char * name = 0, bool FS = false );
~ Nehewidget ();
Protected:
Void initializegl ();
Void paintgl ();
Void resizegl (INT width, int height );
Void keypressevent (qkeyevent * E );
Void loadgltextures ();
Protected:
Bool fullscreen;
Glfloat xrot, yrot, Zrt;
Glfloat zoom;
Glfloat xspeed, yspeed;
Gluint texture [3];
Gluint filter;
Bool light;
};
# Endif // nehewidget_h
//////////////////////////////////////// //////////////////////////////////////// ////////////
//////////////////////////////////////// ////
Nehewidget. cpp File
/*
Qt OpenGL tutorial-Lesson 07
Nehewidget. cpp
V 1.00
2002/12/19
Copyright (c) 2002 carendish
Cavendish@qiliang.net
Http://www.qiliang.net/nehe_qt
This program is free software; you can redistribute it and/or modify
It under the terms of the GNU General Public License as published
The Free Software Foundation; either version 2 of the license, or
(At Your option) any later version.
*/
# Include <qtgui/qapplication>
# Include <qtgui/qmainwindow>
# Include <qtgui/qwidget>
# Include <qtgui/qimage. h>
# Include <qtgui/qevent. h>
# Include "nehewidget. H"
Glfloat lightambient [4] = {0.5, 0.5, 0.5, 1.0 };
Glfloat lightdiffuse [4] = {1.0, 1.0, 1.0, 1.0 };
Glfloat lightposition [4] = {0.0, 0.0, 2.0, 1.0 };
Nehewidget: nehewidget (qwidget * parent, const char * Name, bool FS)
: Qglwidget (parent/*, name */)
{
Xrot = yrot = roroot = 0.0;
Scale =-5.0;
Xspeed = yspeed= 0.0;
Filter = 0;
Light = false;
Fullscreen = FS;
Setgeometry (100,100,640,480 );
// Setcaption ("nehe's texture, Lighting & keyboard tutorial ");
If (fullscreen)
Showfullscreen ();
}
Nehewidget ::~ Nehewidget ()
{
}
Void nehewidget: paintgl ()
{
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );
Glloadidentity ();
Gltranslatef (0.0, 0.0, zoom );
Glrotatef (xrot, 1.0, 0.0, 0.0 );
Glrotatef (yrot, 0.0, 1.0, 0.0 );
Glbindtexture (gl_texture_2d, texture [filter]);
Glbegin (gl_quads );
Glnormal3f (0.0, 0.0, 1.0 );
Gltexcoord2f (0.0, 0.0); glvertex3f (-1.0,-1.0, 1.0 );
Gltexcoord2f (1.0, 0.0); glvertex3f (1.0,-1.0, 1.0 );
Gltexcoord2f (1.0, 1.0); glvertex3f (1.0, 1.0, 1.0 );
Gltexcoord2f (0.0, 1.0); glvertex3f (-1.0, 1.0, 1.0 );
Glnormal3f (0.0, 0.0,-1.0 );
Gltexcoord2f (1.0, 0.0); glvertex3f (-1.0,-1.0,-1.0 );
Gltexcoord2f (1.0, 1.0); glvertex3f (-1.0, 1.0,-1.0 );
Gltexcoord2f (0.0, 1.0); glvertex3f (1.0, 1.0,-1.0 );
Gltexcoord2f (0.0, 0.0); glvertex3f (1.0,-1.0,-1.0 );
Glnormal3f (0.0, 1.0, 0.0 );
Gltexcoord2f (0.0, 1.0); glvertex3f (-1.0, 1.0,-1.0 );
Gltexcoord2f (0.0, 0.0); glvertex3f (-1.0, 1.0, 1.0 );
Gltexcoord2f (1.0, 0.0); glvertex3f (1.0, 1.0, 1.0 );
Gltexcoord2f (1.0, 1.0); glvertex3f (1.0, 1.0,-1.0 );
Glnormal3f (0.0,-1.0, 0.0 );
Gltexcoord2f (1.0, 1.0); glvertex3f (-1.0,-1.0,-1.0 );
Gltexcoord2f (0.0, 1.0); glvertex3f (1.0,-1.0,-1.0 );
Gltexcoord2f (0.0, 0.0); glvertex3f (1.0,-1.0, 1.0 );
Gltexcoord2f (1.0, 0.0); glvertex3f (-1.0,-1.0, 1.0 );
Glnormal3f (1.0, 0.0, 0.0 );
Gltexcoord2f (1.0, 0.0); glvertex3f (1.0,-1.0,-1.0 );
Gltexcoord2f (1.0, 1.0); glvertex3f (1.0, 1.0,-1.0 );
Gltexcoord2f (0.0, 1.0); glvertex3f (1.0, 1.0, 1.0 );
Gltexcoord2f (0.0, 0.0); glvertex3f (1.0,-1.0, 1.0 );
Glnormal3f (-1.0, 0.0, 0.0 );
Gltexcoord2f (0.0, 0.0); glvertex3f (-1.0,-1.0,-1.0 );
Gltexcoord2f (1.0, 0.0); glvertex3f (-1.0,-1.0, 1.0 );
Gltexcoord2f (1.0, 1.0); glvertex3f (-1.0, 1.0, 1.0 );
Gltexcoord2f (0.0, 1.0); glvertex3f (-1.0, 1.0,-1.0 );
Glend ();
Xrot + = xspeed;
Yrot + = yspeed;
}
Void nehewidget: initializegl ()
{
Loadgltextures ();
Glable (gl_texture_2d );
Glshademodel (gl_smooth );
Glclearcolor (0.0, 0.0, 0.0, 0.5 );
Glcleardepth (1.0 );
Glenable (gl_depth_test );
Gldepthfunc (gl_lequal );
Glhint (gl_perspective_correction_hint, gl_nicest );
Gllightfv (gl_light1, gl_ambient, lightambient );
Gllightfv (gl_light1, gl_diffuse, lightdiffuse );
Gllightfv (gl_light1, gl_position, lightposition );
Glable (gl_light1 );
}
Void nehewidget: resizegl (INT width, int height)
{
If (Height = 0)
{
Height = 1;
}
Glviewport (0, 0, (glint) width, (glint) height );
Glmatrixmode (gl_projection );
Glloadidentity ();
Gluperspective (45.0, (glfloat) width/(glfloat) height, 0.1, 100.0 );
Glmatrixmode (gl_modelview );
Glloadidentity ();
}
Void nehewidget: keypressevent (qkeyevent * E)
{
Switch (e-> key ())
{
Case QT: key_l:
Light =! Light;
If (! Light)
{
Gldisable (gl_lighting );
}
Else
{
Glable (gl_lighting );
}
Updategl ();
Break;
Case QT: key_f:
Filter + = 1 ;;
If (filter> 2)
{
Filter = 0;
}
Updategl ();
Break;
Case QT: key_pageup:
Zoom-= 0.2;
Updategl ();
Break;
Case QT: key_pagedown:
Zoom ++ = 0.2;
Updategl ();
Break;
Case QT: key_up:
Xspeed-= 0.01;
Updategl ();
Break;
Case QT: key_down:
Xspeed ++ = 0.01;
Updategl ();
Break;
Case QT: key_right:
Yspeed ++ = 0.01;
Updategl ();
Break;
Case QT: key_left:
Yspeed-= 0.01;
Updategl ();
Break;
Case QT: key_f2:
Fullscreen =! Fullscreen;
If (fullscreen)
{
Showfullscreen ();
}
Else
{
Shownormal ();
Setgeometry (100,100,640,480 );
}
Update ();
Break;
Case QT: key_escape:
Close ();
}
}
Void nehewidget: loadgltextures ()
{
Qimage Tex, Buf;
If (! Buf. Load (":/mybmp/crate.bmp "))
{
Qwarning ("cocould not read image file, using single-color instead .");
Qimage dummy( 128,128,/* 32 */qimage: format_indexed8 );
Dummy. Fill (QT: Green );
Buf = dummy;
}
Tex = qglwidget: converttoglformat (BUF );
Glgentextures (3, & texture [0]);
Glbindtexture (gl_texture_2d, texture [0]);
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_nearest );
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_nearest );
Glteximage2d (gl_texture_2d, 0, 3, Tex. Width (), Tex. Height (), 0,
Gl_rgba, gl_unsigned_byte, Tex. Bits ());
Glbindtexture (gl_texture_2d, texture [1]);
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear );
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_linear );
Glteximage2d (gl_texture_2d, 0, 3, Tex. Width (), Tex. Height (), 0,
Gl_rgba, gl_unsigned_byte, Tex. Bits ());
Glbindtexture (gl_texture_2d, texture [2]);
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear );
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_linear_mipmap_nearest );
Glubuild2dmipmaps (gl_texture_2d, gl_rgb, Tex. Width (), Tex. Height (),
Gl_rgba, gl_unsigned_byte, Tex. Bits ());
}
//////////////////////////////////////// //////////////////////////////////////// ////////////
////////////////////////////////////
Main. cpp File
/*
Qt OpenGL tutorial-Lesson 07
Main. cpp
V 1.00
2002/12/19
Copyright (c) 2002 carendish
Cavendish@qiliang.net
Http://www.qiliang.net/nehe_qt
This program is free software; you can redistribute it and/or modify
It under the terms of the GNU General Public License as published
The Free Software Foundation; either version 2 of the license, or
(At Your option) any later version.
*/
# Include <qtgui/qapplication>
# Include <qtgui/qmainwindow>
# Include <qtgui/qwidget>
# Include <qtgui/qmessagebox>
# Include <QT/qgl. h>
# Include "nehewidget. H"
Int main (INT argc, char ** argv)
{
Bool FS = false;
Qapplication A (argc, argv );
Switch (qmessagebox: Information (0,
"Start fullscreen? ",
"Wocould you like to run in fullscreen mode? ",
Qmessagebox: Yes,
Qmessagebox: No | qmessagebox: Default ))
{
Case qmessagebox: Yes:
FS = true;
Break;
Case qmessagebox: No:
FS = false;
Break;
}
Nehewidget W (0, 0, FS );
// A. setmainwidget (& W); // qt4.x or a later version is not required
// A. setactivewindow (& W );
W. Show ();
Return a.exe C ();
}
The method for creating a project is the same as that before
1. Use vs2005 to create an empty console project and create a. qrc resource file.
<RCC>
<Qresource prefix = "mybmp">
<File> crate.bmp </File>
</Qresource>
</RCC>
Save crate.bmp as an image file
After that, change the crate.bmp,. qrc resource file, Main. cpp, nehewidget. H, nehewidget. cpp, and lesson07.cpro.
Myogl. Pro) copy to your project directory and import the. qrc resource file as the source file into your project;
2. Then, go to CMD and enter your project directory,
D:/mygl> qmake-project-T vcapp-O mygl. Pro
D:/mygl> qmake
D:/mygl> moc.exe nehewidget. H-o debug/moc_nehewidget.cpp
D:/mygl> moc.exe nehewidget. H-o release/moc_nehewidget.cpp (you can also directly copy the debug file)
This will generate a file named moc_nehewidget.cpp, which cannot be run without. Put it in the same directory as main. cpp (I didn't expect QT
The main reason for this is that nehewidget. H. Your class is q_object, which is much more troublesome than VC)
Use the generated. vcproj file instead of the original one, which is the same as the method described in the previous two articles.
3. Open your project
Set "use MFC in shared DLL", "use multi-Byte Character Set", and add glu32.lib to the additional dependency.
Opengl32.lib
D:/QT/4.4.3/lib/qtopengl. Lib (depending on your directory)
Then it is OK to re-import the main. cpp and nehewidget. cpp files. Compile the file and run it to view the side of a wooden box.
Picture ^_^ (this picture also exists in the book of X-Dragon, huh, huh)
This article from the csdn blog, from: http://blog.csdn.net/feilinhe/archive/2009/07/24/4377074.aspx