OpenGL程序演示
OpenGL 程序演示
一. OpenGL 基本繪制? ?
?開發環境:VC++2008 + OpenGL 3
? ?用兩種不同的方法繪制兩個正方形;繪制四個頂點;繪制兩條平行線;如下圖;
代碼如下:
#include <GL/glut.h>
#include <math.h>
const int n = 200;
const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926536f;
void myDisplay(void)
{
int i;
// 畫一個綠色正方形
? ? glClear(GL_COLOR_BUFFER_BIT);
? ? glColor3f(0.0f,1.0f,0.0f);
? ? glRectf(-0.5f, 0.4f, 0.0f, 0.9f); //畫一個綠色的正方形
? ? //glFlush();
//glClearColor(0.0,0.0,0.0,0.0);
? ? ?//glClear(GL_COLOR_BUFFER_BIT);
? ? ?glColor4f(1.0,0.0,0.0,1.0); ? ? // set the quad color
? ? ?glBegin(GL_QUADS);
? ? ? ? ?glVertex3f(-0.2,-0.2,0.0);
? ? ? ? ?glVertex3f(0.2,-0.2,0.0);
? ? ? ? ?glVertex3f(0.2,0.2,0.0);
? ? ? ? ?glVertex3f(-0.2,0.2,0.0);
? ? glEnd();
glColor4f(1.0,0.0,0.0,1.0); ? // set the point color
? ? ? glPointSize(10);
? ? ? glBegin(GL_POINTS);
? ? ? ? ? glVertex3f(-0.5,-0.5,0.0);
? ? ? ? ? glVertex3f(0.5,-0.5,0.0);
? ? ? ? ? glVertex3f(0.5,0.5,0.0);
? ? ? ? ? glVertex3f(-0.5,0.5,0.0);
? ? ?glEnd();
glColor4f(1.0,0.0,0.0,1.0); // set the line color
? ? ? glLineWidth(5);
? ? ? glBegin(GL_LINES);
? ? ? ? ? ? glVertex3f(-0.5,-0.5,0.0);
? ? ? ? ? ? glVertex3f(0.5,-0.5,0.0);
? ? ? ? ? ? glVertex3f(0.5,0.5,0.0);
? ? ? ? ? ? glVertex3f(-0.5,0.5,0.0);
? ? ? glEnd();
?/*glBegin(GL_POLYGON);
for(i=0; i<n; ++i)
glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
glEnd();*/
? ? glFlush();
}
int main(int argc, char *argv[])
{
? ? glutInit(&argc, argv);
? ? glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
? ? glutInitWindowPosition(100, 0);
? ? glutInitWindowSize(1024, 768);
? ? glutCreateWindow("OpenGL基本繪制");
? ? glutDisplayFunc(&myDisplay);
? ? glutMainLoop();
? ? return 0;
}
二. OpenGL 機器人(用長方形構成的機器人)
?
代碼分為以下部分:
頭文件和變量,
DrawCube,在指定位置繪制一個立方體,
DrawArm,繪制機器人的一條手臂,
DrawHead,繪制機器人的頭,
DrawTorso,繪制機器人的軀干,
DrawLeg,繪制機器人的一條腿,
DrawRobot,在位置xPos、yPos、zPos處繪制機器人,
Render,處理場景的繪制,
SetupPixelFormat,為設備環境設置像素格式,
WndProc,窗口過程
兩個機器人:
?
總結
以上是生活随笔為你收集整理的OpenGL程序演示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win32汇编扩展教程
- 下一篇: WPF太极八卦程序