/**************************
* Includes
*
**************************/
#include <windows.h>
#include <gl/gl.h>
/**************************
* Function Declarations
*
**************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow) {
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);
/* create main window */
hWnd = CreateWindow (
"GLSample", "MY openGL",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 256, 256,
NULL, NULL, hInstance, NULL);
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);
/* program main loop */
while (!bQuit) {
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
/* handle or dispatch messages */
if (msg.message == WM_QUIT) {
bQuit = TRUE;
} else {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
} else {
/* OpenGL animation code goes here */
for(float i=1.0; i<=1; i+=0.01) {
glPushMatrix ();
glBegin (GL_TRIANGLES);
glColor3f (i,i, i);
glVertex2f ((0-i), i);
glColor3f (i, i, i);
glVertex2f (i, i);
glColor3f (i, i, i);
glVertex2f ( i,(0-i));
glEnd ();
glPopMatrix ();
glPushMatrix ();
glBegin (GL_TRIANGLES);
glColor3f (i, i, i);
glVertex2f ((0-i), i);
glColor3f (i, i, i);
glVertex2f ( (0-i),(0-i));
glColor3f (i, i, i);
glVertex2f ( i,(0-i));
glEnd ();
glPopMatrix ();
SwapBuffers (hDC);
Sleep (1);
}
for(int i=1; i<=360; i++) {
if(i<=90){
float x1=1.0/90*i*0.5;
float y1=1.0/90*(90-i)*0.5;
float x2=1.0/90*(i+1)*0.5;
float y2=1.0/90*(90-(i+1))*0.5;
glPushMatrix ();
glBegin (GL_TRIANGLES);
glColor3f (1.0f,0.0f, 0.0f);
glVertex2f (x1,y1);
glColor3f (1.0f,0.0f, 0.0f);
glVertex2f (x2,y2);
glColor3f (1.0f,0.0f, 0.0f);
glVertex2f (0,0);
glEnd ();
glPopMatrix ();
SwapBuffers (hDC);
}
}
}
}
/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow (hWnd);
return msg.wParam;
}
/********************
* Window Procedure
*
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch (wParam) {
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/*******************
* Enable OpenGL
*
*******************/
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC) {
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
/******************
* Disable OpenGL
*
******************/
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC) {
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
c++****
OPenGL
2024-07-05 20:47:41 By syxx_zhangyifan
星云生成代码
2024-03-30 12:50:34 By syxx_zhangyifan
at http://www.yuyaoi.cn:8000/blog/yyhs-shihaotian/post/734
#include<bits/stdc++.h>
#define int long long
using namespace std;
const double eps=5e-4;
const int INF=1e4,mod=1e9+7;
int cur;
static int rdm=1e6;
int real_rand(){
rdm=rdm*rdm+rand();
rdm%=mod;
return rdm;
}//大范围随机整数
bool fail(double prob){
return (real_rand()%INF)>=prob*INF;
}//单次概率事件成功与否
int limited_rand(int minx,int maxn){
return real_rand()%(maxn-minx+1)+minx;
}//特定范围内随机整数
double double_rand(double minx,double maxn){
return (double)limited_rand(minx*INF,maxn*INF)/(double)INF;
}//特定范围内随机浮点数
void adjust(double &gen,double noise_val){
if (noise_val>gen) gen=(0.25*gen+0.75*noise_val);
}
const double nbl_r_min=10,nbl_r_max=30;//星云半径范围
const double nbl_den_min=0.4,nbl_den_max=0.6;//星云密度范围
struct nbl_dot{
double x,y;//坐标
double r;//广义半径
double den;//密度
nbl_dot(){
r=double_rand(nbl_r_min,nbl_r_max);//初始化半径
x=-2.0*r-10.0;y=cur;//初始化坐标
den=double_rand(nbl_den_min,nbl_den_max);//初始化密度
}
double dis(){
return sqrt(pow(x,2)+pow(y-cur,2));
}//与当前光标的距离
double den_val(){
return (1.0-dis()/r)*den;
}//调整灰度
};
bool nbl_touched(nbl_dot x1,nbl_dot y1){
return sqrt(pow(x1.x-y1.x,2)+pow(x1.y-y1.y,2))<=0.8*(x1.r+y1.r);
}
vector<nbl_dot> nbl;
void nbl_add(){
struct nbl_dot tmp=nbl_dot();
if (!nbl.empty()&&abs(tmp.x-nbl[nbl.size()-1].x)<=10.0){
if (fail(4e-3)) return;
}//横向偏向集群生成
else if (fail(5e-4)) return;//未尝试生成
nbl.push_back(tmp);
int tmp_cnt1=0,tmp_cnt2=0;
double tmp_sum1=0.0,tmp_sum2=0.0;
for(int i=0;i<nbl.size()-1;i++){
if (nbl_touched(nbl[i],nbl[nbl.size()-1])){
tmp_cnt1++;
tmp_sum1+=nbl[i].y;
}
else{
tmp_cnt2++;
tmp_sum2+=2*nbl[nbl.size()-1].y-nbl[i].y;
}
}
if (tmp_cnt1){
nbl[nbl.size()-1].y+=0.5*tmp_sum1;
nbl[nbl.size()-1].y/=0.5*(double)tmp_cnt1+1.0;//纵向偏向集群生成
}
else{
nbl[nbl.size()-1].y+=tmp_sum2;
nbl[nbl.size()-1].y/=(double)tmp_cnt2+1.0; //偏向单体生成
}
}//新增星云点
void nbl_adj_den(double &den){
for(int i=0;i<nbl.size();i++) adjust(den,nbl[i].den_val());
}//调整灰度
void nbl_release(){
while(!nbl.empty()&&nbl[0].x>nbl[0].r) nbl.erase(nbl.begin());
}//释放
const double pln_r_min=10.0,pln_r_max=70.0;//行星半径范围
const double pln_den=0.8;
struct pln_dot{
double x,y;//坐标
double r;//半径
pln_dot(){
r=double_rand(pln_r_min,pln_r_max);//初始化半径
x=-2.0*r-10.0;y=cur;//初始化坐标
}
int id(){
if (r<=20) return 1;
else if (r<=40) return 2;
else return 3;
}//体量标记
double dis(){
return sqrt(pow(x,2)+pow(y-cur,2));
}//与光标位置的距离
double den_val(){
if (dis()<=0.8*r) return 0.9;//行星主体
if (id()==1) return 0.0;//小型行星
else if (id()==2){
if (dis()>=1.1*r&&dis()<=1.6*r) return 0.6;
return 0.0;
}//中型行星
else{
if (dis()>=1.2*r&&dis()<=1.35*r||dis()>=1.75*r&&dis()<=1.8*r) return 0.66;
else if (dis()>=1.35*r&&dis()<=1.75*r) return 0.3;
else return 0.0;
}//大型行星
//行星环
}//灰度更新
double sz(){
if (id()==1) return r;
else if (id()==2) return 1.6*r;
else return 1.8*r;
}//实际大小
};//行星
bool pln_touched(pln_dot x1,pln_dot y1){
return sqrt(pow(x1.x-y1.x,2)+pow(x1.y-y1.y,2))<=x1.sz()+y1.sz();
}
vector<pln_dot> pln;
void pln_check();
void pln_add(){
if (fail(1e-3)||fail(5e-2)) return;//未尝试生成
if (fail(pow(0.5,pln.size()))) return;//防止行星密度过大
pln.push_back(pln_dot());
pln_check();
}//新增行星
void pln_adj_den(double &den){
for(int i=0;i<pln.size();i++) adjust(den,pln[i].den_val());
}//调整灰度
void pln_check(){
if (pln[pln.size()-1].id()==3){
if (fail(0.25)) pln[pln.size()-1].r=double_rand(20.0,40.0);
}
if (pln[pln.size()-1].id()==2){
if (fail(0.5)) pln[pln.size()-1].r=double_rand(10.0,20.0);
}//适当缩小行星
for(int i=0;i<pln.size()-1;i++){
if (pln_touched(pln[i],pln[pln.size()-1])){
pln.erase(pln.begin()+pln.size()-1);return;
}
}//删除重影
}
void pln_release(){
while(!pln.empty()&&pln[0].x>pln[0].sz()) pln.erase(pln.begin());
}//释放
void up(){
for(int i=0;i<nbl.size();i++) nbl[i].x+=2.0;
for(int i=0;i<pln.size();i++) pln[i].x+=2.0;
}//天体上行
void print(double &den){
putchar(fail(den)?' ':'.');
if(cur==235){
putchar('\n');
cur=0;
up();
}
else ++cur;
den=0.01;
}//输出特定灰度值像素
void add(){
nbl_add();pln_add();
}
void adj_den(double &den){
nbl_adj_den(den);pln_adj_den(den);
}
void release(){
nbl_release();pln_release();
}
signed main(){
srand(time(0));
for(;;release()){
double den=0.01;
add();
adj_den(den);
print(den);
}
return 0;
}
AC机源代码
2024-03-19 20:46:27 By syxx_zhangyifan