一、材质:
二、相关函数:
GLfloat mat_yellow[4] = {0.7, 0.6, 0.1, 1};
GLfloat mat_white[4] = {1,1,1, 1};
GLfloat mat_ambient[4] = {0.5,0.5,0.5, 1};
GLfloat mat_black[4] = {0,0,0, 1};
GLfloat mat_red[4] = {0.8, 0.0, 0.0, 1.0};
glMaterialf(GL_FRONT, GL_EMISSION, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_yellow);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_black);
glMaterialf(GL_FRONT, GL_SHININESS, 0.0f);
三、代码:
#define GLEW_STATIC
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/ext/matrix_clip_space.hpp>
#include "FreeImage.h"
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
char szTitle[64] = "opengl view";
const GLuint WIDTH = 800, HEIGHT = 600;
GLfloat ratio = 1.f;
GLfloat xpos, ypos;
glm::vec3 _eye(0, 0, 10);
glm::vec3 _lookAt(0, 0, 0);
glm::vec3 _up(0, 1, 0);
glm::mat4 _matProj = glm::mat4(1.0f);
glm::mat4 _matView = glm::mat4(1.0f);
float size = 100.0f;
struct Vertex {
float x, y, z;
float u, v;
};
Vertex g_cubeVertices[] =
{
{ -1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
{ 1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f, 1.0f, 0.0f, 1.0f },
{ -1.0f, -1.0f, -1.0f, 1.0f, 0.0f },
{ -1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, -1.0f, 0.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f, 0.0f, 0.0f },
{ -1.0f, 1.0f, -1.0f, 0.0f, 1.0f },
{ -1.0f, 1.0f, 1.0f, 0.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
{ -1.0f, -1.0f, -1.0f, 1.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f, 0.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
{ -1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
{ 1.0f, -1.0f, -1.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f, -1.0f, 1.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f, 0.0f, 1.0f },
{ 1.0f, -1.0f, 1.0f, 0.0f, 0.0f },
{ -1.0f, -1.0f, -1.0f, 0.0f, 0.0f },
{ -1.0f, -1.0f, 1.0f, 1.0f, 0.0f },
{ -1.0f, 1.0f, 1.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f, -1.0f, 0.0f, 1.0f },
{ -size, -1.0f, -size, 0.0f, 0.0f },
{ -size, -1.0f, size, 100.0f, 0.0f },
{ size, -1.0f, size, 100.0f, 100.0f },
{ size, 0.0f, -size, 0.0f, 100.0f }
};
GLuint _texture;
GLuint _vbo;
GLuint _texGround;
static void error_callback(int error, const char* description) {
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
std::cout << "key " << key << std::endl;
switch(key) {
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GL_TRUE);
break;
case GLFW_KEY_UP:
_eye.z -= 0.1f;
break;
case GLFW_KEY_DOWN:
_eye.z += 0.1f;
break;
case GLFW_KEY_RIGHT:
_eye.x -= 1.0f;
break;
case GLFW_KEY_LEFT:
_eye.x += 1.0f;
break;
default:
break;
}
}
static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
if(action != GLFW_PRESS)
return;
switch(button) {
case GLFW_MOUSE_BUTTON_LEFT:
std::cout << "Mosue left button clicked!" << std::endl;
break;
case GLFW_MOUSE_BUTTON_MIDDLE:
std::cout << "Mosue middle button clicked!" << std::endl;
break;
case GLFW_MOUSE_BUTTON_RIGHT:
std::cout << "Mosue right button clicked!" << std::endl;
break;
default:
return;
}
return;
}
static void cursor_position_callback(GLFWwindow* window, double x, double y) {
xpos = float((x - WIDTH / 2) / WIDTH) * 2;
ypos = float(0 - (y - HEIGHT / 2) / HEIGHT) * 2;
return;
}
static void scroll_callback(GLFWwindow* window, double x, double y) {
return;
}
static unsigned createTexture(int w, int h, const void* data, GLenum type) {
unsigned texId;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, type, w, h, 0, type, GL_UNSIGNED_BYTE, data);
return texId;
}
static unsigned createTextureFromImage(const char* fileName) {
FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(fileName, 0);
if(fifmt == FIF_UNKNOWN) {
return 0;
}
FIBITMAP *dib = FreeImage_Load(fifmt, fileName, 0);
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
FIBITMAP* temp = dib;
dib = FreeImage_ConvertTo32Bits(dib);
FreeImage_Unload(temp);
BYTE* pixels = (BYTE*)FreeImage_GetBits(dib);
int width = FreeImage_GetWidth(dib);
int height = FreeImage_GetHeight(dib);
for(int i = 0; i < width * height * 4; i += 4) {
BYTE temp = pixels[i];
pixels[i] = pixels[i + 2];
pixels[i + 2] = temp;
}
unsigned res = createTexture(width, height, pixels, GL_RGBA);
FreeImage_Unload(dib);
return res;
}
static void onInit() {
_texture = createTextureFromImage("../res/1.jpg");
_texGround = createTextureFromImage("../res/dimian.jpg");
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_cubeVertices), g_cubeVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
static void onDestory() {
glDeleteTextures(1, &_texture);
glDeleteTextures(1, &_texGround);
glDeleteBuffers(1, &_vbo);
}
static void render(GLFWwindow * window) {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat light_ambient[4] ={ 0.7, 0.7, 0.7, 1.0 };
GLfloat light_diffuse[4] ={ 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[4] ={ 1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0f);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05f);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0f);
GLfloat light_position[4] ={ 30, 30, 30, 1 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT1);
GLfloat spot_dir[4] ={ -1, -2.5, -10 };
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_dir);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 7.5f);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 128.0f);
GLfloat light_position1[4] ={ 0, 0, 20, 1 };
glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
glMatrixMode(GL_MODELVIEW);
_matView = glm::lookAt(_eye, _lookAt, _up);
static float angle = 0;
float r = 10;
_eye.x = cos(angle * glm::pi<double>() / 180) * r;
_eye.z = sin(angle * glm::pi<double>() / 180) * r;
angle += 1;
glm::mat4 matVP = _matProj * _matView;
glLoadMatrixf(&matVP[0][0]);
glBindTexture(GL_TEXTURE_2D, _texture);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
float* addrVertex = (float*)0;
float* uvAddress = (float*)12;
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), addrVertex);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), uvAddress);
GLfloat mat_yellow[4] ={ 0.7, 0.6, 0.1, 1 };
GLfloat mat_white[4] ={ 1, 1, 1, 1 };
GLfloat mat_ambient[4] ={ 0.5, 0.5, 0.5, 1 };
GLfloat mat_black[4] ={ 0, 0, 0, 1 };
GLfloat mat_red[4] ={ 0.8, 0.0, 0.0, 1.0 };
glMaterialf(GL_FRONT, GL_EMISSION, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_yellow);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_black);
glMaterialf(GL_FRONT, GL_SHININESS, 0.0f);
glDrawArrays(GL_QUADS, 0, 24);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
int main(void) {
GLFWwindow * window;
glfwSetErrorCallback(error_callback);
if(!glfwInit())
return -1;
window = glfwCreateWindow(WIDTH, HEIGHT, szTitle, NULL, NULL);
if(!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glewInit();
onInit();
glViewport(0, 0, WIDTH, HEIGHT);
_matProj = glm::perspective(glm::radians(60.0f), float(WIDTH) / float(HEIGHT), 0.1f, 1000.0f);
while(!glfwWindowShouldClose(window)) {
render(window);
}
onDestory();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
四、运行结果:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ae95a9b285ff04fbb77c6e5814e546c6.png#pic_center)