C语言中的OpenGL添加另一个对象
Posted
技术标签:
【中文标题】C语言中的OpenGL添加另一个对象【英文标题】:OpenGL in C language add another object 【发布时间】:2020-03-01 10:20:49 【问题描述】:我需要帮助将另一个对象添加到我的“绘图”中。 在我的大学教师代码的帮助下,我能够创建一个脸型。
我想给脸上添加笑容 在绘图中添加什么形状?
我尝试在 void face()
后面添加类似:
glVertex2f(100, 125);
glVertex2f(400, 125);
这是C语言的代码:
#include <GL/glut.h>
#include <math.h>
#include<Windows.h>
#include <GL/gl.h>
#include <GL/GLU.h>
#include <stdlib.h>
float counter = 200;
static GLfloat spin = 0.0;
static GLfloat R = 0.2;
static GLfloat G = 0.9;
static GLfloat B = 0.8;
float carx = 500, cary = 50, fr = 0, fl = 0, fo = 0, fu = 0, mx = 0, my = 0;
void initOpenGl()
glClearColor(0.0, 0.0, 1.8, 0); //Background Color
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 1500, 0, 500);
glMatrixMode(GL_MODELVIEW);
void eye(int x, int y)
float th;
glBegin(GL_POLYGON);
glColor3f(0, 0, 0);
for (int i = 0; i < 360; i++)
th = i * (3.1416 / 180);
glVertex2f(x + 25 * cos(th), y + 20 * sin(th));
glEnd();
void face()
glLoadIdentity();//Bottom Par
counter = counter - 0.03;
if (fr > 0)
carx += fr;
if (fl > 0)
carx -= fl;
if (fu > 0)
cary += fu;
if (fo > 0)
cary -= fo;
if (mx > 0)
carx += mx;
if (my > 0)
carx -= my;
glTranslated(carx, cary, 0.0);
glBegin(GL_POLYGON); //Top Part
glVertex2f(100, 125);
glVertex2f(400, 125);
glVertex2f(400, 300);
glVertex2f(100, 300);
glEnd();
eye(165, 230);
eye(315, 230);
void display()
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.5, 0.9, 0.33); //Push and pop matrix for separating circle object from Background
face();
pa();
glutSwapBuffers();
glFlush();
void mouse(int button)
switch (button)
case GLUT_LEFT_BUTTON: mx = 0.3;
break;
case GLUT_RIGHT_BUTTON:my = 0.2;
break;
default:
break;
void keyFunc(unsigned char key, int x, int y)
switch (key)
case 'w': fu = 0.3, fo = 0, fr = 0, fl = 0;
break;
case 's': fu = 0, fo = 0.2, fr = 0, fl = 0;
break;
case 'a': fu = 0.01, fo = 0, fr = 0, fl = 0.2;
break;
case 'd': fu = 0.01, fo = 0, fr = 0.2, fl = 0;
break;
case 'z': fu = 0, fo = 0.2, fr = 0, fl = 0.2;
break;
case 'c': fu = 0, fo = 0.2, fr = 0.2, fl = 0;
break;
default:
break;
glutPostOverlayRedisplay();
int main(int argc, char **argv)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(1500, 700);
glutInitWindowPosition(0, 0);
glutCreateWindow("Shai Goldenebrg");
initOpenGl();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyFunc);
glutIdleFunc(display);
glutMainLoop();
return 0;
【问题讨论】:
【参考方案1】:微笑的嘴是Concave polygon。我建议使用GL_TRIANGLE_STRIP
primitive 来生成嘴巴。例如:
void mouth(int x, int y)
float th, s, c;
glBegin(GL_TRIANGLE_STRIP);
glColor3f(0, 0, 0);
for (int i = 240; i < 300; i++)
th = i * (3.1416 / 180);
c = cos(th);
s = sin(th);
glVertex2f(x + 90 * cos(th), y + 100 + 90 * sin(th));
glVertex2f(x + 110 * cos(th), y + 100 + 110 * sin(th));
glEnd();
void face()
// [...]
eye(165, 230);
eye(315, 230);
mouth(240, 150);
【讨论】:
以上是关于C语言中的OpenGL添加另一个对象的主要内容,如果未能解决你的问题,请参考以下文章
在同一个C语言程序项目中,其它.c文件可以访问另一个.c文件中的静态变量吗?