Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: stuck on lack of rendering with glDrawArrays, allegro 5 and Opengl

  1. #1
    Newbie Newbie
    Join Date
    Sep 2018
    Posts
    2

    stuck on lack of rendering with glDrawArrays, allegro 5 and Opengl

    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.

    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!\n");
    		return -1;
    	}
     
    	timer = al_create_timer(1.0 / FPS);
    	if (!timer) {
    		fprintf(stderr, "failed to create timer!\n");
    		return -1;
    	}
     
    	al_set_new_display_flags(ALLEGRO_OPENGL);
    	display = al_create_display(800, 600);
    	if (!display) {
    		fprintf(stderr, "failed to create display!\n");
    		al_destroy_timer(timer);
    		return -1;
    	}
     
    	event_queue = al_create_event_queue();
    	if (!event_queue) {
    		fprintf(stderr, "failed to create event_queue!\n");
    		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;
     
     
    }

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,926
    Quote Originally Posted by MasterComputer202 View Post
    Code :
    			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();
    			}
    This isn't even remotely correct. It's not that there's something specific wrong with it; it doesn't even resemble something which could read an OBJ file.

  3. #3
    Newbie Newbie
    Join Date
    Sep 2018
    Posts
    2
    Hi! Thank you for the reply. To start, I thought that an obj file is just like a txt file, so I loaded it like a txt file. It is obvious now though, like you said that I am loading it wrong no matter what the case.
    This is the tutorial I learned how to open and read txt files https://www.youtube.com/watch?v=lzxWNtjii8U

    Is there any resource I can look at such as a tutorial or documents that you personally suggest that could possibly lead me to understand loading different types of files? Or are there any libs you think would be good for model loading in opengl 1.0? not any other version. I know there's assimp, but I just want to make sure since its only a loader its safe to use with every opengl version.

    I would prefer though an obj library which I could export per frame in opengl for simple animation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •