当前位置:网站首页>Draw a circle and a square on the screen. The square is in front and the circle is behind. You can move the square through the keyboard. In the following cases, the square can only move within the cir
Draw a circle and a square on the screen. The square is in front and the circle is behind. You can move the square through the keyboard. In the following cases, the square can only move within the cir
2022-07-24 05:32:00 【Mo xiaodai ^o^】
/* Draw a square on the screen , use ice.bmp Texture map the square ; Draw a yellow teapot behind the square , Suppose the square is transparent , Draw the mixing effect of teapot and square ; adopt A,D,W and K Press the key to adjust the teapot in X Axis and Y Position of the shaft , As follows :[1] A:x Value reduction on axis ;
[2] D:x The value on the axis increases ;[3] W:y The value on the axis increases ; [4] K:y Value reduction on axis . Add a template cache to the program so that the teapot can only move in a square area .
*/
#define GLUT_DISABLE_ATEXIT_HACK
#include "windows.h"
#include <gl/glut.h>
#include "math.h"
float alpha = 0.5;
float p1=0.0;
float p2=0.0;
// initialization OpenGL
void init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);// Set the background color
glClearStencil(1);
glClearDepth(1.0f);
}
// The main drawing process
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Clear color cache
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 0x01, 0xFFFFFFFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);//
// Draw a teapot
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0, 0.0, -6.0);
glColor3f(1, 1, 0);
//glBlendFunc(GL_ZERO, GL_ONE);
glutSolidSphere(2, 36, 36);
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glPopMatrix();
glStencilFunc(GL_NOTEQUAL, 0x00, 0xFFFFFFFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
// Draw irregular quadrilateral
glPushMatrix();
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glTranslatef(-1+p1, -1+p2, -4.0);
glShadeModel(GL_FLAT);
//glEnable(GL_TEXTURE_2D);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
// Call when the window changes size
void reshape(int w, int h) {
glViewport(0, 0, w, h);// Set up the viewport
glMatrixMode(GL_PROJECTION);// Set the current projection transformation mode
glLoadIdentity();// Replace the current transformation matrix with the identity matrix
gluPerspective(90, (float)w / h, 4, 10.0);// Set the orthographic projection body
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
// Dealing with keyboards
void keyboard(unsigned char key, int x, int y) {
switch (key) {
// [1] A:x Value reduction on axis ;[2] D:x The value on the axis increases ;[3] W:y The value on the axis increases ; [4] K:y Value reduction on axis .
case 27://esc Key to exit
exit(0);
break;
case 'a':
case 'A':
p1=p1-1;
glutPostRedisplay();
break;
case 'd':
case 'D':
p1=p1+1;
glutPostRedisplay();
break;
case 'w':
case 'W':
p2=p2+1;
glutPostRedisplay();
break;
case 'k':
case 'K':
p2=p2-1;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char* argv[]) // The main function : The number of arguments & Parameter values
{
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutCreateWindow("Basic");// Set the window title
init();// initialization OpenGL
glutDisplayFunc(display);// Set display callback function
glutReshapeFunc(reshape);// Set up reshape Callback function
glutKeyboardFunc(keyboard);// Set the keyboard callback function
glutMainLoop();// Enter the main loop
}
边栏推荐
猜你喜欢
随机推荐
Detailed explanation of string constant pool and intern() of string
T 6-10
按钮 渐变
JS输出字符串中出现最多次数的字符
String_ Method_ 01match method
Scikit learn notes
设计一个函数print打印字符串,如果只传string型参数s,则字符串长度跟10比较,大于10,打印前10个字符,小于10,全部输出s;如果传string型参数s和int型n,则字符串长度跟n比
Constructor_ Map constructor
空杯心态,重新开始
canvas - Bezier 贝塞尔曲线
关于作为行业人从业人员的一点思考
special effects - 星空宇宙背景特效
Array_ 01return in foreach
Opengl在屏幕上绘制一个锥体,该锥体有四个面,每个面都是三角形。为该锥体添加光照和纹理效果
OPENGL在屏幕上绘制2个点,右边一个蓝色的点,采用反走样技术,左边一个红色的点,不采用反走样技术。比较两个点的区别。
useRef 创建动态引用
Basic usage of analog Addition & structure
special effects - 鼠标移动,出现泡泡拖尾
canvas - 圆形
字符串_方法_01match方法









