Top Tweets for #FloatLight
GLのコード出てきたから貼るよ~!C++だけど、多分、今どきのWebGL系でも参考になると思う。最後の方の座標計算とか。AIに渡せば翻訳してくれると思います。
// HRGSView.cpp : CHRGSView クラスの実装
//
#include "stdafx.h"
#include "HRGS.h"
#include "HRGSDoc.h"
#include "HRGSView.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CHRGSView
const char* const CHRGSView::_ErrorStrings[]={
{"No Error"},
{"Unable to get a DC"},
{"ChoosePixelFormat failed"},
{"SelectPixelFormat failed"},
{"wglCreateContext failed"},
{"wglMakeCurrent failed"},
{"wglDeleteContext failed"},
{"SwapBuffers failed"},
};
const double CHRGSView::WHEEL_ZDELTA = 120.0;
const double CHRGSView::PER_WHEEL = 0.25;
const double CHRGSView::MAX_ZOOM = 10.0;
const double CHRGSView::MIN_ZOOM = 0.25;
IMPLEMENT_DYNCREATE(CHRGSView, CView)
BEGIN_MESSAGE_MAP(CHRGSView, CView)
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_ERASEBKGND()
ON_WM_DESTROY()
ON_WM_MOUSEWHEEL()
ON_WM_SIZE()
ON_WM_RBUTTONDBLCLK()
END_MESSAGE_MAP()
// CHRGSView コンストラクション/デストラクション
CHRGSView::CHRGSView()
{
m_pDC = NULL;
m_bInit = FALSE;
m_bMouseMove = FALSE;
m_bMouseRotate = FALSE;
m_bMoveEyePt = TRUE;
m_bMoveLight = TRUE;
m_dScale = 2.0;// デフォルトの拡大率
}
CHRGSView::~CHRGSView()
{
}
// ウィンドウ生成時
BOOL CHRGSView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: この位置で CREATESTRUCT cs を修正して Window クラスまたはスタイルを
// 修正してください。
//cs.style |= WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
// インスタンス消滅時
void CHRGSView::OnDestroy()
{
CView::OnDestroy();
if (::wglGetCurrentContext() == m_hRC)
{
if (!::wglMakeCurrent(0,0))
{
SetError(2);
}
}
if (!::wglDeleteContext(m_hRC))
{
SetError(6);
}
if (m_pDC)
{
delete m_pDC;
}
}
// CHRGSView 描画
// OnDrawが呼び出される直前
BOOL CHRGSView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CHRGSView::OnDraw(CDC* /*pDC*/)
{
CHRGSDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CheckHRC(0);
// レンダリング
Display(pDoc);
//if (m_bLight)
//{// 光線の表示
//::glBegin(GL_LINES);
//::glVertex3d(m_dCenterPt[0],m_dCenterPt[1],m_dCenterPt[2]);
//::glVertex3d(m_dLightPos[0],m_dLightPos[1],m_dLightPos[2]);
//::glEnd();
//}
::glFinish();
if (!::SwapBuffers(m_pDC->GetSafeHdc()))
{
SetError(7);
}
}
void CHRGSView::Display(CHRGSDoc* pDoc)
{
::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT/* | GL_STENCIL_BUFFER_BIT*/);
if (!m_bInit)
return;
::glMatrixMode(GL_PROJECTION);
::glLoadIdentity();
::glOrtho(WindowLeft,
WindowRight,
WindowBottom,
WindowTop,
-WindowLen*3.0,
WindowLen*3.0*3.0);
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity();
::gluLookAt(m_dEyePt[0],
m_dEyePt[1],
m_dEyePt[2],
m_dCenterPt[0],
m_dCenterPt[1],
m_dCenterPt[2],
m_dUpVector[0],
m_dUpVector[1],
m_dUpVector[2]);
// 光源の設定
LightPosition(m_dLightPos[0],m_dLightPos[1],m_dLightPos[2]);
pDoc->Draw();
}
// 光源の設定
void CHRGSView::LightPosition(double lx,double ly,double lz)
{
floatlight_loc[4];
light_loc[0] = (float)lx;
light_loc[1] = (float)ly;
light_loc[2] = (float)lz;
light_loc[3] = 0.0;// 0.0 : 平行光源、1.0 : 点光源
::glLightfv(GL_LIGHT0,GL_POSITION,light_loc);
}
// OpenGL初期化・設定関連処理
// インスタンス生成時
int CHRGSView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
InitializeOpenGL();
return 0;
}
// ドキュメントと結びついた時点
void CHRGSView::OnInitialUpdate()
{
intw,h;
CRectrect;
CHRGSDoc* pDoc = GetDocument();
// ドキュメントが初期化された。
pDoc->GetBoundBox(&left,&right,&top,&bottom,&clip_near,&clip_far);
InitLookAtData();
m_bInit = TRUE;
GetClientRect(&rect);
w = rect.right;
h = rect.bottom;
InitView(w,h);
}
// レンダリングコンテキストのチェック
void CHRGSView::CheckHRC(int proc)
{
if (::wglGetCurrentContext() != m_hRC)
{
if (!::wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC))
{
SetError(5);
return;
}
}
}
// OpenGLの初期化
BOOL CHRGSView::InitializeOpenGL()
{
m_pDC = new CClientDC(this);
if (NULL == m_pDC)
{
SetError(1);
return FALSE;
}
if (!SetupPixelFormat())
{
return FALSE;
}
if ((m_hRC= ::wglCreateContext(m_pDC->GetSafeHdc())) == NULL)
{
SetError(4);
return FALSE;
}
if (!::wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC))
{
SetError(5);
return FALSE;
}
::glClearColor(0.0f,0.0f,0.0f,0.0f);
::glClearDepth(1.0f);
::glEnable(GL_DEPTH_TEST);
return TRUE;
}
// OpenGLのピクセルフォーマット
BOOL CHRGSView::SetupPixelFormat()
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,// depth buffer
0,// stencil buffer
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
int pixelformat;
if (0 == (pixelformat=::ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd)))
{
SetError(2);
return FALSE;
}
if (!::SetPixelFormat(m_pDC->GetSafeHdc(),pixelformat,&pfd))
{
SetError(3);
return FALSE;
}
return TRUE;
}
void CHRGSView::SetError(int e)
{
m_ErrorString = _ErrorStrings[e];
TRACE("*** error : %s\n",m_ErrorString);
}
// 視点・描画範囲の決定
void CHRGSView::InitLookAtData()
{
inti;
doublelength;
doublevect[3] = {0.0,0.0,1.0};
m_dUpVector[0] = m_dUpVector[2] = 0.0;
m_dUpVector[1] = 1.0;
m_dCenterPt[0] = (left+right)/2.0;
m_dCenterPt[1] = (top+bottom)/2.0;
m_dCenterPt[2] = (clip_far+clip_near)/2.0;
length = right-left;
if (length < (top-bottom)) length = top-bottom;
Window2Len = length;
length = clip_near-clip_far;
if (Window2Len > length) length = Window2Len;
for (i = 0; i < 3; i++)
{
vect[i] *= length*1.5;
m_dEyePt[i] = vect[i];
}
::brVectAdd(m_dCenterPt,m_dEyePt,m_dEyePt);
WindowLeft = left;
WindowRight = right;
WindowTop = top;
WindowBottom = bottom;
WindowLen = length;
// 光源位置は視点に初期化する。
::brVectMove(m_dEyePt,m_dLightPos);
::glClearColor(0.0,0.0,0.0,0.0);
::glDisable(GL_LIGHTING);
::glEnable(GL_DEPTH_TEST);
}
// 物体をウインドウに適合させて表示する。
void CHRGSView::InitView(int w,int h)
{
doublewidth,height;
WindowHeight = (double)h;
if (WindowHeight < 0.00001) WindowHeight = 0.00001;
WindowWidth = (double)w;
if (WindowWidth < 0.00001) WindowWidth = 0.00001;
width = (right-left)/2.0;
height= (top-bottom)/2.0;
if(width/height < WindowWidth/WindowHeight)
{
WindowLeft = -height*WindowWidth/WindowHeight*m_dScale;
WindowRight = height*WindowWidth/WindowHeight*m_dScale;
WindowBottom = -height*m_dScale;
WindowTop = height*m_dScale;
}
else
{
WindowLeft = -width*m_dScale;
WindowRight = width*m_dScale;
WindowBottom = -width*WindowHeight/WindowWidth*m_dScale;
WindowTop = width*WindowHeight/WindowWidth*m_dScale;
}
}
// 物体をウインドウに適合させて表示する。
// ウィンドウサイズの変更時に呼び出し
void CHRGSView::SetWinRect(int w,int h)
{
doubledw, dh,
pw, ph,
x, y,
width,height;
// 投影幅・高さ(ワールド座標)
pw = WindowRight - WindowLeft;
ph = WindowTop - WindowBottom;
// 増分(スクリーン座標)
dw = w - WindowWidth;
dh = h - WindowHeight;
// 新投影幅・高さ(ワールド座標)
x = pw + dw/WindowWidth * pw;
y = ph + dh/WindowHeight * ph;
// 新画面サイズ(スクリーン座標)
WindowHeight = (double)h;
if (WindowHeight < 0.00001) WindowHeight = 0.00001;
WindowWidth = (double)w;
if (WindowWidth < 0.00001) WindowWidth = 0.00001;
// 物体範囲(ワールド座標)
width = right-left;
height= top-bottom;
// 物体をウインドウに適合させて表示する。
if(width/height < WindowWidth/WindowHeight)
{
WindowRight = WindowLeft + y*WindowWidth/WindowHeight;
WindowBottom = WindowTop - y;
m_dScale = (WindowTop-WindowBottom)/height;
}
else
{
WindowRight = WindowLeft + x;
WindowBottom = WindowTop - x*WindowHeight/WindowWidth;
m_dScale = (WindowRight-WindowLeft)/width;
}
//TRACE("m_dScale=%f\n", m_dScale);
}
// CHRGSView 診断
#ifdef _DEBUG
void CHRGSView::AssertValid() const
{
CView::AssertValid();
}
void CHRGSView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CHRGSDoc* CHRGSView::GetDocument() const // デバッグ以外のバージョンはインラインです。
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHRGSDoc)));
return (CHRGSDoc*)m_pDocument;
}
#endif //_DEBUG
// 座標等計算処理
// 投影座標系と物体座標系の間の座標変換
// bFlag = FALSE : 物体座標系 ---> 投影座標系
// TRUE : 投影座標系 ---> 物体座標系
void CHRGSView::TransCoord(double s[3], double d[3],BOOL bFlag)
{
doublemodelMat[16],projMat[16],work[3];
intviewport[4];
::glMatrixMode(GL_PROJECTION);
::glPushMatrix();
::glLoadIdentity();
::glMatrixMode(GL_MODELVIEW);
::glPushMatrix();
::glLoadIdentity();
::gluLookAt(m_dEyePt[0],m_dEyePt[1],m_dEyePt[2],
m_dCenterPt[0],m_dCenterPt[1],m_dCenterPt[2],
m_dUpVector[0],m_dUpVector[1],m_dUpVector[2]);
::glGetDoublev(GL_MODELVIEW_MATRIX,modelMat);
::glGetDoublev(GL_PROJECTION_MATRIX,projMat);
::glGetIntegerv(GL_VIEWPORT,viewport);
if (bFlag)
{
::gluUnProject(s[0],s[1],s[2],
modelMat,projMat,viewport,&work[0],&work[1],&work[2]);
}
else
{
::gluProject(s[0],s[1],s[2],
modelMat,projMat,viewport,&work[0],&work[1],&work[2]);
}
d[0] = work[0];
d[1] = work[1];
d[2] = work[2];
::glPopMatrix();
::glMatrixMode(GL_PROJECTION);
::glPopMatrix();
}
// 画面座標からワールド座標へ変換する
void CHRGSView::TransCoordW(double p[3], double w[3])
{
doublemodel[16], proj[16];
intview[4];
::glGetDoublev(GL_MODELVIEW_MATRIX, model);
::glGetDoublev(GL_PROJECTION_MATRIX, proj);
::glGetIntegerv(GL_VIEWPORT, view);
::glReadPixels((GLint)p[0], (GLint)p[1], 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &p[2]);
w[2] = (double)p[2];
::gluUnProject(p[0], p[1], p[2], model, proj, view, &w[0], &w[1], &w[2]);
}
// ベクトルの指定軸回りの指定角度の回転
void CHRGSView::RotateVect(double angle,double rotvect[3],double vect[3])
{
doublemodelMat[16],pt[3];
intk;
inti = 0;
::glMatrixMode(GL_MODELVIEW);
::glPushMatrix();
::glLoadIdentity();
::glRotated(angle,rotvect[0],rotvect[1],rotvect[2]);
::glGetDoublev(GL_MODELVIEW_MATRIX,modelMat);
for (k = 0; k < 3; k++)
{
pt[k] =
vect[0]*modelMat[i]+vect[1]*modelMat[i+1]+vect[2]*modelMat[i+2]+modelMat[i+3];
i += 4;
}
vect[0] = pt[0];
vect[1] = pt[1];
vect[2] = pt[2];
::brVectUnit(vect);
::glPopMatrix();
}
// 物体座標系における回転軸ベクトルを計算
void CHRGSView::CalcRotAxis(double dirVect[3],double rotAxis[3])
{
doubledummy[3] ={0.0,0.0,0.0};
doublework[3];
::brVectProd(dirVect,CHRGSDoc::zAxis,rotAxis);
::brVectUnit(rotAxis);
TransCoord(rotAxis,rotAxis,TRUE);
TransCoord(dummy,work,TRUE);
::brVectSub(rotAxis, work, rotAxis);
::brVectUnit(rotAxis);
}
// 光源の回転
void CHRGSView::CalcLightLoc(double angle,double rotAxis[3])
{
doublework[3];
inti;
::brVectSub(m_dLightPos,m_dCenterPt,work);
doublelen = ::brVectlength(work);
RotateVect(angle,rotAxis,work);
for (i = 0;i <3; i++)
work[i] *= len;
::brVectAdd(m_dCenterPt,work,m_dLightPos);
LightPosition(m_dLightPos[0],m_dLightPos[1],m_dLightPos[2]);
}
// 視点の回転
void CHRGSView::CalcEyePt(double angle,double rotAxis[3])
{
doublework[3];
inti;
::brVectSub(m_dEyePt,m_dCenterPt,work);
doublelen = ::brVectlength(work);
RotateVect(angle,rotAxis,work);
for (i = 0;i <3; i++)
work[i] *= len;
::brVectAdd(m_dCenterPt,work,m_dEyePt);
RotateVect(angle,rotAxis,m_dUpVector);
}
// マウス操作関連
// 左ボタン押下
void CHRGSView::OnLButtonDown(UINT nFlags, CPoint point)
{
if(m_bMouseMove)
return;
m_px = point.x;
m_py = point.y;
m_bMouseRotate = TRUE;
}
// 左ボタン解放
void CHRGSView::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bMouseRotate = FALSE;
}
// マウス移動
void CHRGSView::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bMouseRotate && !m_bMouseMove)
return;
intcur_x,cur_y;
doubledirVect[3],rotAxis[3];
doublelen,theta,angle,rotscale;
intx,y;
cur_x = point.x;
cur_y = point.y;
// マウスの移動量
x = cur_x-m_px;
y = m_py-cur_y;
m_px = cur_x;
m_py = cur_y;
if(m_bMouseRotate){// 回転時
dirVect[0] = x;
dirVect[1] = y;
dirVect[2] = 0.0;
CalcRotAxis(dirVect,rotAxis);
len = ::brVectlength(dirVect);
rotscale = 280.0;
theta = BRMODEL_PI/rotscale*len;
angle = -180.0*theta/BRMODEL_PI;
if (m_bMoveEyePt)
{
CalcEyePt(angle,rotAxis);
}
if (m_bMoveLight)
{
CalcLightLoc(angle,rotAxis);
}
}
Invalidate();
}
// マウスホイール
BOOL CHRGSView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// クライアント領域外でのホイール回転はNG
double cx, cy;
ScreenToClient(&pt);
cx = pt.x;
cy = pt.y;
if(cx < 0 || cx > WindowWidth ||
cy < 0 || cy > WindowHeight)
return FALSE;
int whl = -1;// 仮設定:ホイール↑で拡大
double tmp,
pw, ph,
width, height,
x, y,
l, r, t, b;
// 増分
tmp = whl * zDelta/WHEEL_ZDELTA;
m_dScale += tmp * PER_WHEEL;
if(m_dScale < MIN_ZOOM){
m_dScale = MIN_ZOOM;
return FALSE;
} else if(m_dScale > MAX_ZOOM) {
m_dScale = MAX_ZOOM;
return FALSE;
}
// 投影範囲(ワールド座標)
pw = WindowRight - WindowLeft;
ph = WindowTop - WindowBottom;
// 図形範囲(ワールド座標)
width = right-left;
height = top-bottom;
// 伸縮中心(マウス位置:ワールド座標)
x = WindowLeft + pw * cx/WindowWidth;
y = WindowTop - ph * cy/WindowHeight;
l = m_dScale * cx/WindowWidth;
r = m_dScale * (WindowWidth-cx)/WindowWidth;
t = m_dScale * cy/WindowHeight;
b = m_dScale * (WindowHeight-cy)/WindowHeight;
if(width/height < WindowWidth/WindowHeight)
{
WindowLeft = x - height * l * WindowWidth/WindowHeight;
WindowRight = x + height * r * WindowWidth/WindowHeight;
WindowTop = y + height * t;
WindowBottom = y - height * b;
} else {
WindowLeft = x - width * l;
WindowRight = x + width * r;
WindowTop = y + width * t * WindowHeight/WindowWidth;
WindowBottom = y - width * b * WindowHeight/WindowWidth;
}
Invalidate();
return CView::OnMouseWheel(nFlags, zDelta, pt);
}
// ウィンドウサイズ変更
void CHRGSView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
::glViewport(0,0,cx,cy);
if (!m_bInit)
return;
SetWinRect(cx,cy);
if (::wglGetCurrentContext() != m_hRC)
{
OnDraw(GetDC());
}
}
// 表示初期化
void CHRGSView::OnRButtonDblClk(UINT nFlags, CPoint point)
{
InitLookAtData();
InitView((int)WindowWidth, (int)WindowHeight);
Invalidate();
CView::OnRButtonDblClk(nFlags, point);
}
Trends for you
Most Popular Users

Elon Musk 
@elonmusk
240.1M followers

Barack Obama 
@barackobama
118.6M followers

Cristiano Ronaldo 
@cristiano
114.4M followers

Donald J. Trump 
@realdonaldtrump
111.8M followers

Narendra Modi 
@narendramodi
106.9M followers

Rihanna 
@rihanna
98.6M followers

NASA 
@nasa
92.1M followers

Justin Bieber 
@justinbieber
91.7M followers

KATY PERRY 
@katyperry
89.9M followers

Taylor Swift 
@taylorswift13
83.9M followers

Lady Gaga 
@ladygaga
75.4M followers

Virat Kohli 
@imvkohli
73.3M followers

Kim Kardashian 
@kimkardashian
70.8M followers

YouTube 
@youtube
68.6M followers

Neymar Jr 
@neymarjr
66.3M followers

Bill Gates 
@billgates
65.2M followers

Selena Gomez 
@selenagomez
63M followers

The Ellen Show
@theellenshow
62.1M followers

CNN 
@cnn
61.7M followers

X 
@x
60.6M followers

