VPN termux about China.net

Saturday 17 August 2024

Sdl&openGl try load obj

 


#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 "SDL_opengles.h"

//#include"sdlobj.cpp"

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_VERTICES 1000

#define MAX_INDICES 3000


GLfloat vertices[MAX_VERTICES][3];

GLubyte indices[MAX_INDICES][3];

GLubyte colors[MAX_VERTICES][4];


int vertex_count = 0;

int index_count = 0;


void parseOBJ(const char* filename) {

    FILE* file = fopen(filename, "r");

    if (!file) {

        printf("Nie można otworzyć pliku: %s\n", filename);

        return;

    }


    char line[128];

    while (fgets(line, sizeof(line), file)) {

        if (strncmp(line, "v ", 2) == 0) {

            // Wczytaj wierzchołek

            sscanf(line, "v %f %f %f", 

                   &vertices[vertex_count][0], 

                   &vertices[vertex_count][1], 

                   &vertices[vertex_count][2]);


            // Dodaj domyślny kolor dla wierzchołka

            colors[vertex_count][0] = 255; // R

            colors[vertex_count][1] = 0;   // G

            colors[vertex_count][2] = 0;   // B

            colors[vertex_count][3] = 255; // A


           vertex_count++;

        } else if (strncmp(line, "f ", 2) == 0) {

            int v1, v2, v3;

            // Wczytaj indeksy do tablicy wierzchołków

            sscanf(line, "f %d//%d//%d", &v1, &v2, &v3);

            indices[index_count][0] = v1 - 1; // OBJ indeksy zaczynają się od 1

            indices[index_count][1] = v2 - 1;

            indices[index_count][2] = v3 - 1;

            index_count++;

        }

    }


    fclose(file);

}

// Funkcja do wyświetlenia wczytanych danych

/*

void printData() {

    printf("Wczytane wierzchołki:\n");

    for (int i = 0; i < vertex_count; i++) {

        printf("v %f %f %f\n", vertices[i].x, vertices[i].y, vertices[i].z);

    }


    printf("Wczytane twarze (indeksy):\n");

    for (int i = 0; i < face_count; i++) {

        printf("f %d %d %d\n", faces[i].v1, faces[i].v2, faces[i].v3);

    }

}*/

/*

int main() {

    loadOBJ("cube.obj");

    printData();

    return 0;

}

*/

struct mouse_handle

{

int x = 1;

int y = 1;

} mouse;


SDLTest_CommonState *state;

SDL_Window *win = NULL;

SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, SDL_WINDOW_OPENGL);

SDL_Event *event;

static SDL_GLContext *context;

struct obj {

    float x;

    float y;

    float z;

    int Render() {

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

    {255, 0, 0, 255},

    {0, 255, 0, 255},

    {0, 255, 0, 255},

    {0, 255, 0, 255},

    {255, 255, 255, 255},

    {255, 0, 255, 255},

    {0, 0, 255, 255}

    };

            static GLubyte indices[36] = { 0, 3, 4,

        4, 5, 0,

        0, 5, 6,

        6, 1, 0,

        6, 7, 2,

        2, 1, 6,

        7, 4, 3,

        3, 2, 7,

        5, 4, 7,

        7, 6, 5,

        2, 3, 1,

        3, 0, 1

    };

 //       static GLfloat vertices[3][3] = { {0.0f, 0.5f, 0.0f}, {-0.5f, -0.5f, 0.0f}, {0.5f, -0.5f, 0.0f} };


        glPushMatrix(); // Save the current transformation matrix

      //  glTranslatef(mouse.x*0.01, mouse.y*0.01, z);

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

        glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);        glEnableClientState(GL_COLOR_ARRAY);

        glVertexPointer(3, GL_FLOAT, 0, vertices);      glEnableClientState(GL_VERTEX_ARRAY);

  //    glDrawElements(GL_TRIANGLES, index_count *3, GL_UNSIGNED_INT, indices);


        glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);        glDisableClientState(GL_VERTEX_ARRAY);  glDisableClientState(GL_COLOR_ARRAY);

      glPopMatrix(); // Restore the previous transformation matrix        

        return 1;

    }

};


int main(int argc, char *argv[])

{

    parseOBJ("cube.obj");

SDL_DisplayMode mode;

SDL_GL_MakeCurrent(win, context);

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

obj gg;

        gg.x = 0.2f;

        gg.y = 0.4f;

        gg.z = 0.6f;

        gg.Render();

  

SDL_GL_SwapWindow(*state->windows);

}

}

return 0;

}


No comments:

Post a Comment