update: 2012/04/28
reference:
OpenGL: A Primer, 3/e
基本幾何圖形之一: 點
A. 說明:
1. 延續之前文章: I touchs: OpenGL: General Strucure 的專案.
2. 將繪圖函式額外獨立成 .h 與 .cpp 檔案.
------------------------------------------------------------------------------
B. 新增給繪圖函式使用的檔案:
1. Xcode > File > New > File...
> Mac OS X > C and C++ > Header File > Next
Save As: draw.h
Targets: OpenGLStart checked> Create
2. Xcode > File > New > File...
> Mac OS X > C and C++ > C++ File > Next
Save As: draw.cpp
Targets: OpenGLStart checked> Create
3. 開啓 draw.h 檔案, 修改如下:
//@update
/*
#ifndef OpenGLStart_draw_h
#define OpenGLStart_draw_h
#endif
*/
4. 開啓 draw.cpp 檔案, 修改如下:
//#include <iostream>
//@add
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
5. 開啓 main.cpp 檔案, 修改如下:
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include "draw.h"
....
------------------------------------------------------------------------------
C. 畫點
1. 開啓 draw.h 檔案, 修改如下://@add: drawing
void drawPoints(); // 畫點
2. 開啓 draw.cpp 檔案, 修改如下:
....
//@add: drawing
//
// 畫點
void drawPoints()
{
// Clear the buffer
glClear(GL_COLOR_BUFFER_BIT);
//@add: Set the color for this polygon (Red)
//
//state: current drawing color
//glColor3f(1.0, 0.0, 0.0);
//@add: sets the point size state variable
//
// default: 1.0
// p.s. glPointSize() 只能在 glBegin() 與 glEnd() 區間之外設定.
glPointSize(10.0);
// Let us begin drawing some points
//glBegin(GL_POLYGON);
glBegin(GL_POINTS);
// Specify the points
glColor3f(1.0, 1.0, 1.0); // Set the color state: white
glVertex2f(-0.5, -0.5);
glColor3f(1.0, 0.0, 0.0); // Set the color state: red
glVertex2f(-0.5, 0.5);
glColor3f(0.0, 1.0, 0.0); // Set the color state: green
glVertex2f(0.5, 0.5);
glColor3f(0.0, 0.0, 1.0); // Set the color state: blue
glVertex2f(0.5, -0.5);
// Ok we are done specifying points
glEnd();
// Write this out to the screen
glFlush();
}
3. 開啓 main.cpp 檔案, 修改如下:
....
int main(int argc, char * argv[])
{
....
// Set the callback function, will be called as needed
//glutDisplayFunc(display);
glutDisplayFunc(drawPoints); // 畫點
....
}
4. 編譯並執行:
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。