Hi. New to the forum but not to opengl 1.0, trying to render obj file vertices with the command glDrawArrays and only vertices are in the file not normals yet, I am using opengl 1.0 and Allegro 5. This problem has been difficult and I could use insight. Apologies for using old version of opengl but it comes with allegro 5. I have learned c++ and I do review online lessons daily for reminder, I would like to code 3d games in c++ without using engines like unity etc. only allegro. Thank you for your time. I’m in a hurry so here is my code.
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <fstream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_opengl.h>
#include <gl/GLU.h>
using namespace std;
const float FPS = 60;
int main(void) {
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
bool redraw = true;
if (!al_init()) {
fprintf(stderr, "failed to initialize allegro!
");
return -1;
}
timer = al_create_timer(1.0 / FPS);
if (!timer) {
fprintf(stderr, "failed to create timer!
");
return -1;
}
al_set_new_display_flags(ALLEGRO_OPENGL);
display = al_create_display(800, 600);
if (!display) {
fprintf(stderr, "failed to create display!
");
al_destroy_timer(timer);
return -1;
}
event_queue = al_create_event_queue();
if (!event_queue) {
fprintf(stderr, "failed to create event_queue!
");
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}
al_install_audio();
al_install_keyboard();
al_init_acodec_addon();
al_init_primitives_addon();
al_init_image_addon();
al_init_font_addon();
al_init_ttf_addon();
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_clear_to_color(al_map_rgb(0, 0, 0));
al_flip_display();
al_start_timer(timer);
while (1)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER) {
redraw = true;
}
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
}
if (redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;
al_clear_to_color(al_map_rgb(0, 0, 0));
glViewport(0, 0, 800, 600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30, 800 / 600, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
glClear(GL_COLOR_BUFFER_BIT);
ifstream objfile("obj.obj");
float VX;
float VY;
float VZ;
if (objfile.is_open())
{
while (objfile >> VX >> VY >> VZ)
{
GLfloat vertices[] = {VX,VY,VZ};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawArrays(GL_TRIANGLES, 0, 507);
}
objfile.close();
}
glDisableClientState(GL_VERTEX_ARRAY);
al_flip_display();
}
}
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}