VPN termux about China.net

Thursday, 29 August 2024

C4droid SDL2 rubixcube.obj


 

 #include <iostream>

#include <vector>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <math.h>

#include "SDL2/SDL.h"

#include "SDL_test_common.h"

#if defined(__IPHONEOS__) || defined(__ANDROID__)

#define HAVE_OPENGLES

#endif

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "SDL_opengles.h"

struct mouse_handle

{

int x = 1;

int y = 1;

} mouse;

// Struktura do przechowywania wierzchołków

int gl;

struct Vertex

{

float x, y, z;

};

    static GLubyte color[8][4] = { {255, 0, 0, 0},};

// Globalne zmienne przechowujące aktualnie rysowane prymitywy

//PrimitiveType currentPrimitiveType;

std::vector < Vertex > vertices;


// Uproszczona funkcja glBegin tutaj zamiast enum misialem uzyc int gl bo juz jest zdwfiniowane w sdl_opengl.h jako int i nie chcialo puscic 

void glBegin(int gl)

{

//currentPrimitiveType = primitiveType;

vertices.clear(); // Wyczyść bufor wierzchołków

}


// Uproszczona funkcja glVertex3f

void glVertex3f(float x, float y, float z)

{

vertices.push_back({x, y, z});

}


// Uproszczona funkcja glEnd

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;


        case GL_LINES:

            if (vertices.size() % 2 != 0) {

                std::cerr << "Warning: Number of vertices is not a multiple of 2 for GL_LINES" << std::endl;

            }

            for (size_t i = 0; i < vertices.size(); i += 2) {

                std::cout << "Line: (" << vertices[i].x << ", " << vertices[i].y << ", " << vertices[i].z << ") -> ("

                          << vertices[i + 1].x << ", " << vertices[i + 1].y << ", " << vertices[i + 1].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_LINES, 0, vertices.size());

            break;


        default:

            std::cerr << "Unsupported primitive type!" << std::endl;

            break;

    }

}



GLuint car;

float carrot;

void loadObj(char *fname)

{

   FILE *fp;

   int read;

   GLfloat x, y, z;

   char ch;


   fp=fopen(fname,"r");

   if (!fp)

  {

    printf("can't open file %s\n", fname);

    exit(1);

  }

   glPointSize(2.0);


    glPushMatrix();

         glRotatef(mouse.x % 360, mouse.y % 360, mouse.x % 360, mouse.y % 360);

  //  glTranslatef(0.0,0.0,-1);

    

glPointSize(10.0f); 

    glBegin(GL_TRIANGLES);

    while(!(feof(fp)))

    {

     read=fscanf(fp,"%c %f %f %f",&ch,&x,&y,&z);

     if(read==4&&ch=='v')

    {

     glVertex3f(x,y,z);

     }

   }

   glEnd();

   

   glPopMatrix();

 

   fclose(fp);

}

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);

}



/* this */

SDLTest_CommonState *state;

SDL_Event *event;

static SDL_GLContext *context;

struct obj

{

float x;

float y;

float z;

int Render()

{

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();


    // Ustawienie perspektywy (fov, aspect ratio, near, far)

    setPerspective(45.0f, 1.0f, 0.1f, 100.0f);


    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();


    // Przesunięcie kamery do tyłu, aby oddalić obiekt

    glTranslatef(0.0f, 0.0f, -5.0f);


    glEnable(GL_LIGHTING);

    glEnable(GL_LIGHT0);


    GLfloat light_position[] = { 0.2, 1.0, 10.0, 1.0 };


    loadObj("/sdcard/RubixCube.obj");


    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;


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);

/* draw() */

obj gg;

gg.Render();

SDL_GL_SwapWindow(*state->windows);

}

}

return 0;

}


No comments:

Post a Comment