#include <iostream>
#include <map>
#include <gl/glew.h>
#include <gl/glut.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "Sphere.h"
#include "ScreenQuad.h"
#include "textfile.h"
#include "Shader.h"
#include "Texture.h"
#pragma comment(lib, "glew32.lib")
using namespace glm;
mat4 modelViewMatrix, viewMatrix, projectionMatrix, MVP;
mat3 normalMatrix;
mat4 transform;
GLfloat lastPosition[3], axis[3] = {0.0, 0.0, -1.0};
GLuint width = 1000, height = 680;
GLfloat angle = 23.6;
GLfloat trans = 0.0;
Shader sh;
ScreenQuad *sq;
Sphere *earth;
const enum renderType{RENDERTOTEXTURE, RENDERTOBUFFER, ENUMCOUNT};
GLuint fb[ENUMCOUNT];
GLsizei samples = 16;//sample number
GLuint pass1Index, pass2Index, pass3Index, pass4Index;
void PointToVector(int x, int y, GLfloat *v)
{
GLfloat d, a;
v[0] = (2.0 * x - width) / width;
v[1] = (height - 2.0 * y) / height;
d = sqrt(v[0] * v[0] + v[1] * v[1]);
v[2] = cos((3.14159265 / 2.0) * ((d < 1.0) ? d : 1.0));
a = 1.0 / sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
v[0] *= a;
v[1] *= a;
v[2] *= a;
}
void changeSize(int w, int h)
{
if(h == 0) h = 1;
glViewport(0, 0, w, h);
projectionMatrix = perspective(45.0, (GLdouble)w / h, 1.0, 100.0);
sh.SetUniformMatrix4fv("ProjectionMatrix", 1, &projectionMatrix[0][0]);
width = w;
height = h;
}
void setFBO()
{
GLuint rb[2];
glGenRenderbuffers(2, rb);
glBindRenderbuffer(GL_RENDERBUFFER, rb[0]);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, rb[1]);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
// Create the texture object
GLuint renderTex, texColor;
glGenTextures(1, &renderTex);
glActiveTexture(GL_TEXTURE1); // Use texture unit 1
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, renderTex);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, width, height, GL_TRUE);
glGenTextures(1, &texColor);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, texColor);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width / 2, height / 2, 0, )
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GLuint texDep;
glGenRenderbuffers(1, &texDep);
glBindRenderbuffer(GL_RENDERBUFFER, texDep);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_DEPTH_COMPONENT24, width, height);
glGenFramebuffers(1, &fb[RENDERTOBUFFER]);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb[RENDERTOBUFFER]);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb[0]);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb[1]);
// Bind the texture to the FBO
glGenFramebuffers(1, &fb[RENDERTOTEXTURE]);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb[RENDERTOTEXTURE]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, renderTex, 0);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, texDep);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void setMatrices()
{
normalMatrix = mat3(vec3(modelViewMatrix[0]), vec3(modelViewMatrix[1]), vec3(modelViewMatrix[2]));
MVP = projectionMatrix * modelViewMatrix;
sh.SetUniformMatrix4fv("ProjectionMatrix", 1, &projectionMatrix[0][0]);
sh.SetUniformMatrix4fv("ModelViewMatrix", 1, &modelViewMatrix[0][0]);
sh.SetUniformMatrix3fv("NormalMatrix", 1, &normalMatrix[0][0]);
sh.SetUniformMatrix4fv("MVP", 1, &MVP[0][0]);
}
void pass1()// render the scene to texture
{
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb[RENDERTOTEXTURE]);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 1, &pass1Index);
/*rendering objects*/
projectionMatrix = perspective(45.0, (GLdouble)width / height, 1.0, 100.0);
mat4 translatef = translate(mat4(1.0f), vec3(0.0, 0.0, trans));
mat4 rotatef = rotate(mat4(1.0f), angle, vec3(axis[0], axis[1], axis[2]));
transform = rotatef * transform;
modelViewMatrix = viewMatrix * translatef * transform;
setMatrices();
earth->Render();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void pass2()
{
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
/*bind custom framebuffer and clear color and depth buffer*/
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb[RENDERTOBUFFER]);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 1, &pass2Index);
projectionMatrix = mat4(1.0);
modelViewMatrix = mat4(1.0f);
setMatrices();
sq->Render();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void pass3()
{
/*read custom framebuffer and draw to system framebuffer*/
glBindFramebuffer(GL_READ_FRAMEBUFFER, fb[RENDERTOBUFFER]);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);//rendering to screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 1.0);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glutSwapBuffers();
glFinish();
}
void renderScene(void)
{
pass1();
pass2();
pass3();
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
earth = new Sphere(10.0, 1, 0, 360, 0, 180);
sq = new ScreenQuad();
Texture tex;
tex.CreateTexture("se", "earth.tga", GL_TEXTURE_2D);
sh.SetUniform1i("EarthTex", tex.GetTextureIndex("se"));
sh.SetUniform1i("screenTex", 1);
sh.SetUniform4f("LightPosition", 20.0f, 0.0f, 0.0f, 0.0f);
sh.SetUniform3f("LightIntensity", 1.0f, 1.0f, 1.0f);
sh.SetUniform3f("Kd", 1.0f, 1.0f, 1.0f);
sh.SetUniform3f("Ks", 0.9f, 0.9f, 0.9f);
sh.SetUniform3f("Ka", 0.1f, 0.1f, 0.1f);
sh.SetUniformf("Shininess", 1, 180.0f);
sh.SetUniform1i("samples", samples);
sh.SetUniform1i("width", width);
sh.SetUniform1i("height", height);
transform = mat4(1.0f);
viewMatrix = lookAt(vec3(0.0, 0.0, 30.0), vec3(0.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0));
setFBO();
GLuint programHandle = sh.GetProgram();
pass1Index = glGetSubroutineIndex(programHandle, GL_FRAGMENT_SHADER, "pass1");
pass2Index = glGetSubroutineIndex(programHandle, GL_FRAGMENT_SHADER, "pass2");
pass3Index = glGetSubroutineIndex(programHandle, GL_FRAGMENT_SHADER, "pass3");
pass4Index = glGetSubroutineIndex(programHandle, GL_FRAGMENT_SHADER, "pass4");
glEnable(GL_DEPTH_TEST);
}
void mouseClickEvent(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
PointToVector(x, y, lastPosition);
}
}
void mouseMotionEvent(int x, int y)
{
GLfloat currentPosition[3], dx, dy, dz;
PointToVector(x, y, currentPosition);
dx = currentPosition[0] - lastPosition[0];
dy = currentPosition[1] - lastPosition[1];
dz = currentPosition[2] - lastPosition[2];
angle = 90.0 * sqrt(dx * dx + dy * dy + dz * dz);
axis[0] = lastPosition[1] * currentPosition[2] - lastPosition[2] * currentPosition[1];
axis[1] = lastPosition[2] * currentPosition[0] - lastPosition[0] * currentPosition[2];
axis[2] = lastPosition[0] * currentPosition[1] - lastPosition[1] * currentPosition[0];
lastPosition[0] = currentPosition[0];
lastPosition[1] = currentPosition[1];
lastPosition[2] = currentPosition[2];
glutPostRedisplay();
}
void keyBoardEvent(unsigned char key, int x, int y)
{
switch(key)
{
case 'k':
trans += 0.1;
break;
case 'l':
trans -= 0.1;
break;
default:
break;
}
glutPostRedisplay();
}
void setShader()
{
sh.CreateProgram();
sh.CreateShader("vt", "earth.vs", GL_VERTEX_SHADER);
sh.CreateShader("fm", "earth.fs", GL_FRAGMENT_SHADER);
sh.LinkProgram();
sh.RunProgram();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Earth.rar (40个子文件)
Earth
Debug
Earth.exe 180KB
Earth.ilk 729KB
Earth.pdb 1.19MB
EarthSetup
Debug
setup.exe 418KB
EarthSetup.msi 19.31MB
Release
EarthSetup.vdproj 35KB
ipch
earth-ec338e9f
earth-dd76cea0.ipch 42.06MB
Earth.sln 1KB
Earth.suo 17KB
Earth
Earth.vcxproj.filters 2KB
Debug
cl.command.1.tlog 990B
Earth.exe.intermediate.manifest 381B
CL.read.1.tlog 19KB
vc100.idb 443KB
mt.read.1.tlog 672B
link.read.1.tlog 3KB
textfile.obj 9KB
link.write.1.tlog 494B
Earth.Build.CppClean.log 749B
CL.write.1.tlog 610B
vc100.pdb 428KB
Earth.lastbuildstate 44B
main.obj 787KB
link.command.1.tlog 1KB
mt.write.1.tlog 196B
Earth.log 3KB
mt.command.1.tlog 368B
textfile.cpp 4KB
Earth.vcxproj.user 143B
Shader.h 4KB
ScreenQuad.h 1KB
textfile.h 482B
main.cpp 8KB
earth.vs 1KB
Earth.vcxproj 4KB
Sphere.h 5KB
Texture.h 2KB
earth.fs 1KB
earth.tga 41.71MB
Earth.sdf 38.02MB
共 40 条
- 1
资源评论
- zsunpl2013-04-01漆黑一片,什么也看不到
- hzz7802013-12-18十分好用,尽管我地球调试完后出了点美丽的小差错
GoldenGraphics
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功