/**************************************/
/*************03:32*******************/
/**************************************/
#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>
#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]);
/*color random*/
colors[vertex_count][0] = 150; // R
colors[vertex_count][1] = 150; // G
colors[vertex_count][2] = 0; // B
colors[vertex_count][3] = 25; // 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;
indices[index_count][1] = v2 ;
indices[index_count][2] = v3;
index_count++;
}
}
fclose(file);
}
struct mouse_handle
{
int x = 1;
int y = 1;
} mouse;
/*this*/
SDLTest_CommonState *state;
SDL_Event *event;
static SDL_GLContext *context;
struct obj {
float x;
float y;
float z;
int Render() {
glPushMatrix(); // Save the current transformation matrix
glRotatef(mouse.x % 360, mouse.y % 360, mouse.x % 360, mouse.y % 360);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors); glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices); glEnableClientState(GL_VERTEX_ARRAY);
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);
/*draw()*/
obj gg;
gg.x = 0.2f;
gg.y = 0.4f;
gg.z = 0.6f;
gg.Render();
SDL_GL_SwapWindow(*state->windows);
}
}
return 0;
}
/*
# cube.obj
# Import into Blender with Y-forward, Z-up
#
# Vertices: Faces:
# f-------g +-------+
# /. /| /. 5 /| 3 back
# / . / | / . / |
# e-------h | 2 +-------+ 1|
# | b . .|. c z right | . . .|. +
# | . | / | /y | . 4 | /
# |. |/ |/ |. |/
# a-------d +---- x +-------+
# 6
# bottom
g cube
# Vertices
v 0.0 0.0 0.0 # 1 a
v 0.0 1.0 0.0 # 2 b
v 1.0 1.0 0.0 # 3 c
v 1.0 0.0 0.0 # 4 d
v 0.0 0.0 1.0 # 5 e
v 0.0 1.0 1.0 # 6 f
v 1.0 1.0 1.0 # 7 g
v 1.0 0.0 1.0 # 8 h
# Normal vectors
# One for each face. Shared by all vertices in that face.
vn 1.0 0.0 0.0 # 1 cghd
vn -1.0 0.0 0.0 # 2 aefb
vn 0.0 1.0 0.0 # 3 gcbf
vn 0.0 -1.0 0.0 # 4 dhea
vn 0.0 0.0 1.0 # 5 hgfe
vn 0.0 0.0 -1.0 # 6 cdab
# Faces v/vt/vn
# 3-------2
# | - |
# | # | Each face = 2 triangles (ccw)
# | - | = 1-2-3 + 1-3-4
# 4-------1
# Face 1: cghd = cgh + chd
f 3//1 7//1 8//1
f 3//1 8//1 4//1
# Face 2: aefb = aef + afb
f 1//2 5//2 6//2
f 1//2 6//2 2//2
# Face 3: gcbf = gcb + gbf
f 7//3 3//3 2//3
f 7//3 2//3 6//3
# Face 4: dhea = dhe + dea
f 4//4 8//4 5//4
f 4//4 5//4 1//4
# Face 5: hgfe = hgf + hfe
f 8//5 7//5 6//5
f 8//5 6//5 5//5
# Face 6: cdab = cda + cab
f 3//6 4//6 1//6
f 3//6 1//6 2//6
*/
No comments:
Post a Comment