VPN termux about China.net

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*/

No comments:

Post a Comment