计算机图形学程序

计算机图形学程序,第1张

#include <GL/glut.h>

#include <stdio.h>

#include <stdlib.h>

void LineGL(int x0,int y0,int x1,int y1)

{

glBegin (GL_LINES)

glColor3f (1.0f, 0.0f, 0.0f) glVertex2f (x0,y0)

glColor3f (0.0f, 1.0f, 0.0f) glVertex2f (x1,y1)

glEnd ()

}

struct outcode

{

unsigned all

unsigned left,right,top,bottom

}

struct Rectangle

{

float xmin,xmax,ymin,ymax

}

Rectangle rect

int x0,y0,x1,y1

GLboolean bClip = false

void compoutcode(float x,float y,Rectangle rect,outcode *outcode)

{

outcode->all=0

outcode->top=outcode->bottom=0

if(y>(float)rect.ymax)

{

outcode->top=1

outcode->all+=1

}

else if(y<(float)rect.ymin)

{

outcode->bottom=1

outcode->all+=1

}

outcode->right=outcode->left=0

if(x>(float)rect.xmax)

{

outcode->right=1

outcode->all+=1

}

else if(x<(float)rect.xmin)

{

outcode->left=1

outcode->all+=1

}

}

int cohensutherlandlineclip(Rectangle rect, int &x0,int &y0,int &x1,int &y1)

{

int accept,done

outcode outcode0,outcode1

outcode * outcodeout

float x,y

accept=0

done=0

compoutcode(x0,y0,rect,&outcode0)

compoutcode(x1,y1,rect,&outcode1)

do{

if(outcode0.all==0&&outcode1.all==0)

{

accept=1

done=1

}

else if(outcode0.all&outcode1.all!=0)

done=1

else

{

if(outcode0.all!=0)

outcodeout=&outcode0

else

outcodeout=&outcode1

if(outcodeout->left)

{

y=y0+(y1-y0)*(rect.xmin-x0)/(x1-x0)

x=(float)rect.xmin

}

else if(outcodeout->top)

{

x=x0+(x1-x0)*(rect.ymax-y0)/(y1-y0)

y=(float)rect.ymax

}

else if(outcodeout->right)

{

y=y0+(y1-y0)*(rect.xmax-x0)/(x1-x0)

x=(float)rect.xmax

}

else if(outcodeout->bottom)

{

x=x0+(x1-x0)*(rect.ymin-y0)/(y1-y0)

y=(float)rect.ymin

}

if(outcodeout->all==outcode0.all)

{

x0=xy0=ycompoutcode(x0,y0,rect,&outcode0)

}

else

{

x1=xy1=y

compoutcode(x1,y1,rect,&outcode1)

}

}

}while(!done)

if(accept)

LineGL(x0,y0,x1,y1)

return accept

}

void myDisplay()

{

glClear(GL_COLOR_BUFFER_BIT)

glColor3f (1.0f, 0.0f, 0.0f)

glRectf(rect.xmin,rect.ymin,rect.xmax,rect.ymax)

if (!bClip)

LineGL(x0,y0,x1,y1)

else

cohensutherlandlineclip(rect, x0,y0,x1,y1)

glFlush()

}

void Init()

{

glClearColor(0.0, 0.0, 0.0, 0.0)

glShadeModel(GL_FLAT)

rect.xmin=100//窗口的大小

rect.xmax=300

rect.ymin=100

rect.ymax=300

x0 = 450,y0 = 0, x1 = 0, y1 = 450//裁剪前的直线端点

printf("Press key 'c' to Clip!\nPress key 'r' to Restore!\n")

}

void Reshape(int w, int h)

{

glViewport(0, 0, (GLsizei) w, (GLsizei) h)

glMatrixMode(GL_PROJECTION)

glLoadIdentity()

gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h)

}

void keyboard(unsigned char key, int x, int y)

{

switch (key)

{

case 'c':

bClip = true

glutPostRedisplay()//重画

break

case 'r':

bClip = false

Init()

glutPostRedisplay()//

break

case 'x':

exit(0)

break

default:

break

}

}

int main(int argc, char *argv[])

{

glutInit(&argc, argv)

glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)

glutInitWindowPosition(100, 100)

glutInitWindowSize(640, 480)

glutCreateWindow("Hello World!")

Init()

glutDisplayFunc(myDisplay)

glutReshapeFunc(Reshape)

glutKeyboardFunc(keyboard)

glutMainLoop()

return 0

}

正好也在做试验, 这个是裁剪算法,我在VC下运行无错误。

还有torbc?是turbo c吧?因为编译环境有些区别,而网上找的又都是VC的,所以都会遇到错误。

建议去装个VC的 ,毕竟VC比turbo c主流

#include <GL/glut.h>

#include <stdlib.h>

static int shoulder = 0, elbow = 0//shoulder:肩部角度,elbow: 肘部角度

void init(void)

{

glClearColor(0.0f, 0.0f, 0.0f, 0.0f)

glShadeModel(GL_FLAT)

}

void display(void)

{

glClear(GL_COLOR_BUFFER_BIT)

glPushMatrix()//把当前的变换矩阵压入OpenGL内部栈中,用以保存当前矩阵

//画机器人的上臂

glTranslatef(-1.0f, 0.0f, 0.0f)//用平移矩阵乘当前矩阵,格式为:glTranslatef(x,y,z)

glRotatef((GLfloat) shoulder, 0.0f, 0.0f, 1.0f)//用旋转矩阵乘当前矩阵,格式为glRotatef (角度,x轴,y轴,z轴迟明),这里是绕Z轴旋转

glTranslatef(1.0f, 0.0f, 0.0f)//再用平移矩阵乘当前矩阵,注意顺序

glPushMatrix()//变换矩阵压栈

glScalef(2.0f, 0.4f, 1.0f)//用缩放矩阵乘以当前矩阵,格式为glScalef(x缩放比例,y缩放比例,z缩放比例)

glutWireCube(1.0f)//glut库函数,画一个三维的Cube,参数为边长

glPopMatrix()//d栈,现在矩阵恢复到首旦铅使用缩放前的样子

//画机器人的前臂,请注意平移矩阵和旋转矩阵的变化

glTranslatef(1.0f, 0.0f, 0.0f)

glRotatef((GLfloat) elbow, 0.0f, 0.0f, 1.0f)

glTranslatef(1.0f, 0.0f, 0.0f)

glPushMatrix()

glScalef(2.0f, 0.4f, 1.0f)

glutWireCube(1.0f)

glPopMatrix()

glPopMatrix()

glFlush()

}

void reshape (int width, int height)

{

glViewport(0, 0, width, height)

glMatrixMode(GL_PROJECTION)

glLoadIdentity()

gluPerspective(65.0f, (GLfloat)width/(GLfloat)height, 1.0f, 20.0f)//建立一个透视投影视图体,格式为:gluPerspective(视域的角度,宽高比,视点到近裁剪面的距离(总为正),视点到远裁剪面的距离(总为正))

glMatrixMode(GL_MODELVIEW)

glLoadIdentity()

glTranslatef(0.0f, 0.0f, -5.0f)//用平移矩阵乘当前矩阵者好,注意,这会将所有绘制过程中绘制的物体平移

}

void keyboard(unsigned char key, int x, int y)

{

switch (key)

{

case 'a'://处理四个按键,改变旋转角度,转动手臂

shoulder = (shoulder + 5) % 360

glutPostRedisplay()//重画

break

case 'd':

shoulder = (shoulder - 5) % 360

glutPostRedisplay()

break

case 'q':

elbow = (elbow + 5) % 360

glutPostRedisplay()

break

case 'e':

elbow = (elbow - 5) % 360

glutPostRedisplay()

break

case 'x':

exit(0)

break

default:

break

}

}

int main(int argc, char** argv)

{

glutInit(&argc, argv)

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)

glutInitWindowSize(500, 500)

glutInitWindowPosition(100, 100)

glutCreateWindow("Transform")

init()

glutDisplayFunc(display)

glutReshapeFunc(reshape)

glutKeyboardFunc(keyboard)

glutMainLoop()

return 0

}

一个机器人手臂控制的三维造型。在VC下运行正确,且是opengl编写。希望能用到


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12545824.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存