Opengl 阅读该文件时,请先阅读这些信息,然后将转换应用于特定的对象/组。谢谢,我不知道这一点,现在请仔细阅读:)我已编辑了我的答案:) #include <stdlib.h> #include <windows.h> #include &l
Opengl 阅读该文件时,请先阅读这些信息,然后将转换应用于特定的对象/组。谢谢,我不知道这一点,现在请仔细阅读:)我已编辑了我的答案:) #include <stdlib.h> #include <windows.h> #include &l,opengl,glut,Opengl,Glut,阅读该文件时,请先阅读这些信息,然后将转换应用于特定的对象/组。谢谢,我不知道这一点,现在请仔细阅读:)我已编辑了我的答案:) #include <stdlib.h> #include <windows.h> #include <GL/glut.h> #include <stdio.h> #include <cmath> #include <GL/glut.h> #include <GL/GL.H> #inclu
阅读该文件时,请先阅读这些信息,然后将转换应用于特定的对象/组。谢谢,我不知道这一点,现在请仔细阅读:)我已编辑了我的答案:)
#include <stdlib.h>
#include <windows.h>
#include <GL/glut.h>
#include <stdio.h>
#include <cmath>
#include <GL/glut.h>
#include <GL/GL.H>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
float v[27982][3];
int f[25130][3];
int mX, mY;
int trX, trY, trZ;
GLfloat amblight[] = { 1.0, 0.0, 0.0, 0.0 };
GLfloat diflight[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat speclight[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat position[] = { 1.0, 0.0, 1.0 };
GLfloat difposition[] = { 1.0, 0.0, 1.0, 0.0 };
GLfloat shinines[] = { 120.0 };
GLfloat ambmatherial[] = { 1.0, 0.0, 0.0, 0.0 };
GLfloat difmatherial[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat specmatherial[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat angle = 0.0;
void init() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, difposition);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diflight);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, difmatherial);
glLightfv(GL_LIGHT0, GL_SPECULAR, speclight);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specmatherial);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shinines);
}
void idle() {
glutPostRedisplay();
}
class Coord
{
public:
float x;
float y;
float z;
} a3d, b3d, c3d, original;
Coord norm(Coord a3d, Coord b3d, Coord c3d)
{
Coord d3D_1, d3D_2, cross;
d3D_1.x = b3d.x - a3d.x;
d3D_1.y = b3d.y - a3d.y;
d3D_1.z = b3d.z - a3d.z;
d3D_2.x = c3d.x - b3d.x;
d3D_2.y = c3d.y - b3d.y;
d3D_2.z = c3d.z - b3d.z;
cross.x = d3D_1.y * d3D_2.z - d3D_1.z * d3D_2.y;
cross.y = d3D_1.z * d3D_2.x - d3D_1.x * d3D_2.z;
cross.z = d3D_1.x * d3D_2.y - d3D_1.y * d3D_2.x;
double l;
l = sqrt(cross.x * cross.x + cross.y * cross.y + cross.z * cross.z);
cross.x /= l;
cross.y /= l;
cross.z /= l;
return cross;
}
void drawHelicopter(){
glPushMatrix();
for (int i = 0; i<25130; i++){
glBegin(GL_TRIANGLES);
a3d.x = v[f[i][0]][0];
a3d.y = v[f[i][0]][1];
a3d.z = v[f[i][0]][2];
b3d.x = v[f[i][1]][0];
b3d.y = v[f[i][1]][1];
b3d.z = v[f[i][1]][2];
c3d.x = v[f[i][2]][0];
c3d.y = v[f[i][2]][1];
c3d.z = v[f[i][2]][2];
glVertex3f(a3d.x, a3d.y, a3d.z);
glVertex3f(b3d.x, b3d.y, b3d.z);
glVertex3f(c3d.x, c3d.y, c3d.z);
Coord response = norm(a3d, b3d, c3d);
glNormal3f(response.x, response.y, response.z);
glEnd();
}
glPopMatrix();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 0);
glLoadIdentity();
glTranslatef(0, 0, -1500);
glRotatef(180.0, 0, 1, 0);
glTranslatef(trX, trY, trZ);
glRotatef(mX, 0, 1, 0);
glRotatef(mY, 1, 0, 0);
glScalef(1.0f, 1.0f, 1.0f);
drawHelicopter();
glutSwapBuffers();
}
void reshape(int w, int h){
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLdouble)w / (GLdouble)h, 1.0, 8000.0);
glMatrixMode(GL_MODELVIEW);
}
void keyPressed(unsigned char key, int x, int y) {
if (key == 'a'){ trX += 20; }
else if (key == 'd'){ trX -= 20; }
else if (key == 'w'){ trZ += 20; }
else if (key == 's'){ trZ -= 20; }
else if (key == 'o'){ trY += 20; }
else if (key == 'l'){ trY -= 20; }
glutPostRedisplay();
}
void mouse(int x, int y){
mX = x;
mY = y;
glutPostRedisplay();
}
int main(){
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(0, 0);
glutInitWindowSize(1200, 640);
glutCreateWindow("Helicopter");
glClearColor(0.0, 0.0, 0.0, 0.0);
int k = 0, m = 0;
char ch;
float aa, bb, cc;
ifstream in("havoc.obj", ios::in);
string line;
while (getline(in, line))
{
//check v for vertices
if (line.substr(0, 2) == "v ")
{
istringstream vert(line.substr(2));
vert >> aa;
vert >> bb;
vert >> cc;
v[k][0] = (float)aa;
v[k][1] = (float)bb;
v[k][2] = (float)cc;
k++;
}
else if (line.substr(0, 2) == "f ")
{
int a, b, c, d; //to store mesh index
int A, B, C, D; //to store texture index
int a1, b1, c1;
const char* chh = line.c_str();
sscanf(chh, "f %i/%i/%i %i/%i/%i %i/%i/%i", &a, &A, &a1, &b, &B, &b1, &c, &C, &c1); //here it read the line start with f and store the corresponding values in the variables
f[m][0] = a - 1;
f[m][1] = b - 1;
f[m][2] = c - 1;
m++;
}
}
printf("Use 'w'/'s' keys to move forward/downward\n");
printf("Use 'a'/'d' keys to move to the left/right\n");
printf("Use 'o'/'l' keys to move up/down\n");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyPressed);
glutMotionFunc(mouse);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}