openGL學習
由小到大繪製多個點,排成螺旋
點選(此處)摺疊或開啟
-
void RenderScene(void)
-
{
-
GLfloat x, y, z, angle;
-
z = -50.0f;
-
GLfloat zStep = 0.5f;
-
GLfloat sizes[2];
-
GLfloat sizeStep;
-
GLfloat curSize;
-
-
glGetFloatv(GL_POINT_SIZE_RANGE, sizes);
-
glGetFloatv(GL_POINT_SIZE_GRANULARITY, &sizeStep);
-
-
curSize = sizes[0] + sizeStep;
-
-
glClear(GL_COLOR_BUFFER_BIT);
-
glPushMatrix();
-
glRotatef(40.0f, 10.0f, -10.0f, 0.0f);
-
-
-
-
for (angle = 0.0f; angle <= (2.0f * GL_PI) * 3.0f; angle += 0.1f)
-
{
-
x = 50.0f * sin(angle);
-
y = 50.0f * cos(angle);
-
z += zStep;
-
glPointSize(curSize);
-
glBegin(GL_POINTS);
-
glVertex3f(x, y, z);
-
glEnd();
-
if (curSize < 10.0f) curSize += sizeStep;
-
-
}
-
-
-
glPopMatrix();
-
glutSwapBuffers();
-
}
-
-
void ChangeSize(GLsizei w, GLsizei h)
-
{
-
GLfloat nRange = 100.0f;
-
if (h == 0) h = 1;
-
glViewport(0, 0, w, h);
-
glMatrixMode(GL_PROJECTION);
-
glLoadIdentity();
-
-
-
if (w <= h)
-
{
-
glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange);
-
}
-
else
-
{
-
glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange);
-
}
-
-
glMatrixMode(GL_MODELVIEW);
-
glLoadIdentity();
- }
繪製直線、折線
點選(此處)摺疊或開啟
-
void RenderScene(void)
-
{
-
glClear(GL_COLOR_BUFFER_BIT);
-
glPushMatrix();
-
glRotatef(40.0f, 10.0f, -10.0f, 0.0f);
-
-
GLfloat x, y, z, angle;
-
-
glBegin(GL_LINE_STRIP); //如果用GL_LINE_LOOP,會自動加一條線,形成環,如果用GL_LINES,則只是畫一條線
-
glVertex3f(x, y, z);
-
glVertex3f(x + 30, y + 30, z + 30);
-
glVertex3f(x + 30, y + 60, z + 30);
-
glVertex3f(x + 20, y + 40, z + 80);
-
glEnd();
-
-
glPopMatrix();
-
glutSwapBuffers();
- }
用折線畫螺旋,並且用上下左右控制旋轉
點選(此處)摺疊或開啟
-
/*
-
* File: main.c
-
* Author: root
-
*
-
* Created on August 30, 2013, 12:57 PM
-
*/
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <time.h>
-
#include <GL/glut.h>
-
#include <GL/gl.h>
-
#include <math.h>
-
-
-
#define GL_PI 3.1415f
-
#define KEY_UP 101
-
#define KEY_DOWN 103
-
#define KEY_LEFT 102
-
#define KEY_RIGHT 100
-
-
GLfloat rotateX = 0.0f;
-
GLfloat rotateY = 0.0f;
-
-
void RenderScene(void)
-
{
-
glClear(GL_COLOR_BUFFER_BIT);
-
glPushMatrix();
-
glRotatef(rotateX, 1.0f, 0.0f, 0.0f);
-
glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
-
-
-
GLfloat x, y, z, angle;
-
z = -50.0f;
-
glBegin(GL_LINE_STRIP);
-
for(angle = 0.0f; angle < (2.0f * GL_PI) * 4; angle += 0.1f)
-
{
-
x = 50.0f * sin(angle);
-
y = 50.0f * cos(angle);
-
z += 0.5f;
-
-
glVertex3f(x, y, z);
-
-
}
-
glEnd();
-
-
-
-
glPopMatrix();
-
glutSwapBuffers();
-
}
-
-
void ChangeSize(GLsizei w, GLsizei h)
-
{
-
GLfloat nRange = 100.0f;
-
if (h == 0) h = 1;
-
glViewport(0, 0, w, h);
-
glMatrixMode(GL_PROJECTION);
-
glLoadIdentity();
-
-
-
if (w <= h)
-
{
-
glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange);
-
}
-
else
-
{
-
glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange);
-
}
-
-
glMatrixMode(GL_MODELVIEW);
-
glLoadIdentity();
-
}
-
-
void SetupRC(void)
-
{
-
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
-
glColor3f(0.0f, 1.0f, 0.0f);
-
}
-
-
void myTimer(int i)
-
{
-
-
}
-
-
void keyAction (int key, int x, int y)
-
{
-
switch (key)
-
{
-
case KEY_UP:
-
rotateX += 1.5f;
-
break;
-
case KEY_DOWN:
-
rotateX -= 1.5f;
-
break;
-
case KEY_LEFT:
-
rotateY -= 1.5f;
-
break;
-
case KEY_RIGHT:
-
rotateY += 1.5f;
-
break;
-
default:
-
break;
-
}
-
RenderScene();
-
}
-
-
-
/*
-
*
-
*/
-
int main(int argc, char** argv) {
-
glutInit(&argc, argv);
-
glutInitWindowPosition(100, 100);
-
glutInitWindowSize(399, 400);
-
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
-
glutCreateWindow("asd");
-
SetupRC();
-
glutDisplayFunc(RenderScene);
-
glutReshapeFunc(ChangeSize);
-
glutSpecialFunc(keyAction);
-
glutMainLoop();
-
return (EXIT_SUCCESS);
- }
用三角形畫圓錐
兩個扇面組成封口的錐形
點選(此處)摺疊或開啟
-
void RenderScene(void)
-
{
-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
glPushMatrix();
-
-
glRotatef(rotateX, 10.0f, 0.0f, 0.0f);
-
glRotatef(rotateY, 0.0f, 10.0f, 0.0f);
-
-
char p0[3] = {0.0f, 50.0f, 0.0f};
-
char p1[3] = {20.0f, 0.0, 20.0f};
-
GLfloat angle;
-
glShadeModel(GL_SMOOTH);
-
glEnable(GL_CULL_FACE); //開啟多邊形剔除,不渲染多邊形的背面,這樣如果要顯示一個正面朝外的封閉多邊形,則背面超前的地方根本就不渲染。如果是建立不發呢關閉的集合形體,如紙,就不能開啟這個選項。
-
glEnable(GL_DEPTH_TEST); // 開啟深度測試,可以避免晚描繪的畫素錯誤遮擋之前描繪的Z座標靠前的畫素
-
-
glPolygonMode(GL_BACK, GL_FILL); // 顯示填充好的多邊形,如果選GL_LINE,則只顯示每個多邊形的輪廓。
-
-
glBegin(GL_TRIANGLE_FAN);
-
glVertex3f(p0[0], p0[1], p0[2]);
-
for (angle = 0; angle <= 2*GL_PI + 0.1f; angle += 0.1f)
-
{
-
glColor3f(sin(angle), 0.0f, 0.5f);
-
glVertex3f(p1[0] * sin(angle), p1[1], p1[2] * cos(angle));
-
}
-
glEnd();
-
-
glBegin(GL_TRIANGLE_FAN);
-
glColor3f(1.0f, 1.0f, 1.0f);
-
glVertex3f(p0[0], p1[1], p0[2]);
-
for (angle = 0; angle <= 2*GL_PI + 0.1f; angle += 0.1f)
-
{
-
glVertex3f(p1[0] * cos(angle), p1[1], p1[2] * sin(angle));
-
}
-
glEnd();
-
-
glPopMatrix();
-
glutSwapBuffers();
- }
用GL_QUAD_STRIP繪製連續的四邊形。提供頂點的方向是折線形的
提供頂點的順序逆時針,則為正面。
點選(此處)摺疊或開啟
-
void RenderScene(void)
-
{
-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
glRotatef(rotateX, 1.0f, 0.0f, 0.0f);
-
glRotatef(rotateY, 0.0f, 1.0f, 0.0f);
-
glEnable(GL_CULL_FACE);
-
glEnable(GL_DEPTH_TEST);
-
glPolygonMode(GL_BACK, GL_FILL);
-
-
GLfloat r = 2.0f;
-
-
glPushMatrix();
-
-
glBegin(GL_QUAD_STRIP);
-
glColor3f(1.0, 0.0f, 0.0f);
-
-
glVertex3f(r, -r, -r);
-
glVertex3f(r, -r, r);
-
glVertex3f(-r, -r, -r);
-
glVertex3f(-r, -r, r);
-
-
glColor3f(0.0, 1.0f, 0.0f);
-
glVertex3f(-r, r, -r);
-
glVertex3f(-r, r, r);
-
-
glColor3f(0.0, 0.0f, 1.0f);
-
glVertex3f(r, r, -r);
-
glVertex3f(r, r, r);
-
-
glColor3f(0.0, 1.0f, 1.0f);
-
glVertex3f(r, -r, -r);
-
glVertex3f(r, -r, r);
-
glEnd();
-
-
glBegin(GL_QUADS);
-
glColor3f(1.0, 1.0f, 1.0f);
-
glVertex3f(-r, -r, r);
-
glVertex3f(r, -r, r);
-
glVertex3f(r, r, r);
-
glVertex3f(-r, r, r);
-
-
glEnd();
-
-
glBegin(GL_QUADS);
-
glColor3f(1.0, 0.0f, 1.0f);
-
glVertex3f(r, -r, -r);
-
glVertex3f(-r, -r, -r);
-
glVertex3f(-r, r, -r);
-
glVertex3f(r, r, -r);
-
-
-
glEnd();
-
-
glPopMatrix();
-
glutSwapBuffers();
- }
透視效果
點選(此處)摺疊或開啟
-
void ChangeSize(GLsizei w, GLsizei h)
-
{
-
if (h == 0) h = 1;
-
glViewport(0, 0, w, h);
-
glMatrixMode(GL_PROJECTION);
-
glLoadIdentity();
-
-
gluPerspective(80.0f, (GLfloat)w/(GLfloat)h, 11.0, 30.0);
-
-
glMatrixMode(GL_MODELVIEW);
-
glLoadIdentity();
-
glTranslatef(0.0f, 0.0f, -15.0f); 把座標向x軸負方向推,使得影像裝到gluPerspective的後兩個引數範圍裡
-
}
方塊回彈
點選(此處)摺疊或開啟
-
/*
-
* File: main.c
-
* Author: root
-
*
-
* Created on August 30, 2013, 12:57 PM
-
*/
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <time.h>
-
#include <GL/glut.h>
-
#include <GL/gl.h>
-
-
-
GLfloat rsize = 25.0f;
-
GLfloat x = 200.0f;
-
GLfloat y = 25.0f;
-
-
GLfloat xStep = 5.0f;
-
GLfloat yStep = 5.0f;
-
-
GLfloat windowWidth;
-
GLfloat windowHight;
-
-
void RenderScene(void)
-
{
-
-
glClear(GL_COLOR_BUFFER_BIT);
-
glColor3f(1.0f, 0.0f, 0.0f);
-
glRectf(x , y , x + rsize, y - rsize);
-
-
-
glutSwapBuffers();
-
}
-
-
void ChangeSize(GLsizei w, GLsizei h)
-
{
-
windowWidth = w;
-
windowHight = h;
-
glViewport(0, 0, windowWidth, windowHight);
-
glMatrixMode(GL_PROJECTION);
-
glLoadIdentity();
-
glOrtho(0, windowWidth, 0, windowHight, 20.0, -20.0);
-
glMatrixMode(GL_MODELVIEW);
-
glLoadIdentity();
-
}
-
-
void SetupRC(void)
-
{
-
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
-
// glColor3f(0.0f, 1.0f, 0.0f);
-
}
-
-
void myTimer(int i)
-
{
-
if (x > windowWidth - rsize || x < 0) xStep *= -1;
-
if (y > windowHight || y < rsize) yStep *= -1;
-
x += xStep;
-
y += yStep;
-
-
glutPostRedisplay();
-
-
glutTimerFunc(30, myTimer, i);
-
}
-
-
/*
-
*
-
*/
-
int main(int argc, char** argv) {
-
glutInit(&argc, argv);
-
glutInitWindowPosition(100, 100);
-
glutInitWindowSize(399, 400);
-
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
-
glutCreateWindow("asd");
-
// glEnable(GL_LIGHTING);
-
glutDisplayFunc(RenderScene);
-
glutReshapeFunc(ChangeSize);
-
glutTimerFunc(30, myTimer, 1);
-
SetupRC();
-
glutMainLoop();
-
return (EXIT_SUCCESS);
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26239116/viewspace-2125609/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- OpenGL 學習 01 Mac 搭建 OpenGL 環境Mac
- OpenGL 學習 04 圖元
- OpenGL ES 學習資源分享
- OpenGL 學習 08 - 球體世界
- OpenGL 學習 03 移動的矩形
- 從零開始學習OpenGL-14複習光照
- OpenGL 學習系列--基礎的繪製流程
- OpenGL 學習系列---基本形狀的繪製
- OpenGL 學習 07 向量 矩陣變換 投影矩陣
- OpenGL ES 2.0學習(一)入門知識點
- OpenGL高階版本學習日誌2:光照模型&材質模型
- 學習OpenGL ES之繪製三角形
- 從顯示一張圖片開始學習OpenGL ES
- OpenGL 學習 02 第一個三角形
- OpenGL 學習 05 花托(剔除 深度測試 多邊形模式)模式
- OpenGL 學習 06 多邊形偏移 裁剪 混合 抗鋸齒
- OpenGL/OpenGL ES 初探
- OpenGL 與 OpenGL ES
- OpenGL筆記之一 開學筆記
- Android日常學習:OpenGL 實踐之貝塞爾曲線繪製Android
- iOS-零基礎學習OpenGL-ES入門教程(一)iOS
- 圖形學_opengl紋理對映
- OpenGL Shader例項,OpenGL 粒子系統
- OpenGL系列教程之一:OpenGL(轉)
- OpenGL概述
- OpenGL入門(1)——建立一個OpenGL專案
- [OpenGL]VS2010配置OpenGL開發環境開發環境
- OpenGL 安裝!
- OpenGL/OpenGL ES入門:紋理初探 - 常用API解析API
- 跟我學Python影像處理丨帶你入門OpenGLPython
- Android OpenGL ES 2.0 手把手教學(6)- 紋理Android
- Android OpenGL ES 2.0 手把手教學(1)- Hello World!Android
- 4次Bezier曲線--計算機圖形學 opengl計算機
- QCustomPlot開啟opengl
- GPU OpenGL 管線GPU
- OpenGL基本概念
- OpenGL ES 入門
- OpenGL 環境搭建
- OpenGL ES on iOS — AssimpiOS