Skip to content
This repository has been archived by the owner on Aug 30, 2020. It is now read-only.

Commit

Permalink
Added OpenGL 4.2 texture cube sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Riccio committed Jul 2, 2012
1 parent ccb6f55 commit 03c3f17
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 0 deletions.
21 changes: 21 additions & 0 deletions data/gl-420/texture-cube.frag
@@ -0,0 +1,21 @@
#version 330 core

#define POSITION 0
#define COLOR 3
#define TEXCOORD 4
#define REFLECT 6
#define FRAG_COLOR 0

uniform samplerCube Environment;

in block
{
vec3 Reflect;
} In;

layout(location = FRAG_COLOR, index = 0) out vec4 Color;

void main()
{
Color = texture(Environment, In.Reflect);
}
32 changes: 32 additions & 0 deletions data/gl-420/texture-cube.vert
@@ -0,0 +1,32 @@
#version 330 core

#define POSITION 0
#define COLOR 3
#define TEXCOORD 4
#define FRAG_COLOR 0

uniform mat4 MV;
uniform mat4 MVP;
uniform vec3 Camera;

const vec3 ConstView = vec3(0, 0,-1);
const vec3 ConstNormal = vec3(0, 0, 1);

layout(location = POSITION) in vec2 Position;

out block
{
vec3 Reflect;
} Out;

void main()
{
mat3 MV3x3 = mat3(MV);

gl_Position = MVP * vec4(Position, 0.0, 1.0);
vec3 P = MV3x3 * vec3(Position, 0.0);
vec3 N = MV3x3 * ConstNormal;
vec3 E = normalize(P - Camera);

Out.Reflect = reflect(E, N);
}
238 changes: 238 additions & 0 deletions samples/gl-420-texture-cube.cpp
@@ -0,0 +1,238 @@
//**********************************
// OpenGL Cube map
// 02/07/2012
//**********************************
// Christophe Riccio
// ogl-samples@g-truc.net
//**********************************
// G-Truc Creation
// www.g-truc.net
//**********************************

#include <glf/glf.hpp>

namespace
{
std::string const SAMPLE_NAME("OpenGL Cube map");
std::string const VERT_SHADER_SOURCE(glf::DATA_DIRECTORY + "gl-420/texture-cube.vert");
std::string const FRAG_SHADER_SOURCE(glf::DATA_DIRECTORY + "gl-420/texture-cube.frag");
std::string const TEXTURE_DIFFUSE(glf::DATA_DIRECTORY + "cube.dds");
int const SAMPLE_SIZE_WIDTH(640);
int const SAMPLE_SIZE_HEIGHT(480);
int const SAMPLE_MAJOR_VERSION(4);
int const SAMPLE_MINOR_VERSION(2);

glf::window Window(glm::ivec2(SAMPLE_SIZE_WIDTH, SAMPLE_SIZE_HEIGHT));

// With DDS textures, v texture coordinate are reversed, from top to bottom
GLsizei const VertexCount(6);
GLsizeiptr const VertexSize = VertexCount * sizeof(glm::vec2);
glm::vec2 const VertexData[VertexCount] =
{
glm::vec2(-1.0f,-1.0f),
glm::vec2( 1.0f,-1.0f),
glm::vec2( 1.0f, 1.0f),
glm::vec2( 1.0f, 1.0f),
glm::vec2(-1.0f, 1.0f),
glm::vec2(-1.0f,-1.0f)
};

GLuint VertexArrayName = 0;
GLuint ProgramName = 0;

GLuint BufferName = 0;
GLuint TextureName = 0;
GLuint SamplerName = 0;

GLint UniformMV = 0;
GLint UniformMVP = 0;
GLint UniformEnvironment = 0;
GLint UniformCamera = 0;

glm::ivec4 Viewport;
}//namespace

bool initDebugOutput()
{
bool Validated(true);

glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
glDebugMessageCallbackARB(&glf::debugOutput, NULL);

return Validated;
}

bool initProgram()
{
bool Validated = true;

if(Validated)
{
GLuint VertShaderName = glf::createShader(GL_VERTEX_SHADER, VERT_SHADER_SOURCE);
GLuint FragShaderName = glf::createShader(GL_FRAGMENT_SHADER, FRAG_SHADER_SOURCE);

ProgramName = glCreateProgram();
glAttachShader(ProgramName, VertShaderName);
glAttachShader(ProgramName, FragShaderName);
glDeleteShader(VertShaderName);
glDeleteShader(FragShaderName);

glLinkProgram(ProgramName);
Validated = glf::checkProgram(ProgramName);
}

if(Validated)
{
UniformMV = glGetUniformLocation(ProgramName, "MV");
UniformMVP = glGetUniformLocation(ProgramName, "MVP");
UniformEnvironment = glGetUniformLocation(ProgramName, "Environment");
UniformCamera = glGetUniformLocation(ProgramName, "Camera");
}

return Validated &&glf::checkError("initProgram");
}

bool initBuffer()
{
glGenBuffers(1, &BufferName);

glBindBuffer(GL_ARRAY_BUFFER, BufferName);
glBufferData(GL_ARRAY_BUFFER, VertexSize, VertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

return glf::checkError("initBuffer");;
}

bool initSampler()
{
glGenSamplers(1, &SamplerName);

glSamplerParameteri(SamplerName, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glSamplerParameteri(SamplerName, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glSamplerParameteri(SamplerName, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glSamplerParameteri(SamplerName, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glSamplerParameteri(SamplerName, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glSamplerParameterfv(SamplerName, GL_TEXTURE_BORDER_COLOR, &glm::vec4(0.0f)[0]);
glSamplerParameterf(SamplerName, GL_TEXTURE_MIN_LOD, -1000.f);
glSamplerParameterf(SamplerName, GL_TEXTURE_MAX_LOD, 1000.f);
glSamplerParameterf(SamplerName, GL_TEXTURE_LOD_BIAS, 0.0f);
glSamplerParameteri(SamplerName, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glSamplerParameteri(SamplerName, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glSamplerParameterf(SamplerName, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);

return glf::checkError("initSampler");
}

bool initTexture()
{
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &TextureName);
glBindTexture(GL_TEXTURE_CUBE_MAP, TextureName);

gli::textureCube Texture = gli::loadTextureCubeDDS9(TEXTURE_DIFFUSE);

glTexStorage2D(GL_TEXTURE_CUBE_MAP, GLint(Texture.levels()), GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GLsizei(Texture[gli::POSITIVE_X][0].dimensions().x), GLsizei(Texture[gli::POSITIVE_X][0].dimensions().y));

for(std::size_t Face = 0; Face < 6; ++Face)
for(std::size_t Level = 0; Level < Texture.levels(); ++Level)
{
glCompressedTexSubImage2D(
GL_TEXTURE_CUBE_MAP_POSITIVE_X + Face,
GLint(Level),
0, 0,
GLsizei(Texture[gli::textureCube::face_type(Face)][Level].dimensions().x),
GLsizei(Texture[gli::textureCube::face_type(Face)][Level].dimensions().y),
GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
GLsizei(Texture[gli::textureCube::face_type(Face)][Level].capacity()),
Texture[gli::textureCube::face_type(Face)][Level].data());
}

return glf::checkError("initTexture2D");
}

bool initVertexArray()
{
glGenVertexArrays(1, &VertexArrayName);
glBindVertexArray(VertexArrayName);
glBindBuffer(GL_ARRAY_BUFFER, BufferName);
glVertexAttribPointer(glf::semantic::attr::POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), GLF_BUFFER_OFFSET(0));
glBindBuffer(GL_ARRAY_BUFFER, 0);

glEnableVertexAttribArray(glf::semantic::attr::POSITION);
glBindVertexArray(0);

return glf::checkError("initVertexArray");
}

bool begin()
{
bool Validated = glf::checkGLVersion(SAMPLE_MAJOR_VERSION, SAMPLE_MINOR_VERSION);

if(Validated && glf::checkExtension("GL_ARB_debug_output"))
Validated = initDebugOutput();
if(Validated)
Validated = initProgram();
if(Validated)
Validated = initBuffer();
if(Validated)
Validated = initVertexArray();
if(Validated)
Validated = initTexture();
if(Validated)
Validated = initSampler();

return Validated && glf::checkError("begin");
}

bool end()
{
glDeleteBuffers(1, &BufferName);
glDeleteProgram(ProgramName);
glDeleteTextures(1, &TextureName);
glDeleteSamplers(1, &SamplerName);
glDeleteVertexArrays(1, &VertexArrayName);

return glf::checkError("end");
}

void display()
{
// Compute the MVP (Model View Projection matrix)
glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 1000.0f);
glm::mat4 ViewTranslate = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -Window.TranlationCurrent.y));
glm::mat4 ViewRotateX = glm::rotate(ViewTranslate, Window.RotationCurrent.y, glm::vec3(1.f, 0.f, 0.f));
glm::mat4 View = glm::rotate(ViewRotateX, Window.RotationCurrent.x, glm::vec3(0.f, 1.f, 0.f));
glm::mat4 Model = glm::mat4(1.0f);
glm::mat4 MVP = Projection * View * Model;
glm::mat4 MV = View * Model;

glViewport(0, 0, Window.Size.x, Window.Size.y);
glClearBufferfv(GL_COLOR, 0, &glm::vec4(1.0f, 0.5f, 0.0f, 1.0f)[0]);

// Bind the program for use
glUseProgram(ProgramName);
glUniformMatrix4fv(UniformMV, 1, GL_FALSE, &MV[0][0]);
glUniformMatrix4fv(UniformMVP, 1, GL_FALSE, &MVP[0][0]);
glUniform1i(UniformEnvironment, 0);
glUniform3fv(UniformCamera, 1, &glm::vec3(0.0f, 0.0f, -Window.TranlationCurrent.y)[0]);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, TextureName);
glBindSampler(0, SamplerName);

glBindVertexArray(VertexArrayName);
glDrawArraysInstanced(GL_TRIANGLES, 0, VertexCount, 1);

glf::checkError("display");
glf::swapBuffers();
}

int main(int argc, char* argv[])
{
return glf::run(
argc, argv,
glm::ivec2(::SAMPLE_SIZE_WIDTH, ::SAMPLE_SIZE_HEIGHT),
WGL_CONTEXT_CORE_PROFILE_BIT_ARB, ::SAMPLE_MAJOR_VERSION,
::SAMPLE_MINOR_VERSION);
}

0 comments on commit 03c3f17

Please sign in to comment.