close

在設定好GLEW、(free)GLUT和GLFW(extension wrangler、utility、framework)程式庫與動態連結dll檔後就可以開始在Visual Studio 2012(為主)寫程式了。

相關介紹可以參考其官網,並下載相關程式庫:

http://www.glfw.org/

http://freeglut.sourceforge.net/

http://glew.sourceforge.net/

 

C/C++
    一般    其他Include目錄

01.PNG01-small.PNG

 

連結器
    一般

02.PNG

 

 

第一個實例是畫出一個有紅綠藍三原色的三角形,在按下ESC鍵後會結束繪圖視窗。

#include <Windows.h>
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
 
void render(void);
void keyboard(unsigned char c, int x, int y);
void mouse(int button, int state, int x, int y);
 
int main(int argc, char** argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640, 480);
glutCreateWindow("Simple GLUT Application");
 
glutDisplayFunc(render);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
 
glutMainLoop();
}
 
void keyboard(unsigned char c, int x, int y){
if (c == 27){
exit(0);
}
}
 
void mouse(int button, int state, int x, int y){
if (button == GLUT_RIGHT_BUTTON){
exit(0);
}
}
 
void render(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex2f(-0.5, -0.5);
glColor3f(0, 1, 0);
glVertex2f(0.5, 0.0);
glColor3f(0, 0, 1);
glVertex2f(0.0, 0.5);
glEnd();
 
glutSwapBuffers();
}
 
 
執行後的成果:

1.png

 

第二個實例是畫出一個紅色的茶壺

#include <Windows.h>
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
 
void init();
void display();

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(0, 0);
    glutInitWindowSize(300, 300);

    glutCreateWindow("OpenGL 3D View");

    init();
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

void init()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glOrtho(-5, 5, -5, 5, 5, 15);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0, 0, 0);
    glutWireTeapot(3);

    glFlush();
}

teapot.png

 
 
第三個實例是以GLFW畫出一個白色的三角形
 
#include <GL/glew.h>
#include <GLFW/glfw3.h>
 
int main(void)
{
GLFWwindow *window;
 
if( !glfwInit() )
{
return -1;
}
 
window = glfwCreateWindow(640, 480, "Hello OpenGL", NULL, NULL);
 
if(!window)
{
glfwTerminate();
return -1;
}
 
glfwMakeContextCurrent(window);
 
float vertices[] =
{
0.0, 0.5, 0.0,
-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
};
 
while( !glfwWindowShouldClose(window) )
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);
 
glfwSwapBuffers(window);
 
glfwPollEvents();
}
 
glfwTerminate();
return 0;
}

 

triangle.png

 
第四個實例是畫出一條線
 
#include <glut.h>
 
void Draw() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glEnd();
glFlush();
}
 
void Initialize() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
 
int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(200, 200);
glutCreateWindow("Daniel");
Initialize();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
 

line.png

 
 
有沒有對電腦繪圖更加了解了呢?
想不想進入動畫的世界呢,像賈伯斯一樣打造出電腦動畫王國呢?
arrow
arrow
    全站熱搜

    丹尼爾 發表在 痞客邦 留言(0) 人氣()