VPN termux about China.net

Monday 2 September 2024

Normalny C4droid SDL2 openGl load obj RubixCube

 #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;
};

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

// Function to load OBJ file
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 (strncmp(data, "v ", 2) == 0) {
            data += 2; // Skip "v "
            float x, y, z;
            if (sscanf(data, "%f %f %f", &x, &y, &z) == 3) {
                vertices.push_back({x, y, z});
            }
        } else if (strncmp(data, "vn ", 3) == 0) {
            data += 3; // Skip "vn "
            float nx, ny, nz;
            if (sscanf(data, "%f %f %f", &nx, &ny, &nz) == 3) {
                normals.push_back({nx, ny, nz});
            }
        }

        // Move to the next line
        while (data < end && *data != '\n') {
            ++data;
        }
        ++data; // Skip newline
    }

    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, 0.0f, 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/RubixCube.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;
}


No comments:

Post a Comment