Opengl 在GLUT中按按钮更改背景色
我一直在关注一些教程和人们在这里提出的其他问题。基本上,当你按下退出键时,背景颜色会变成另一种颜色 用全部代码编辑了这篇文章Opengl 在GLUT中按按钮更改背景色,opengl,glut,Opengl,Glut,我一直在关注一些教程和人们在这里提出的其他问题。基本上,当你按下退出键时,背景颜色会变成另一种颜色 用全部代码编辑了这篇文章 #include "stdafx.h" #include <glut.h> void render(void); void keyboard(int key, int x, int y); int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDispl
#include "stdafx.h"
#include <glut.h>
void render(void);
void keyboard(int key, int x, int y);
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100); //Position of the window
glutInitWindowSize(620, 440); //Screen Size
glClearColor (0.0, 1.0, 0.0, 0.0 );
glutCreateWindow("Greeting Card"); //Creates the window and names it
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Enables Alpha channel
glutDisplayFunc(render);
glutDisplayFunc(draw);
glutKeyboardFunc(keyboard);
glutMainLoop(); //Finished, now render
}
void keyboard (unsigned char key, int x, int y)
{
GLfloat colors[][3] = { { 0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f } };
static int back;
switch (key) {
case 27:
exit(0);
default:
back ^= 1;
glClearColor(colors[back][0], colors[back][1], colors[back][2], 1.0f);
glutPostRedisplay();
}
}
void render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// World Snow //
glPushMatrix();
glTranslatef(0, -0.35, 0); //Position of the shape
glBegin(GL_POLYGON); //Defines the type of shape
glColor3f(1, 1,1); //Colour of the shape 'RED, GREEN, BLUE'
glVertex2f(-1.5,-0.7); //Vertex 2F Gives the vertex some coords
glColor3f(1, 1, 1);
glVertex2f(-1.5, 0.7);
glColor3f(1, 1, 1);
glVertex2f( 1.5, 0.7);
glColor3f(1, 1, 1);
glVertex2f( 1.5,-0.7);
glEnd();
glPopMatrix();
glFlush();
// Grey gradient world
glPushMatrix();
glTranslatef(0, -0.35, 0);
glBegin(GL_POLYGON);
glColor3f(1, 1, 1);
glVertex2f(-1.5,-0.7);
glColor3f(1, 1, 1);
glVertex2f(-1.5, 0.7);
glColor3f(0.658824, 0.658824, 0.658824);
glVertex2f( 1.5, 0.7);
glColor3f(1, 1, 1);
glVertex2f( 1.5,-1.7);
glEnd();
glPopMatrix();
glFlush();
// Top of the first Tree //
glPushMatrix();
glTranslatef(-0.6, 0.5, 0);
glBegin(GL_TRIANGLES); //Defines the shape as being a triangle
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.1);
glEnd();
glPopMatrix();
// Middle of the first tree
glPushMatrix();
glTranslatef(-0.6, 0.4, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.1);
glEnd();
glPopMatrix();
// Bottom of the first tree
glPushMatrix();
glTranslatef(-0.6, 0.3, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.1);
glEnd();
glPopMatrix();
glFlush();
//Stump of first tree
glPushMatrix();
glTranslatef(-0.6, 0.16, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02,-0.04);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02, 0.04);
glColor3f( 0.647059, 0.164706, 0.164706);
glVertex2f( 0.02, 0.04);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.02,-0.04);
glEnd();
glPopMatrix();
// Large Tree TOP
glPushMatrix();
glTranslatef(-0.2, 0, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.15);
glEnd();
glPopMatrix();
glFlush();
//Large Tree MIDDLE
glPushMatrix();
glTranslatef(-0.2, -0.15, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);;
glVertex2f(0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.15);
glEnd();
glPopMatrix();
glFlush();
//Large Tree Bottom
glPushMatrix();
glTranslatef(-0.2, -0.30, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.15);
glEnd();
glPopMatrix();
glFlush();
//Smaller tree Top
glPushMatrix();
glTranslatef(0.05, 0.45, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.05);
glEnd();
glPopMatrix();
glFlush();
//Smaller tree MIDDLE
glPushMatrix();
glTranslatef(0.05, 0.40, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.05);
glEnd();
glPopMatrix();
glFlush();
//smaller tree bottom
glPushMatrix();
glTranslatef(0.05, 0.50, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.05, -0.05);
glColor3f(0.32, 0.49, 0.46);
glVertex2f(0.0, 0.05);
glEnd();
glPopMatrix();
glFlush();
//Stump of smaller tree
glPushMatrix();
glTranslatef(0.05, 0.32, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.01,-0.03);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.01, 0.03);
glColor3f( 0.647059, 0.164706, 0.164706);
glVertex2f( 0.01, 0.03);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.01,-0.03);
glEnd();
glPopMatrix();
//Stump of MAIN tree
glPushMatrix();
glTranslatef(-0.2, -0.50, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02,-0.05);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02, 0.05);
glColor3f( 0.647059, 0.164706, 0.164706);
glVertex2f( 0.02, 0.05);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.02,-0.05);
glEnd();
glPopMatrix();
// Red Present
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_POLYGON);
glColor3f( 1, 0, 1);
glVertex2f(-0.04,-0.05);
glColor3f( 1, 0, 0);
glVertex2f(-0.04, 0.05);
glColor3f( 1, 0, 0);
glVertex2f( 0.04, 0.05);
glColor3f( 1, 0, 0);
glVertex2f( 0.04,-0.05);
glEnd();
glPopMatrix();
//Blue Present
glPushMatrix();
glTranslatef(-0.2, -0.7, 0);
glBegin(GL_POLYGON);
glColor3f( 0, 0, 1);
glVertex2f(-0.07,-0.06);
glColor3f( 0, 0, 1);
glVertex2f(-0.07, 0.06);
glColor3f( 0, 0, 1);
glVertex2f( 0.07, 0.06);
glColor3f( 0.196078, 0.6, 0.8);
glVertex2f( 0.07,-0.06);
glEnd();
glPopMatrix();
// BLUE Ribbon RED present VERT
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_POLYGON);
glColor3f( 0, 0, 1);
glVertex2f(-0.04,-0.01);
glColor3f( 0, 0, 1);
glVertex2f(-0.04, 0.01);
glColor3f( 0, 0, 1);
glVertex2f( 0.04, 0.01);
glColor3f( 0, 0, 1);
glVertex2f( 0.04,-0.01);
glEnd();
glPopMatrix();
//BLUE ribbon RED present HORIZ
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_POLYGON);
glColor3f( 0, 0, 1);
glVertex2f(-0.01,-0.05);
glColor3f( 0, 0, 1);
glVertex2f(-0.01, 0.05);
glColor3f( 0, 0, 1);
glVertex2f( 0.01, 0.05);
glColor3f( 0, 0, 1);
glVertex2f( 0.01,-0.05);
glEnd();
glPopMatrix();
//Yellow Ribbon Blue Present VERT
glPushMatrix();
glTranslatef(-0.2, -0.7, 0);
glBegin(GL_POLYGON);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.07,-0.01);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.07, 0.01);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.07, 0.01);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.07,-0.01);
glEnd();
glPopMatrix();
// BLUE present YELLOW ribbon VERT
glPushMatrix();
glTranslatef(-0.2, -0.7, 0);
glBegin(GL_POLYGON);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.01,-0.06);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.01, 0.06);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.01, 0.06);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.01,-0.06);
glEnd();
glPopMatrix();
//Sign Post
glPushMatrix();
glTranslatef(0.5, -0.1, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02,-0.25);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02, 0.25);
glColor3f( 0.35, 0.16, 0.14);
glVertex2f( 0.02, 0.25);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.02,-0.25);
glEnd();
glPopMatrix();
//Sign, Attatched to the post
glPushMatrix();
glTranslatef(0.5, -0.001, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.15,-0.10);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.15, 0.10);
glColor3f( 0.35, 0.16, 0.14);
glVertex2f( 0.15, 0.10);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.15,-0.10);
glEnd();
glPopMatrix();
//Moon
glPushMatrix();
glTranslatef(-0.9, 0.90, 0);
glBegin(GL_POLYGON);
glColor4f( 0.90, 0.91, 0.98, 1); //RGBA
glVertex2f(-0.10,-0.2);
glColor4f( 0.329412, 0.329412, 0.329412, 1);
glVertex2f(-0.10, 0.2);
glColor4f( 0.90, 0.91, 0.98, 1);
glVertex2f( 0.10, 0.2);
glColor4f( 0.90, 0.91, 0.98, 1);
glVertex2f( 0.10,-0.2);
glEnd();
glPopMatrix();
//MAIN PRESENT UNDER SIGN
glPushMatrix();
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.89, 0.47, 0.20);
glVertex2f(-0.20,-0.20);
glColor3f( 0.89, 0.47, 0.20);
glVertex2f(-0.20, 0.20);
glColor3f( 0.89, 0.47, 0.20);
glVertex2f( 0.20, 0.20);
glColor3f( 1.0, 0.25, 0);
glVertex2f( 0.20,-0.20);
glEnd();
glPopMatrix();
//Orange Present Purple Ribbon VERT
glPushMatrix();
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f(-0.20,-0.06);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f(-0.20, 0.06);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f( 0.20, 0.06);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f( 0.20,-0.06);
glEnd();
glPopMatrix();
//Orange Present Purple Ribbon HORIZ
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06,-0.20);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06,-0.20);
glEnd();
glPopMatrix();
glFlush();
//'North Pole' TEXT sign
glPushMatrix();
glLoadIdentity();
glTranslatef(0.360, -0.010, 0);
glRotatef(90,0.0f,0.0f,0.0f);
glColor3f( 0.0, 0.0, 0.0 ); //Colour is black
glRasterPos3i(10,100,1);
char text[50]="North Pole"; //Text
for(int i=0; i<50; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,(int)text[i]);
}
glPopMatrix();
glutSwapBuffers();
}
#包括“stdafx.h”
#包括
无效渲染(无效);
无效键盘(int键,int x,int y);
int main(int argc,字符**argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_深度| GLUT_双精度| GLUT_RGBA);
glutInitWindowPosition(100100);//窗口的位置
GLUTINITWindowsSize(620440);//屏幕大小
glClearColor(0.0,1.0,0.0,0.0);
glutCreateWindow(“贺卡”);//创建窗口并命名
glEnable(GL_混合物);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_减去_SRC_ALPHA);//启用ALPHA通道
glutDisplayFunc(渲染);
glutDisplayFunc(draw);
键盘Func(键盘);
glutMainLoop();//已完成,现在渲染
}
无效键盘(无符号字符键,整数x,整数y)
{
GLfloat colors[][3]={{0.0f,0.0f,1.0f},{1.0f,0.0f,0.0f};
静态int-back;
开关(钥匙){
案例27:
出口(0);
违约:
背面^=1;
glClearColor(颜色[背面][0],颜色[背面][1],颜色[背面][2],1.0f);
再发现();
}
}
无效渲染(无效){
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
//世界雪//
glPushMatrix();
glTranslatef(0,-0.35,0);//形状的位置
glBegin(GL_POLYGON);//定义形状的类型
glColor3f(1,1,1);//形状“红、绿、蓝”的颜色
glVertex2f(-1.5,-0.7);//顶点2F为顶点提供一些坐标
gl3f(1,1,1);
glVertex2f(-1.5,0.7);
gl3f(1,1,1);
glVertex2f(1.5,0.7);
gl3f(1,1,1);
glVertex2f(1.5,-0.7);
格伦德();
glPopMatrix();
glFlush();
//灰色梯度世界
glPushMatrix();
glTranslatef(0,-0.35,0);
glBegin(GL_多边形);
gl3f(1,1,1);
glVertex2f(-1.5,-0.7);
gl3f(1,1,1);
glVertex2f(-1.5,0.7);
glColor3f(0.658824,0.658824,0.658824);
glVertex2f(1.5,0.7);
gl3f(1,1,1);
glVertex2f(1.5,-1.7);
格伦德();
glPopMatrix();
glFlush();
//第一棵树的顶端//
glPushMatrix();
glTranslatef(-0.6,0.5,0);
glBegin(GL_三角形);//将形状定义为三角形
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(-0.1,-0.1);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.1,-0.1);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.0,0.1);
格伦德();
glPopMatrix();
//第一棵树的中间
glPushMatrix();
glTranslatef(-0.6,0.4,0);
glBegin(GL_三角形);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(-0.1,-0.1);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.1,-0.1);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.0,0.1);
格伦德();
glPopMatrix();
//第一棵树的底部
glPushMatrix();
glTranslatef(-0.6,0.3,0);
glBegin(GL_三角形);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(-0.1,-0.1);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.1,-0.1);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.0,0.1);
格伦德();
glPopMatrix();
glFlush();
//第一棵树的树桩
glPushMatrix();
glTranslatef(-0.6,0.16,0);
glBegin(GL_多边形);
GL3F(0.36,0.25,0.20);
glVertex2f(-0.02,-0.04);
GL3F(0.36,0.25,0.20);
glVertex2f(-0.02,0.04);
glColor3f(0.647059,0.164706,0.164706);
glVertex2f(0.02,0.04);
GL3F(0.36,0.25,0.20);
glVertex2f(0.02,-0.04);
格伦德();
glPopMatrix();
//大树梢
glPushMatrix();
glTranslatef(-0.2,0,0);
glBegin(GL_三角形);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(-0.15,-0.15);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.15,-0.15);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.0,0.15);
格伦德();
glPopMatrix();
glFlush();
//大树中
glPushMatrix();
glTranslatef(-0.2,-0.15,0);
glBegin(GL_三角形);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(-0.15,-0.15);
glColor3f(0.137255,0.556863,0.137255);;
glVertex2f(0.15,-0.15);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.0,0.15);
格伦德();
glPopMatrix();
glFlush();
//大树底
glPushMatrix();
glTranslatef(-0.2,-0.30,0);
glBegin(GL_三角形);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(-0.15,-0.15);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.15,-0.15);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.0,0.15);
格伦德();
glPopMatrix();
glFlush();
//小树梢
glPushMatrix();
glTranslatef(0.05,0.45,0);
glBegin(GL_三角形);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(-0.05,-0.05);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.05,-0.05);
glColor3f(0.137255,0.556863,0.137255);
glVertex2f(0.0,0.05);
格伦德();
glPopMatrix();
glFlush();
//小树中部
glPushMatrix();
glTranslatef(0.05,0.40,0);
glBegin(GL_三角形);
glColor3f(0.137255,0.556863,0.137
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
...
//Orange Present Purple Ribbon HORIZ
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06,-0.20);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06,-0.20);
glEnd();
glPopMatrix();
^^^^^^^^^^^^^ wha-hey?