#include<glew.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <Windows.h>
#include<wingdi.h>
#include <glut.h>
#include<glaux.h>
#pragma comment(lib, "glew32.lib")
#pragma comment( lib, "glaux.lib ")
float eye[3] = {0, 0, 0};
void Kongjian()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,400/400,1,10);
}
void guangzhao()
{
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
float huanjin[]={0.3,0.3,0.3,1};
float sanshe[]={0.7,0.7,0.7,1};
float weizhi[]={1,1,1,0};
float jinmian[]={1,1,1,1};
float fanshelv[]={1,1,1,1};
glLightfv(GL_LIGHT0,GL_POSITION,weizhi);
glLightfv(GL_LIGHT0,GL_AMBIENT,huanjin);
glLightfv(GL_LIGHT0,GL_DIFFUSE,sanshe);
glLightfv(GL_LIGHT0,GL_SPECULAR,jinmian);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT,GL_SPECULAR,fanshelv);
glMateriali(GL_FRONT,GL_SHININESS,120);
}
void key(unsigned char k, int x, int y)
{
switch(k)
{
case 'a': {
eye[0]+=1;
break;
}
case 'd': {
eye[0]-=1;
break;
}
case 'w': {
eye[1]-=1;
break;
}
case 's': {
eye[1]+=1;
break;
}
case 'q':
{
eye[2]=eye[2]+1;break;
}
case 'e':
{
eye[2]=eye[2]-1;break;
}
}
}
void redraw()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2],0, 0, -100,0, 1, 0);
guangzhao() ;
glTranslated(0,0,-9);
glColor3f(0.5,0.5,0.5);
glutSolidTeapot(1);
glutSwapBuffers();
}
void idle()
{
glutPostRedisplay();//达到循环的效果
}
int main (int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(400,400);
int windowHandle = glutCreateWindow("键盘控制视角变换");
Kongjian();
glutDisplayFunc(redraw);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
如果是学图形就学opengl,qt是一个c++的gui,和mfc是类似的
// Triangle.cpp
// OpenGL SuperBible
// Demonstrates OpenGL Triangle Fans, backface culling, and depth testing
// Program by Richard S. Wright Jr.
#include "../../shared/gltools.h" // OpenGL toolkit
#include <math.h>
// Define a constant for the value of PI
#define GL_PI 3.1415f
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// Flags for effects
// Flags for effects
int iCull = 0;
int iOutline = 0;
int iDepth = 0;
///////////////////////////////////////////////////////////////////////////////
// Reset flags as appropriate in response to menu selections
void ProcessMenu(int value)
{
switch(value)
{
case 1:
iDepth = !iDepth;
break;
case 2:
iCull = !iCull;
break;
case 3:
iOutline = !iOutline;
default:
break;
}
glutPostRedisplay();
}
// Called to draw scene
void RenderScene(void)
{
GLfloat x,y,angle; // Storage for coordinates and angles
int iPivot = 1; // Used to flag alternating colors
// Clear the window and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Turn culling on if flag is set
if(iCull)
glEnable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE);
// Enable depth testing if flag is set
if(iDepth)
glEnable(GL_DEPTH_TEST);
else
glDisable(GL_DEPTH_TEST);
// Draw back side as a polygon only, if flag is set
if(iOutline)
glPolygonMode(GL_BACK,GL_LINE);//多边形模式
else
glPolygonMode(GL_BACK,GL_FILL);
// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
// Begin a triangle fan
glBegin(GL_TRIANGLE_FAN);
// Pinnacle of cone is shared vertex for fan, moved up Z axis
// to produce a cone instead of a circle
glVertex3f(0.0f, 0.0f, 75.0f);
// Loop around in a circle and specify even points along the circle
// as the vertices of the triangle fan
for(angle = 0.0f; angle < (2.0f*GL_PI); angle += (GL_PI/8.0f))
{
// Calculate x and y position of the next vertex
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
// Alternate color between red and green
if((iPivot %2) == 0)
glColor3f(0.0f, 1.0f, 0.0f);
else
glColor3f(1.0f, 0.0f, 0.0f);
// Increment pivot to change color next time
iPivot++;
// Specify the next vertex for the triangle fan
glVertex2f(x, y);
}
// Done drawing fan for cone
glEnd();
// Begin a new triangle fan to cover the bottom
glBegin(GL_TRIANGLE_FAN);
// Center of fan is at the origin
glVertex2f(0.0f, 0.0f);
for(angle = 0.0f; angle < (2.0f*GL_PI); angle += (GL_PI/8.0f))
{
// Calculate x and y position of the next vertex
x = 50.0f*sin(angle);
y = 50.0f*cos(angle);
// Alternate color between red and green
if((iPivot %2) == 0)
glColor3f(0.0f, 1.0f, 0.0f);
else
glColor3f(1.0f, 0.0f, 0.0f);
// Increment pivot to change color next time
iPivot++;
// Specify the next vertex for the triangle fan
glVertex2f(x, y);
}
// Done drawing the fan that covers the bottom
glEnd();
// Restore transformations
glPopMatrix();
glutSwapBuffers();
}
// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
// Set drawing color to green
glColor3f(0.0f, 1.0f, 0.0f);
// Set color shading model to flat
glShadeModel(GL_FLAT);/*void glShadeModel ( GLenum mode);
设置着色模式。参数mode可以是GL_SMOOTH(默认值)或GL_FLAT。*/
// Clock wise wound polygons are front facing, this is reversed
// because we are using triangle fans
glFrontFace(GL_CW);
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void ChangeSize(int w, int h)
{
GLfloat nRange = 100.0f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Triangle Culling Example");
// Create the Menu
glutCreateMenu(ProcessMenu);
glutAddMenuEntry("Toggle depth test",1);
glutAddMenuEntry("Toggle cull backface",2);//
glutAddMenuEntry("Toggle outline back",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
Photoshop|
Dreamweaver|
SVG|
WebGL|
Visual Studio|
PowerDesigner|
Eclipse|
Git|
Apache Ant|
Atom|
Composer|
CodeSmith|
Flex|
Gradle|
Maven|
Sublime Text3|
SVN|
Tableau|
Vim|
Chrome开发者工具|
OpenGL|
Unity|
Direct3D|
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....