OpenGLを学びたいので、非常に簡単な例から始めることにしました。スターダスト宇宙船からの測定値から推測されるように、ワイルド2彗星の形状をレンダリングします(データの詳細:http://nssdc.gsfc.nasa.gov/ nmc / masterCatalog.do?ds = PSSB-00133)。私はOpenGLについてまったく何も知らないことを覚えておいてください。いくつかのGoogle-fuは、以下に示すコードまで到達するのに役立ちました。私の最善の努力にもかかわらず、私の彗星は最悪です:
見栄えを良くしたいのですが、どうすればいいのかわかりません(レッドブックなどを読む以外に)。例えば:
- 形状の非常に基本的な「ワイヤーフレーム」レンダリングを作成するにはどうすればよいですか?
- 太陽が「下」方向に沿っている(つまり、-Yに沿っている)とすると、光を追加して反対側の影を見るにはどうすればよいですか?
- 「マウスイベント」を追加して、ビューを回転させたり、ズームイン/ズームアウトしたりするにはどうすればよいですか?
どうすればこのモンスターをより美しく見せることができますか?オンラインチュートリアルやコード例への参照はありますか?
ソースコード、データ、およびmakefile(OS Xの場合)をbitbucketに配置しました。
hg clone https://arrieta@bitbucket.org/arrieta/learning-opengl
データは、8,761個のトリプレット(ボディ固定フレーム内の頂点)と17,518個の三角形(各三角形は、8,761個の頂点トリプレットの1つを参照する整数のトリプレット)で構成されています。
#include<stdio.h>
#include<stdlib.h>
#include<OpenGL/gl.h>
#include<OpenGL/glu.h>
// I added this in case you want to "copy/paste" the program into a
// non-Mac computer
#ifdef __APPLE__
# include <GLUT/glut.h>
#else
# include <GL/glut.h>
#endif
/* I hardcoded the data and use globals. I know it sucks, but I was in
a hurry. */
#define NF 17518
#define NV 8761
unsigned int fs[3 * NF];
float vs[3 * NV];
float angle = 0.0f;
/* callback when the window changes size (copied from Internet example) */
void changeSize(int w, int h) {
if (h == 0) h = 1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45.0f, ratio, 0.2f, 50000.0f); /* 45 degrees fov in Y direction; 50km z-clipping*/
glMatrixMode(GL_MODELVIEW);
}
/* this renders and updates the scene (mostly copied from Internet examples) */
void renderScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 10000.0f, /* eye is looking down along the Z-direction at 10km */
0.0f, 0.0f, 0.0f, /* center at (0, 0, 0) */
0.0f, 1.0f, 0.0f); /* y direction along natural y-axis */
/* just add a simple rotation */
glRotatef(angle, 0.0f, 0.0f, 1.0f);
/* use the facets and vertices to insert triangles in the buffer */
glBegin(GL_TRIANGLES);
unsigned int counter;
for(counter=0; counter<3 * NF; ++counter) {
glVertex3fv(vs + 3 * fs[counter]); /* here is where I'm loading
the data - why do I need to
load it every time? */
}
glEnd();
angle += 0.1f; /* update the rotation angle */
glutSwapBuffers();
}
int main(int argc, char* argv[]) {
FILE *fp;
unsigned int counter;
/* load vertices */
fp = fopen("wild2.vs", "r");
counter = 0;
while(fscanf(fp, "%f", &vs[counter++]) > 0);
fclose(fp);
/* load facets */
fp = fopen("wild2.fs", "r");
counter = 0;
while(fscanf(fp, "%d", &fs[counter++]) > 0);
fclose(fp);
/* this initialization and "configuration" is mostly copied from Internet */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0, 0);
glutInitWindowSize(1024, 1024);
glutCreateWindow("Wild-2 Shape");
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 30.0 };
GLfloat light_position[] = {3000.0, 3000.0, 3000.0, 0.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);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
glutMainLoop();
return 0;
}
編集
見た目は良くなり始めており、当面は調査するリソースがたくさんあります。それはまだひどいですが、私の質問は答えられました!
法線を追加し、「テクスチャ」とワイヤフレームを切り替えることができます。
PS。リポジトリには、SeedmanJの提案に従って行われた変更が表示されます。