#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"
float calculateNormal( float * coord1, float * coord2, float * coord3 )
{
/* calculate Vector1 and Vector2 */
float va[ 3 ], vb[ 3 ], vr[ 3 ], val;
va[ 0 ] = coord1[ 0 ] - coord2[ 0 ];
va[ 1 ] = coord1[ 1 ] - coord2[ 1 ];
va[ 2 ] = coord1[ 2 ] - coord2[ 2 ];
vb[ 0 ] = coord1[ 0 ] - coord3[ 0 ];
vb[ 1 ] = coord1[ 1 ] - coord3[ 1 ];
vb[ 2 ] = coord1[ 2 ] - coord3[ 2 ];
/* cross product */
vr[ 0 ] = va[ 1 ] * vb[ 2 ] - vb[ 1 ] * va[ 2 ];
vr[ 1 ] = vb[ 0 ] * va[ 2 ] - va[ 0 ] * vb[ 2 ];
vr[ 2 ] = va[ 0 ] * vb[ 1 ] - vb[ 0 ] * va[ 1 ];
/* normalization factor */
val = sqrt( vr[ 0 ] * vr[ 0 ] + vr[ 1 ] * vr[ 1 ] + vr[ 2 ] * vr[ 2 ] );
float norm[ 3 ];
norm[ 0 ] = vr[ 0 ] / val;
norm[ 1 ] = vr[ 1 ] / val;
norm[ 2 ] = vr[ 2 ] / val;
return norm[3];
}
struct mouse_handle {
int x = 1;
int y = 1;
} mouse;
int gl;
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;
void glBegin(int gl) {
vertices.clear();
}
//wieszcholki
void glVertex3f(float x, float y, float z) {
vertices.push_back({x, y, z});
}
//ślad węglowy
void glNormal3f(float nx, float ny, float nz) {
normals.push_back({nx, ny, nz});
}
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;
default:
std::cerr << "Unsupported primitive type!" << std::endl;
break;
}
}
GLuint car;
float carrot;
// Zmodyfikowana funkcja do ładowania OBJ, zapisująca wierzchołki w pamięci
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 (*data == 'v' && *(data + 1) == ' ') {
data += 2; // pomiń "v "
float x, y, z;
if (sscanf(data, "%f %f %f", &x, &y, &z) == 3) {
vertices.push_back({x, y, z});
}
}
if (*data == 'vn' && *(data + 1) == ' ') {
data += 3; // Skip "vn "
float nx, ny, nz;
if (sscanf(data, "%f %f %f", &nx, &ny, &nz) == 3) {
calculateNormal(&nx,&ny,&nz);
// normals.push_back({nx, ny, nz});
}
}
// Przejdź do następnej linii
while (data < end && *data != '\n') {
++data;
}
++data; // Pomiń nową linię
}
if (munmap(map, fileSize) == -1) {
perror("munmap");
}
}
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);
}
SDLTest_CommonState *state;
SDL_Event *event;
static SDL_GLContext *context;
struct obj {
float x;
float y;
float z;
int Render(const std::vector<Vertex>& vertices) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
setPerspective(105.0f, 1.0f, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
// Ustawienie światła
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat light_position[] = {0.2, 1.0, 10.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
// Ustawienie materiału (opcjonalne, aby oświetlenie działało poprawnie)
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {50.0};
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glRotatef(mouse.x % 360, mouse.y % 360, mouse.x % 360, mouse.y % 360);
// Renderowanie z normalnymi i wierzchołkami
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());
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(Normal), (void*)normals.data());
glDrawArrays(GL_TRIANGLES, 0, normals.size());
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;
//czy tutaj musimy definiować nowe wektory czy wczyta jako globalne
//std::vector<Normal> normals;
// std::vector<Vertex> vertices;
loadObj("/sdcard/tea.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;
}
SDL_GL_CreateContext(*state->windows);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
obj gg;
gg.Render(vertices); // Renderowanie z wcześniej wczytanych wierzchołków
SDL_GL_SwapWindow(*state->windows);
}
}
return 0;
}
No comments:
Post a Comment