OpenGL2(画图基本操作)

#include "stdafx.h"
#include <GL/glut.h> 
#include <iostream>


void myDisplay(void)  
{  
    glClear(GL_COLOR_BUFFER_BIT);  // 这种以gl开头的函数都是OpenGL的标准函数 // 清除。GL_COLOR_BUFFER_BIT表示清除颜色
    //glRectf(-0.5f, -0.5f, 0.5f, 0.5f);  // 画一个矩形。四个参数分别表示了位于对角线上的两个点的横、纵坐标。
	//glBegin(GL_POINTS);
	//glBegin(GL_LINES); // 线
	glBegin(GL_LINE_LOOP); // 封闭图形
     glVertex2f(0.0f, 0.0f); // 画点(0, 0)是中心点
     glVertex2f(0.1f, 0.0f); // 单位一的长度不是一个像素
	 glVertex2f(0.2f, 1.0f);
	glEnd();


    glFlush();  // 保证前面的OpenGL命令立即执行(而不是让它们在缓冲区中等待)。其作用跟fflush(stdout)类似
} 


      // 画圆
	//const int n = 100;
	//const GLfloat R = 0.5f;
	//const GLfloat Pi = 3.1415926536f;
	//void myDisplay(void)
	//{
	//	 int i;
	//	 glClear(GL_COLOR_BUFFER_BIT);
	//	 glBegin(GL_POLYGON);
	//	 for(i=0; i<n; ++i)
	//		 glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
	//	 glEnd();
	//	 glFlush();
	//}


///*
//设五角星的五个顶点分布位置关系如下:
//      A
//E			B
//
//    D    C
//首先,根据余弦定理列方程,计算五角星的中心到顶点的距离a
//(假设五角星对应正五边形的边长为.0)
//a = 1 / (2-2*cos(72*Pi/180));
//然后,根据正弦和余弦的定义,计算B的x坐标bx和y坐标by,以及C的y坐标
//(假设五角星的中心在坐标原点)
//bx = a * cos(18 * Pi/180);
//by = a * sin(18 * Pi/180);
//cy = -a * cos(18 * Pi/180);
//五个点的坐标就可以通过以上四个量和一些常数简单的表示出来
//*/
//#include <math.h>
//const GLfloat Pi = 3.1415926536f;
//void myDisplay(void)
//{
//     GLfloat a = 1 / (2-2*cos(72*Pi/180));
//     GLfloat bx = a * cos(18 * Pi/180);
//     GLfloat by = a * sin(18 * Pi/180);
//     GLfloat cy = -a * cos(18 * Pi/180);
//     GLfloat
//         PointA[2] = { 0, a },
//         PointB[2] = { bx, by },
//         PointC[2] = { 0.5, cy },
//         PointD[2] = { -0.5, cy },
//         PointE[2] = { -bx, by };
//
//     glClear(GL_COLOR_BUFFER_BIT);
//     // 按照A->C->E->B->D->A的顺序,可以一笔将五角星画出
//     glBegin(GL_LINE_LOOP);
//         glVertex2fv(PointA);
//         glVertex2fv(PointC);
//         glVertex2fv(PointE);
//         glVertex2fv(PointB);
//         glVertex2fv(PointD);
//     glEnd();
//     glFlush();
//}


/*
	画正弦函数
由于OpenGL默认坐标值只能从-1到1,(可以修改,但方法留到以后讲)
所以我们设置一个因子factor,把所有的坐标值等比例缩小,
这样就可以画出更多个正弦周期
试修改factor的值,观察变化情况
*/
#include <math.h>
const GLfloat factor = 0.1f;
void myDisplay(void)
{
     GLfloat x;
     glClear(GL_COLOR_BUFFER_BIT);
     glBegin(GL_LINES);
         glVertex2f(-1.0f, 0.0f);
         glVertex2f(1.0f, 0.0f);         // 以上两个点可以画x轴
         glVertex2f(0.0f, -1.0f);
         glVertex2f(0.0f, 1.0f);         // 以上两个点可以画y轴
     glEnd();
     glBegin(GL_LINE_STRIP);
     for(x=-1.0f/factor; x<1.0f/factor; x+=0.01f)
     {
         glVertex2f(x*factor, sin(x)*factor);
     }
     glEnd();
     glFlush();
}




int main(int argc, char* argv[])
{    
	glutInit(&argc, argv);  // glutInit,对GLUT进行初始化,这个函数必须在其它的GLUT使用之前调用一次。其格式比较死板,一般照抄这句glutInit(&argc, argv)就可以了。
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);  // glutInitDisplayMode,设置显示方式,其中GLUT_RGB表示使用RGB颜色,与之对应的还有GLUT_INDEX(表示使用索引颜色)。
									// GLUT_SINGLE表示使用单缓冲,与之对应的还有GLUT_DOUBLE(使用双缓冲)
    glutInitWindowPosition(100, 100);  // 设置窗口在屏幕中的位置。
    glutInitWindowSize(400, 400);  // 设置窗口的大小
    glutCreateWindow("第一个OpenGL程序");  
    glutDisplayFunc(&myDisplay);  // 
    glutMainLoop();  // 进行一个消息循环。(这个可能初学者也不太明白,现在只需要知道这个函数可以显示窗口,并且等待窗口关闭后才会返回,这就足够了。)

	std::cout<<"END!"<<std::endl;

	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
--------------------------- Qt Data Visualization 5.7.0 --------------------------- Qt Data Visualization module provides multiple graph types to visualize data in 3D space both with C++ and Qt Quick 2. System Requirements =================== - Qt 5.2.1 or newer - OpenGL 2.1 or newer (recommended) or OpenGL ES2 (reduced feature set) - Manipulating Qt Data Visualization graphs with QML Designer requires Qt Creator 3.3 or newer Building ======== Configure the project with qmake: qmake After running qmake, build the project with make: (Linux) make (Windows with MinGw) mingw32-make (Windows with Visual Studio) nmake (OS X) make The above generates the default makefiles for your configuration, which is typically the release build if you are using precompiled binary Qt distribution. To build both debug and release, or one specifically, use one of the following qmake lines instead. For debug builds: qmake CONFIG+=debug make or qmake CONFIG+=debug_and_release make debug For release builds: qmake CONFIG+=release make or qmake CONFIG+=debug_and_release make release For both builds (Windows/OS X only): qmake CONFIG+="debug_and_release build_all" make After building, install the module to your Qt directory: make install If you want to uninstall the module: make uninstall Building as a statically linked library ======================================= The same as above applies, you will just have to add static to the CONFIG: qmake CONFIG+=static Documentation ============= The documentation can be generated with: make docs The documentation is generated into the doc folder under the build folder. Both Qt Assistant (qtdatavisualization.qch) and in HTML format (qtdatavisualization subfolder) documentation is generated. Please refer to the generated documentation for more information: doc/qtdatavisualization/qtdatavisualization-index.html Known Issues ============ - Some platforms like Android and WinRT cannot handle multiple native windows properly, so only the Qt Quick 2 versions of graphs are available in practice for those platforms. - Shadows are not supported with OpenGL ES2 (including Angle builds in Windows). - Anti-aliasing doesn't work with OpenGL ES2 (including Angle builds in Windows). - QCustom3DVolume items are not supported with OpenGL ES2 (including Angle builds in Windows). - Surfaces with non-straight rows and columns do not always render properly. - Q3DLight class (and Light3D QML item) are currently not usable for anything. - Changing most of Q3DScene properties affecting subviewports currently has no effect. - Widget based examples layout incorrectly in iOS. - Reparenting a graph to an item in another QQuickWindow is not supported. - Android builds of QML applications importing QtDataVisualization also require "QT += datavisualization" in the pro file. This is because Qt Data Visualization QML plugin has a dependency to Qt Data Visualization C++ library, which Qt Creator doesn't automatically add to the deployment package. - Only OpenGL ES2 emulation is available for software renderer (that is, when using QCoreApplication::setAttribute(Qt::AA_UseSoftwareOpenGL))
#include // Header File For Windows #include // Header File For The OpenGL32 Library #include // Header File For The GLu32 Library #include // Header File For The Glaux Library #include HDC hDC=NULL; // Private GDI Device Context HGLRC hRC=NULL; // Permanent Rendering Context HWND hWnd=NULL; // Holds Our Window Handle HINSTANCE hInstance; // Holds The Instance Of The Application int x,y; int xx,yy; int chakela; int amd[200][200]; int rmd[200][200]; int gmd[200][200]; int bmd[200][200]; int yanser,yanseg,yanseb; FILE *zuojian; bool keys[256]; // Array Used For The Keyboard Routine bool active=TRUE; // Window Active Flag Set To TRUE By Default bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default void aa(); void bb(); void cc(); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window { if (height==0) // Prevent A Divide By Zero By { height=1; // Making Height Equal One } glViewport(0,0,width,height); // Reset The Current Viewport glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glLoadIdentity(); // Reset The Projection Matrix // Calculate The Aspect Ratio Of The Window gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,1000.0f); glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glLoadIdentity(); // Reset The Modelview Matrix } int InitGL(GLvoid) // All Setup For OpenGL Goes Here { glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations return TRUE; // Initialization Went OK } int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); glColor3f(0.5f,0.5f,1.0f); aa(); cc(); bb(); // Reset The Current Modelview Matrix return TRUE; // Keep Going } GLvoid KillGLWindow(GLvoid) // Properly Kill The Window { if (fullscreen) // Are We In Fullscreen Mode? { ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop ShowCursor(TRUE); // Show Mouse Pointer } if (hRC) // Do We Have A Rendering Context? { if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts? { MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); } if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC? { MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); } hRC=NULL; // Set RC To NULL } if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC { MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); hDC=NULL; // Set DC To NULL } if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window? { MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); hWnd=NULL; // Set hWnd To NULL } if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class { MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); hInstance=NULL; // Set hInstance To NULL } } /* This Code Creates Our OpenGL Window. Parameters Are: * * title - Title To Appear At The Top Of The Window * * width - Width Of The GL Window Or Fullscreen Mode * * height - Height Of The GL Window Or Fullscreen Mode * * bits - Number Of Bits To Use For Color (8/16/24/32) * * fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */ BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag) { GLuint PixelFormat; // Holds The Results After Searching For A Match WNDCLASS wc; // Windows Class Structure DWORD dwExStyle; // Window Extended Style DWORD dwStyle; // Window Style RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values WindowRect.left=(long)0; // Set Left Value To 0 WindowRect.right=(long)width; // Set Right Value To Requested Width WindowRect.top=(long)0; // Set Top Value To 0 WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height fullscreen=fullscreenflag; // Set The Global Fullscreen Flag hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window. wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages wc.cbClsExtra = 0; // No Extra Window Data wc.cbWndExtra = 0; // No Extra Window Data wc.hInstance = hInstance; // Set The Instance wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer wc.hbrBackground = NULL; // No Background Required For GL wc.lpszMenuName = NULL; // We Don't Want A Menu wc.lpszClassName = "OpenGL"; // Set The Class Name if (!RegisterClass(&wc)) // Attempt To Register The Window Class { MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if (fullscreen) // Attempt Fullscreen Mode? { DEVMODE dmScreenSettings; // Device Mode memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure dmScreenSettings.dmPelsWidth = width; // Selected Screen Width dmScreenSettings.dmPelsHeight = height; // Selected Screen Height dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar. if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) { // If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode. if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) { fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE } else { // Pop Up A Message Box Letting User Know The Program Is Closing. MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP); return FALSE; // Return FALSE } } } if (fullscreen) // Are We Still In Fullscreen Mode? { dwExStyle=WS_EX_APPWINDOW; // Window Extended Style dwStyle=WS_POPUP; // Windows Style ShowCursor(FALSE); // Hide Mouse Pointer } else { dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style } AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size // Create The Window if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window "OpenGL", // Class Name title, // Window Title dwStyle | // Defined Window Style WS_CLIPSIBLINGS | // Required Window Style WS_CLIPCHILDREN, // Required Window Style 0, 0, // Window Position WindowRect.right-WindowRect.left, // Calculate Window Width WindowRect.bottom-WindowRect.top, // Calculate Window Height NULL, // No Parent Window NULL, // No Menu hInstance, // Instance NULL))) // Dont Pass Anything To WM_CREATE { KillGLWindow(); // Reset The Display MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format bits, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 16, // 16Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context? { KillGLWindow(); // Reset The Display MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format? { KillGLWindow(); // Reset The Display MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format? { KillGLWindow(); // Reset The Display MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context? { KillGLWindow(); // Reset The Display MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context { KillGLWindow(); // Reset The Display MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } ShowWindow(hWnd,SW_SHOW); // Show The Window SetForegroundWindow(hWnd); // Slightly Higher Priority SetFocus(hWnd); // Sets Keyboard Focus To The Window ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen if (!InitGL()) // Initialize Our Newly Created GL Window { KillGLWindow(); // Reset The Display MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } return TRUE; // Success } LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window UINT uMsg, // Message For This Window WPARAM wParam, // Additional Message Information LPARAM lParam) // Additional Message Information { switch (uMsg) // Check For Windows Messages { case WM_CHAR: // Is A Key Being Held Down? { switch (wParam) { case 'a': { zuojian=fopen("c:/a.txt","wb+"); fwrite(amd,4,40000,zuojian); fwrite(rmd,4,40000,zuojian); fwrite(gmd,4,40000,zuojian); fwrite(bmd,4,40000,zuojian); fclose(zuojian); MessageBox(hWnd,"ok","ok",0); break; } case 'q': { zuojian=fopen("c:/a.txt","rb+"); fread(amd,4,40000,zuojian); fread(rmd,4,40000,zuojian); fread(gmd,4,40000,zuojian); fread(bmd,4,40000,zuojian); fclose(zuojian); MessageBox(hWnd,"ok","ok",0); break; } break; } return 0; } case WM_LBUTTONDOWN: { chakela=1; // amd[LOWORD(lParam)/10][HIWORD(lParam)/10]=1; //MessageBox(0,"cvxcv","fgfd",2); return 0; } case WM_MOUSEMOVE : { xx=LOWORD(lParam)/10; yy=HIWORD(lParam)/10; if(chakela==1) { amd[xx][yy]=1; rmd[xx][yy]=yanser; gmd[xx][yy]=yanseg; bmd[xx][yy]=yanseb; } return 0; } case WM_LBUTTONUP: { chakela=0; //MessageBox(0,"cvxcv","fgfd",2); return 0; } case WM_RBUTTONDOWN: { if(xx<=5) { yanser=yy; } if(xx5) yanseg=yy; } if(xx10) yanseb=yy; } return 0; } case WM_ACTIVATE: // Watch For Window Activate Message { if (!HIWORD(wParam)) // Check Minimization State { active=TRUE; // Program Is Active } else { active=FALSE; // Program Is No Longer Active } return 0; // Return To The Message Loop } case WM_SYSCOMMAND: // Intercept System Commands { switch (wParam) // Check System Calls { case SC_SCREENSAVE: // Screensaver Trying To Start? case SC_MONITORPOWER: // Monitor Trying To Enter Powersave? return 0; // Prevent From Happening } break; // Exit } case WM_CLOSE: // Did We Receive A Close Message? { PostQuitMessage(0); // Send A Quit Message return 0; // Jump Back } case WM_KEYDOWN: // Is A Key Being Held Down? { keys[wParam] = TRUE; // If So, Mark It As TRUE return 0; // Jump Back } case WM_KEYUP: // Has A Key Been Released? { keys[wParam] = FALSE; // If So, Mark It As FALSE return 0; // Jump Back } case WM_SIZE: // Resize The OpenGL Window { ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height return 0; // Jump Back } } // Pass All Unhandled Messages To DefWindowProc return DefWindowProc(hWnd,uMsg,wParam,lParam); } int WINAPI WinMain( HINSTANCE hInstance, // Instance HINSTANCE hPrevInstance, // Previous Instance LPSTR lpCmdLine, // Command Line Parameters int nCmdShow) // Window Show State { MSG msg; // Windows Message Structure BOOL done=FALSE; // Bool Variable To Exit Loop // Ask The User Which Screen Mode They Prefer if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO) { fullscreen=FALSE; // Windowed Mode } // Create Our OpenGL Window if (!CreateGLWindow("NeHe's Color Tutorial",640,480,16,fullscreen)) { return 0; // Quit If Window Was Not Created } while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=TRUE; // If So done=TRUE } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } else // If There Are No Messages { // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene() if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was There A Quit Received? { done=TRUE; // ESC or DrawGLScene Signalled A Quit } else // Not Time To Quit, Update Screen { SwapBuffers(hDC); // Swap Buffers (Double Buffering) } if (keys[VK_F1]) // Is F1 Being Pressed? { keys[VK_F1]=FALSE; // If So Make Key FALSE KillGLWindow(); // Kill Our Current Window fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode // Recreate Our OpenGL Window if (!CreateGLWindow("NeHe's Color Tutorial",640,480,16,fullscreen)) { return 0; // Quit If Window Was Not Created } } } } // Shutdown KillGLWindow(); // Kill The Window return (msg.wParam); // Exit The Program } void aa() { x=y=0; while(x<200) { while(y<200) { if(amd[x][y]==1) { glLoadIdentity(); glTranslatef(x,-y,-200.0f); glColor3f(rmd[x][y]/50.0f,gmd[x][y]/50.0f,bmd[x][y]/50.0f); glBegin(GL_QUADS); glVertex3f(-1, 1, 0.0f); glVertex3f( 1, 1, 0.0f); glVertex3f( 1,-1, 0.0f); glVertex3f(-1,-1, 0.0f); glEnd(); } y++; } y=0; x++;} } void bb() { glLoadIdentity(); glTranslatef(xx,-yy,-200.0f); glColor3f(1,1,0); glBegin(GL_QUADS); glVertex3f(-1, 1, 0.0f); glVertex3f( 1, 1, 0.0f); glVertex3f( 1,-1, 0.0f); glVertex3f(-1,-1, 0.0f); glEnd(); } void cc() { glLoadIdentity(); glTranslatef(0,0,-201.0f); glColor3f(1,0,0); glBegin(GL_QUADS); glVertex3f(0, -50, 0.0f); glVertex3f( 0, 0, 0.0f); glVertex3f( 5,0, 0.0f); glVertex3f(5,-50, 0.0f); glEnd(); glLoadIdentity(); glTranslatef(5,0,-201.0f); glColor3f(0,1,0); glBegin(GL_QUADS); glVertex3f(0, -50, 0.0f); glVertex3f( 0, 0, 0.0f); glVertex3f( 5,0, 0.0f); glVertex3f(5,-50, 0.0f); glEnd(); glLoadIdentity(); glTranslatef(10,0,-201.0f); glColor3f(0,0,1); glBegin(GL_QUADS); glVertex3f(0, -50, 0.0f); glVertex3f( 0, 0, 0.0f); glVertex3f( 5,0, 0.0f); glVertex3f(5,-50, 0.0f); glEnd(); }
OpenGLES是一个专门用于嵌入式系统和移动设备的图形渲染API,可以用来绘制各种2D和3D图形。下面是一个简单的使用OpenGLES绘制图形的步骤: 1. 创建OpenGL ES上下文,需要使用EGL来完成这个步骤。 2. 配置OpenGL ES的状态,包括背景颜色、深度缓冲区、清除颜色和深度缓冲区等。 3. 创建OpenGL ES对象,包括着色器程序、顶点缓冲区、纹理等。 4. 绘制图形,使用绑定的着色器程序、顶点缓冲区、纹理等,调用OpenGL ES绘制函数进行绘制。 5. 交换前后缓冲区,将绘制结果显示到屏幕上。 下面是一个简单的OpenGL ES绘制三角形的示例代码: ```c++ #include <GLES2/gl2.h> #include <EGL/egl.h> EGLDisplay display; EGLSurface surface; EGLContext context; GLuint programObject; const GLfloat vertices[] = { 0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f }; const char *vertexShaderSource = "attribute vec4 a_Position;\n" "void main() {\n" " gl_Position = a_Position;\n" "}\n"; const char *fragmentShaderSource = "precision mediump float;\n" "void main() {\n" " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" "}\n"; void init() { // 创建OpenGL ES上下文 EGLint configAttribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_NONE }; EGLint majorVersion; EGLint minorVersion; EGLConfig config; EGLint numConfigs; EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); eglInitialize(display, &majorVersion, &minorVersion); eglChooseConfig(display, configAttribs, &config, 1, &numConfigs); EGLSurface surface = eglCreateWindowSurface(display, config, nativeWindow, NULL); EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL); eglMakeCurrent(display, surface, surface, context); // 配置OpenGL ES状态 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepthf(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); // 创建OpenGL ES对象 GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glCompileShader(vertexShader); GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); glCompileShader(fragmentShader); programObject = glCreateProgram(); glAttachShader(programObject, vertexShader); glAttachShader(programObject, fragmentShader); glLinkProgram(programObject); glViewport(0, 0, width, height); } void render() { // 清除颜色缓冲区和深度缓冲区 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 绑定着色器程序 glUseProgram(programObject); // 绑定顶点缓冲区 GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // 设置顶点属性 GLuint positionLocation = glGetAttribLocation(programObject, "a_Position"); glEnableVertexAttribArray(positionLocation); glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0); // 绘制三角形 glDrawArrays(GL_TRIANGLES, 0, 3); // 交换前后缓冲区 eglSwapBuffers(display, surface); } int main() { init(); while (true) { render(); } return 0; } ``` 这是一个OpenGL ES 2.0的示例代码,包含了创建OpenGL ES上下文、配置OpenGL ES状态、创建OpenGL ES对象、绘制三角形等步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值