MaterialPtr NormalMapScene::createTextureMaterial( const QString& diffuseFileName, const QString& normalFileName ) { // Create a material and set the shaders MaterialPtr material( new Material ); #if !defined(Q_OS_MAC) material->setShaders( ":/shaders/normalmap.vert", ":/shaders/normalmap.frag" ); #else material->setShaders( ":/shaders/normalmap_2_1.vert", ":/shaders/normalmap_2_1.frag" ); #endif // Create a diffuse texture TexturePtr texture0( new Texture( Texture::Texture2D ) ); texture0->create(); texture0->bind(); texture0->setImage( QImage( diffuseFileName ) ); texture0->generateMipMaps(); // Create a normal map TexturePtr texture1( new Texture( Texture::Texture2D ) ); texture1->create(); texture1->bind(); texture1->setImage( QImage( normalFileName ) ); texture1->generateMipMaps(); #if !defined(Q_OS_MAC) // Create a sampler. This can be shared by many textures SamplerPtr sampler( new Sampler ); sampler->create(); sampler->setWrapMode( Sampler::DirectionS, GL_CLAMP_TO_EDGE ); sampler->setWrapMode( Sampler::DirectionT, GL_CLAMP_TO_EDGE ); sampler->setMinificationFilter( GL_LINEAR_MIPMAP_LINEAR ); sampler->setMagnificationFilter( GL_LINEAR ); // Associate the textures with the first 2 texture units material->setTextureUnitConfiguration( 0, texture0, sampler, QByteArrayLiteral( "texture0" ) ); material->setTextureUnitConfiguration( 1, texture1, sampler, QByteArrayLiteral( "texture1" ) ); #else texture0->bind(); texture0->setWrapMode( Texture::DirectionS, GL_CLAMP_TO_EDGE ); texture0->setWrapMode( Texture::DirectionT, GL_CLAMP_TO_EDGE ); texture0->setMinificationFilter( GL_LINEAR_MIPMAP_LINEAR ); texture0->setMagnificationFilter( GL_LINEAR ); texture1->bind(); texture1->setWrapMode( Texture::DirectionS, GL_CLAMP_TO_EDGE ); texture1->setWrapMode( Texture::DirectionT, GL_CLAMP_TO_EDGE ); texture1->setMinificationFilter( GL_LINEAR_MIPMAP_LINEAR ); texture1->setMagnificationFilter( GL_LINEAR ); // Associate the textures with the first 2 texture units material->setTextureUnitConfiguration( 0, texture0, QByteArrayLiteral( "texture0" ) ); material->setTextureUnitConfiguration( 1, texture1, QByteArrayLiteral( "texture1" ) ); #endif return material; }
int main() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); GLFWwindow* window = glfwCreateWindow(800, 600, "Learn OpenGL", nullptr, nullptr); if (window == nullptr) { std::cerr << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { std::cerr << "Failed to initialize GLEW" << std::endl; return -1; } // Set up OpenGL to draw a triangle glViewport(0, 0, 800, 600); Shader shaderProgram("shaders/transform.vs", "shaders/textured.frag"); GLfloat vertices[] = { 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left }; GLuint indices[] = { 0, 1, 3, // First triangle 1, 2, 3 // Second triangle }; GLuint VBO; glGenBuffers(1, &VBO); GLuint EBO; glGenBuffers(1, &EBO); GLuint VAO; glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(1); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat))); glEnableVertexAttribArray(2); glBindVertexArray(0); Texture texture1("textures/container.jpg"); Texture texture2("textures/awesomeface.png"); glfwSetKeyCallback(window, key_callback); while(!glfwWindowShouldClose(window)) { glfwPollEvents(); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); GLuint transformLoc = shaderProgram.UniformLocation("transform"); shaderProgram.Use(); glUniform1f(shaderProgram.UniformLocation("faceVisibility"), g_visibility); // Bind textures using texture units glActiveTexture(GL_TEXTURE0); texture1.Bind(); glUniform1i(shaderProgram.UniformLocation("ourTexture1"), 0); glActiveTexture(GL_TEXTURE1); texture2.Bind(); glUniform1i(shaderProgram.UniformLocation("ourTexture2"), 1); glBindVertexArray(VAO); glm::mat4 trans; trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f)); trans = glm::rotate(trans, glm::radians((GLfloat)glfwGetTime() * 50.0f), glm::vec3(0.0, 0.0, 1.0)); glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans)); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glm::mat4 trans2; GLfloat scaleValue = sin((GLfloat)glfwGetTime()); trans2 = glm::translate(trans2, glm::vec3(-0.5f, 0.5f, 0.0f)); trans2 = glm::scale(trans2, glm::vec3(scaleValue, scaleValue, scaleValue)); glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans2)); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glBindVertexArray(0); glfwSwapBuffers(window); } glfwTerminate(); return 0; }
MaterialPtr EarthScene::createMaterial( const QString& colorFileName, const QString& specularFileName, const QString& nightlightsFileName ) { // Create a material and set the shaders MaterialPtr material( new Material ); material->setShaders( ":/shaders/earth.vert", ":/shaders/earth.frag" ); // Create a diffuse color texture TexturePtr texture0( new Texture( Texture::Texture2D ) ); texture0->create(); texture0->bind(); texture0->setImage( QImage( colorFileName ) ); texture0->generateMipMaps(); // Create a specular texture map TexturePtr texture1( new Texture( Texture::Texture2D ) ); texture1->create(); texture1->bind(); texture1->setImage( QImage( specularFileName ) ); texture1->generateMipMaps(); // Create a nighttime lights texture TexturePtr texture2( new Texture( Texture::Texture2D ) ); texture2->create(); texture2->bind(); texture2->setImage( QImage( nightlightsFileName ) ); texture2->generateMipMaps(); #if !defined(Q_OS_MAC) // Create a sampler. This can be shared by many textures SamplerPtr sampler( new Sampler ); sampler->create(); sampler->setWrapMode( Sampler::DirectionS, GL_CLAMP_TO_EDGE ); sampler->setWrapMode( Sampler::DirectionT, GL_CLAMP_TO_EDGE ); sampler->setMinificationFilter( GL_LINEAR_MIPMAP_LINEAR ); sampler->setMagnificationFilter( GL_LINEAR ); // Associate the textures with the first 2 texture units material->setTextureUnitConfiguration( 0, texture0, sampler, QByteArrayLiteral( "dayColor" ) ); material->setTextureUnitConfiguration( 1, texture1, sampler, QByteArrayLiteral( "specularMap" ) ); material->setTextureUnitConfiguration( 2, texture2, sampler, QByteArrayLiteral( "nightColor" ) ); #else texture0->bind(); texture0->setWrapMode( Texture::DirectionS, GL_CLAMP_TO_EDGE ); texture0->setWrapMode( Texture::DirectionT, GL_CLAMP_TO_EDGE ); texture0->setMinificationFilter( GL_LINEAR_MIPMAP_LINEAR ); texture0->setMagnificationFilter( GL_LINEAR ); texture1->bind(); texture1->setWrapMode( Texture::DirectionS, GL_CLAMP_TO_EDGE ); texture1->setWrapMode( Texture::DirectionT, GL_CLAMP_TO_EDGE ); texture1->setMinificationFilter( GL_LINEAR_MIPMAP_LINEAR ); texture1->setMagnificationFilter( GL_LINEAR ); texture2->bind(); texture2->setWrapMode( Texture::DirectionS, GL_CLAMP_TO_EDGE ); texture2->setWrapMode( Texture::DirectionT, GL_CLAMP_TO_EDGE ); texture2->setMinificationFilter( GL_LINEAR_MIPMAP_LINEAR ); texture2->setMagnificationFilter( GL_LINEAR ); // Associate the textures with the first 2 texture units material->setTextureUnitConfiguration( 0, texture0, QByteArrayLiteral( "dayColor" ) ); material->setTextureUnitConfiguration( 1, texture1, QByteArrayLiteral( "specularMap" ) ); material->setTextureUnitConfiguration( 2, texture2, QByteArrayLiteral( "nightColor" ) ); #endif return material; }