Imagem não carrega, opengl

0 respostas
Sandro_Maurer

Preciso carregar 2 imagens mas, só fica com tela branca .

#include “SOIL.h”

#include 

#include 

#include 

#include “GL/glew.h”	// Important - this header must come before glfw3 header

#include “GLFW/glfw3.h”

#include “glm/glm.hpp”

#define STB_IMAGE_IMPLEMENTATION

#include “stb_image_aug.h”

#include “image_DXT.h”

#include"image_helper.h"

#include “System.h”

#include <stdlib.h>

#include <string.h>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);

int main() {

if (!glfwInit()) {

fprintf(stderr, ERROR: could not start GLFW3\n);

return 1;

}
GLFWwindow* window = glfwCreateWindow(
	1600, 900, "Teste de versão OpenGL", NULL, NULL);
if (!window) {
	fprintf(stderr, "ERROR: could not open window with GLFW3\n");
	glfwTerminate();
	return 1;
}
glfwMakeContextCurrent(window);
// inicia manipulador da extensão GLEW
glewExperimental = GL_TRUE;
glewInit();

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

float vertices[] = {
	// positions // colors // texture coords
	 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.5f, 1.5f, // top right
	0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.5f, -0.5f, // bottom right
	-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, -0.5f, -0.5f, // bottom left
	-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, -0.5f, 1.5f // top left
};

unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};

const char* vertex_shader =
	"#verson 410"
	"layout(location = 0) in vec3 aPos;"
	"layout(location = 1) in vec3 aColor;"
	"	layout(location = 2) in vec2 aTexCoord;"

	"out vec3 ourColor;"
	"out vec2 TexCoord;"

	"void main()"
	"{"
	"gl_Position = vec4(aPos, 1.0);"
	"	ourColor = aColor;"
	"TexCoord = vec2(aTexCoord.x, aTexCoord.y);"
	"}";

const char* fragment_shader =
	"#version 410"
	"out vec4 FragColor;"

	"	in vec3 ourColor;"
	"in vec2 TexCoord;"

	// texture samplers
	"uniform sampler2D texture1;"
	"uniform sampler2D texture2;"

	"void main()"
	"{"
	// linearly interpolate between both textures (80% container, 20% awesomeface)
	"	FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);"
	"}";




GLuint VBO, VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);




unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);


GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
// identifica fs e o associa com fragment_shader
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);
// identifica do programa, adiciona partes e faz "linkagem"
GLuint shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme);
// carrega img

int width, height, nrChannels;

unsigned int texture1;

unsigned int texture2;
unsigned char* data = stbi_load(crate.jpg, &width, &height, &nrChannels, 0);

glGenTextures(1, &texture1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	// set texture wrapping to GL_REPEAT (default wrapping method)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

// set texture filtering parameters

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (data)

{

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

glGenerateMipmap(GL_TEXTURE_2D);

}

else

{

std::cout << Failed to load texture << std::endl;

}

stbi_image_free(data);

//___________________________________

glGenTextures(1, &texture2);

glBindTexture(GL_TEXTURE_2D, texture2);

// set the texture wrapping parameters

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	// set texture wrapping to GL_REPEAT (default wrapping method)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

// set texture filtering parameters

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

// load image, create texture and generate mipmaps

data = stbi_load(montanha.png, &width, &height, &nrChannels, 0);

if (data)

{

// note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

glGenerateMipmap(GL_TEXTURE_2D);

}

else

{

std::cout << Failed to load texture << std::endl;

}

stbi_image_free(data);

glUniform1i(
glGetUniformLocation(shader_programme, “texture1”), 0);

glUniform1i(
glGetUniformLocation(shader_programme, “texture1”), 0);

while (!glfwWindowShouldClose(window)) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);




// bind Texture

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);

// render container

glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

glfwSwapBuffers(window);
glfwPollEvents();

}

glfwTerminate();

return 0;

}
Criado 18 de abril de 2019
Respostas 0
Participantes 1