C语言中的OpenGL添加另一个对象
OpenGL in C language add another object
我需要帮助将另一个对象添加到我的 "drawing"。
在我的大学老师代码 Google 的帮助下,我能够创建一个脸型。
我想在脸上添加笑容
什么要在绘图中添加其他形状?
我试图在空白处添加 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;
}
一张微笑的嘴巴是Concave polygon. I recommend to use a 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);
}
我需要帮助将另一个对象添加到我的 "drawing"。
在我的大学老师代码 Google 的帮助下,我能够创建一个脸型。
我想在脸上添加笑容
什么要在绘图中添加其他形状?
我试图在空白处添加 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;
}
一张微笑的嘴巴是Concave polygon. I recommend to use a 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);
}