Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[OpenGL GLFW GLAD] Przypisanie tekstury do Modelu 3D

Ostatnio zmodyfikowano 2024-12-31 14:55
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
[OpenGL GLFW GLAD] Przypisanie tekstury do Modelu 3D
» 2024-12-31 14:44:48
Witam. Ucze się OpenGL a dokładniej GLFW GLAD. Przerabiam kurs LearnOpenGL i jestem na etapie tranfsormowania modelu (https://learnopengl.com/Getting-started/Coordinate-Systems). Problem, który napotkałem polega na tym, że program nie renderuje poprawnie tekstury na modelu sześcianu za to renderuje jednolity kolor. Program się kompiluje. Próbowałem namierzyć błąd wraz z ChatemGPT ale nic z tego :-/

tekstura:

wynik programu:

C/C++
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#define STB_IMAGE_IMPLEMENTATION    
// for work of stb_image.h
#include "stb_image.h"
#include <iostream>



const char * vertex_shader_source = R"( #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aColor; layout (location = 2) in vec2 aTexCoord; out vec3 ourColor; out vec2 TexCoord; uniform mat4 projection; uniform mat4 view; uniform mat4 transform; void main() { gl_Position = projection * view * transform * vec4(aPos, 1.0); ourColor = aColor; TexCoord = aTexCoord; } )";

const char * fragment_shader_source = R"( #version 330 core out vec4 FragColor; in vec3 ourColor; // z vertex shadera in vec2 TexCoord; // współrzędne tekstury uniform sampler2D texture1; // tekstura void main() { FragColor = texture(texture1, TexCoord); // użycie tekstury } )";

///////////////////////////////////////////////////////////////////////////////////////////////////////////////


GLFWwindow * window;
unsigned int vertex_shader;
unsigned int fragment_shader;
unsigned int shader_program;


class Model {
public:
   
unsigned int VAO; // VERTEX ARRAY OBJECTS (VAO stores information about how data in VBO and EBO is used)
   
unsigned int VBO; // VERTEX BUFFER OBJECTS (VERTEXES)
   
unsigned int EBO; // ELEMENT BUFFER OBJECTS (INDICES)
   
unsigned int program;
   
   
int width, height, nrChannels;
   
unsigned int texture;
   
   
glm::vec3 position = glm::vec3( 0.f, 0.f, 0.f );
   
glm::vec3 rotation = glm::vec3( 1.0f, 0.3f, 0.5f );
   
float angle_y = 1.0f;
   
   
Model() {
       
       
// vertexes and indices
       
glGenVertexArrays( 1, & VAO ); // VAO
       
glGenBuffers( 1, & VBO ); // ...
       
glGenBuffers( 1, & EBO ); // ...
       
glBindVertexArray( VAO ); // VAO
       
        ///////////////////////////////////////////////////////
        // texture
       
width = height = 0;
       
glGenTextures( 1, & texture );
       
glBindTexture( GL_TEXTURE_2D, texture );
       
       
///////////////////////////////////////////////////////
       
        // texture repeating
        // set the texture wrapping parameters
       
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
       
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
       
// set texture filtering parameters
       
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
       
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
       
   
}
   
   
~Model() {
       
glDeleteVertexArrays( 1, & VAO );
       
glDeleteBuffers( 1, & VBO );
       
glDeleteBuffers( 1, & EBO );
       
glDeleteTextures( 1, & texture );
   
}
   
   
void Load() {
       
       
// cube
       
float vertices[ ] = {
           
// x, y, z, tu, tv
            // tu - texture x
            // tv - texture y
           
- 0.5f, - 0.5f, - 0.5f, 0.0f, 0.0f,
           
0.5f, - 0.5f, - 0.5f, 1.0f, 0.0f,
           
0.5f, 0.5f, - 0.5f, 1.0f, 1.0f,
           
0.5f, 0.5f, - 0.5f, 1.0f, 1.0f,
           
- 0.5f, 0.5f, - 0.5f, 0.0f, 1.0f,
           
- 0.5f, - 0.5f, - 0.5f, 0.0f, 0.0f,
           
           
- 0.5f, - 0.5f, 0.5f, 0.0f, 0.0f,
           
0.5f, - 0.5f, 0.5f, 1.0f, 0.0f,
           
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
           
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
           
- 0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
           
- 0.5f, - 0.5f, 0.5f, 0.0f, 0.0f,
           
           
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
           
- 0.5f, 0.5f, - 0.5f, 1.0f, 1.0f,
           
- 0.5f, - 0.5f, - 0.5f, 0.0f, 1.0f,
           
- 0.5f, - 0.5f, - 0.5f, 0.0f, 1.0f,
           
- 0.5f, - 0.5f, 0.5f, 0.0f, 0.0f,
           
- 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
           
           
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
           
0.5f, 0.5f, - 0.5f, 1.0f, 1.0f,
           
0.5f, - 0.5f, - 0.5f, 0.0f, 1.0f,
           
0.5f, - 0.5f, - 0.5f, 0.0f, 1.0f,
           
0.5f, - 0.5f, 0.5f, 0.0f, 0.0f,
           
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
           
           
- 0.5f, - 0.5f, - 0.5f, 0.0f, 1.0f,
           
0.5f, - 0.5f, - 0.5f, 1.0f, 1.0f,
           
0.5f, - 0.5f, 0.5f, 1.0f, 0.0f,
           
0.5f, - 0.5f, 0.5f, 1.0f, 0.0f,
           
- 0.5f, - 0.5f, 0.5f, 0.0f, 0.0f,
           
- 0.5f, - 0.5f, - 0.5f, 0.0f, 1.0f,
           
           
- 0.5f, 0.5f, - 0.5f, 0.0f, 1.0f,
           
0.5f, 0.5f, - 0.5f, 1.0f, 1.0f,
           
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
           
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
           
- 0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
           
- 0.5f, 0.5f, - 0.5f, 0.0f, 1.0f
       
};
       
       
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
       
glBindVertexArray( VAO );
       
       
// VBO - VERTEXES
       
glBindBuffer( GL_ARRAY_BUFFER, VBO );
       
glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );
       
       
// position attribute
       
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof( float ),( void * ) 0 );
       
glEnableVertexAttribArray( 0 );
       
       
// texture coord attribute
       
glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof( float ),( void * )( 3 * sizeof( float ) ) );
       
glEnableVertexAttribArray( 1 );
       
       
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
        // load the texture
       
stbi_set_flip_vertically_on_load( true ); // tell stb_image.h to flip loaded texture's on the y-axis.
       
unsigned char * data = stbi_load( "wall.jpg", & width, & height, & nrChannels, 0 );
       
       
if( data ) {
           
GLenum format = nrChannels == 4 ? GL_RGBA: GL_RGB;
           
glTexImage2D( GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data );
           
glGenerateMipmap( GL_TEXTURE_2D );
       
}
       
else
           
 std::cout << "Failed to load texture\n";
       
       
stbi_image_free( data );
       
       
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
        // load the program
       
program = shader_program;
       
   
}
   
   
void draw() {
       
       
// set the texture for program
       
glActiveTexture( GL_TEXTURE0 ); // activation texture 0
       
glBindTexture( GL_TEXTURE_2D, texture ); // set the texture
       
        // calculate time for effects
       
float timeValue = glfwGetTime();
       
float greenValue =( sin( timeValue ) / 2.0f ) + 0.5f;
       
int vertexColorLocation = glGetUniformLocation( program, "ourColor" );
       
       
angle_y += 1.0f;
       
if( angle_y >= 360.0f )
           
 angle_y -= 360.0f;
       
       
// transformations
       
glm::mat4 view = glm::mat4( 1.0f );
       
glm::mat4 projection = glm::mat4( 1.0f );
       
glm::mat4 transform = glm::mat4( 1.0f );
       
transform = glm::translate( transform, position );
       
transform = glm::rotate( transform, glm::radians( angle_y ), rotation );
       
       
glm::vec2 screen_size( 800, 600 );
       
projection = glm::perspective( glm::radians( 45.0f ), screen_size.x / screen_size.y, 0.1f, 100.0f );
       
view = glm::translate( view, glm::vec3( 0.0f, 0.0f, - 3.0f ) );
       
       
glUseProgram( program );
       
glUniform1i( glGetUniformLocation( program, "texture1" ), 0 ); // set the uniform texture
       
glUniform4f( vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f );
       
glUniformMatrix4fv( glGetUniformLocation( program, "projection" ), 1, GL_FALSE, & projection[ 0 ][ 0 ] );
       
glUniformMatrix4fv( glGetUniformLocation( program, "view" ), 1, GL_FALSE, & view[ 0 ][ 0 ] );
       
glUniformMatrix4fv( glGetUniformLocation( program, "transform" ), 1, GL_FALSE, & transform[ 0 ][ 0 ] );
       
       
glBindVertexArray( VAO );
       
glDrawArrays( GL_TRIANGLES, 0, 36 );
   
}
   
}
;

void window_set_size( int width, int height ) {
   
glViewport( 0, 0, width, height );
}

void events()
{
   
if( glfwGetKey( window, GLFW_KEY_ESCAPE ) == GLFW_PRESS )
       
 glfwSetWindowShouldClose( window, true );
   
}

void loadShaders() {
   
vertex_shader = glCreateShader( GL_VERTEX_SHADER );
   
glShaderSource( vertex_shader, 1, & vertex_shader_source, NULL );
   
glCompileShader( vertex_shader );
   
   
fragment_shader = glCreateShader( GL_FRAGMENT_SHADER );
   
glShaderSource( fragment_shader, 1, & fragment_shader_source, NULL );
   
glCompileShader( fragment_shader );
}

void deleteShaders() {
   
glDeleteShader( vertex_shader );
   
glDeleteShader( fragment_shader );
   
}

void loadPrograms() {
   
shader_program = glCreateProgram();
   
glAttachShader( shader_program, vertex_shader );
   
glAttachShader( shader_program, fragment_shader );
   
glLinkProgram( shader_program );
}

void deletePrograms() {
   
glDeleteProgram( shader_program );
}

int main()
{
   
// initialize glfw
   
glfwInit();
   
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
   
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
   
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
   
   
// create window
   
window = glfwCreateWindow( 800, 600, "LearnOpenGL", NULL, NULL );
   
if( window == NULL )
   
{
       
std::cout << "Failed to create GLFW window" << std::endl;
       
glfwTerminate();
       
return - 1;
   
}
   
   
// set the context
   
glfwMakeContextCurrent( window );
   
   
// initialize glad
   
if( !gladLoadGLLoader(( GLADloadproc ) glfwGetProcAddress ) )
   
{
       
std::cout << "Failed to initialize GLAD" << std::endl;
       
return - 1;
   
}
   
   
glEnable( GL_DEPTH_TEST );
   
   
   
loadShaders();
   
loadPrograms();
   
   
Model * model = new Model();
   
model->Load();
   
   
// main loop
   
while( !glfwWindowShouldClose( window ) )
   
{
       
events();
       
       
// render
       
glClearColor( 0.2f, 0.3f, 0.3f, 1.0f );
       
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
       
       
model->draw();
       
       
glfwSwapBuffers( window );
       
glfwPollEvents();
   
}
   
   
// CLEAR THE MEMORY
   
delete model;
   
deletePrograms();
   
deleteShaders();
   
   
glfwTerminate();
   
return 0;
}
P-182003
tBane
Temat założony przez niniejszego użytkownika
» 2024-12-31 14:55:52
Shadery były źle napisane. Poprawne Shadery to:


C/C++
const char * vertex_shader_source = R"( #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 projection; uniform mat4 view; uniform mat4 transform; void main() { gl_Position = projection * view * transform * vec4(aPos, 1.0); TexCoord = aTexCoord; } )";

const char * fragment_shader_source = R"( #version 330 core out vec4 FragColor; in vec2 TexCoord; // współrzędne tekstury uniform sampler2D texture1; // tekstura void main() { FragColor = texture(texture1, TexCoord); // użycie tekstury } )";
P-182004
« 1 »
  Strona 1 z 1