/******ten kod dziala ale nie chciał bym żeby tak wyglądał mój kot**************/
/*************21:31********************/
/**************pon*********************/
#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 <stdio.h>
#include <stdlib.h>
#include <string.h>
// brak #include <glad/glad.h>
#define MAX_VERTICES 1000
#define MAX_INDICES 3000
struct Vertex {
GLfloat x, y, z; // Współrzędne wierzchołka
GLubyte r, g, b, a; // Kolor
GLfloat nx, ny, nz; // Normalne
};
struct Vertex* vertices = NULL;
int* indices = NULL; // Zakładamy, że indeksy są całkowite
int vertex_count = 0;
int index_count = 0;
int vertex_capacity = 1000;
int index_capacity = 1000;
void parseOBJ(const char* filename) {
FILE* file = fopen(filename, "r");
if (!file) {
printf("Nie można otworzyć pliku: %s\n", filename);
return;
}
vertices = (struct Vertex*)malloc(vertex_capacity * sizeof(struct Vertex));
indices = (int*)malloc(index_capacity * sizeof(int));
char line[128];
while (fgets(line, sizeof(line), file)) {
if (strncmp(line, "v ", 2) == 0) {
if (vertex_count >= vertex_capacity) {
vertex_capacity *= 2;
vertices = (struct Vertex*)realloc(vertices, vertex_capacity * sizeof(struct Vertex));
}
sscanf(line, "v %f %f %f",
&vertices[vertex_count].x,
&vertices[vertex_count].y,
&vertices[vertex_count].z);
// Ustaw losowe kolory (lub inne dane)
vertices[vertex_count].r = 150;
vertices[vertex_count].g = 10;
vertices[vertex_count].b = 0;
vertices[vertex_count].a = 25;
vertex_count++;
} else if (strncmp(line, "vn ", 3) == 0) {
if (vertex_count > 0) {
sscanf(line, "vn %f %f %f",
&vertices[vertex_count - 1].nx,
&vertices[vertex_count - 1].ny,
&vertices[vertex_count - 1].nz);
}
} else if (strncmp(line, "f ", 2) == 0) {
if (index_count >= index_capacity) {
index_capacity *= 2;
indices = (int*)realloc(indices, index_capacity * sizeof(int));
}
/*tutaj zazwyczaj jest blad bo występują rózne formaty .obj
if (sscanf(line, "f %d//%d %d//%d %d//%d", &v1, &vn1, &v2, &vn2, &v3, &vn3) == 6) {
// Obsłuż format v//vn
} else if (sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d", &v1, &vt1, &vn1, &v2, &vt2, &vn2, &v3, &vt3, &vn3) == 9) {
// Obsłuż format v/vt/vn
} else {
// Obsłuż prostsze przypadki
}
*/
int v1, v2, v3;
sscanf(line, "f %d//%d//%d", &v1, &v2, &v3);
indices[index_count++] = v1;
indices[index_count++] = v2;
indices[index_count++] = v3;
index_count++;
v1--;
v2--;
v3--;
}
}
fclose(file);
}
/*tutaj chciałem użyć vao i vbo ale nic z tego
GLuint VAO, VBO;
void createVAO() {
glGenVertexArraysOES(1, &VAO);
glBindVertexArrayOES(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Konfiguracja atrybutu pozycji
// glVertexPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glVertexPointer(3, GL_FLOAT, sizeof(&vertices), vertices);
//glEnableVertexAttribArray(0);
glBindVertexArrayOES(0);
}
*/
struct mouse_handle
{
int x = 1;
int y = 1;
} mouse;
/*this openGL in SDL*/
SDLTest_CommonState *state;
SDL_Event *event;
static SDL_GLContext *context;
struct obj {
float x;
float y;
float z;
void renderCube() {
// glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glRotatef(mouse.x % 360, mouse.y % 360, mouse.x % 360, mouse.y % 360);
glOrthox(-80.0, -80.0, -80.0, -80.0, -80.0, -80.0);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0f, 0.0f, -1.0f);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat light_position[] = { 0.0, 0.0, 10.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
GLfloat ambient_light[] = { 0.3, 0.2, 0.2, 0.0 };
GLfloat diffuse_light[] = { 0.5, 0.2, 0.1, 1.0 };
GLfloat specular_light[] = { 1.0, 0.3, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_light);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(struct Vertex), &vertices[0].r);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(struct Vertex), &vertices[0].x);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(struct Vertex), &vertices[0].nx);
glDrawElements(GL_TRIANGLES, index_count*3, GL_UNSIGNED_INT, indices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glLoadIdentity();
// glPopMatrix();
}
};
int main(int argc, char *argv[])
{
parseOBJ("cat.obj");
//createVAO();
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;
//createVAO();
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);
//createVAO();
/*draw()*/
/*
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthox(-30.0, -30.0, -30.0, -30.0, -30.0, -30.0);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0f, 0.0f, -1.0f); // Oddalenie widoku
*/
obj gg;
gg.x = 0.2f;
gg.y = 0.4f;
gg.z = 0.6f;
gg.renderCube();
SDL_GL_SwapWindow(*state->windows);
}
}
free(vertices);
free(indices);
return 0;
}
No comments:
Post a Comment