VPN termux about China.net

Wednesday 11 September 2024

Telnet.c clijent FTP


/*
gcc -c -fPIE telnet.cpp -o telnet.o
gcc telnet.o -o telnet -pie
chmod +x telnet
./telnet
test
ftp.dlptest.com 21
dlpuser
rNrKYTX9g7z3RgJRmxWuGHbeu
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/select.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <termios.h>
#include <termios.h>
#include <fcntl.h>
 #include<iostream>
#include<stdio.h> // printf
#include<string.h> // strlen
#include<string> // string
#include<sys/socket.h> // socket
#include<arpa/inet.h> // inet_addr
#include<netdb.h> // hostenta
#include <unistd.h>
#include<iostream>
#include<sys/socket.h> // socket
#include<arpa/inet.h> // inet_addr
#include <sys/time.h> 
//using namespace std;
#define BUFLEN 20
int len;
 unsigned char buf[BUFLEN + 1];
struct sockaddr_in server;
/*telnet*/
#define DO 0xfd
#define WONT 0xfc
#define WILL 0xfb
#define DONT 0xfe
#define CMD 0xff
#define CMD_ECHO 1
#define CMD_WINDOW_SIZE 31
#define BUFLEN 20
#define IAC 255
#define CMD 0xff
#define DONT 0xfe
#define DO 0xfd
#define WONT 0xfc
#define WILL 0xfb
#define SB 0xfa
#define GA 0xf9
#define EL 0xf8
#define EC 0xf7
#define AYT 0xf6
#define AO 0xf5
#define IP 0xf4
#define BREAK 0xf3
#define SYNCH 0xf2
#define NOP 0xf1
#define SE 0xf0
#define EOR 0xef
#define ABORT 0xee
#define SUSP 0xed
#define xEOF 0xec
//#define BUFLEN 512
#define PRELIM 1
#define COMPLETE 2
#define CONTINUE 3
#define TRANSIENT 4
#define ERROR 5
#define RRQ 01
#define WRQ 02
#define DATA 03
#define ACK 04
#define REC_ESC '\377'
#define REC_EOR '\001'
#define REC_EOF '\002'
#define BLK_EOR 0x80
#define BLK_EOF 0x40
#define BLK_ERRORS 0x20
#define BLK_RESTART 0x10
static struct termios tin;
// Konwersja hosta na IP
char* resolve_hostname(char* hostname) {
    struct hostent* he;
    struct in_addr** addr_list;
    printf("Rozwiązywanie nazwy hosta: %s\n", hostname);
    if ((he = gethostbyname(hostname)) == NULL) {
        herror("gethostbyname");
        return NULL;
    }
    addr_list = (struct in_addr**) he->h_addr_list;
    if (addr_list[0] != NULL) {
        printf("Rozwiązano IP: %s\n", inet_ntoa(*addr_list[0]));
        return inet_ntoa(*addr_list[0]);
    }
    return NULL;
}
void negotiate2(int sock, unsigned char *buf, int len) {
    int i;
   
    if (buf[1] == DO && buf[2] == CMD_WINDOW_SIZE) {
        unsigned char tmp1[10] = {255, 251, 31};
        if (send(sock, tmp1, 3 , 0) < 0)
            exit(1);
       
        unsigned char tmp2[10] = {255, 250, 31, 0, 80, 0, 24, 255, 240};
        if (send(sock, tmp2, 9, 0) < 0)
            exit(1);
        return;
    }
   
    for (i = 0; i < len; i++) {
        if (buf[i] == DO)
            buf[i] = WONT;
        else if (buf[i] == WILL)
            buf[i] = DO;
    }
    if (send(sock, buf, len , 0) < 0)
        exit(1);
}
void negotiate(int sock, unsigned char *buf, int len) {
    for (int i = 0; i < len; i += 3) {
        if (buf[i] != IAC) continue; // Sprawdzamy, czy jest to komenda Telnetu
        unsigned char option = buf[i+1]; // Druga wartość to opcja DO, DONT, WILL, WONT
        unsigned char command = buf[i+2]; // Trzecia wartość to konkretna opcja, np. ECHO
        if (option == DO) {
            // Odrzucamy wszystkie opcje DO (nie chcemy ich obsługiwać)
            unsigned char response[3] = { IAC, WONT, command };
            send(sock, response, 3, 0);
        } else if (option == DONT) {
            // Potwierdzamy, że nie będziemy robić tego, o co prosi serwer
            unsigned char response[3] = { IAC, WONT, command };
            send(sock, response, 3, 0);
        } else if (option == WILL) {
            // Odrzucamy wszystkie opcje WILL (nie chcemy ich obsługiwać)
            unsigned char response[3] = { IAC, DONT, command };
            send(sock, response, 3, 0);
        } else if (option == WONT) {
            // Potwierdzamy, że serwer nie będzie tego robił
            unsigned char response[3] = { IAC, DONT, command };
            send(sock, response, 3, 0);
        }
    }
}
static void terminal_set(void) {
    // save terminal configuration
    tcgetattr(STDIN_FILENO, &tin);
   
    static struct termios tlocal;
    memcpy(&tlocal, &tin, sizeof(tin));
    cfmakeraw(&tlocal);
    tcsetattr(STDIN_FILENO,TCSANOW,&tlocal);
}
static void terminal_reset(void) {
    // restore terminal upon exit
    tcsetattr(STDIN_FILENO,TCSANOW,&tin);
}
int main(int argc, char* argv[]) {
    if (argc < 3) {
        printf("Użycie: %s <adres_serwera> <port>\n", argv[0]);
        return 1;
    }
    char* hostname = argv[1];
    int port = atoi(argv[2]);
  struct timeval ts;
    ts.tv_sec = 1; // Ustawienie timeoutu na 1 sekundę
    ts.tv_usec = 0; 
    // Sprawdzenie, czy port jest dozwolony (omijanie portów 80 i 443)
    if (port == 80 || port == 443) {
        printf("Port %d jest zablokowany. Wybierz inny port.\n", port);
        return 1;
    }
    // Rozwiązywanie nazwy hosta na IP
    char* ip_address = resolve_hostname(hostname);
    if (ip_address == NULL) {
        printf("Nie udało się rozwiązać hosta.\n");
        return 1;
    }
    // Tworzenie socketu
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        printf("Nie można utworzyć socketu\n");
        return 1;
    }
    // Konfiguracja serwera
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(ip_address);
    server.sin_port = htons(port);
    // Łączenie z serwerem
    if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
        perror("Błąd połączenia");
        close(sock);
        return 1;
    }
    printf("Połączono z serwerem %s na porcie %d\n", ip_address, port);
    // Inicjalizacja zmiennych dla select()
    fd_set fds;
    struct timeval timeout;
    timeout.tv_sec = 1; // 1 sekunda
    timeout.tv_usec = 0;
    // Otwieranie pliku do zapisu
    FILE* fp = fopen("telnet_output.txt", "w");
    if (fp == NULL) {
        printf("Błąd otwierania pliku\n");
        close(sock);
        return 1;
    }
  // Wstawiamy funkcję negotiate w odpowiednie miejsce w kodzie
while (1) {

  // terminal_set();
    atexit(terminal_reset);  

    fd_set fds;
    /* Set up polling. */
    FD_ZERO(&fds);
    if (sock != 0)
        FD_SET(sock, &fds);
    FD_SET(0, &fds);
    // wait for data
    int nready = select(sock + 1, &fds, (fd_set *) 0, (fd_set *) 0, &ts);
    if (nready < 0) {
        perror("select. Error");
        return 1;
    }
    else if (nready == 0) {
        ts.tv_sec = 1; // 1 second
        ts.tv_usec = 0;
    }
    else if (sock != 0 && FD_ISSET(sock, &fds)) {
        // start by reading a single byte
        int rv;
        if ((rv = recv(sock, buf, 1, 0)) < 0) {
            return 1;
        }
        else if (rv == 0) {
            printf("Connection closed by the remote end\n\r");
            return 0;
        }
        if (buf[0] == IAC) {
            // Odczytujemy 2 dodatkowe bajty i negocjujemy
            len = recv(sock, buf + 1, 2, 0);
            if (len < 0) {
                return 1;
            } else if (len == 0) {
                printf("Connection closed by the remote end\n\r");
                return 0;
            }
            // Wywołanie funkcji negotiate
            negotiate(sock, buf, 3);
        } else {
            len = 1;
            buf[len] = '\0';
            printf("%s", buf);
            fprintf(fp, "%s", buf);
            fflush(0);
        }
    } else if (FD_ISSET(0, &fds)) {
        buf[0] = getc(stdin); //fgets(buf, 1, stdin);
        if (send(sock, buf, 1, 0) < 0)
 
           return 1;
        if (buf[0] == '\n') // with the terminal in raw mode we need to force a LF
            putchar('\r');
    }
}

    fclose(fp);
    close(sock);
    return 0;
}


Saturday 7 September 2024

SDL2 openGl load obj

 #include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL_opengles.h>
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <jni.h>
#include <errno.h>
#include <math.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
#include "SDL2/SDL.h"
//#include<GL/gl.h>
//#include<GL/glxext.h>
//#include<GL/glu.h>
#include "SDL_test_common.h"
#if defined(__IPHONEOS__) || defined(__ANDROID__)
#define HAVE_OPENGLES
#endif
#include "SDL_opengles.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL_opengles.h>
#include "SDL_image.h"
#include <stdio.h>
#include <string.h>
#include <SDL_image.h>
#if defined(__IPHONEOS__) || defined(__ANDROID__)
#define HAVE_OPENGLES
#endif
#include "SDL_opengles.h"
#define PATH "/storage/emulated/0/img.jpg"
SDL_Surface* surface;
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_BPP 255
#define FALSE 0
#define TRUE 1
GLuint texture[1];
SDLTest_CommonState *state;
SDL_Event *event;
SDL_GLContext *context; // No need to have this as a pointer
struct mouse_handle {
    int x = 1;
    int y = 1;
} mouse;
struct Vertex {
    float x, y, z;
};
struct Normal {
    float nx, ny, nz;
};
struct TexCoord {
    float u, v;
};
std::vector<Vertex>* vertices = nullptr;
std::vector<Normal>* normals = nullptr;
std::vector<TexCoord>* texCoords = nullptr;
void loadObj(const char *filename) {
    vertices = new std::vector<Vertex>();
    normals = new std::vector<Normal>();
    texCoords = new std::vector<TexCoord>();
    std::ifstream objFile(filename);
    if (!objFile) {
        std::cerr << "Unable to open file: " << filename << std::endl;
        exit(1);
    }
    std::vector<Vertex> tempVertices;
    std::vector<Normal> tempNormals;
    std::vector<TexCoord> tempTexCoords;
    std::string line;
    while (std::getline(objFile, line)) {
        std::istringstream iss(line);
        std::string prefix;
        iss >> prefix;
        if (prefix == "v") {
            Vertex vertex;
            iss >> vertex.x >> vertex.y >> vertex.z;
            tempVertices.push_back(vertex);
        } else if (prefix == "vn") {
            Normal normal;
            iss >> normal.nx >> normal.ny >> normal.nz;
            tempNormals.push_back(normal);
        } else if (prefix == "vt") {
            TexCoord texCoord;
            iss >> texCoord.u >> texCoord.v;
            tempTexCoords.push_back(texCoord);
        } else if (prefix == "f") {
            std::vector<int> vIndices, tIndices, nIndices;
            std::string vertexData;
            while (iss >> vertexData) {
                std::replace(vertexData.begin(), vertexData.end(), '/', ' ');
                std::istringstream vertexStream(vertexData);
                int vIndex, tIndex = 0, nIndex = 0;
                vertexStream >> vIndex;
                if (vertexStream.peek() == ' ') { vertexStream >> tIndex; }
                if (vertexStream.peek() == ' ') { vertexStream >> nIndex; }
                vIndices.push_back(vIndex);
                tIndices.push_back(tIndex);
                nIndices.push_back(nIndex);
            }
            // Tworzenie trójkątów
            for (size_t i = 1; i < vIndices.size() - 1; i++) {
                vertices->push_back(tempVertices[vIndices[0] - 1]);
                vertices->push_back(tempVertices[vIndices[i] - 1]);
                vertices->push_back(tempVertices[vIndices[i + 1] - 1]);
                normals->push_back(tempNormals[nIndices[0] - 1]);
                normals->push_back(tempNormals[nIndices[i] - 1]);
                normals->push_back(tempNormals[nIndices[i + 1] - 1]);
                // Dodanie współrzędnych tekstury, jeśli są dostępne
                if (!tempTexCoords.empty()) {
                    texCoords->push_back(tempTexCoords[tIndices[0] - 1]);
                    texCoords->push_back(tempTexCoords[tIndices[i] - 1]);
                    texCoords->push_back(tempTexCoords[tIndices[i + 1] - 1]);
                }
            }
        }
    }
    objFile.close();
}
int LoadGLTextures() {
    SDL_Surface *TextureImage = IMG_Load("elo.bmp");
    if (!TextureImage) {
        std::cerr << "Unable to load texture: " << IMG_GetError() << std::endl;
        return 0;
    }
    glGenTextures(1, &texture[0]);  // Poprawka: indeks tekstury powinien być texture[0], nie [1]
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    int mode = (TextureImage->format->BytesPerPixel == 4) ? GL_RGBA : GL_RGB;
    glTexImage2D(GL_TEXTURE_2D, 0, mode, TextureImage->w, TextureImage->h, 0, mode, GL_UNSIGNED_BYTE, TextureImage->pixels);
           glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
        glGenerateMipmapOES(GL_TEXTURE_2D);  // OpenGL ES 2.0: zmieniono z glGenerateMipmapOES na glGenerateMipmap
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    SDL_FreeSurface(TextureImage);  // Zwalnianie pamięci po załadowaniu tekstury
    return 1;
}



void setPerspective(float fov, float aspect, float znear, float zfar) {
    float ymax = znear * tanf(fov * M_PI / 360.0f);
    float ymin = -ymax;
    float xmin = ymin * aspect;
    float xmax = ymax * aspect;
    glFrustumf(xmin, xmax, ymin, ymax, znear, zfar);
}
void cleanup() {
    delete vertices;
    delete normals;
    delete texCoords;
}
void renderScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    setPerspective(60.0f, 1.0f, 0.1f, 80.0f);  // Adjusted FOV and aspect ratio
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    // Apply transformations
    glTranslatef(0.0f, 0.0f, -5.0f);  // Move the object back
    glScalef(1.0f, 1.0f, 1.0f);  // Scale the object
    glRotatef(mouse.x % 360, 0.0f, 1.0f, 0.0f);  // Rotate around Y-axis
    glRotatef(mouse.y % 360, 1.0f, 0.0f, 0.0f);  // Rotate around X-axis
    // Lighting setup
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    GLfloat light_position[] = {0.0f, 1.0f, 1.0f, 0.0f};
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
       glEnable(GL_TEXTURE_2D);
   glShadeModel( GL_SMOOTH );
    // Enable and set vertex arrays
      glShadeModel( GL_SMOOTH );
//    glClearDepthx( 1.0f );                                                        // specify the clear value for the depth buffer
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );                        // specify implementation-specific hints
    GLfloat amb_light[] = { 0.1, 0.1, 0.1, 1.0 };
    GLfloat diffuse[] = { 0.6, 0.6, 0.6, 1 };
    GLfloat specular[] = { 0.7, 0.7, 0.3, 1 };
    glLightModelfv( GL_LIGHT_MODEL_AMBIENT, amb_light );
    glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
    glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
    glEnable( GL_LIGHT0 );
    glEnable( GL_COLOR_MATERIAL );
    glShadeModel( GL_SMOOTH );
  //  glLightModelx( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
    glDepthFunc( GL_LEQUAL );
    glEnable( GL_DEPTH_TEST );
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glClearColor(0.0, 0.0, 0.0, 1.0); glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(Vertex), vertices->data());
    // Enable and set normal arrays
    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, sizeof(Normal), normals->data());
    // Enable and set texture coordinates arrays
    if (!texCoords->empty()) {
     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glTexCoordPointer(2, GL_FLOAT, sizeof(TexCoord), texCoords->data());
        glBindTexture(GL_TEXTURE_2D, texture[0]);
    }
    //gluLookAt( 4,2,0, 0,0,0, 0,1,0);   
    
//GLUquadricObj *sphere=NULL;
//  sphere = gluNewQuadric();
  //gluQuadricDrawStyle(sphere, GLU_FILL);
  //gluQuadricTexture(sphere, TRUE);
//  gluQuadricNormals(sphere, GLU_SMOOTH);
glBindTexture( GL_TEXTURE_2D,texture[0] );
    // Draw the object
    glDrawArrays(GL_TRIANGLES, 0, vertices->size());
    // Disable client states
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    if (!texCoords->empty()) {
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    }
    // Swap buffers to display the scene
    SDL_GL_SwapWindow(state->windows[0]);
}


int main(int argc, char *argv[]) {
if (!(IMG_Init(IMG_INIT_JPG) & IMG_INIT_JPG)) {
    std::cerr << "IMG_Init failed: " << IMG_GetError() << std::endl;
    return 0;
}
    SDL_DisplayMode mode;
    state = SDLTest_CommonCreateState(argv, SDL_INIT_EVERYTHING);
    SDLTest_CommonInit(state);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
    bool sdlmainloop = true;
    bool running = true;
LoadGLTextures(); 
  
    loadObj("/sdcard/cubec.obj");  // Wczytanie pliku OBJ na początku
SDL_GL_CreateContext(*state->windows);
    
    while (running) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
            }
            if (event.type == SDL_MOUSEMOTION) {
                mouse.x = event.motion.x;
                mouse.y = event.motion.y;
            }
            // 
            // Render the scene
          //  SDL_GL_SwapWindow(state->windows[0]);
            renderScene();
             //  SDL_GL_SwapWindow(*state->windows);
        }
    }
cleanup();
    // Cleanup
    SDL_GL_DeleteContext(context);
    SDLTest_CommonQuit(state);
    return 0;
}

Thursday 5 September 2024

Try texture

 #include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL_opengles.h>
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>

#include "SDL2/SDL.h"
#include "SDL_test_common.h"
#if defined(__IPHONEOS__) || defined(__ANDROID__)
#define HAVE_OPENGLES
#endif
#include "SDL_opengles.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL_opengles.h>
#include "SDL_image.h"

#include <stdio.h>
#include <string.h>
#include <SDL_image.h>
#if defined(__IPHONEOS__) || defined(__ANDROID__)
#define HAVE_OPENGLES
#endif
#include "SDL_opengles.h"
#define PATH "/storage/emulated/0/img.jpg"
GLuint texture[1]; // Changed to have at least one texture slot
SDLTest_CommonState *state;
SDL_Event *event;
SDL_GLContext *context; // No need to have this as a pointer

struct mouse_handle {
    int x = 1;
    int y = 1;
} mouse;

struct Vertex {
    float x, y, z;
};

struct Normal {
    float nx, ny, nz;
};

struct TexCoord {
    float u, v;
};

std::vector<Vertex>* vertices = nullptr;
std::vector<Normal>* normals = nullptr;
std::vector<TexCoord>* texCoords = nullptr;

void loadObj(const char *filename) {
    vertices = new std::vector<Vertex>();
    normals = new std::vector<Normal>();
    texCoords = new std::vector<TexCoord>();

    std::ifstream objFile(filename);
    if (!objFile) {
        std::cerr << "Unable to open file: " << filename << std::endl;
        exit(1);
    }

    std::vector<Vertex> tempVertices;
    std::vector<Normal> tempNormals;
    std::vector<TexCoord> tempTexCoords;

    std::string line;
    while (std::getline(objFile, line)) {
        std::istringstream iss(line);
        std::string prefix;
        iss >> prefix;

        if (prefix == "v") {
            Vertex vertex;
            iss >> vertex.x >> vertex.y >> vertex.z;
            tempVertices.push_back(vertex);
        } else if (prefix == "vn") {
            Normal normal;
            iss >> normal.nx >> normal.ny >> normal.nz;
            tempNormals.push_back(normal);
        } 
        else if (prefix == "vt") {
            TexCoord texCoord;
            iss >> texCoord.u >> texCoord.v;
          texCoords->push_back(texCoord);
             // tempTexCoords.push_back(texCoord);
        } 
        else if (prefix == "f") {
            std::vector<int> vIndices, nIndices;
            std::string vertexData;
            while (iss >> vertexData) {
                std::replace(vertexData.begin(), vertexData.end(), '/', ' ');
                std::istringstream vertexStream(vertexData);
                int vIndex, tIndex, nIndex;
                vertexStream >> vIndex >> tIndex >> nIndex;
                vIndices.push_back(vIndex);
                nIndices.push_back(nIndex);
            }
            for (size_t i = 1; i < vIndices.size() - 1; i++) {
                vertices->push_back(tempVertices[vIndices[0] - 1]);
                vertices->push_back(tempVertices[vIndices[i] - 1]);
                vertices->push_back(tempVertices[vIndices[i + 1] - 1]);

                normals->push_back(tempNormals[nIndices[0] - 1]);
                normals->push_back(tempNormals[nIndices[i] - 1]);
                normals->push_back(tempNormals[nIndices[i + 1] - 1]);
            }
        }
    }
    objFile.close();
}

int LoadGLTextures() {
    SDL_Surface *TextureImage = IMG_Load(PATH);
    if (!TextureImage) {
        std::cerr << "Unable to load texture" << std::endl;
        return 0;
    }

    glGenTextures(1, &texture[1]);
    glBindTexture(GL_TEXTURE_2D, texture[1]);

    // Determine the texture format (RGB or RGBA)
    int mode = (TextureImage->format->BytesPerPixel == 4) ? GL_RGBA : GL_RGB;
// Assuming TextureImage is a structure containing width, height, and image data
glBindTexture(GL_TEXTURE_2D, texture[1]); // Bind the texture object
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureImage->w, TextureImage->h, 0, GL_RGB, GL_UNSIGNED_BYTE,TextureImage->pixels);
    // Upload the texture
   // glTexImage2D(GL_TEXTURE_2D, 0, mode, TextureImage->w, TextureImage->h, 0, mode, GL_UNSIGNED_BYTE, TextureImage->image);

    // Optionally generate mipmaps
    glGenerateMipmapOES(GL_TEXTURE_2D);

    // Set filtering
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);  // Use mipmaps
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    //glEnable(GL_TEXTURE_2D);
    SDL_FreeSurface(TextureImage);
    return 1;
}




void setPerspective(float fov, float aspect, float znear, float zfar) {
    float ymax = znear * tanf(fov * M_PI / 360.0f);
    float ymin = -ymax;
    float xmin = ymin * aspect;
    float xmax = ymax * aspect;
    glFrustumf(xmin, xmax, ymin, ymax, znear, zfar);
}

void cleanup() {
    delete vertices;
    delete normals;
    delete texCoords;
}

void renderScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    setPerspective(60.0f, 1.0f, 0.1f, 80.0f);  // Adjusted FOV and aspect ratio

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // Apply transformations here
    glTranslatef(0.0f, 0.0f, 0.0f);  // Move the object back
    glScalef(1.0f, 1.0f, 1.0f);  // Scale the object
    glRotatef(mouse.x % 360, 0.0f, 1.0f, 0.0f);  // Rotate around Y-axis
    glRotatef(mouse.y % 360, 1.0f, 0.0f, 0.0f);  // Rotate around X-axis

    // Lighting setup
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    GLfloat light_position[] = {0.0f, 1.0f, 1.0f, 0.0f};
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    // Vertex and normal array setup
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(Vertex), vertices->data());

    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, sizeof(Normal), normals->data());

    // Texture array setup
 glEnable(GL_TEXTURE_2D);
      glBindTexture(GL_TEXTURE_2D, texture[1]);
       glGenTextures(1, &texture[1]);
    glBindTexture(GL_TEXTURE_2D, texture[1]); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
    glTexCoordPointer(2, GL_FLOAT, sizeof(TexCoord), texCoords->data());
 //TexCoord(texCoords->data());

    // Render the object
    glDrawArrays(GL_TRIANGLES, 0, vertices->size());

    // Cleanup states
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    // Swap buffers to display the scene
    SDL_GL_SwapWindow(state->windows[0]);
}


int main(int argc, char *argv[]) {
if (!(IMG_Init(IMG_INIT_JPG) & IMG_INIT_JPG)) {
    std::cerr << "IMG_Init failed: " << IMG_GetError() << std::endl;
    return 0;
}

    SDL_DisplayMode mode;
    state = SDLTest_CommonCreateState(argv, SDL_INIT_EVERYTHING);
    SDLTest_CommonInit(state);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
    bool sdlmainloop = true;
    bool running = true;
LoadGLTextures(); 
  
    loadObj("/sdcard/sphere.obj");  // Wczytanie pliku OBJ na początku
SDL_GL_CreateContext(*state->windows);
    while (running) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
            }
            if (event.type == SDL_MOUSEMOTION) {
                mouse.x = event.motion.x;
                mouse.y = event.motion.y;
            }
    // 
            // Render the scene
         
            renderScene();
               // SDL_GL_SwapWindow(*state->windows);
        }
    }
cleanup();
    // Cleanup
    SDL_GL_DeleteContext(context);
    SDLTest_CommonQuit(state);

    return 0;
}

Wednesday 4 September 2024

NEW obj sdl2 opengl C4 all blender obj

 


#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL_opengles.h>
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include "SDL2/SDL.h"
#include "SDL_test_common.h"
#if defined(__IPHONEOS__) || defined(__ANDROID__)
#define HAVE_OPENGLES
#endif
#include "SDL_opengles.h"
SDLTest_CommonState *state;
SDL_Event *event;
static SDL_GLContext *context;
struct mouse_handle {
    int x = 1;
    int y = 1;
} mouse;
struct Vertex {
    float x, y, z;
};
struct Normal {
    float nx, ny, nz;
};
struct TexCoord {
    float u, v;
};
std::vector<Vertex>* vertices = nullptr;
std::vector<Normal>* normals = nullptr;
std::vector<TexCoord>* texCoords = nullptr;
void loadObj(const char *filename) {
    vertices = new std::vector<Vertex>();
    normals = new std::vector<Normal>();
    texCoords = new std::vector<TexCoord>();
    std::ifstream objFile(filename);
    if (!objFile) {
        std::cerr << "Unable to open file: " << filename << std::endl;
        exit(1);
    }
    std::vector<Vertex> tempVertices;
    std::vector<Normal> tempNormals;
    std::vector<TexCoord> tempTexCoords;
    std::string line;
    while (std::getline(objFile, line)) {
        std::istringstream iss(line);
        std::string prefix;
        iss >> prefix;
        if (prefix == "v") {
            Vertex vertex;
            iss >> vertex.x >> vertex.y >> vertex.z;
            tempVertices.push_back(vertex);
        } else if (prefix == "vn") {
            Normal normal;
            iss >> normal.nx >> normal.ny >> normal.nz;
            tempNormals.push_back(normal);
        }else if (prefix == "f") {
    std::vector<int> vIndices, nIndices;
    std::string vertexData;
    while (iss >> vertexData) {
        std::replace(vertexData.begin(), vertexData.end(), '/', ' ');
        std::istringstream vertexStream(vertexData);
        int vIndex, tIndex, nIndex;
        vertexStream >> vIndex >> tIndex >> nIndex;
        vIndices.push_back(vIndex);
        nIndices.push_back(nIndex);
    }
    for (size_t i = 1; i < vIndices.size() - 1; i++) {
        vertices->push_back(tempVertices[vIndices[0] - 1]);
        vertices->push_back(tempVertices[vIndices[i] - 1]);
        vertices->push_back(tempVertices[vIndices[i + 1] - 1]);
        normals->push_back(tempNormals[nIndices[0] - 1]);
        normals->push_back(tempNormals[nIndices[i] - 1]);
        normals->push_back(tempNormals[nIndices[i + 1] - 1]);
    }
}
    }
    objFile.close();
}
// Function to set perspective
void setPerspective(float fov, float aspect, float znear, float zfar) {
    float ymax = znear * tanf(fov * M_PI / 360.0f);
    float ymin = -ymax;
    float xmin = ymin * aspect;
    float xmax = ymax * aspect;
    glFrustumf(xmin, xmax, ymin, ymax, znear, zfar);
}
void cleanup() {
    delete vertices;
    delete normals;
    delete texCoords;
}
void renderScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //!!!!!!!!!!!
    setPerspective(80.0, 1.0, 0.1, 80.0);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    //!!!!!!!!!!!!!!!!!
    glTranslatef(-12.50f, 0.0f, -2.0f);
    
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    GLfloat light_position[] = {0.0f, 1.0f, 1.0f, 0.0f};
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    //to dobra struktura bo moge obracac obiektem 
        glRotatef(mouse.y% 360, mouse.y% 360, 0.0f, 0.0f);
   glScalef(2.0f, 2.0f, 2.0f); // Skaluje obiekt 5x w każdym kierunku
 glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(Vertex), vertices->data());
    
    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, sizeof(Normal), normals->data());
    glDrawArrays(GL_TRIANGLES, 0, vertices->size());
    
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    
    SDL_GL_SwapWindow(state->windows[0]);
}
int main(int argc, char *argv[]) {

    SDL_DisplayMode mode;
    state = SDLTest_CommonCreateState(argv, SDL_INIT_EVERYTHING);
    SDLTest_CommonInit(state);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
    bool sdlmainloop = true;
    bool running = true;
 
    loadObj("/sdcard/sphere.obj"); // Wczytanie pliku OBJ na początku
SDL_GL_CreateContext(*state->windows);
    while (running) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
            }
            if (event.type == SDL_MOUSEMOTION) {
                mouse.x = event.motion.x;
                mouse.y = event.motion.y;
            }
    // 
            // Render the scene
            renderScene();
               // SDL_GL_SwapWindow(*state->windows);
        }
    }
cleanup();
    // Cleanup
    SDL_GL_DeleteContext(context);
    SDLTest_CommonQuit(state);
    return 0;
}
/*wczytuje wszystko brak ladowania tekstur ale to na jutro chyba zostawie bo i tak jest progress*/

Tuesday 3 September 2024

Normalny

 tea.obj



#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>

#include "SDL2/SDL.h"
#include "SDL_test_common.h"
#if defined(__IPHONEOS__) || defined(__ANDROID__)
#define HAVE_OPENGLES
#endif
#include "SDL_opengles.h"

SDLTest_CommonState *state;
SDL_Event *event;
static SDL_GLContext *context;
struct mouse_handle {
    int x = 1;
    int y = 1;
} mouse;

struct Vertex {
    float x, y, z;
};
struct Normal {
    float nx, ny, nz;
};
Normal calculateNormal(float* coord1, float* coord2, float* coord3) {
    float va[3], vb[3], vr[3], val;

    va[0] = coord1[0] - coord2[0];
    va[1] = coord1[1] - coord2[1];
    va[2] = coord1[2] - coord2[2];

    vb[0] = coord1[0] - coord3[0];
    vb[1] = coord1[1] - coord3[1];
    vb[2] = coord1[2] - coord3[2];

    // Iloczyn wektorowy
    vr[0] = va[1] * vb[2] - vb[1] * va[2];
    vr[1] = vb[0] * va[2] - va[0] * vb[2];
    vr[2] = va[0] * vb[1] - vb[0] * va[1];

    // Normalizacja
    val = sqrt(vr[0] * vr[0] + vr[1] * vr[1] + vr[2] * vr[2]);

    return { vr[0] / val, vr[1] / val, vr[2] / val };
}

static GLubyte color[8][4] = {{255, 0, 0, 0}};
std::vector<Vertex> vertices;
std::vector<Normal> normals;

void loadObj(const char *fname, std::vector<Vertex>& vertices, std::vector<Normal>& normals) {
    int fd = open(fname, O_RDONLY);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    off_t fileSize = lseek(fd, 0, SEEK_END);
    if (fileSize == -1) {
        perror("lseek");
        close(fd);
        exit(EXIT_FAILURE);
    }

    void *map = mmap(NULL, fileSize, PROT_READ, MAP_PRIVATE, fd, 0);
    if (map == MAP_FAILED) {
        perror("mmap");
        close(fd);
        exit(EXIT_FAILURE);
    }

    close(fd);

    const char *data = (const char *)map;
    const char *end = data + fileSize;

    std::vector<Vertex> tempVertices;

    while (data < end) {
        if (strncmp(data, "v ", 2) == 0) {
            data += 2;
            float x, y, z;
            if (sscanf(data, "%f %f %f", &x, &y, &z) == 3) {
                tempVertices.push_back({x, y, z});
            }
        } else if (strncmp(data, "f ", 2) == 0) {
            data += 2;
            int v1, v2, v3;
            if (sscanf(data, "%d %d %d", &v1, &v2, &v3) == 3) {
                Vertex& vertex1 = tempVertices[v1 - 1];
                Vertex& vertex2 = tempVertices[v2 - 1];
                Vertex& vertex3 = tempVertices[v3 - 1];
                
                vertices.push_back(vertex1);
                vertices.push_back(vertex2);
                vertices.push_back(vertex3);
                
                Normal normal = calculateNormal((float*)&vertex1, (float*)&vertex2, (float*)&vertex3);
                normals.push_back(normal);
                normals.push_back(normal);
                normals.push_back(normal);
            }
        }

        while (data < end && *data != '\n') {
            ++data;
        }
        ++data;
    }

    if (munmap(map, fileSize) == -1) {
        perror("munmap");
    }
}


// Function to set perspective
void setPerspective(float fov, float aspect, float znear, float zfar) {
    float ymax = znear * tanf(fov * M_PI / 360.0f);
    float ymin = -ymax;
    float xmin = ymin * aspect;
    float xmax = ymax * aspect;
    glFrustumf(xmin, xmax, ymin, ymax, znear, zfar);
}

// Rendering function
void renderScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    setPerspective(80.0f, 1.0f, 0.1f, 100.0f);

    // Modelview matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -5.0f);
    glRotatef(mouse.x % 360, mouse.y% 360, 1.0f, 0.0f);

    // Lighting settings
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    GLfloat light_position[] = {0.2f, 1.0f, 10.0f, 1.0f};
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    
    GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
    GLfloat mat_shininess[] = {50.0f};
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());
    
    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, sizeof(Normal), (void*)normals.data());

    // Draw arrays
    glDrawArrays(GL_TRIANGLES, 0, vertices.size());
    
    // Draw arrays
   

    // Disable client states
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);

    // Swap the window buffer
    SDL_GL_SwapWindow(state->windows[0]);
}


int main(int argc, char *argv[]) {
    SDL_DisplayMode mode;
    state = SDLTest_CommonCreateState(argv, SDL_INIT_EVERYTHING);
    SDLTest_CommonInit(state);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
    bool sdlmainloop = true;
    bool running = true;
 
    loadObj("/sdcard/tea.obj", vertices,normals);  // Wczytanie pliku OBJ na początku
SDL_GL_CreateContext(*state->windows);
    while (running) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
            }
            if (event.type == SDL_MOUSEMOTION) {
                mouse.x = event.motion.x;
                mouse.y = event.motion.y;
            }
    // 
            // Render the scene
            renderScene();
               // SDL_GL_SwapWindow(*state->windows);
        }
    }

    // Cleanup
    SDL_GL_DeleteContext(context);
    SDLTest_CommonQuit(state);

    return 0;
}

Monday 2 September 2024

Nonormal c4droid SDL2 tea.obj

 


#include <iostream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>

#include "SDL2/SDL.h"
#include "SDL_test_common.h"
#if defined(__IPHONEOS__) || defined(__ANDROID__)
#define HAVE_OPENGLES
#endif
#include "SDL_opengles.h"
float calculateNormal( float * coord1, float * coord2, float * coord3 )
{
    /* calculate Vector1 and Vector2 */
    float va[ 3 ], vb[ 3 ], vr[ 3 ], val;
    va[ 0 ] = coord1[ 0 ] - coord2[ 0 ];
    va[ 1 ] = coord1[ 1 ] - coord2[ 1 ];
    va[ 2 ] = coord1[ 2 ] - coord2[ 2 ];
  
    vb[ 0 ] = coord1[ 0 ] - coord3[ 0 ];
    vb[ 1 ] = coord1[ 1 ] - coord3[ 1 ];
    vb[ 2 ] = coord1[ 2 ] - coord3[ 2 ];
  
    /* cross product */
    vr[ 0 ] = va[ 1 ] * vb[ 2 ] - vb[ 1 ] * va[ 2 ];
    vr[ 1 ] = vb[ 0 ] * va[ 2 ] - va[ 0 ] * vb[ 2 ];
    vr[ 2 ] = va[ 0 ] * vb[ 1 ] - vb[ 0 ] * va[ 1 ];
  
    /* normalization factor */
    val = sqrt( vr[ 0 ] * vr[ 0 ] + vr[ 1 ] * vr[ 1 ] + vr[ 2 ] * vr[ 2 ] );
  
    float norm[ 3 ];
    norm[ 0 ] = vr[ 0 ] / val;
    norm[ 1 ] = vr[ 1 ] / val;
    norm[ 2 ] = vr[ 2 ] / val;
  
  
    return norm[3];
}

struct mouse_handle {
    int x = 1;
    int y = 1;
} mouse;

int gl;

struct Vertex {
    float x, y, z;
};
struct Normal {
    float nx, ny, nz;
};
static GLubyte color[8][4] = {{255, 0, 0, 0}};
std::vector<Vertex> vertices;
std::vector<Normal> normals;


void glBegin(int gl) {
    vertices.clear();
}
//wieszcholki
void glVertex3f(float x, float y, float z) {
    vertices.push_back({x, y, z});
}
//ślad węglowy
void glNormal3f(float nx, float ny, float nz) {
    normals.push_back({nx, ny, nz});
}
void glEnd() {
    switch (gl) {
        case GL_POINTS:
            for (const auto &vertex : vertices) {
                std::cout << "Point: (" << vertex.x << ", " << vertex.y << ", " << vertex.z << ")" << std::endl;
            }
            glEnableClientState(GL_COLOR_ARRAY);
            glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
            glEnableClientState(GL_VERTEX_ARRAY);
            glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());
            glDrawArrays(GL_POINTS, 0, vertices.size());
            break;
        case GL_TRIANGLES:
            if (vertices.size() % 3 != 0) {
                std::cerr << "Warning: Number of vertices is not a multiple of 3 for GL_TRIANGLES" << std::endl;
            }
            for (size_t i = 0; i < vertices.size(); i += 3) {
                std::cout << "Triangle: (" << vertices[i].x << ", " << vertices[i].y << ", " << vertices[i].z << ") -> ("
                          << vertices[i + 1].x << ", " << vertices[i + 1].y << ", " << vertices[i + 1].z << ") -> ("
                          << vertices[i + 2].x << ", " << vertices[i + 2].y << ", " << vertices[i + 2].z << ")" << std::endl;
            }
            glEnableClientState(GL_COLOR_ARRAY);
            glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
            glEnableClientState(GL_VERTEX_ARRAY);
            glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());
            glDrawArrays(GL_TRIANGLES, 0, vertices.size());
            break;
        default:
            std::cerr << "Unsupported primitive type!" << std::endl;
            break;
    }
}

GLuint car;
float carrot;

// Zmodyfikowana funkcja do ładowania OBJ, zapisująca wierzchołki w pamięci
void loadObj(const char *fname, std::vector<Vertex>& vertices, std::vector<Normal>& normals) {
    int fd = open(fname, O_RDONLY);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    off_t fileSize = lseek(fd, 0, SEEK_END);
    if (fileSize == -1) {
        perror("lseek");
        close(fd);
        exit(EXIT_FAILURE);
    }

    void *map = mmap(NULL, fileSize, PROT_READ, MAP_PRIVATE, fd, 0);
    if (map == MAP_FAILED) {
        perror("mmap");
        close(fd);
        exit(EXIT_FAILURE);
    }

    close(fd);

    const char *data = (const char *)map;
    const char *end = data + fileSize;

    while (data < end) {
   
    
        if (*data == 'v' && *(data + 1) == ' ') {
            data += 2; // pomiń "v "
            float x, y, z;
            if (sscanf(data, "%f %f %f", &x, &y, &z) == 3) {
                vertices.push_back({x, y, z});
            }
        }
    if (*data == 'vn' && *(data + 1) == ' ') {
        data += 3; // Skip "vn "
        float nx, ny, nz;
        if (sscanf(data, "%f %f %f", &nx, &ny, &nz) == 3) {
        calculateNormal(&nx,&ny,&nz);
           // normals.push_back({nx, ny, nz});
        }
    }
        // Przejdź do następnej linii
        while (data < end && *data != '\n') {
            ++data;
        }
        ++data; // Pomiń nową linię
    }

    if (munmap(map, fileSize) == -1) {
        perror("munmap");
    }
}

void setPerspective(float fov, float aspect, float znear, float zfar) {
    float ymax = znear * tanf(fov * M_PI / 360.0f);
    float ymin = -ymax;
    float xmin = ymin * aspect;
    float xmax = ymax * aspect;
    glFrustumf(xmin, xmax, ymin, ymax, znear, zfar);
}

SDLTest_CommonState *state;
SDL_Event *event;
static SDL_GLContext *context;


struct obj {
    float x;
    float y;
    float z;

    int Render(const std::vector<Vertex>& vertices) {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        setPerspective(105.0f, 1.0f, 0.1f, 100.0f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.0f, 0.0f, -5.0f);

        // Ustawienie światła
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        GLfloat light_position[] = {0.2, 1.0, 10.0, 1.0};
        glLightfv(GL_LIGHT0, GL_POSITION, light_position);
        
        // Ustawienie materiału (opcjonalne, aby oświetlenie działało poprawnie)
        GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
        GLfloat mat_shininess[] = {50.0};
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
        
        glRotatef(mouse.x % 360, mouse.y % 360, mouse.x % 360, mouse.y % 360);
        
        // Renderowanie z normalnymi i wierzchołkami
        glEnableClientState(GL_COLOR_ARRAY);
        glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
        
         glEnableClientState(GL_VERTEX_ARRAY);

        glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());
        
        glDrawArrays(GL_TRIANGLES, 0, vertices.size());
  glEnableClientState(GL_NORMAL_ARRAY);
       
        glNormalPointer(GL_FLOAT, sizeof(Normal), (void*)normals.data());
            glDrawArrays(GL_TRIANGLES, 0, normals.size());
        return 1;
    }
};


int main(int argc, char *argv[]) {
    SDL_DisplayMode mode;
    state = SDLTest_CommonCreateState(argv, SDL_INIT_EVERYTHING);
    SDLTest_CommonInit(state);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);
    bool sdlmainloop = true;
    bool running = true;
    //czy tutaj musimy definiować nowe wektory czy wczyta jako globalne 
//std::vector<Normal> normals;
  //  std::vector<Vertex> vertices;
    loadObj("/sdcard/tea.obj", vertices,normals);  // Wczytanie pliku OBJ na początku
SDL_GL_CreateContext(*state->windows);
    while (running) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
            }
            if (event.type == SDL_MOUSEMOTION) {
                mouse.x = event.motion.x;
                mouse.y = event.motion.y;
            }
                   
SDL_GL_CreateContext(*state->windows);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            obj gg;
            gg.Render(vertices);  // Renderowanie z wcześniej wczytanych wierzchołków
       SDL_GL_SwapWindow(*state->windows);
    
        }
    }
    return 0;
}

Normalny C4droid SDL2 openGl load obj RubixCube