// Triangle.c
// OpenGL SuperBible, Chapter 4
// Demonstrates OpenGL Triangle Fans, backface culling, and depth testing
// Program by Richard S. Wright Jr.
#include "../../Common/OpenGLSB.h" // System and OpenGL Stuff
#include <math.h>
// Define a constant for the value of PI
#define GL_PI 3.1415f
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// Flags for effects
// Flags for effects
int iCull = 0;
int iOutline = 0;
int iDepth = 0;
///////////////////////////////////////////////////////////////////////////////
// Reset flags as appropriate in response to menu selections
void ProcessMenu(int value)
{
switch(value)
{
case 1:
iDepth = !iDepth;
break;
case 2:
iCull = !iCull;
break;
case 3:
iOutline = !iOutline;
default:
break;
}
glutPostRedisplay();
}
// Called to draw scene
void RenderScene(void)
{
GLfloat x,y,angle; // Storage for coordinates and angles
int iPivot = 1; // Used to flag alternating colors
// Clear the window and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Turn culling on if flag is set
if(iCull)
glEnable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE);
// Enable depth testing if flag is set
if(iDepth)
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
// Draw back side as a polygon only, if flag is set
if(iOutline)
glPolygonMode(GL_BACK,GL_LINE);
else
glPolygonMode(GL_BACK,GL_FILL);
// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
// Begin a triangle fan
glBegin(GL_TRIANGLE_FAN);
// Pinnacle of cone is shared vertex for fan, moved up Z axis
// to produce a cone instead of a circle
glVertex3f(0.0f, 0.0f, 75.0f);
// Loop around in a circle and specify even points along the circle
// as the vertices of the triangle fan
for(angle = 0.0f; angle < (2.0f*GL_PI); angle += (GL_PI/8.0f))
{
// Calculate x and y position of the next vertex
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
// Alternate color between red and green
if((iPivot %2) == 0)
glColor3f(0.0f, 1.0f, 0.0f);
else
glColor3f(1.0f, 0.0f, 0.0f);
// Increment pivot to change color next time
iPivot++;
// Specify the next vertex for the triangle fan
glVertex2f(x, y);
}
// Done drawing fan for cone
glEnd();
// Begin a new triangle fan to cover the bottom
glBegin(GL_TRIANGLE_FAN);
// Center of fan is at the origin
glVertex2f(0.0f, 0.0f);
for(angle = 0.0f; angle < (2.0f*GL_PI); angle += (GL_PI/8.0f))
{
// Calculate x and y position of the next vertex
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
// Alternate color between red and green
if((iPivot %2) == 0)
glColor3f(0.0f, 1.0f, 0.0f);
else
glColor3f(1.0f, 0.0f, 0.0f);
// Increment pivot to change color next time
iPivot++;
// Specify the next vertex for the triangle fan
glVertex2f(x, y);
}
// Done drawing the fan that covers the bottom
glEnd();
// Restore transformations
glPopMatrix();
// Flush drawing commands
glutSwapBuffers();
}
// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
// Set drawing color to green
glColor3f(0.0f, 1.0f, 0.0f);
// Set color shading model to flat
glShadeModel(GL_FLAT);
// Clock wise wound polygons are front facing, this is reversed
// because we are using triangle fans
glFrontFace(GL_CW);
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
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);
// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Triangle Culling Example");
// Create the Menu
glutCreateMenu(ProcessMenu);
glutAddMenuEntry("Toggle depth test",1);
glutAddMenuEntry("Toggle cull backface",2);
glutAddMenuEntry("Toggle outline back",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
opengl.rar_OpenGL多边形_OpenGl画点
共93个文件
exe:16个
dsp:12个
dsw:12个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 54 浏览量
2022-09-24
19:11:13
上传
评论
收藏 1.98MB RAR 举报
温馨提示
OpenGl-12个例子,画点、线、三角形、多边形,附加效果。
资源详情
资源评论
资源推荐
收起资源包目录
opengl.rar (93个子文件)
Chapter 3
Lstipple
Lstipple.plg 1KB
LSTIPPLE.C 3KB
Lstipple.dsw 539B
Lstipple.dsp 4KB
Lstipple.opt 58KB
Lstipple.exe 44KB
Single
Single.exe 40KB
Single.opt 58KB
Single.dsp 4KB
Single.dsw 537B
Single.plg 1KB
.DS_Store 6KB
Single.c 2KB
Triangle
Triangle.exe 44KB
Triangle.opt 58KB
Triangle.plg 1KB
Triangle.dsp 4KB
Triangle.dsw 539B
Triangle.c 5KB
Linesw
Release
Linesw.exe 32KB
vc60.idb 41KB
linesw.obj 5KB
Linesw.ncb 41KB
Linesw.exe 44KB
Linesw.dsw 535B
Linesw.dsp 4KB
Linesw.opt 57KB
Linesw.plg 1KB
LINESW.C 3KB
.DS_Store 6KB
Scissor
Scissor.ncb 33KB
Release
vc60.idb 41KB
Scissor.exe 32KB
Scissor.pch 3.06MB
scissor.obj 2KB
Scissor.opt 57KB
scissor.c 2KB
Scissor.plg 1KB
.DS_Store 6KB
Scissor.dsp 4KB
Scissor.exe 40KB
Scissor.dsw 539B
Star
Star.c 5KB
Star.exe 44KB
Star.plg 1KB
Star.opt 58KB
Star.dsp 4KB
Star.dsw 531B
LStrips
LStrips.opt 57KB
Release
LStrips.obj 5KB
vc60.idb 41KB
LStrips.exe 32KB
LStrips.plg 1KB
LStrips.c 3KB
LStrips.dsp 4KB
LStrips.ncb 41KB
LStrips.exe 44KB
LStrips.dsw 537B
Lines
Lines.dsw 533B
Release
vc60.idb 41KB
Lines.obj 5KB
Lines.pch 3.07MB
Lines.exe 32KB
Lines.opt 57KB
Lines.dsp 4KB
Lines.c 3KB
Lines.plg 1KB
Lines.exe 44KB
Lines.ncb 41KB
Stencil
Stencil.opt 58KB
Stencil.dsp 4KB
Stencil.plg 1KB
Stencil.exe 44KB
Stencil.dsw 539B
stencil.c 4KB
Pointsz
Pointsz.plg 1KB
Pointsz.c 3KB
Pointsz.opt 58KB
Pointsz.exe 44KB
Pointsz.dsw 537B
Pointsz.dsp 4KB
Points
POINTS.C 3KB
Points.plg 1KB
Points.dsp 4KB
Points.exe 44KB
Points.dsw 535B
Points.opt 58KB
Pstipple
Pstipple.dsw 539B
Pstipple.opt 58KB
Pstipple.exe 44KB
Pstipple.c 4KB
Pstipple.plg 1KB
Pstipple.dsp 4KB
共 93 条
- 1
APei
- 粉丝: 63
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Docker容器配置进阶
- tensorflow-gpu-2.7.4-cp37-cp37m-manylinux2010-x86-64.whl
- 多段线、 圆、弧转多段线(仅我可见)
- tensorflow-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- yeyue-p8Yi4-ve4a83792.apk
- tensorflow-gpu-2.7.3-cp38-cp38-manylinux2010-x86-64.whl
- 五相感应电机矢量控制模型MATLAB
- RGLED (1) (1).circ
- IMG_20240427_215747.jpg
- python下前端WEB学习笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0