// OpenGL_Test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <glew.h>
#include "glut.h"
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string>
#include "png.h"
#include "zlib.h"
#include <glfw3.h>
#pragma comment(lib, "glut.lib")
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "libpng16.lib")
#pragma comment(lib, "zlib.lib")
#pragma comment (lib,"glew32d.lib")
#pragma comment (lib,"glfw3.lib")
#pragma comment (lib,"glfw3dll.lib")
///* Makes pngtest verbose so we can find problems. */
//#ifndef PNG_DEBUG
//# define PNG_DEBUG 2
//#endif
//
//#if PNG_DEBUG > 1
//# define pngtest_debug(m) ((void)fprintf(stderr, m "\n"))
//# define pngtest_debug1(m,p1) ((void)fprintf(stderr, m "\n", p1))
//# define pngtest_debug2(m,p1,p2) ((void)fprintf(stderr, m "\n", p1, p2))
//#else
//# define pngtest_debug(m) ((void)0)
//# define pngtest_debug1(m,p1) ((void)0)
//# define pngtest_debug2(m,p1,p2) ((void)0)
//#endif
//
//unsigned char* buffer = NULL;
//png_uint_32 width, height;
//int color_type;
//
获取每一行所用的字节数,需要凑足4的倍数
//int getRowBytes(int width){
// //刚好是4的倍数
// if ((width * 3) % 4 == 0){
// return width * 3;
// }
// else{
// return ((width * 3) / 4 + 1) * 4;
// }
//}
//
//
//void readpng__001(char* name)
//
//{
//
// // 前边几句是扯淡,初始化各种结构
//
// FILE* file = fopen(name, "rb");
//
// png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
//
// png_infop info_ptr = png_create_info_struct(png_ptr);
//
// setjmp(png_jmpbuf(png_ptr));
//
// // 这句很重要
//
// png_init_io(png_ptr, file);
//
// // 读文件了
//
// png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND, 0);
//
// // 得到文件的宽高色深
//
// int m_width = png_get_image_width(PngPtr, InfoPtr);
// int m_height = png_get_image_height(PngPtr, InfoPtr);
//
// int color_type = png_get_color_type(PngPtr, InfoPtr);
//
// // 申请个内存玩玩,这里用的是c++语法,甭想再c上编过
//
// int size = m_height * m_width * 4;
//
// unsigned char* bgra = new unsigned char[size];
//
// int pos = 0;
//
// // row_pointers里边就是传说中的rgba数据了
//
// png_bytep* row_pointers = png_get_rows(PngPtr, InfoPtr);
//
// // 拷贝!!注意,如果你读取的png没有A通道,就要3位3位的读。还有就是注意字节对其的问题,最简单的就是别用不能被4整除的宽度就行了。读过你实在想用,就要在这里加上相关的对齐处理。
//
// for (int i = 0; i < m_height; i++)
// {
// for (int j = 0; j < (4 * m_width); j += 4)
// {
// bgra[pos++] = row_pointers[i][j + 2]; // blue
// bgra[pos++] = row_pointers[i][j + 1]; // green
// bgra[pos++] = row_pointers[i][j]; // red
// bgra[pos++] = row_pointers[i][j + 3]; // alpha
// }
// }
//
// // 好了,你可以用这个数据作任何的事情了。。。把它显示出来或者打印出来都行。
//
// png_destroy_read_struct(&png_ptr, &info_ptr, 0);
//
// fclose(file);
//
// return;
//
//}
//
//
//int main(int c, char** v) {
//
// png_structp png_ptr;
// png_infop info_ptr;
// int bit_depth;
// FILE *fp;
//
// printf("lpng[%s], zlib[%s]\n", PNG_LIBPNG_VER_STRING, ZLIB_VERSION);
//
// if ((fp = fopen("1.png", "rb")) == NULL) {
// return EXIT_FAILURE;
// }
// png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
// if (png_ptr == NULL)
// {
// fclose(fp);
// return EXIT_FAILURE;
// }
// info_ptr = png_create_info_struct(png_ptr);
// if (info_ptr == NULL)
// {
// fclose(fp);
// png_destroy_read_struct(&png_ptr, NULL, NULL);
// return EXIT_FAILURE;
// }
// if (setjmp(png_jmpbuf(png_ptr))) {
// /* Free all of the memory associated with the png_ptr and info_ptr */
// png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
// fclose(fp);
// /* If we get here, we had a problem reading the file */
// return EXIT_FAILURE;
// }
// /* Set up the input control if you are using standard C streams */
// png_init_io(png_ptr, fp);
// //读取png文件
// png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND, 0);
// //获取png图片相关信息
// png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
// printf("width[%d], height[%d], bit_depth[%d], color_type[%d]\n",
// width, height, bit_depth, color_type);
//
// //获得所有png数据
// png_bytep* row_pointers = png_get_rows(png_ptr, info_ptr);
// //计算buffer大小
// unsigned int bufSize = 0;
// if (color_type == PNG_COLOR_TYPE_RGB) {
// bufSize = getRowBytes(width) * height;
// }
// else if (color_type == PNG_COLOR_TYPE_RGBA) {
// bufSize = width * height * 4;
// }
// else{
// return EXIT_FAILURE;
// }
// //申请堆空间
// buffer = (unsigned char*)malloc(bufSize);
// int i;
// for (i = 0; i < height; i++) {
// //拷贝每行的数据到buffer,
// //opengl原点在下方,拷贝时要倒置一下
// if (color_type == PNG_COLOR_TYPE_RGB){
// memcpy(buffer + getRowBytes(width) * i, row_pointers[height - i - 1], width * 3);
// }
// else if (color_type == PNG_COLOR_TYPE_RGBA){
// memcpy(buffer + i * width * 4, row_pointers[height - i - 1], width * 4);
// }
// }
// png_destroy_read_struct(&png_ptr, &info_ptr, 0);
// fclose(fp);
// return 0;
//}
//int window_width = 600;
//int window_height = 600;
//
//int pic_width;
//int pic_height;
//int size;
//
//GLuint texName;
//unsigned char *content;
//
//void init()
//{
// //glewInit();
//
// glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// glGenTextures(1, &texName);
// glBindTexture(GL_TEXTURE_2D, texName);
//
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pic_width, pic_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, content);
//}
//
//void display_func()
//{
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// gluOrtho2D(0.0, 1.0f, 0.0, 1.0f);
//
// glViewport(0, 0, window_width, window_height);
// glClearColor(1.0f, 1.0f, 1.0f, 1.0);
// glClear(GL_COLOR_BUFFER_BIT);
//
// glEnable(GL_TEXTURE_2D);
// glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// glBindTexture(GL_TEXTURE_2D, texName);
// //glTranslated(0.2f, 0.0f, 0.2f);
// glRotated(30.0f, 0.2f, 0.0f, 0.0f);
//
// glBegin(GL_QUADS);
// glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 0.0f);
// glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 0.0f);
// glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, 1.0f);
// glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 1.0f);
//
// glEnd();
// glutSwapBuffers();
//}
//
//void reshape_func(int width, int height)
//{
// window_width = width;
// window_height = height;
// glutReshapeWindow(window_width, window_height);
//}
//
//int main(int argc, char **argv)
//{
// FILE* file = fopen("1.png", "rb");
// png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
// png_infop info_ptr = png_create_info_struct(png_ptr);
// setjmp(png_jmpbuf(png_ptr));
// png_init_io(png_ptr, file);
// png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND, 0);
//
// pic_width = png_get_image_width(png_ptr, info_ptr);
// pic_height = png_get_image_height(png_ptr, info_ptr);
// int color_type = png_get_color_type(png_ptr, info_ptr);
//
// size = pic_height * pic_width * 4;
// int pos = 0;
// content = (unsigned char *)malloc(sizeof(unsigned char)*size);
//
// png_bytep* row_pointers = png_get_rows(png_ptr, info_ptr);
//
// for (int i = 0; i < pic_height; i++)
// {
// for (int j = 0; j < pic_width; j++)
// {
// content[i*pic_width * 4 + j * 4 + 0] = row_pointers[i][j * 4 + 0];
// content[i*pic_width * 4 + j * 4 + 1] = row_pointers[i][j * 4 + 1];
// content[i*pic_width * 4 + j * 4 + 2] = row_pointers[i][j * 4 + 2];
// content[i*pic_width * 4 + j * 4 + 3] = row_pointers[i][j * 4 + 3];
// }
// }
//
// png_destroy_read_struct(&png_ptr, &info_ptr, 0);
// fclose(file);
//
// window_width = pic_width;
// window_height = pic_height;
//
// glutInit(&argc, argv);
// glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
// glutInitWindowSize(window_width, window_height);
// glutCreateWindow("Picture");
//
// init();
//
// glutDisplayFunc(display_func);
// glutReshapeFunc(reshape_func);
//
// glutMainLoop();
//
// free(content);
//
// return 0;
//}
// OGLTtest.cpp : 定义控制台应用程序的入口点。
//
//#define GLEW_STATIC
//
//
//class gl_texture_t{
//public:
// GLsizei width;
// GLsizei height;
// GLenum format;
// GLint internalFormat;
// GLuint id;
// GLubyte *texels;
//};
///* Texture id for the demo */
//gl_texture_t *ReadPNGFromFile(const char *filename);
//GLuint loadPNGTexture(const char *filename);
//void GetPNGtextureInfo(int color_type, gl_texture_t *texinfo);
//
//using namespace std;
//
//unsigned int LoadTex(string Image)
//{
// unsigned int Texture = 0;
//
// FILE* img = NULL;
// fopen_s(&img, Image.c_str(), "rb");
//
// unsigned long bWidth = 0;
// unsigned long bHeight = 0;
// unsigned long size = 0;
//
// fseek(img, 18, SEEK_SET);
// fread(&bWidth, 4, 1, img);
// fread(&bHeight, 4, 1, img);
// fseek(img, 0, SEEK_END);
// size = ftell(img) - 54;
//
// unsigned char *data = (unsigned char*)malloc(size);
// fseek(img, 54, SEEK_SET); // image data
// fread(data, size, 1, img);
//
// fclose(img);
//
// unsigned int t2 = 0;
// glGenTextures(1, &Texture);
// glGenTextures(1, &t2);
// glBindTexture(GL_TEXTURE_2D, Texture);
// glTexImage2D(GL_TEXTURE_2D, 0, 3, bWidth, bHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//
// if (data)
// free(data);
//
// return Texture;
//}
//
//int main(void)
//{
//
// GLFWwindow* window;
//
// /* Initialize the library */
// if (!glfwInit())
// return -1;
//
// /* Create a windowed mode window and its OpenGL context */
// window = glfwCreateWindow(480, 640, "Hello World", NULL, NULL);
// if (!window)
// {
// glfwTerminate();
// return -1;
// }
//
//
// /* Make the window's context current */
// glfwMakeContextCurrent(window);
//
// //unsigned int img = LoadTex("d:\\b0.bmp");
// unsigned int img = loadPNGTexture("1.png");
// unsigned int mask_img = LoadTex("1.bmp");
// //unsigned int tid = ATLLoadTexture(L"d:\\b0.png");
// /* Loop until the user closes the window */
// int a = 10;
// while (!glfwWindowShouldClose(window))
// {
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// glClearColor(0.0, 0.8, 0.0, 0.5);
//
// glEnable(GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//
// //glTranslated(0.2,0.2,0);
// //glRotated(a, 0.2, 0.2, 0.2);
//
// //a += 10;
//
// glEnable(GL_STENCIL_TEST);
// glDepthMask(GL_FALSE);
//
// //根据掩码图设置模板缓冲区的值
// glStencilFunc(GL_NEVER, 1, 1);
// glStencilOp(GL_REPLACE , GL_KEEP, GL_KEEP);
//
// glEnable(GL_TEXTURE_2D);
// glBindTexture(GL_TEXTURE_2D, mask_img);
// glBegin(GL_QUADS);
// glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
// glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
// glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// glEnd();
//
// glStencilFunc(GL_EQUAL, 1, 0xff);
// glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
//
//
//
// //glEnable(GL_ALPHA_TEST);//非必须
// //glAlphaFunc(GL_GREATER, 0.1);
//
// glEnable(GL_TEXTURE_2D);
// glBindTexture(GL_TEXTURE_2D, img);
// glBegin(GL_QUADS);
// glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 1.0f);
// glTexCoord2f(1.0f, 0.0f); glVertex3f(.5f, -.5f, 1.0f);
// glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5f, 0.5f, 1.0f);
// glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, 1.0f);
// glEnd();
//
//
// /* Swap front and back buffers */
// glfwSwapBuffers(window);
//
// /* Poll for and process events */
// glfwPollEvents();
// Sleep(200);
// }
//
// glfwTerminate();
// return 0;
//}
//
以下函数引用别人的。
//gl_texture_t * ReadPNGFromFile(const char *filename)
//{
// gl_texture_t *texinfo;
// png_byte magic[8];
// png_structp png_ptr;
// png_infop info_ptr;
// int bit_depth, color_type;
// FILE *fp = NULL;
// png_bytep *row_pointers = NULL;
// png_uint_32 w, h;
// int i;
// /* Open image file */
// fopen_s(&fp, filename, "rb");
// if (!fp)
// {
// fprintf(stderr, "error: couldn't open \"%s\"!\n", filename);
// return NULL;
// }
// /* Read magic number */
// fread(magic, 1, sizeof(magic), fp);
// /* Check for valid magic number */
// if (!png_check_sig(magic, sizeof(magic)))
// {
// fprintf(stderr, "error: \"%s\" is not a valid PNG image!\n", filename);
// fclose(fp);
// return NULL;
// }
// /* Create a png read struct */
// png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
// if (!png_ptr)
// {
// fclose(fp);
// return NULL;
// }
// /* Create a png info struct */
// info_ptr = png_create_info_struct(png_ptr);
// if (!info_ptr)
// {
// fclose(fp);
// png_destroy_read_struct(&png_ptr, NULL, NULL);
// return NULL;
// }
// /* Create our OpenGL texture object */
// texinfo = (gl_texture_t *)malloc(sizeof(gl_texture_t));
// /* Initialize the setjmp for returning properly after a libpng error occured */
// if (setjmp(png_jmpbuf(png_ptr)))
// {
// fclose(fp);
// png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
// if (row_pointers) free(row_pointers);
// if (texinfo) {
// if (texinfo->texels)
// free(texinfo->texels);
// free(texinfo);
// }
// return NULL;
// }
// /* Setup libpng for using standard C fread() function with our FILE pointer */
// png_init_io(png_ptr, fp);
// /* Tell libpng that we have already read the magic number */
// png_set_sig_bytes(png_ptr, sizeof(magic));
// /* Read png info */
// png_read_info(png_ptr, info_ptr);
// /* Get some usefull information from header */
// bit_depth = png_get_bit_depth(png_ptr, info_ptr);
// color_type = png_get_color_type(png_ptr, info_ptr);
// /* Convert index color images to RGB images */
// if (color_type == PNG_COLOR_TYPE_PALETTE)
// png_set_palette_to_rgb(png_ptr);
// /* Convert 1-2-4 bits grayscale images to 8 bits grayscale. */
// if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
// png_set_expand_gray_1_2_4_to_8(png_ptr);
//
// if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
// png_set_tRNS_to_alpha(png_ptr);
// if (bit_depth == 16) png_set_strip_16(png_ptr);
// else if (bit_depth < 8) png_set_packing(png_ptr);
// /* Update info structure to apply transformations */
// png_read_update_info(png_ptr, info_ptr);
// /* Retrieve updated information */
// png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, NULL, NULL, NULL);
// texinfo->width = w;
// texinfo->height = h;
// /* Get image format and components per pixel */
// GetPNGtextureInfo(color_type, texinfo);
// /* We can now allocate memory for storing pixel data */
// texinfo->texels = (GLubyte *)malloc(sizeof(GLubyte) * texinfo->width * texinfo->height * texinfo->internalFormat);
// /* Setup a pointer array. Each one points at the begening of a row. */
// row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * texinfo->height);
// for (i = 0; i < texinfo->height; ++i)
// {
// row_pointers[i] = (png_bytep)(texinfo->texels + ((texinfo->height - (i + 1)) * texinfo->width * texinfo->internalFormat));
// }
// /* Read pixel data using row pointers */
// png_read_image(png_ptr, row_pointers);
// /* Finish decompression and release memory */
// png_read_end(png_ptr, NULL);
// png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
// /* We don't need row pointers anymore */
// free(row_pointers);
// fclose(fp);
// return texinfo;
//}
//GLuint loadPNGTexture(const char *filename)
//{
// gl_texture_t *png_tex = NULL;
// GLuint tex_id = 0;
// GLint alignment;
// png_tex = ReadPNGFromFile(filename);
// if (png_tex && png_tex->texels)
// {
// /* Generate texture */
// glGenTextures(1, &png_tex->id);
// glBindTexture(GL_TEXTURE_2D, png_tex->id);
// /* Setup some parameters for texture filters and mipmapping */
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//
// glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
// glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// glTexImage2D(GL_TEXTURE_2D, 0, png_tex->internalFormat, png_tex->width, png_tex->height, 0, png_tex->format, GL_UNSIGNED_BYTE, png_tex->texels);
// glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
// tex_id = png_tex->id;
// /* OpenGL has its own copy of texture data */
// free(png_tex->texels);
// free(png_tex);
// }
// return tex_id;
//}
//
//void GetPNGtextureInfo(int color_type, gl_texture_t *texinfo)
//{
// switch (color_type)
// {
// case PNG_COLOR_TYPE_GRAY:
// texinfo->format = GL_LUMINANCE;
// texinfo->internalFormat = 1;
// break;
//
// case PNG_COLOR_TYPE_GRAY_ALPHA:
// texinfo->format = GL_LUMINANCE_ALPHA;
// texinfo->internalFormat = 2;
// break;
//
// case PNG_COLOR_TYPE_RGB:
// texinfo->format = GL_RGB;
// texinfo->internalFormat = 3;
// break;
//
// case PNG_COLOR_TYPE_RGB_ALPHA:
// texinfo->format = GL_RGBA;
// texinfo->internalFormat = 4;
// break;
//
// default:
// /* Badness */
// break;
// }
//}
//void init(void)
//{
// glClearColor(0.0, 0.0, 0.0, 0.0); //背景黑色
//}
//
//void display(void)
//{
// glClear(GL_COLOR_BUFFER_BIT);
// glColor3f(1.0, 0.0, 1.0); //画笔白色
//
// glLoadIdentity(); //加载单位矩阵
//
// gluLookAt(0.0, 0.0, 6.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0);
// glutWireTeapot(2);
// glutSwapBuffers();
//}
//
//void reshape(int w, int h)
//{
// glViewport(0, 0, (GLsizei)w, (GLsizei)h);
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
// gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//}
//int main(int argc, char** argv)
//{
// glutInit(&argc, argv);
// glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
// glutInitWindowSize(500, 500);
// glutInitWindowPosition(100, 100);
// glutCreateWindow(argv[0]);
// init();
// glutDisplayFunc(display);
// glutReshapeFunc(reshape);
// glutMainLoop();
// return 0;
//}
// Initialize material property, light source, lighting model, * and depth buffer.
//void init(void)
//
//{
//
// GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
//
// GLfloat mat_shininess[] = { 50.0 };
//
// GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
//
// GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
//
// GLfloat Light_Model_Ambient[] = { 0.2, 0.2, 0.2, 1.0 }; //
//
// glClearColor(0.0, 0.0, 0.0, 0.0);
//
// glShadeModel(GL_SMOOTH);
//
// glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
//
// glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
//
// glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//
// glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
//
// glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
//
// glLightModelfv(GL_LIGHT_MODEL_AMBIENT, Light_Model_Ambient); //
//
// glEnable(GL_LIGHTING);
//
// glEnable(GL_LIGHT0);
//
// glEnable(GL_DEPTH_TEST);
//
//}
//
//void display(void)
//
//{
//
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
// //glutSolidSphere (1.0, 20, 16);
//
// glutSolidTeapot(0.2);
//
// glFlush();
//
//}
//
//void reshape(int w, int h)
//
//{
// glViewport(0, 0, (GLsizei)w, (GLsizei)h);
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
//
// if (w <= h)
// glOrtho(-1.5, 1.5, -1.5*(GLfloat)h / (GLfloat)w, 1.5*(GLfloat)h / (GLfloat)w, -10.0, 10.0);
// else
// glOrtho(-1.5*(GLfloat)w / (GLfloat)h, 1.5*(GLfloat)w / (GLfloat)h, -1.5, 1.5, -10.0, 10.0);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
//
//}
//
//int main(int argc, char** argv)
//
//{
// glutInit(&argc, argv);
//
// glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
//
// glutInitWindowSize(500, 500);
//
// glutInitWindowPosition(100, 100);
//
// glutCreateWindow(argv[0]);
//
// init();
//
// glutDisplayFunc(display);
//
// glutReshapeFunc(reshape);
//
// glutMainLoop();
//
// return 0;
//
//}
//#define SOLID 1
//#define WIRE 2
//
//int moveX, moveY;
//int spinX = 0;
//int spinY = 0;
//int des = 0;
//
//
//void init() {
// //定义光源的颜色和位置
// GLfloat ambient[] = { 0.5, 0.8, 0.1, 0.1 };
// GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
// GLfloat position[] = { -80.0, 50.0, 25.0, 1.0 };
// //选择光照模型
// GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
// GLfloat local_view[] = { 0.0 };
// glClearColor(0.0, 0.0, 0.0, 0.0);
// glShadeModel(GL_SMOOTH);
// //设置环境光
// glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
// //设置漫射光
// glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
// //设置光源位置
// glLightfv(GL_LIGHT0, GL_POSITION, position);
// glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
// glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
// //启动光照
// glEnable(GL_LIGHTING);
// //启用光源
// glEnable(GL_LIGHT0);
//}
//
画球
//void drawBall(double R, double x, double y, double z, int MODE) {
// glPushMatrix();
// glTranslated(x, y, z);
// if (MODE == SOLID) {
// glutSolidSphere(R, 20, 20);
// }
// else if (MODE == WIRE) {
// glutWireSphere(R, 20, 20);
// }
// glPopMatrix();
//}
//
画半球
//void drawHalfBall(double R, double x, double y, double z, int MODE) {
// glPushMatrix();
// glTranslated(x, y, z);
// GLdouble eqn[4] = { 0.0, 1.0, 0.0, 0.0 };
// glClipPlane(GL_CLIP_PLANE0, eqn);
// glEnable(GL_CLIP_PLANE0);
// if (MODE == SOLID) {
// glutSolidSphere(R, 20, 20);
// }
// else if (MODE == WIRE) {
// glutWireSphere(R, 20, 20);
// }
// glDisable(GL_CLIP_PLANE0);
// glPopMatrix();
//}
//
画长方体
//void drawSkewed(double l, double w, double h, double x, double y, double z, int MODE) {
// glPushMatrix();
// glScaled(l, w, h);
// glTranslated(x, y, z);
// if (MODE == SOLID) {
// glutSolidCube(1);
// }
// else if (MODE == WIRE) {
// glutWireCube(1);
// }
// glPopMatrix();
//}
//
//void display(void) {
// //清除缓冲区颜色
// glClear(GL_COLOR_BUFFER_BIT);
// //定义白色
// glColor3f(1.0, 1.0, 1.0);
// //圆点放坐标中心
// glLoadIdentity();
// //从哪个地方看
// gluLookAt(-2.0, -1.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// glPushMatrix();
// glRotated(spinX, 0, 1, 0);
// glRotated(spinY, 1, 0, 0);
// glTranslated(0, 0, des);
// //头
// drawBall(2, 0, 1, 0, SOLID);
// //身体
// drawSkewed(5, 4.4, 4, 0, -0.75, 0, SOLID);
// //肩膀
// drawHalfBall(1, 3.5, -2.1, 0, SOLID);
// drawHalfBall(1, -3.5, -2.1, 0, SOLID);
// //胳膊
// drawSkewed(1, 3, 1, 3.5, -1.3, 0, SOLID);
// drawSkewed(1, 3, 1, -3.5, -1.3, 0, SOLID);
// //手
// drawBall(1, 3.5, -6.4, 0, SOLID);
// drawBall(1, -3.5, -6.4, 0, SOLID);
// //腿
// drawSkewed(1.2, 3, 2, 1, -2.4, 0, SOLID);
// drawSkewed(1.2, 3, 2, -1, -2.4, 0, SOLID);
// //脚
// drawSkewed(1.5, 1, 3, 0.9, -9.2, 0, SOLID);
// drawSkewed(1.5, 1, 3, -0.9, -9.2, 0, SOLID);
// glPopMatrix();
// glutSwapBuffers();
//}
鼠标点击事件
//void mouseClick(int btn, int state, int x, int y) {
// moveX = x;
// moveY = y;
// GLfloat ambient[] = { (float)rand() / RAND_MAX, (float)rand() / RAND_MAX, (float)rand() / RAND_MAX, 0.1 };
// //设置环境光
// glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
// //启用光源
// glEnable(GL_LIGHT0);
//}
//
键盘事件
//void keyPressed(unsigned char key, int x, int y) {
// switch (key) {
// case 'a':
// spinX -= 2;
// break;
// case 'd':
// spinX += 2;
// break;
// case 'w':
// des += 2;
// break;
// case 's':
// des -= 2;
// break;
// }
// glutPostRedisplay();
//}
鼠标移动事件
//void mouseMove(int x, int y) {
// int dx = x - moveX;
// int dy = y - moveY;
// printf("dx;%dx,dy:%dy\n", dx, dy);
// spinX += dx;
// spinY += dy;
// glutPostRedisplay();
// moveX = x;
// moveY = y;
//}
//
//void reshape(int w, int h) {
// //定义视口大小
// glViewport(0, 0, (GLsizei)w, (GLsizei)h);
// //投影显示
// glMatrixMode(GL_PROJECTION);
// //坐标原点在屏幕中心
// glLoadIdentity();
// //操作模型视景
// gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
// glMatrixMode(GL_MODELVIEW);
//}
//
//int main(int argc, char** argv) {
// //初始化
// glutInit(&argc, argv);
// //设置显示模式
// glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
// //初始化窗口大小
// glutInitWindowSize(500, 500);
// //定义左上角窗口位置
// glutInitWindowPosition(100, 100);
// //创建窗口
// glutCreateWindow(argv[0]);
// //初始化
// init();
// //显示函数
// glutDisplayFunc(display);
// //窗口大小改变时的响应
// glutReshapeFunc(reshape);
// //鼠标点击事件,鼠标点击或者松开时调用
// glutMouseFunc(mouseClick);
// //鼠标移动事件,鼠标按下并移动时调用
// glutMotionFunc(mouseMove);
// //键盘事件
// glutKeyboardFunc(keyPressed);
// //循环
// glutMainLoop();
// return 0;
//}
/*
GLubyte mipmapImage512[512][512][4];
GLubyte mipmapImage256[256][256][4];
GLubyte mipmapImage128[128][128][4];
GLubyte mipmapImage64[64][64][4];
GLubyte mipmapImage32[32][32][4];
GLubyte mipmapImage16[16][16][4];
GLubyte mipmapImage8[8][8][4];
GLubyte mipmapImage4[4][4][4];
GLubyte mipmapImage2[2][2][4];
GLubyte mipmapImage1[1][1][4];
static GLuint texName;
int distance = -5.0f;
void makeImages(void)
{
int i, j;
for (i = 0; i < 512; i++) {
for (j = 0; j < 512; j++) {
mipmapImage512[i][j][0] = 255;
mipmapImage512[i][j][1] = 255;
mipmapImage512[i][j][2] = 255;
mipmapImage512[i][j][3] = 255;
}
}
for (i = 0; i < 256; i++) {
for (j = 0; j < 256; j++) {
mipmapImage256[i][j][0] = 255;
mipmapImage256[i][j][1] = 0;
mipmapImage256[i][j][2] = 0;
mipmapImage256[i][j][3] = 255;
}
}
for (i = 0; i < 128; i++) {
for (j = 0; j < 128; j++) {
mipmapImage128[i][j][0] = 0;
mipmapImage128[i][j][1] = 255;
mipmapImage128[i][j][2] = 0;
mipmapImage128[i][j][3] = 255;
}
}
for (i = 0; i < 64; i++) {
for (j = 0; j < 64; j++) {
mipmapImage64[i][j][0] = 0;
mipmapImage64[i][j][1] = 0;
mipmapImage64[i][j][2] = 255;
mipmapImage64[i][j][3] = 255;
}
}
for (i = 0; i < 32; i++) {
for (j = 0; j < 32; j++) {
mipmapImage32[i][j][0] = 255;
mipmapImage32[i][j][1] = 255;
mipmapImage32[i][j][2] = 0;
mipmapImage32[i][j][3] = 255;
}
}
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
mipmapImage16[i][j][0] = 255;
mipmapImage16[i][j][1] = 0;
mipmapImage16[i][j][2] = 255;
mipmapImage16[i][j][3] = 255;
}
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
mipmapImage8[i][j][0] = 255;
mipmapImage8[i][j][1] = 0;
mipmapImage8[i][j][2] = 0;
mipmapImage8[i][j][3] = 255;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
mipmapImage4[i][j][0] = 0;
mipmapImage4[i][j][1] = 255;
mipmapImage4[i][j][2] = 0;
mipmapImage4[i][j][3] = 255;
}
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
mipmapImage2[i][j][0] = 0;
mipmapImage2[i][j][1] = 0;
mipmapImage2[i][j][2] = 255;
mipmapImage2[i][j][3] = 255;
}
}
mipmapImage1[0][0][0] = 255;
mipmapImage1[0][0][1] = 255;
mipmapImage1[0][0][2] = 255;
mipmapImage1[0][0][3] = 255;
}
void initTexture(void)
{
makeImages();
//生成纹理名字
glGenTextures(1, &texName);
//将纹理名字与纹理绑定
glBindTexture(GL_TEXTURE_2D, texName);
//设置当纹理坐标超过1后,纹理在s,t方向上的模式
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//设置纹理图像放大时,采用的过滤方法
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//设置纹理图像缩小时,采用的过滤方法
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
//从0到9,指定了10个纹理细节层
int level = 0;
glTexImage2D(GL_TEXTURE_2D, level++, GL_RGBA, 512, 512, 0,
GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage512);
glTexImage2D(GL_TEXTURE_2D, level++, GL_RGBA, 256, 256, 0,
GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage256);
glTexImage2D(GL_TEXTURE_2D, level++, GL_RGBA, 128, 128, 0,
GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage128);
glTexImage2D(GL_TEXTURE_2D, level++, GL_RGBA, 64, 64, 0,
GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage64);
glTexImage2D(GL_TEXTURE_2D, level++, GL_RGBA, 32, 32, 0,
GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage32);
glTexImage2D(GL_TEXTURE_2D, level++, GL_RGBA, 16, 16, 0,
GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage16);
glTexImage2D(GL_TEXTURE_2D, level++, GL_RGBA, 8, 8, 0,
GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage8);
glTexImage2D(GL_TEXTURE_2D, level++, GL_RGBA, 4, 4, 0,
GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage4);
glTexImage2D(GL_TEXTURE_2D, level++, GL_RGBA, 2, 2, 0,
GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage2);
glTexImage2D(GL_TEXTURE_2D, level++, GL_RGBA, 1, 1, 0,
GL_RGBA, GL_UNSIGNED_BYTE, mipmapImage1);
//激活二维纹理的使用
glEnable(GL_TEXTURE_2D);
}
void drawScene()
{
//激活纹理
glBindTexture(GL_TEXTURE_2D, texName);
//绘制一个正方形
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
}
void draw(void)
{
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, distance);
drawScene();
glFlush();
}
void keyboards(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
case ' ':
distance -= 1.0f;
glutPostRedisplay();
break;
case 'a':
distance += 1.0f;
glutPostRedisplay();
break;
default:
break;
}
}
void init()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
initTexture();
}
void reshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)w / (float)h, 1.0f, 1000.0f);
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("texture-mipmap-demo");
init();
glutReshapeFunc(reshape);
glutDisplayFunc(draw);
glutKeyboardFunc(keyboards);
glutMainLoop();
return(0);
}*/
/*
int SCREEN_HEIGHT = 480;
int NUMPOINTS = 0;
class Point
{
public:
float x, y;
void setxy(float x2, float y2)
{
x = x2;
y = y2;
}
Point operator&(const Point & rPoint)
{
x = rPoint.x;
y = rPoint.y;
return *this;
}
};
Point abc[4];
void myInit()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0f, 0.0, 0.0);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640, 0.0, 480.0);
}
void drawDot(Point pt)
{
glBegin(GL_POINTS);
glVertex2f(pt.x, pt.y);
glEnd();
glFlush();
}
void drawLine(Point p1, Point p2)
{
glBegin(GL_LINES);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glEnd();
glFlush();
}
//四个控制点的贝塞尔曲线 即三次Bezier曲线
Point drawBezier(Point A, Point B, Point C, Point D, double t)
{
Point P;
double a1 = pow((1 - t), 3);
double a2 = pow((1 - t), 2) * 3 * t;
double a3 = 3 * t*t*(1 - t);
double a4 = t*t*t;
P.x = a1*A.x + a2*B.x + a3*C.x + a4*D.x;
P.y = a1*A.y + a2*B.y + a3*C.y + a4*D.y;
return P;
}
void myMouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
abc[NUMPOINTS].setxy((float)x, (float)(SCREEN_HEIGHT - y));
NUMPOINTS++;
if (NUMPOINTS == 4)
{
glColor3f(1.0, 0.0, 1.0);
drawDot(abc[0]);
drawDot(abc[1]);
drawDot(abc[2]);
drawDot(abc[3]);
glColor3f(1.0, 1.0, 0.0);
drawLine(abc[0], abc[1]);
drawLine(abc[1], abc[2]);
drawLine(abc[2], abc[3]);
glColor3f(0.0, 1.0, 1.0);
Point POld = abc[0];
for (double t = 0.0; t <= 1.0; t += 0.1)
{
Point P = drawBezier(abc[0], abc[1], abc[2], abc[3], t);
drawLine(POld, P);
POld = P;
}
glColor3f(1.0, 0.0, 0.0);
NUMPOINTS = 0;
}
}
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
int main(int argc, char * agrv[])
{
glutInit(&argc, agrv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("Bezier Curve");
glutMouseFunc(myMouse);
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
return 0;
}
*/
/*
#define checkImageWidth 64
#define checkImageHeight 64
#define subImageWidth 45
#define subImageHeight 45
static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
static GLubyte subImage[subImageHeight][subImageWidth][4];
static GLuint texName;
//纹理数据(黑白相间的棋盘和蓝色区域)
void makeCheckImages(void)
{
int i, j, c;
for (i = 0; i < checkImageHeight; i++) {
for (j = 0; j < checkImageWidth; j++) {
c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;
checkImage[i][j][0] = (GLubyte)c;
checkImage[i][j][1] = (GLubyte)c;
checkImage[i][j][2] = (GLubyte)c;
checkImage[i][j][3] = (GLubyte)255;
}
}
for (i = 0; i < subImageHeight; i++) {
for (j = 0; j < subImageWidth; j++) {
subImage[i][j][0] = (GLubyte)0;
subImage[i][j][1] = (GLubyte)0;
subImage[i][j][2] = (GLubyte)255;
subImage[i][j][3] = (GLubyte)255;
}
}
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
makeCheckImages();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight,
0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.6);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 's':
case 'S':
//指定当前活动的纹理
glBindTexture(GL_TEXTURE_2D, texName);
//用subImage纹理替换原纹理的(12,12)至
//(subImageWidth,subImageHeight)区域
glTexSubImage2D(GL_TEXTURE_2D, 0, 12, 12,
subImageWidth, subImageHeight,
GL_RGBA, GL_UNSIGNED_BYTE, subImage);
glutPostRedisplay();
break;
case 'r':
case 'R':
glBindTexture(GL_TEXTURE_2D, texName);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
checkImageWidth, checkImageHeight, 0,
GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}*/
/*
#define checkImageWidth 64
#define checkImageHeight 64
GLubyte checkImage[checkImageHeight][checkImageWidth][3];
static GLdouble zoomFactor = 1.0;
static GLint height;
void makeCheckImage(void)
{
int i, j, c;
//棋盘图像数据
for (i = 0; i < checkImageHeight; i++) {
for (j = 0; j < checkImageWidth; j++) {
c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;
checkImage[i][j][0] = (GLubyte)c;
checkImage[i][j][1] = (GLubyte)c;
checkImage[i][j][2] = (GLubyte)c;
}
}
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
makeCheckImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);//设置像素存储模式
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);//设置光栅化位置
glDrawPixels(checkImageWidth, checkImageHeight, GL_RGB, GL_UNSIGNED_BYTE, checkImage);//绘制图像
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
height = (GLint)h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//鼠标拖动
void motion(int x, int y)
{
static GLint screeny;
screeny = height - (GLint)y;
glRasterPos2i(x, screeny);
glPixelZoom(zoomFactor, zoomFactor);//x,y方向进行缩放
//复制颜色缓冲区指定的矩形区域到帧缓冲区
glCopyPixels(0, 0, checkImageWidth, checkImageHeight, GL_COLOR);
glPixelZoom(1.0, 1.0);
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'r':
case 'R':
zoomFactor = 1.0;
glutPostRedisplay();
printf("zoomFactor reset to 1.0\n");
break;
case 'z':
zoomFactor += 0.5;
if (zoomFactor >= 3.0)
zoomFactor = 3.0;
printf("zoomFactor is now %4.1f\n", zoomFactor);
break;
case 'Z':
zoomFactor -= 0.5;
if (zoomFactor <= 0.5)
zoomFactor = 0.5;
printf("zoomFactor is now %4.1f\n", zoomFactor);
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMainLoop();
return 0;
}
*/
const int n = 1000;
const GLfloat R = 0.5f;
const GLfloat Pi = 3.1415926536f;
const GLfloat factor = 0.1f;
static GLubyte mask[128] =
{
0x00, 0x00, 0x00, 0x00, // 这是最下面的一行
0x00, 0x00, 0x00, 0x00,
0x03, 0x80, 0x01, 0xC0, // 麻
0x06, 0xC0, 0x03, 0x60, // 烦
0x04, 0x60, 0x06, 0x20, // 的
0x04, 0x30, 0x0C, 0x20, // 初
0x04, 0x18, 0x18, 0x20, // 始
0x04, 0x0C, 0x30, 0x20, // 化
0x04, 0x06, 0x60, 0x20, // ,
0x44, 0x03, 0xC0, 0x22, // 不
0x44, 0x01, 0x80, 0x22, // 建
0x44, 0x01, 0x80, 0x22, // 议
0x44, 0x01, 0x80, 0x22, // 使
0x44, 0x01, 0x80, 0x22, // 用
0x44, 0x01, 0x80, 0x22,
0x44, 0x01, 0x80, 0x22,
0x66, 0x01, 0x80, 0x66,
0x33, 0x01, 0x80, 0xCC,
0x19, 0x81, 0x81, 0x98,
0x0C, 0xC1, 0x83, 0x30,
0x07, 0xE1, 0x87, 0xE0,
0x03, 0x3F, 0xFC, 0xC0,
0x03, 0x31, 0x8C, 0xC0,
0x03, 0x3F, 0xFC, 0xC0,
0x06, 0x64, 0x26, 0x60,
0x0C, 0xCC, 0x33, 0x30,
0x18, 0xCC, 0x33, 0x18,
0x10, 0xC4, 0x23, 0x08,
0x10, 0x63, 0xC6, 0x08,
0x10, 0x30, 0x0C, 0x08,
0x10, 0x18, 0x18, 0x08,
0x10, 0x00, 0x00, 0x08 // 这是最上面的一行
};
static int day = 300; // day的变化:从0到359
void myDisplay(void)
{
///第一个程序----矩形
//glClear(GL_COLOR_BUFFER_BIT);
//glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
//glFlush();
///第二个程序----圆
//int i;
//glClear(GL_COLOR_BUFFER_BIT);
//glBegin(GL_POLYGON);//有顶点就必须要有glBegin() glEnd();
//for(i=0; i<n; ++i)
// glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
//glEnd();
//glFlush();
///第三个程序---五星
//GLfloat a = 1 / (2 - 2 * cos(72 * Pi / 180));
//GLfloat bx = a * cos(18 * Pi / 180);
//GLfloat by = a * sin(18 * Pi / 180);
//GLfloat cy = -a * cos(18 * Pi / 180);
//GLfloat PointA[2] = { 0, a };
//GLfloat PointB[2] = { bx, by };
//GLfloat PointC[2] = { 0.5, cy };
//GLfloat PointD[2] = { -0.5, cy };
//GLfloat PointE[2] = { -bx, by };
//glClear(GL_COLOR_BUFFER_BIT);
按照A->C->E->B->D->A的顺序,将五角星画出
//glBegin(GL_LINE_LOOP);//闭合折线
//glVertex2fv(PointA);
//glVertex2fv(PointC);
//glVertex2fv(PointE);
//glVertex2fv(PointB);
//glVertex2fv(PointD);
//glEnd();
//glFlush();
///第四个程序----画出正弦函数图形
//GLfloat x;
//glClear(GL_COLOR_BUFFER_BIT);
//glBegin(GL_LINES);
//glVertex2f(-1.0f, 0.0f);
//glVertex2f(1.0f, 0.0f); // 以上两个点可以画x轴
//glVertex2f(0.0f, -1.0f);
//glVertex2f(0.0f, 1.0f); // 以上两个点可以画y轴
//glEnd();
//glBegin(GL_LINE_STRIP);
//for(x=-1.0f/factor; x<1.0f/factor; x+=0.01f)
//{
// glVertex2f(x*factor, sin(x)*factor);
//}
//glEnd();
//glFlush();
//glClear(GL_COLOR_BUFFER_BIT);
//glPointSize(15.0f);
//glLineWidth(15.0f);
//glBegin(GL_LINES);
//glVertex2f(0.0f, 0.0f);
//glVertex2f(0.5f, 0.5f);
//glEnd();
//glFlush();
//glClear(GL_COLOR_BUFFER_BIT);
//glEnable(GL_POLYGON_STIPPLE);
//glPolygonStipple(mask);
//glRectf(-0.5f, -0.5f, 0.0f, 0.0f); // 在左下方绘制一个有镂空效果的正方形
//glDisable(GL_POLYGON_STIPPLE);
//glRectf(0.0f, 0.0f, 0.5f, 0.5f); // 在右上方绘制一个无镂空效果的正方形
//glFlush();
//glClear(GL_COLOR_BUFFER_BIT);
//glPolygonMode(GL_FRONT, GL_FILL); // 设置正面为填充模式
//glPolygonMode(GL_BACK, GL_LINE); // 设置反面为线形模式
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); // 设置两面均为顶点绘制方式
//glFrontFace(GL_CCW); // 设置逆时针方向为正面
//glBegin(GL_POLYGON); // 按逆时针绘制一个正方形,在左下方
//glVertex2f(-0.5f, -0.5f);
//glVertex2f(0.0f, -0.5f);
//glVertex2f(0.0f, 0.0f);
//glVertex2f(-0.5f, 0.0f);
//glEnd();
//glBegin(GL_POLYGON); // 按顺时针绘制一个正方形,在右上方
//glVertex2f(0.0f, 0.0f);
//glVertex2f(0.0f, 0.5f);
//glVertex2f(0.5f, 0.5f);
//glVertex2f(0.5f, 0.0f);
//glEnd();
//glFlush();
//glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(0.0f, 1.0f, 1.0f);
//glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
//glFlush();
//int i;
glShadeModel(GL_FLAT);
//glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0f, 1.0f, 1.0f);
//glBegin(GL_TRIANGLE_FAN);
//glVertex2f(0.0f, 0.0f);
//for (i = 0; i <= 8; ++i)
//{
// glColor3f(i & 0x04, i & 0x02, i & 0x01);
// glVertex2f(cos(i*Pi / 4), sin(i*Pi / 4));
//}
//glEnd();
//glFlush();
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glEnable(GL_DEPTH_TEST);
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//gluPerspective(75, 1, 1, 400000000);
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
//gluLookAt(0, -200000000, 200000000, 0, 0, 0, 0, 0, 1);
绘制红色的“太阳”
//glColor3f(1.0f, 0.0f, 0.0f);
//glutSolidSphere(69600000, 20, 20);
绘制蓝色的“地球”
//glColor3f(0.0f, 0.0f, 1.0f);
//glRotatef(day / 360.0*360.0, 0.0f, 0.0f, -1.0f);
//glTranslatef(150000000, 0.0f, 0.0f);
//glutSolidSphere(15945000, 20, 20);
绘制黄色的“月亮”
//glColor3f(1.0f, 1.0f, 0.0f);
//glRotatef(day / 30.0*360.0 - day / 360.0*360.0, 0.0f, 0.0f, -1.0f);
//glTranslatef(38000000, 0.0f, 0.0f);
//glutSolidSphere(4345000, 20, 20);
//glFlush();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75, 1, 1, 40.0000000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, -20.0000000, 20.0000000, 0, 0, 0, 0, 0, 1);
// 绘制红色的“太阳”
glColor3f(1.0f, 0.0f, 0.0f);
glutSolidSphere(6.9600000, 20, 20);
// 绘制蓝色的“地球”
glColor3f(0.0f, 0.0f, 1.0f);
glRotatef(day / 360.0*360.0, 0.0f, 0.0f, -1.0f);
glTranslatef(15.0000000, 0.0f, 0.0f);
glutSolidSphere(1.5945000, 20, 20);
// 绘制黄色的“月亮”
glColor3f(1.0f, 1.0f, 0.0f);
glRotatef(day / 30.0*360.0 - day / 360.0*360.0, 0.0f, 0.0f, -1.0f);
glTranslatef(3.8000000, 0.0f, 0.0f);
glutSolidSphere(.4345000, 20, 20);
glFlush();
glutSwapBuffers();
}
void myIdle(void)
{
/* 新的函数,在空闲时调用,作用是把日期往后移动一天并重新绘制,达到动画效果 */
++day;
if (day >= 360)
day = 0;
myDisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); // 修改了参数为GLUT_DOUBLE
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("太阳,地球和月亮"); // 改了窗口标题
glutDisplayFunc(&myDisplay);
glutIdleFunc(&myIdle); // 新加入了这句
glutMainLoop();
return 0;
}