Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////
- void InitMyOpenGL() // Создадим OpenGL-контекст
- {
- Windows = 1;
- HGLRC hglrc;
- PIXELFORMATDESCRIPTOR pfd;
- int iPixelFormat;
- memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
- pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 24;
- pfd.cDepthBits = 32;
- pfd.iLayerType = PFD_MAIN_PLANE;
- hdc = GetDC(hWnd);
- iPixelFormat = ChoosePixelFormat(hdc, &pfd); // int ChoosePixelFormat(HDC, const PIXELFORMATDESCRIPTOR)
- SetPixelFormat(hdc, iPixelFormat, &pfd); // BOOL SetPixelFormat(HDC hDC, inl pixelFormat, const PIXELFORMATDESCRIPTOR)
- hglrc = wglCreateContext(hdc); // HGLRC wglCreateContext(HDC hDC)
- if(hglrc) wglMakeCurrent(hdc, hglrc);
- }
- ///////////////////////////////////////////////////////
- void ResizeMyOpenGL() // Подстраиваемся, при изменении размеров окна пользователем
- {
- RECT rc;
- GetClientRect(hWnd, &rc);
- glViewport(0, 0, rc.right, rc.bottom);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- }
- ///////////////////////////////////////////////////////
- void CloseMyOpenGL() // Закроем OpenGL-контекст
- {
- HGLRC hglrc;
- hglrc = wglGetCurrentContext();
- if(hglrc)
- {
- hdc = wglGetCurrentDC();
- wglMakeCurrent(0, 0);
- ReleaseDC(hWnd, hdc);
- wglDeleteContext(hglrc); // BOOL wglDeleteContext(HGLRC hGLRC)
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////// Examples ///////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////
- void DrawMyExampleOpenGL_1() //
- {
- GLubyte fillpattern[128];
- glOrtho(-400, 400, -300, 300, -1, 1);
- // gluOrtho2D(-400, 400, -300, 300); // -lglu32
- glMatrixMode(GL_MODELVIEW);
- glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.4, 0.4, 0.4); // Устанавливаем цвет
- glLineWidth(1.0f) ; // Толщина линии
- glEnable(GL_LINE_STIPPLE); // Включает пунктирный режим
- glLineStipple(3 , 0x08ff); // Штрихпунктир ( задает вид пунктира )
- glBegin(GL_LINES);
- glVertex2f(-400, 0);
- glVertex2f( 400, 0);
- glEnd();
- glBegin(GL_LINES);
- glVertex2f(0, 300);
- glVertex2f(0,-300) ;
- glEnd();
- glDisable(GL_LINE_STIPPLE); // вЫключает пунктирный режим
- glColor3f(0.1, 0.1, 0.75);
- glPointSize(10) ;
- glBegin(GL_POINTS) ;
- glVertex2f(0, 0);
- glEnd();
- glLineWidth(2.0f) ;
- glBegin(GL_LINE_STRIP) ;
- for(int i=0; i<10; i++)
- {
- glVertex2f(-350+70*i, 60);
- glVertex2f (-315+70*i,-60);
- }
- glEnd();
- glColor3f(0.0, 0.3, 0.0);
- glLineWidth(1.0f);
- glBegin(GL_TRIANGLES);
- glVertex2f(-300, 100);
- glVertex2f(-200, 240);
- glVertex2f(-100, 100);
- glEnd();
- glColor3f(0.4, 0.1, 0.6);
- glBegin(GL_QUADS);
- glVertex2f(-50, 150);
- glVertex2f(200, 200);
- glVertex2f(250, 150);
- glVertex2f(200, 100);
- glEnd();
- memset(fillpattern,0,sizeof(fillpattern));
- for(int j=0; j <16; j++)
- for(int i=0; i < 2; i++)
- {
- fillpattern[j*4+i] = 0xff;
- fillpattern[64+j*4+i+2] = 0xff;
- }
- glColor3f(0.4, 0.0, 0.2);
- glPointSize(1);
- glLineWidth(1.0f);
- glPolygonStipple(fillpattern);
- glEnable(GL_POLYGON_STIPPLE);
- glBegin(GL_POLYGON);
- glVertex2f(-300, -150);
- glVertex2f(-200, - 80);
- glVertex2f( 0, -100);
- glVertex2f( 200, -200);
- glVertex2f(-100, -250);
- glEnd();
- glDisable(GL_POLYGON_STIPPLE);
- glFinish();
- }
- //////////////////////////////////////////////////////////////////////
- void NormalVector(float *x, float *y, float *z, //
- float x1, float y1, float z1,
- float x2, float y2, float z2,
- float x3, float y3, float z3)
- {
- *x = (y2-y1)*(z3-z1) - (y3-y1)*(z2-z1);
- *y = -(x2-x1)*(z3-z1) - (x3-x1)*(z2-z1);
- *z = (x2-x1)*(y3-y1) - (x3-x1)*(y2-y1);
- }
- //////////////////////////////////////////////////////////////////////
- void DrawMyExampleOpenGL_2() //
- {
- RECT rc;
- float nx,ny,nz;
- GLfloat lightpos[4] = {3,3,4,1};
- GLfloat lightdirection[3] = {-0.5, -0.6, -0.7};
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glClearDepth(1.0);
- glEnable(GL_DEPTH_TEST);
- GetClientRect(hWnd,&rc);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluPerspective(50, (double)rc.right/rc.bottom, 1, 40);
- glTranslatef(0, 0, -10);
- glRotatef( 27, 1, 0, 0);
- glRotatef(-19, 0, 1, 0);
- glLightfv(GL_LIGHT0,GL_POSITION,lightpos);
- glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,lightdirection);
- glLightf (GL_LIGHT0,GL_SPOT_EXPONENT,4);
- glLightf (GL_LIGHT0,GL_SPOT_CUTOFF,50);
- glEnable (GL_LIGHT0);
- glEnable (GL_LIGHTING);
- glEnable (GL_COLOR_MATERIAL);
- glEnable (GL_NORMALIZE);
- glColor3f(0.5, 1, 0);
- NormalVector(&nx, &ny, &nz, 0, 4, 0,
- 2, 0.7, 2,
- 2, 0.7, -2);
- glNormal3f(nx,ny,nz);
- glBegin(GL_TRIANGLES);
- glVertex3f(0, 4, 0);
- glVertex3f(2, 0.7, 2);
- glVertex3f(2, 0.7, -2);
- glEnd();
- NormalVector(&nx, &ny, &nz, 0, 4 , 0,
- -2, 0.7, 2,
- 2, 0.7, 2);
- glNormal3f(nx, ny, nz);
- glBegin (GL_TRIANGLES);
- glVertex3f( 0, 4, 0);
- glVertex3f(-2, 0.7, 2);
- glVertex3f( 2, 0.7, 2);
- glEnd();
- glNormal3f(0, 1, 0);
- for (int j=-5;j<5;j++)
- {
- for (int i=-5;i<5;i++)
- {
- if((abs(i+j) % 2) == 0) glColor3f(1,0,0);
- else glColor3f(1,1,1);
- glBegin(GL_QUADS);
- glVertex3f(i, 0, j);
- glVertex3f(i+1, 0, j);
- glVertex3f(i+1, 0, j+1);
- glVertex3f(i, 0, j+1);
- glEnd();
- }
- }
- glFinish();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement