blobt
5 years ago
37 changed files with 647 additions and 29 deletions
-
BIN.vs/shader/v14/.suo
-
BINRes/back.bmp
-
BINRes/bottom.bmp
-
BINRes/earth.bmp
-
BINRes/front.bmp
-
26Res/ground.fs
-
4Res/ground.vs
-
BINRes/left.bmp
-
41Res/model.fs
-
4Res/model.vs
-
BINRes/niutou.bmp
-
12Res/particle.fs
-
14Res/particle.vs
-
BINRes/right.bmp
-
9Res/skybox.fs
-
11Res/skybox.vs
-
9Res/texture.fs
-
11Res/texture.vs
-
BINRes/top.bmp
-
72framebufferobject.cpp
-
17framebufferobject.h
-
7ground.cpp
-
4main.cpp
-
36model.cpp
-
7model.h
-
44particleSystem.cpp
-
12particleSystem.h
-
69scene.cpp
-
BINshader.VC.db
-
16shader.cpp
-
5shader.h
-
12shader.vcxproj
-
56shader.vcxproj.filters
-
124skybox.cpp
-
17skybox.h
-
34utils.cpp
-
3utils.h
@ -1,8 +1,32 @@ |
|||
#ifdef GL_ES |
|||
precision mediump float; |
|||
#endif |
|||
uniform vec4 U_LightPos; |
|||
uniform vec4 U_LightAmbient; |
|||
uniform vec4 U_LightDiffuse; |
|||
uniform vec4 U_AmbientMaterial; |
|||
uniform vec4 U_DiffuseMaterial; |
|||
varying vec4 V_Color; |
|||
varying vec3 V_Normal; |
|||
varying vec3 V_WorldPos; |
|||
void main() |
|||
{ |
|||
gl_FragColor=V_Color; |
|||
vec4 color=vec4(0.0,0.0,0.0,0.0); |
|||
vec4 ambientColor=U_LightAmbient*U_AmbientMaterial; |
|||
|
|||
float distance=0.0; |
|||
float constantFactor=1.0; |
|||
float linearFactor=0.0; |
|||
float quadricFactor=0.0; |
|||
vec3 L=U_LightPos.xyz-V_WorldPos; |
|||
distance=length(L); |
|||
float attenuation=1.0/(constantFactor+linearFactor*distance+quadricFactor*quadricFactor*distance); |
|||
L=normalize(L); |
|||
vec3 n=normalize(V_Normal); |
|||
float diffuseIntensity=max(0.0,dot(L,n)); |
|||
vec4 diffuseColor=U_LightDiffuse*U_DiffuseMaterial*diffuseIntensity*attenuation*4.0; |
|||
|
|||
color=ambientColor+diffuseColor; |
|||
gl_FragData[0]=color*V_Color; |
|||
gl_FragData[1]=color*V_Color; |
|||
} |
@ -0,0 +1,12 @@ |
|||
#version 120 |
|||
#ifdef GL_ES |
|||
precision mediump float; |
|||
#endif |
|||
uniform sampler2D U_Texture; |
|||
varying vec4 V_Color; |
|||
void main() |
|||
{ |
|||
vec4 color=texture2D(U_Texture,gl_PointCoord.xy)*V_Color; |
|||
gl_FragData[0]=color; |
|||
gl_FragData[1]=color; |
|||
} |
@ -0,0 +1,14 @@ |
|||
attribute vec4 position; |
|||
attribute vec4 color; |
|||
attribute vec4 normal; |
|||
uniform mat4 ModelMatrix; |
|||
uniform mat4 ViewMatrix; |
|||
uniform mat4 ProjectionMatrix; |
|||
varying vec4 V_Color; |
|||
void main() |
|||
{ |
|||
V_Color=color; |
|||
gl_PointSize=64.0; |
|||
vec4 pos=vec4(position.x+normal.x,position.y+normal.y,position.z+normal.z,1.0); |
|||
gl_Position=ProjectionMatrix*ViewMatrix*ModelMatrix*pos; |
|||
} |
@ -0,0 +1,9 @@ |
|||
#ifdef GL_ES |
|||
precision mediump float; |
|||
#endif |
|||
uniform sampler2D U_Texture; |
|||
varying vec4 V_Texcoord; |
|||
void main() |
|||
{ |
|||
gl_FragColor=texture2D(U_Texture,V_Texcoord.xy); |
|||
} |
@ -0,0 +1,11 @@ |
|||
attribute vec4 position; |
|||
attribute vec4 texcoord; |
|||
uniform mat4 ModelMatrix; |
|||
uniform mat4 ViewMatrix; |
|||
uniform mat4 ProjectionMatrix; |
|||
varying vec4 V_Texcoord; |
|||
void main() |
|||
{ |
|||
V_Texcoord=texcoord; |
|||
gl_Position=ProjectionMatrix*ViewMatrix*ModelMatrix*position; |
|||
} |
@ -0,0 +1,9 @@ |
|||
#ifdef GL_ES |
|||
precision mediump float; |
|||
#endif |
|||
uniform sampler2D U_Texture; |
|||
varying vec4 V_Texcoord; |
|||
void main() |
|||
{ |
|||
gl_FragColor=texture2D(U_Texture,V_Texcoord.xy); |
|||
} |
@ -0,0 +1,11 @@ |
|||
attribute vec4 position; |
|||
attribute vec4 texcoord; |
|||
uniform mat4 ModelMatrix; |
|||
uniform mat4 ViewMatrix; |
|||
uniform mat4 ProjectionMatrix; |
|||
varying vec4 V_Texcoord; |
|||
void main() |
|||
{ |
|||
V_Texcoord=texcoord; |
|||
gl_Position=ProjectionMatrix*ViewMatrix*ModelMatrix*position; |
|||
} |
@ -0,0 +1,72 @@ |
|||
#include "framebufferobject.h"
|
|||
|
|||
FrameBufferObject::FrameBufferObject() |
|||
{ |
|||
glGenFramebuffers(1, &mFrameBufferObject); |
|||
} |
|||
void FrameBufferObject::AttachColorBuffer(const char*bufferName, GLenum attachment, int width, int height) { |
|||
|
|||
//在显存中申请一块空间,并置空
|
|||
GLuint colorBuffer; |
|||
glGenTextures(1, &colorBuffer); |
|||
glBindTexture(GL_TEXTURE_2D, colorBuffer); |
|||
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, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
|||
glBindTexture(GL_TEXTURE_2D, 0); |
|||
|
|||
//把上面申请的空间和frame buffer 绑定
|
|||
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject); |
|||
glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, colorBuffer, 0); |
|||
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
|||
|
|||
mDrawBuffers.push_back(attachment); |
|||
mBuffers.insert(std::pair<std::string, GLuint>(bufferName, colorBuffer)); |
|||
} |
|||
void FrameBufferObject::AttachDepthBuffer(const char*bufferName, int width, int height) { |
|||
GLuint depthMap; |
|||
glGenTextures(1, &depthMap); |
|||
glBindTexture(GL_TEXTURE_2D, depthMap); |
|||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|||
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, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); |
|||
glBindTexture(GL_TEXTURE_2D, 0); |
|||
|
|||
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject); |
|||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0); |
|||
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
|||
|
|||
mBuffers.insert(std::pair<std::string, GLuint>(bufferName, depthMap)); |
|||
} |
|||
void FrameBufferObject::Finish() { |
|||
int nCount = (int)mDrawBuffers.size(); |
|||
if (nCount > 0) { |
|||
GLenum *buffers = new GLenum[nCount]; |
|||
int i = 0; |
|||
while (i<nCount) { |
|||
buffers[i] = mDrawBuffers[i]; |
|||
i++; |
|||
} |
|||
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject); |
|||
glDrawBuffers(nCount, buffers); |
|||
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
|||
} |
|||
} |
|||
void FrameBufferObject::Bind() { |
|||
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &mPrevFrameBuffer);//绑定前记录一下buffer,为解绑返回这个buffer做准备
|
|||
glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject); |
|||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
|||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|||
} |
|||
void FrameBufferObject::Unbind() { |
|||
glBindFramebuffer(GL_FRAMEBUFFER, mPrevFrameBuffer); |
|||
} |
|||
GLuint FrameBufferObject::GetBuffer(const char*bufferName) { |
|||
auto iter = mBuffers.find(bufferName); |
|||
if (iter != mBuffers.end()) { |
|||
return iter->second; |
|||
} |
|||
return 0; |
|||
} |
@ -0,0 +1,17 @@ |
|||
#pragma once |
|||
#include "ggl.h" |
|||
class FrameBufferObject { |
|||
public: |
|||
GLuint mFrameBufferObject; |
|||
GLint mPrevFrameBuffer; |
|||
std::map<std::string, GLuint> mBuffers; |
|||
std::vector<GLenum> mDrawBuffers; |
|||
public: |
|||
FrameBufferObject(); |
|||
void AttachColorBuffer(const char*bufferName, GLenum attachment, int width, int height); |
|||
void AttachDepthBuffer(const char*bufferName, int width, int height); |
|||
void Finish(); |
|||
void Bind(); |
|||
void Unbind(); |
|||
GLuint GetBuffer(const char*bufferName); |
|||
}; |
@ -0,0 +1,44 @@ |
|||
#include "particleSystem.h"
|
|||
#include "utils.h"
|
|||
|
|||
void ParticleSystem::Init(float x, float y, float z) |
|||
{ |
|||
mModelMatrix = glm::translate(x, y, z); |
|||
mVertexBuffer = new VertexBuffer; |
|||
int particleCount = 180; |
|||
mVertexBuffer->SetSize(particleCount); |
|||
for (int i = 0; i < particleCount; ++i) { |
|||
mVertexBuffer->SetPosition(i, 2.0f*cosf(float(i) * 8.0f*3.14f / 180.0f), 0.0f, 2.0f*sinf(float(i) * 8.0f*3.14f / 180.0f)); |
|||
mVertexBuffer->SetColor(i, 0.1f, 0.4f, 0.6f); |
|||
} |
|||
|
|||
mShader = new Shader; |
|||
mShader->Init("Res/particle.vs", "Res/particle.fs"); |
|||
mShader->SetTexture("U_Texture", CreateProcedureTexture(128)); |
|||
} |
|||
void ParticleSystem::Draw(glm::mat4 & viewMatrix, glm::mat4 & projectionMatrix) { |
|||
glEnable(GL_POINT_SPRITE); |
|||
glEnable(GL_PROGRAM_POINT_SIZE); |
|||
glDisable(GL_DEPTH_TEST); |
|||
glEnable(GL_BLEND); |
|||
glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
|||
mVertexBuffer->Bind(); |
|||
mShader->Bind(glm::value_ptr(mModelMatrix), glm::value_ptr(viewMatrix), glm::value_ptr(projectionMatrix)); |
|||
glDrawArrays(GL_POINTS, 0, mVertexBuffer->mVertexCount); |
|||
mVertexBuffer->Unbind(); |
|||
glDisable(GL_BLEND); |
|||
glDisable(GL_POINT_SPRITE); |
|||
glDisable(GL_PROGRAM_POINT_SIZE); |
|||
} |
|||
void ParticleSystem::Update(float deltaTime) { |
|||
static float angle = 0.0f; |
|||
angle += deltaTime*10.0f; |
|||
mModelMatrix = glm::rotate(angle, 0.0f, 1.0f, 0.0f); |
|||
for (int i = 0; i < mVertexBuffer->mVertexCount; ++i) { |
|||
Vertex &vertex = mVertexBuffer->Get(i); |
|||
vertex.Normal[1] = 0.1f*i; |
|||
if (i > 90) { |
|||
break; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,12 @@ |
|||
#pragma once |
|||
#include "vertexbuffer.h" |
|||
#include "shader.h" |
|||
class ParticleSystem { |
|||
VertexBuffer *mVertexBuffer; |
|||
glm::mat4 mModelMatrix; |
|||
Shader *mShader; |
|||
public: |
|||
void Init(float x, float y, float z); |
|||
void Draw(glm::mat4 & viewMatrix, glm::mat4 & projectionMatrix); |
|||
void Update(float deltaTime); |
|||
}; |
@ -0,0 +1,124 @@ |
|||
#include "skybox.h"
|
|||
#include "utils.h"
|
|||
void SkyBox::Init(const char *imageDir) { |
|||
mShader = new Shader[6]; |
|||
mVertexBuffer = new VertexBuffer[6]; |
|||
InitFront(imageDir); |
|||
InitBack(imageDir); |
|||
InitLeft(imageDir); |
|||
InitRight(imageDir); |
|||
InitTop(imageDir); |
|||
InitBottom(imageDir); |
|||
} |
|||
void SkyBox::InitFront(const char *imageDir) { |
|||
mShader[0].Init("Res/skybox.vs", "Res/skybox.fs"); |
|||
char temp[256]; |
|||
memset(temp, 0, 256); |
|||
strcpy(temp, imageDir); |
|||
strcat(temp, "front.bmp"); |
|||
mShader[0].SetTexture("U_Texture", temp); |
|||
mVertexBuffer[0].SetSize(4); |
|||
mVertexBuffer[0].SetPosition(0, -0.5f, -0.5f, -0.5f); |
|||
mVertexBuffer[0].SetTexcoord(0, 0.0f, 0.0f); |
|||
mVertexBuffer[0].SetPosition(1, 0.5f, -0.5f, -0.5f); |
|||
mVertexBuffer[0].SetTexcoord(1, 1.0f, 0.0f); |
|||
mVertexBuffer[0].SetPosition(2, -0.5f, 0.5f, -0.5f); |
|||
mVertexBuffer[0].SetTexcoord(2, 0.0f, 1.0f); |
|||
mVertexBuffer[0].SetPosition(3, 0.5f, 0.5f, -0.5f); |
|||
mVertexBuffer[0].SetTexcoord(3, 1.0f, 1.0f); |
|||
} |
|||
void SkyBox::InitBack(const char *imageDir) { |
|||
mShader[1].Init("Res/skybox.vs", "Res/skybox.fs"); |
|||
char temp[256]; |
|||
memset(temp, 0, 256); |
|||
strcpy(temp, imageDir); |
|||
strcat(temp, "back.bmp"); |
|||
mShader[1].SetTexture("U_Texture", temp); |
|||
mVertexBuffer[1].SetSize(4); |
|||
mVertexBuffer[1].SetPosition(0, 0.5f, -0.5f, 0.5f); |
|||
mVertexBuffer[1].SetTexcoord(0, 0.0f, 0.0f); |
|||
mVertexBuffer[1].SetPosition(1, -0.5f, -0.5f, 0.5f); |
|||
mVertexBuffer[1].SetTexcoord(1, 1.0f, 0.0f); |
|||
mVertexBuffer[1].SetPosition(2, 0.5f, 0.5f, 0.5f); |
|||
mVertexBuffer[1].SetTexcoord(2, 0.0f, 1.0f); |
|||
mVertexBuffer[1].SetPosition(3, -0.5f, 0.5f, 0.5f); |
|||
mVertexBuffer[1].SetTexcoord(3, 1.0f, 1.0f); |
|||
} |
|||
void SkyBox::InitLeft(const char *imageDir) { |
|||
mShader[2].Init("Res/skybox.vs", "Res/skybox.fs"); |
|||
char temp[256]; |
|||
memset(temp, 0, 256); |
|||
strcpy(temp, imageDir); |
|||
strcat(temp, "left.bmp"); |
|||
mShader[2].SetTexture("U_Texture", temp); |
|||
mVertexBuffer[2].SetSize(4); |
|||
mVertexBuffer[2].SetPosition(0, -0.5f, -0.5f, 0.5f); |
|||
mVertexBuffer[2].SetTexcoord(0, 0.0f, 0.0f); |
|||
mVertexBuffer[2].SetPosition(1, -0.5f, -0.5f, -0.5f); |
|||
mVertexBuffer[2].SetTexcoord(1, 1.0f, 0.0f); |
|||
mVertexBuffer[2].SetPosition(2, -0.5f, 0.5f, 0.5f); |
|||
mVertexBuffer[2].SetTexcoord(2, 0.0f, 1.0f); |
|||
mVertexBuffer[2].SetPosition(3, -0.5f, 0.5f, -0.5f); |
|||
mVertexBuffer[2].SetTexcoord(3, 1.0f, 1.0f); |
|||
} |
|||
void SkyBox::InitRight(const char *imageDir) { |
|||
mShader[3].Init("Res/skybox.vs", "Res/skybox.fs"); |
|||
char temp[256]; |
|||
memset(temp, 0, 256); |
|||
strcpy(temp, imageDir); |
|||
strcat(temp, "right.bmp"); |
|||
mShader[3].SetTexture("U_Texture", temp); |
|||
mVertexBuffer[3].SetSize(4); |
|||
mVertexBuffer[3].SetPosition(0, 0.5f, -0.5f, -0.5f); |
|||
mVertexBuffer[3].SetTexcoord(0, 0.0f, 0.0f); |
|||
mVertexBuffer[3].SetPosition(1, 0.5f, -0.5f, 0.5f); |
|||
mVertexBuffer[3].SetTexcoord(1, 1.0f, 0.0f); |
|||
mVertexBuffer[3].SetPosition(2, 0.5f, 0.5f, -0.5f); |
|||
mVertexBuffer[3].SetTexcoord(2, 0.0f, 1.0f); |
|||
mVertexBuffer[3].SetPosition(3, 0.5f, 0.5f, 0.5f); |
|||
mVertexBuffer[3].SetTexcoord(3, 1.0f, 1.0f); |
|||
} |
|||
void SkyBox::InitTop(const char *imageDir) { |
|||
mShader[4].Init("Res/skybox.vs", "Res/skybox.fs"); |
|||
char temp[256]; |
|||
memset(temp, 0, 256); |
|||
strcpy(temp, imageDir); |
|||
strcat(temp, "top.bmp"); |
|||
mShader[4].SetTexture("U_Texture", temp); |
|||
mVertexBuffer[4].SetSize(4); |
|||
mVertexBuffer[4].SetPosition(0, -0.5f, 0.5f, -0.5f); |
|||
mVertexBuffer[4].SetTexcoord(0, 0.0f, 0.0f); |
|||
mVertexBuffer[4].SetPosition(1, 0.5f, 0.5f, -0.5f); |
|||
mVertexBuffer[4].SetTexcoord(1, 1.0f, 0.0f); |
|||
mVertexBuffer[4].SetPosition(2, -0.5f, 0.5f, 0.5f); |
|||
mVertexBuffer[4].SetTexcoord(2, 0.0f, 1.0f); |
|||
mVertexBuffer[4].SetPosition(3, 0.5f, 0.5f, 0.5f); |
|||
mVertexBuffer[4].SetTexcoord(3, 1.0f, 1.0f); |
|||
} |
|||
void SkyBox::InitBottom(const char *imageDir) { |
|||
mShader[5].Init("Res/skybox.vs", "Res/skybox.fs"); |
|||
char temp[256]; |
|||
memset(temp, 0, 256); |
|||
strcpy(temp, imageDir); |
|||
strcat(temp, "bottom.bmp"); |
|||
mShader[5].SetTexture("U_Texture", temp); |
|||
mVertexBuffer[5].SetSize(4); |
|||
mVertexBuffer[5].SetPosition(0, -0.5f, -0.5f, 0.5f); |
|||
mVertexBuffer[5].SetTexcoord(0, 0.0f, 0.0f); |
|||
mVertexBuffer[5].SetPosition(1, 0.5f, -0.5f, 0.5f); |
|||
mVertexBuffer[5].SetTexcoord(1, 1.0f, 0.0f); |
|||
mVertexBuffer[5].SetPosition(2, -0.5f, -0.5f, -0.5f); |
|||
mVertexBuffer[5].SetTexcoord(2, 0.0f, 1.0f); |
|||
mVertexBuffer[5].SetPosition(3, 0.5f, -0.5f, -0.5f); |
|||
mVertexBuffer[5].SetTexcoord(3, 1.0f, 1.0f); |
|||
} |
|||
void SkyBox::Draw(glm::mat4 &V, glm::mat4&P, float x, float y, float z) { |
|||
glDisable(GL_DEPTH_TEST); |
|||
mModelMatrix = glm::translate(x, y, z); |
|||
for (int i = 0; i < 6; ++i) { |
|||
mVertexBuffer[i].Bind(); |
|||
mShader[i].Bind(glm::value_ptr(mModelMatrix), glm::value_ptr(V), glm::value_ptr(P)); |
|||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
|||
mVertexBuffer[i].Unbind(); |
|||
} |
|||
} |
@ -0,0 +1,17 @@ |
|||
#pragma once |
|||
#include "shader.h" |
|||
#include "vertexbuffer.h" |
|||
class SkyBox { |
|||
Shader *mShader; |
|||
VertexBuffer *mVertexBuffer; |
|||
glm::mat4 mModelMatrix; |
|||
public: |
|||
void Init(const char *imageDir); |
|||
void InitFront(const char *imageDir); |
|||
void InitBack(const char *imageDir); |
|||
void InitLeft(const char *imageDir); |
|||
void InitRight(const char *imageDir); |
|||
void InitTop(const char *imageDir); |
|||
void InitBottom(const char *imageDir); |
|||
void Draw(glm::mat4 &V, glm::mat4&P, float x, float y, float z); |
|||
}; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue