Hej gruppe,
jeg tænkte der var nogle som havde arbejdet meget med OpenGL og kunne
fortælle hvilke ændringer der skal til i flg. main-fil for at lyset
følger kameraet:
-------------------------
#include "opengl.h"
#include <GL/glut.h>
#include <stdlib.h>
#include "Graphics/Geometry.h"
#include "Graphics/Triangle.h"
#include "Graphics/Robot.h"
#include "Graphics/Link.h"
#include "Graphics/Frame.h"
#include "Math/Rotation3D.h"
using Graphics::Geometry;
using Graphics::Triangle;
using Graphics::Robot;
using Graphics::Link;
float angle=0.0;
float red=1.0, blue=1.0, green=1.0;
int activejoint = 0;
Robot rob;
int x_last, y_last;
bool zoom = false, pan = false;
Vector3D vec(3,0,0), vec2(0,0,1), vec3(0,0,0);
void mouseButtonFunc(int button, int state, int x, int y) {
x_last = x; y_last = y;
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
zoom = true;
else if(glutGetModifiers() == GLUT_ACTIVE_SHIFT && state == GLUT_DOWN)
pan = true;
if(state == GLUT_UP){
pan = false; zoom = false;
}
}
void motionFunc(int x, int y) {
if(zoom)
vec *= 1 + 0.01*(y_last - y);
else if(pan){
Math::Rotation3D rot(0,0.001*(y_last - y),0.001*(x_last - x));
vec3 = rot*(vec3-vec) + vec;
}else{
Math::Rotation3D rot(0,0.01*(y_last - y),0.01*(x_last - x));
vec = rot*vec;
vec2 = rot*vec2;
}
x_last = x; y_last = y;
}
void renderScene(void) {
// notice that we're now clearing the depth buffer
// as well this is required, otherwise the depth buffer
// gets filled and nothing gets rendered.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
{
gluLookAt(vec(0), vec(1), vec(2),vec3(0), vec3(1), vec3(2),
vec2(0), vec2(1), vec2(2));
rob.draw();
}
glPopMatrix();
glutSwapBuffers();
glFlush();
}
void processSpecialKeys(int key, int x, int y) {
switch(key) {
case GLUT_KEY_F1 :
red = 1.0;
green = 0.0;
blue = 0.0; break;
case GLUT_KEY_F2 :
red = 0.0;
green = 1.0;
blue = 0.0; break;
case GLUT_KEY_F3 :
red = 0.0;
green = 0.0;
blue = 1.0; break;
case GLUT_KEY_RIGHT :
angle = 0.05;
break;
case GLUT_KEY_LEFT:
angle = -0.05;
break;
}
Link *l = rob.getLink()->getNextLink();
for(int i = 0; i < activejoint; i++)
l = l->getNextLink();
//std::cout << angle << " ";
l->setParam(l->getParam() + angle);
//std::cout << l->getParam() << std::endl;
}
void processNormalKeys(unsigned char key, int x, int y) {
if (key == 27)
exit(0);
switch(key){
case '0':
activejoint = 0;
break;
case '1':
activejoint = 1;
break;
case '2':
activejoint = 2;
break;
case '3':
activejoint = 3;
break;
case '4':
activejoint = 4;
break;
case '5':
activejoint = 5;
break;
}
angle = 0;
std::cout << activejoint << std::endl;
}
void changeSize(int w, int h) {
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
// Projection
glViewport(0, 0, w-1, h-1);
glMatrixMode(GL_PROJECTION);
{
glLoadIdentity();
gluPerspective(45,ratio,1,1000);
}
// ModelView
glMatrixMode(GL_MODELVIEW);
}
void parseParameters(int count, char **c) {
Link *link = rob.getLink();
std::cout << count << std::endl;
if(count != 8)
return;
for(int i = 2; i < 8; i++) {
link = link->getNextLink();
link->setParam(atof(c[i])*0.0174532925);
}
}
void init(unsigned int h, unsigned int w) {
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
glViewport(0, 0, w-1, h-1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
GLfloat mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
GLfloat mat_shininess[] = { 25.0 };
GLfloat mat_diffuse[] = {0.9, 0.7, 0.1, 0.7};
GLfloat light_position[] = { 1, 1, 1, 0 };
//GLfloat light_position1[] = { -5, -5, 5, 1.0 };
glClearColor (0.0, 0.0, 0.0, 0.0);
//glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
//glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//glEnable(GL_LIGHT1);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("A-Team - Robot Simulator Deluxe");
init(320,320);
if (argc > 1)
rob.parseDevFile(argv[1]);
parseParameters(argc, argv);
/*this just prints some stuff
Link *l = rob.getLink();
Math::Transform3D trans = *l;
while((l = l->getNextLink()) != NULL)
trans = trans*(*l);
std::cout << trans << std::endl;*/
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glutMotionFunc(motionFunc);
glutMouseFunc(mouseButtonFunc);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);
glutMainLoop();
return 0;
}
-------------------------
På forhånd mange tak!!!
Med venlig hilsen
Preben Holm
|