@Pigmon
2016-11-28T14:56:19.000000Z
字数 10884
阅读 1266
上课
如果还没能让 Cg-CourseExamples 工程正常工作,参考之前这篇(以下简称配置文档1):
老师的Cgcourse-examples环境的配置方法 (pdf)
链接:http://pan.baidu.com/s/1jI2ToUE 密码:yxea
直接用现有工程,只替换程序。这样项目名字没法改,不过反正交作业的时候也只是提供一个源程序,一个exe,一个文档。所以其实完全是可以的。如果你能接受,可以直接跳到后面的“参考大作业1指南”。
需要注意的是,有的大作业需要MITK,而有的工程没有,所以要选好工程。比如,参考大作业1可以在texture2d工程直接修改;而参考大作业2可以在dyncubemap工程里直接修改。
增加库的方法,可以参考下面的“正常的方法”
注意,如果要自己建项目,那么之前的包含Cg-CourseExamples源程序的目录必须改成全英文的(否则引用目录会出问题)。原来是“图形学技术实践”之类的名字,改成C:\CG-Course,这样后面的目录什么的就可以复制粘贴了。
一句话形容:
在解决方案里新建一个项目然后删掉,把新建的项目文件拷贝回cgcourse-examples目录,再添加回解决方案。然后配置新项目的include目录和附加的lib。
以下是今天的详细内容:
在Visual Studio里打开了 Cg-CourseExamples 解决方案;在“解决方案资源管理器”中右键点击最上面的解决方案名,在弹出菜单中选择“添加-新建项目”
图1 在解决方案管理器中增加新项目
项目类型就是控制台空项目,目录随意
图2 新建控制台项目
记住选“空项目”,建立好应该是这样:
图3 在解决方案管理器中的新建项目
在刚刚的解决方案资源管理器中,右键点击刚刚建好的项目,选择“移除”,没错就是删了。为了后面配置目录比较方便不容易出错。
找到刚刚建立新项目的目录,将刚刚建立好的两个项目文件拷贝到cgcourse-examples目录下,即在配置文档1中生成的项目目录。
在刚刚的Visual Studio解决方案管理器中右键点击解决方案名,在弹出菜单中选择“添加”->"现有项目",注意,是“现有项目”! 然后选择刚刚拷贝过来的新建的项目,如本例子中的TestTutor.
现在又跟图3看起来一样了。
展开新建的项目,右键点击“源文件”,在弹出菜单中选择“添加”->“新建项”->“C++文件(.cpp)”,选好名字,比如main.cpp,点确定(这样项目属性中的C/C++部分才能出来)。
从下一步开始,Debug和Release都要分别配置。
右键点击新建的项目,在弹出菜单中选择“属性”,然后按这个顺序点击:
把这3条依次加进去,注意第3条,要按自己的路径配置;前2条如果是按照配置文档1的方法配置,应该是一样的:
C:\Program Files (x86)\Windows Kits\8.1\Include\um
C:\Program Files (x86)\Windows Kits\8.1\Include\shared
C:\CG-Course\examples\cgcourse-examples\include
然后点“确定”关闭此对话框。
然后在属性对话框点“应用”。
9.在项目属性中选择“链接器”->“输入”,按以下顺序点击:
glu32.lib
opengl32.lib
C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\freeglut.lib
C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\FreeImage.lib
C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86\Mitk_dll.lib
注意最后一个是MITK库的lib,不是每个项目都需要。
然后点击“确定”关闭该对话框,在属性对话框中点“应用”
这样就可以按照Cg-CourseExamples其他项目那样编译程序了。
将以下程序复制粘贴到刚刚建好项目的.cpp中覆盖,注意注释中,标注了“鼠标转动”,“显示列表”,“双缓存”和“题目要求”的地方是相对于texture2d项目进行修改的,也是题目要求的部分;而“字符”“菜单”“字符”部分留给大家自己完成。其中“菜单”按照老师讲义完全copy就行,字符部分我会在之后的文档中放出。
注意1:我并不会OpenGL,所以以下程序肯定不是最简方案,我只是简单的把2个例子凑在了一起,如果有修改意见及时通知我。
注意2:最上面的注释是版权声明,是老师自己写的,留不留你自己看着办 :P
/*
-----------------------------------------------------------------------
This source file is part of "cgcourse-examples"
(Examples for Computer Graphics Course of CCCE GUCAS.)
Copyright (C) 2011 Xue Jian (jian.xue.cn@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-----------------------------------------------------------------------
*/
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include "glinc.h"
#include "matrix.h"
#include "geotypes.h"
#include "trackball.h"
#include "camera.h"
#include "FreeImage.h"
using std::vector;
GLuint g_tex_id;
int g_window_width = 512;
int g_window_height = 512;
// light
GLfloat g_light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat g_light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat g_light_position[] = { -3.5f, 1.5f, 2.5f, 1.0f };
GLfloat g_light_direction[] = { 3.5f, -1.5f, -2.5f, 0.0f };
GLfloat g_light_rot = 0.0f;
GLfloat g_light_rot_speed = 0.2f;
// material
GLfloat g_material_ambient[] = { 1.0f, 0.57f, 0.04f, 1.0f };
GLfloat g_material_diffuse[] = { 1.0f, 0.57f, 0.04f, 1.0f };
GLfloat g_material_specular[] = { 0.33f, 0.33f, 0.52f, 0.0f };
GLfloat g_material_emission[] = { 0.0f, 0.0f, 0.0f, 0.0f };
GLfloat g_material_specular_power = 80.0f;
// bg color
GLfloat g_color_background[] = { 0.0f, 0.0f, 0.0f, 0.0f };
GLfloat g_color_foreground[] = { 1.0f - g_color_background[0], 1.0f - g_color_background[1], 1.0f - g_color_background[2], 1.0f };
// 是否打开光照,你可以做个菜单项控制它(菜单是题目要求)
bool g_light = false;
// cam
Cameraf g_cam;
float g_z = 5.0f;
float const Z_STEP = 0.1f;
// --------------------------------------------------------
// 鼠标转动 -->
// --------------------------------------------------------
bool g_left_button_down = false;
bool g_middle_button_down = false;
bool g_right_button_down = false;
int g_last_x = 0;
int g_last_y = 0;
GLint g_viewport[4];
TrackBallf g_trackball(1.5f);
Matrixf g_rotm;
// --------------------------------------------------------
// 鼠标转动 <--
// --------------------------------------------------------
// 盒子顶点
float g_cube_v[][3] = {
{ -1.0f, -1.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f, 1.0f },
{ -1.0f, -1.0f, -1.0f },
{ -1.0f, 1.0f, -1.0f },
{ 1.0f, 1.0f, -1.0f },
{ 1.0f, -1.0f, -1.0f }
};
// 建立盒子的显示列表,题目要求
void init_cube_list();
void set_material()
{
GLenum face = GL_FRONT_AND_BACK;
glMaterialfv(face, GL_AMBIENT, g_material_diffuse);
glMaterialfv(face, GL_DIFFUSE, g_material_diffuse);
glMaterialfv(face, GL_SPECULAR, g_material_specular);
glMaterialfv(face, GL_EMISSION, g_material_emission);
glMaterialf(face, GL_SHININESS, g_material_specular_power);
}
FIBITMAP* load_image(char const *filename, int flag = 0)
{
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(filename, 0);
if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(filename);
if (fif == FIF_UNKNOWN) return false;
return FreeImage_Load(fif, filename, flag);
}
bool load_textures()
{
/// 这个目录是题目要求的木箱子贴图
FIBITMAP *tdib = load_image("data/texture2d/Crate.png");
if (!tdib) return false;
bool status(false);
unsigned int bpp = FreeImage_GetBPP(tdib);
FIBITMAP *dib = tdib;
if (bpp != 24) dib = FreeImage_ConvertTo24Bits(tdib);
BYTE *bits = FreeImage_GetBits(dib);
unsigned int width = FreeImage_GetWidth(dib);
unsigned int height = FreeImage_GetHeight(dib);
GLenum format = FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR ? GL_BGR : GL_RGB;
RGBQUAD *pal = FreeImage_GetPalette(dib);
if (bits != 0 && width>0 && height>0) {
status = true; // Set The Status To TRUE
glGenTextures(1, &g_tex_id); // Create Three Textures
// Create Linear Filtered Texture
glBindTexture(GL_TEXTURE_2D, g_tex_id);
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, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, bits);
}
if (bpp != 24) FreeImage_Unload(dib);
FreeImage_Unload(tdib);
return status; // Return The Status
}
bool init()
{
if (!load_textures()) {
return false;
}
g_rotm.IdentityMatrix();
if (g_light) glEnable(GL_LIGHTING);
else glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(g_color_background[0], g_color_background[1], g_color_background[2], g_color_background[3]); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.0f);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.0f);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.0f);
//glLightfv(GL_LIGHT1, GL_AMBIENT, g_light_ambient); // Setup The Ambient Light
glLightfv(GL_LIGHT1, GL_DIFFUSE, g_light_diffuse); // Setup The Diffuse Light
glLightfv(GL_LIGHT1, GL_SPECULAR, g_light_diffuse);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 1.0f);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 30.0f);
glLightfv(GL_LIGHT1, GL_POSITION, g_light_position); // Position The Light
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, g_light_direction);
glEnable(GL_LIGHT1); // Enable Light One
set_material();
return true; // Initialization Went OK
}
// --------------------------------------------------------
// 鼠标转动 -->
// --------------------------------------------------------
void motion(int x, int y)
{
if (g_left_button_down)
{
g_last_x = x;
g_last_y = y;
g_trackball.Update((float)(2.0*(x - g_viewport[0]) / g_viewport[2] - 1), (float)(2.0*(g_viewport[3] - y - 1 - g_viewport[1]) / g_viewport[3] - 1));
g_trackball.BuildRotMatrix(g_rotm);
glutPostRedisplay();
}
}
void mouse(int button, int state, int x, int y)
{
bool down = (state == GLUT_DOWN);
switch (button)
{
case GLUT_LEFT_BUTTON: g_left_button_down = down; break;
case GLUT_MIDDLE_BUTTON: g_middle_button_down = down; break;
case GLUT_RIGHT_BUTTON: g_right_button_down = down; break;
}
if (g_left_button_down)
{
g_trackball.Start((float)(2.0*(x - g_viewport[0]) / g_viewport[2] - 1), (float)(2.0*(g_viewport[3] - y - 1 - g_viewport[1]) / g_viewport[3] - 1));
}
g_last_x = x;
g_last_y = y;
}
// --------------------------------------------------------
// 鼠标转动 <--
// --------------------------------------------------------
// --------------------------------------------------------
// 显示列表,题目要求
// --------------------------------------------------------
void init_cube_list()
{
glNewList(1, GL_COMPILE);
glColor3f(1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, g_tex_id);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); // Draw A Quad
// Front Face
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(g_cube_v[0]);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(g_cube_v[1]);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(g_cube_v[2]);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(g_cube_v[3]);
// Back Face
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(g_cube_v[4]);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(g_cube_v[5]);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(g_cube_v[6]);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(g_cube_v[7]);
// Top Face
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(g_cube_v[5]);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(g_cube_v[3]);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(g_cube_v[2]);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(g_cube_v[6]);
// Bottom Face
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(g_cube_v[4]);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(g_cube_v[7]);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(g_cube_v[1]);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(g_cube_v[0]);
// Right face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(g_cube_v[7]);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(g_cube_v[6]);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(g_cube_v[2]);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(g_cube_v[1]);
// Left Face
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(g_cube_v[4]);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(g_cube_v[0]);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(g_cube_v[3]);
glTexCoord2f(0.0f, 1.0f); glVertex3fv(g_cube_v[5]);
glEnd(); // Done Draw
glEndList();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
g_cam.LookAt(0.0f, 0.0f, g_z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
g_material_diffuse[0] = 1.0f;
g_material_diffuse[1] = 0.57f;
g_material_diffuse[2] = 0.04f;
set_material();
// 鼠标转动
glMultMatrixf(g_rotm);
glCallList(1);
glutSwapBuffers();
}
void reshape(int width, int height)
{
if (height == 0)
{
height = 1;
}
glViewport(0, 0, width, height);
glGetIntegerv(GL_VIEWPORT, g_viewport);
g_cam.Perspective(45.0f, (float)(width) / (float)(height), 1.1f, 100.0f);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
/// 双缓存,题目要求
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(g_window_width, g_window_height);
glutCreateWindow("参考大作业1");
if (!init()) return -1;
init_cube_list();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutIdleFunc(display);
glutMainLoop();
return 0;
}