#texture not loading (getting crazy trying to abstract opengl for the first time)
11 messages · Page 1 of 1 (latest)
When your question is answered use !solved or the button below to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
void initTriangleMesh(unsigned int& VAO_tri, unsigned int& VBO_tri) {
float vertex[]{
//triangle texcoord
-0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f,
0.5f, 0.5f, 0.0f, 1.0f
};
glGenVertexArrays(1, &VAO_tri);
glBindVertexArray(VAO_tri);
glGenBuffers(1, &VBO_tri);
glBindBuffer(GL_ARRAY_BUFFER, VBO_tri);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), nullptr);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2*sizeof(float)));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
};
void triangle::draw(unsigned int program) {
glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glUniformMatrix4fv(uniformMV, 1, GL_FALSE, glm::value_ptr(mvmatrix));
glUniformMatrix4fv(uniformPerspective, 1, GL_FALSE, glm::value_ptr(perspective));
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
}
#include "../definitions/textures.h"
#include "../definitions/stb_image.h"
#include <GL/glew.h>
#include <iostream>
texture::texture(
const char* img_path, int visualoutput1,
int visualoutput2, int filter1, int filter2,
int texture_type
)
: //path(img_path),
f1(filter1),
f2(filter2),
viso1(visualoutput1),
viso2(visualoutput2),
textureType(texture_type)
{
data = stbi_load(img_path, &w, &h, &nrChannels, 0);
if (!data) {
std::cout << "ERROR: Could not find or load texture at: " << img_path << std::endl;
}
}
void texture::init(int level, int format1, int format2)
{
glGenTextures(1, &ID);
glBindTexture(textureType, ID);
glTexParameteri(textureType, GL_TEXTURE_WRAP_S, viso1);
glTexParameteri(textureType, GL_TEXTURE_WRAP_T, viso2);
glTexParameteri(textureType, GL_TEXTURE_MIN_FILTER, f1);
glTexParameteri(textureType, GL_TEXTURE_MAG_FILTER, f2);
glTexImage2D(textureType, level, format1, w, h, 0, format2, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
glGenerateMipmap(textureType);
glBindTexture(textureType, 0);
}
void texture::useTexture() {
glBindTexture(textureType, ID);
}
``` this one is the texture thing
const char* vertexShader_src{
R"(
#version 330 core
layout(location=0) in vec3 position;
layout(location=1) in vec2 tex_coord;
out vec2 textCoordinate;
uniform mat4 mv;
uniform mat4 perspective;
void main(){
textCoordinate = tex_coord;
gl_Position = perspective * mv * vec4(position, 1.0f);
}
)"
};
const char* fragmentShader_src{
R"(
#version 330 core
in vec2 textCoordinate;
uniform sampler2D tex;
out vec4 color;
void main(){
color = texture(tex, textCoordinate);
}
)"
};
``` this one is the shader code
my question is why isnt it rendering the texture
ignore the triangle rotation i was messing around with the transofrmation
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "definitions/shapes.h"
#include "definitions/shaders.h"
int main() {
if (!glfwInit()) {
std::cout << "glfw failed to initialize\n";
return -1;
}
GLFWwindow* window = glfwCreateWindow(800, 600, "dumb engine", nullptr, nullptr);
if (!window) {
std::cout << "window failed to create\n";
return -1;
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
std::cout << "glew failed to initialize\n";
return -1;
}
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback([](GLenum src, GLenum type, GLuint id, GLenum sev, GLsizei len, const GLchar* msg, const void*) {
std::cerr << "GL debug: " << msg << '\n';
}, nullptr);
const char* wall_texture = "../../../Users/HOME/Documents/openglimage/wall.jpg";
unsigned int VAO_triangle, VBO_triangle;
initTriangleMesh(VAO_triangle, VBO_triangle);
unsigned int program = initProgramAndShaders(); // sets up the program and shaders in the program
texture* wall_tex = new texture{ wall_texture,
GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR,
GL_TEXTURE_2D
};
wall_tex->init(0, GL_RGB, GL_RGB);
triangle test(VAO_triangle);
test.setTexture(wall_tex);
test.setTranslateWorld(glm::vec3(0.0f, 0.0f, -1.0f));
test.rotate(45.0f, glm::vec3(0, 0, 1));
test.setProgramUniLoc(program);
glUseProgram(program);
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
test.draw(program);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteProgram(program);
}
``` this is the main code if u want to see it
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity