|
|
考题2的图片在附件里
考题1
要求考生利用提供的框架,编写出三种不同的动作。考生可参考‘test3D sample.exe’,实现要求的动作。注意所有动作须以时间作为计算单位。
动作一:球体以缧旋状移动,往外扩展,然后返回中心,再往外扩展,不断循环。
动作二:球体垂直摆动,摆幅慢慢增加,然后减少,不断循环。
动作三:球体绕圈移动,并同时上下以固定幅度摆动。
操作:
‘1’键 - 初始动作一
‘2’键 - 初始动作二
‘3’键 - 初始动作三
空格键 - 跳到下一个动作
提示:考生可利用以下数式,完成各个动作
x = r * cos(t)
y = r * sin(t)
程序框架如下:
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "gl/glut.h"
#include <math.h>
GLUquadricObj* sphere;
int time, lastTime, elapsed;
void Display(void)
{
time = GetTickCount();
elapsed = time - lastTime;
lastTime = time;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -20.0f);
// code here
glColor3f(0.0f, 0.4f, 0.8f);
gluSphere(sphere, 1.0f, 20, 20);
glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 49:
// code here
break;
case 50:
// code here
break;
case 51:
// code here
break;
case 32:
// code here
break;
case 27:
exit(0);
break;
}
}
void Reshape(GLsizei width, GLsizei height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)(width)/(float)(height), 1.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Init()
{
float position[4] = {10.0f, 10.0f, 10.0f, 1.0f};
float ambient[4] = {0.2f, 0.2f, 0.2f, 1.0f};
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
sphere = gluNewQuadric();
gluQuadricNormals(sphere, GLU_SMOOTH);
lastTime = GetTickCount();
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition (0, 0);
glutInitWindowSize(640, 480);
glutCreateWindow("3D Test");
glutDisplayFunc(Display);
glutIdleFunc(Display);
glutKeyboardFunc(Keyboard);
glutReshapeFunc(Reshape);
Init();
glutMainLoop();
}
考题2
Test Description:
A 8-bit height map (map.raw) is given to you, each pixel represent a height value. Please use the height map to complete the test program (test.cpp) with shading and shadowing effects. You can find a sample executable program (test sample.exe) for your reference.
Hint:
use cross product for normal calculation
use dot product and line rasterization to implement all necessary effects
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include "Include/glut.h"
using namespace std;
#define dimen 256
#define X 0
#define Y 1
#define Z 2
/******************************************************************************
* Global variables
* - feel free to add more global variables if you need more.
*******************************************************************************/
unsigned char pixels[dimen][dimen][3];
unsigned char map[dimen][dimen];
float normal[dimen][dimen][3];
int elapsed=0, time=0, lastTime=0, frame;
int mx=0, my=0;
char fps[32];
/******************************************************************************
* Helper functions
*******************************************************************************/
inline float max(float a, float b) { return (a > b) ? a : b;}
inline float min(float a, float b) { return (a < b) ? a : b;}
inline float fabs(float a) {return (a > 0.0f) ? a : -a;}
inline float dot(float* a, float *b) {return (a[X] * b[X] + a[Y]*b[Y] + a[Z] * b[Z]);}
/******************************************************************************
* File reader (10 pts)
*
* TODO:
* - you're provded a 256 x 256 greyscale heightmap called "map.raw"
* - finish this function to read in the height values
*
* TIPS:
* - display the values on the screen, and verify that with "map.jpg"
* - be ware of the orientation
*******************************************************************************/
void readFile(char *fn) {
// code here
}
/******************************************************************************
* Normal calculations (20 pts)
*
* TODO:
* - calculate the normals of the heightmaps
*
* TIPS:
* - display the values on the screen, but make sure you convert floats to bytes accordingly
* - each normal has three values (ijk), which can be seen on the screen as RGB
* - if your alogrithm cannot find the normals at the edges, set them to zeros.
*******************************************************************************/
void calNormals() {
// code here
}
/******************************************************************************
* Shading and shadowing (40 pts)
*
* TODO:
* - finish the shading of the heightmap using DOT product (N.L)
* - finish the self-shadowing
*
* TIPS:
* - a direction light vector has been calculated for you, it changes as the mouse moves
* - try to do the shading first, display and verify result
* - feel free to create new functions
*******************************************************************************/
void cal() {
// directional light vector
float light[3];
float len;
light[X] = mx - dimen/2;
light[Y] = my - dimen/2;
light[Z] = 150;
len = sqrt( fabs( dot(light, light) ) );
light[X] /= len;
light[Y] /= len;
light[Z] /= len;
// code here
}
void mouse(int x, int y) {
mx = x;
my = dimen-y;
}
void keyboard(unsigned char k, int x, int y) {
if (k == 27)
exit(0);
}
void renderScene(void) {
cal();
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2f(0, 0);
glDrawPixels (dimen, dimen, GL_RGB, GL_UNSIGNED_BYTE, (unsigned char *)pixels);
for (unsigned int i=0; i<strlen(fps); i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, fps);
//glFlush();
glutSwapBuffers();
// fps
frame++;
time = glutGet(GLUT_ELAPSED_TIME);
elapsed += time - lastTime;
lastTime = time;
if (elapsed > 100) {
sprintf(fps, "fps: %2.1f", (float)frame * 1000 / elapsed);
elapsed = 0;
frame = 0;
}
}
void main(int argc, char **argv) {
int x, y;
// init pixels
for (x=0; x<dimen; x++) {
for (y=0; y<dimen; y++) {
pixels[x][y][0] = (unsigned char)((float) x / dimen * 256);
pixels[x][y][1] = (unsigned char)((float) y / dimen * 256);
pixels[x][y][2] = 0;
}
}
// read map
readFile("map.raw");
// cal normals
calNormals();
// set up window
sprintf(fps, "fps: %1.2", 0.0f);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(dimen, dimen);
glutCreateWindow("Kogence Online");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutPassiveMotionFunc(mouse);
glutKeyboardFunc(keyboard);
glColor3f(1.0, 0.2, 0.2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, dimen, 0, dimen);
glutMainLoop();
}
|
-
|