Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy won't my OpenGL draw anything?
    primarykey
    data
    text
    <p>I'm working on porting my open source particle engine test from SDL to SDL + OpenGL. I've managed to get it compiling, and running, but the screen stays black no matter what I do. main.cpp:</p> <pre><code>#include "glengine.h" int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { //Create a glengine instance ultragl::glengine *gle = new ultragl::glengine(); if(gle-&gt;init()) gle-&gt;run(); else std::cout &lt;&lt; "glengine initializiation failed!" &lt;&lt; std::endl; //If we can't initialize, or the lesson has quit we delete the instance delete gle; return 0; }; </code></pre> <p>glengine.h:</p> <pre><code>//we need to include window first because GLee needs to be included before GL.h #include "window.h" #include &lt;math.h&gt; // Math Library Header File #include &lt;vector&gt; #include &lt;stdio.h&gt; using namespace std; namespace ultragl { class glengine { protected: window m_Window; ///&lt; The window for this lesson unsigned int m_Keys[SDLK_LAST]; ///&lt; Stores keys that are pressed float piover180; virtual void draw(); virtual void resize(int x, int y); virtual bool processEvents(); void controls(); private: /* * We need a structure to store our vertices in, otherwise we * just had a huge bunch of floats in the end */ struct Vertex { float x, y, z; Vertex(){} Vertex(float x, float y, float z) { this-&gt;x = x; this-&gt;y = y; this-&gt;z = z; } }; struct particle { public : double angle; double speed; Vertex v; int r; int g; int b; int a; particle(double angle, double speed, Vertex v, int r, int g, int b, int a) { this-&gt;angle = angle; this-&gt;speed = speed; this-&gt;v = v; this-&gt;r = r; this-&gt;g = g; this-&gt;b = b; this-&gt;a = a; } particle() { } }; particle p[500]; float particlesize; public: glengine(); ~glengine(); virtual void run(); virtual bool init(); void glengine::test2(int num); void glengine::update(); }; }; </code></pre> <p>window.h:</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; #include "GLee/GLee.h" #include &lt;SDL/SDL.h&gt; #include &lt;SDL/SDL_opengl.h&gt; #include &lt;GL/glu.h&gt; using namespace std; namespace ultragl { class window { private: int w_height; int w_width; int w_bpp; bool w_fullscreen; string w_title; public: window(); ~window(); bool createWindow(int width, int height, int bpp, bool fullscreen, const string&amp; title); void setSize(int width, int height); int getHeight(); int getWidth(); }; }; </code></pre> <p>glengine.cpp (the main one to look at):</p> <pre><code>#include "glengine.h" namespace ultragl{ glengine::glengine() { piover180 = 0.0174532925f; } glengine::~glengine() { } void glengine::resize(int x, int y) { std::cout &lt;&lt; "Resizing Window to " &lt;&lt; x &lt;&lt; "x" &lt;&lt; y &lt;&lt; std::endl; if (y &lt;= 0) { y = 1; } glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)x/(GLfloat)y,1.0f,100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } bool glengine::processEvents() { SDL_Event event; while (SDL_PollEvent(&amp;event))//get all events { switch (event.type) { // Quit event case SDL_QUIT: { // Return false because we are quitting. return false; } case SDL_KEYDOWN: { SDLKey sym = event.key.keysym.sym; if(sym == SDLK_ESCAPE) //Quit if escape was pressed { return false; } m_Keys[sym] = 1; break; } case SDL_KEYUP: { SDLKey sym = event.key.keysym.sym; m_Keys[sym] = 0; break; } case SDL_VIDEORESIZE: { //the window has been resized so we need to set up our viewport and projection according to the new size resize(event.resize.w, event.resize.h); break; } // Default case default: { break; } } } return true; } bool glengine::init() { srand( time( NULL ) ); for(int i = 0; i &lt; 500; i++) p[i] = particle(0, 0, Vertex(0.0f, 0.0f, 0.0f), 0, 0, 0, 0); if (!m_Window.createWindow(640, 480, 32, false, "Paricle Test GL")) { return false; } particlesize = 0.01; glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glEnable(GL_BLEND); glBlendFunc(GL_ONE , GL_ONE_MINUS_SRC_ALPHA); return true; } void glengine::test2(int num) { glPushMatrix(); glTranslatef(p[num].v.x, p[num].v.y, p[num].v.z); glBegin(GL_QUADS); glColor4i(p[num].r, p[num].g, p[num].b, p[num].a); // Green for x axis glVertex3f(-particlesize, -particlesize, particlesize); glVertex3f( particlesize, -particlesize, particlesize); glVertex3f( particlesize, particlesize, particlesize); glVertex3f(-particlesize, particlesize, particlesize); glEnd(); glPopMatrix(); } void glengine::draw() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); // Reset The Current Modelview Matrix gluLookAt(0, 5, 20, 0, 0, 0, 0, 0, 0); for(int i = 0; i &lt; 500; i++) test2(i); } void glengine::update() { for(int i = 0; i &lt; 500; i++) { if(p[i].a &lt;= 0) p[i] = particle(5 + rand() % 360, (rand() % 10) * 0.1, Vertex(0.0f, 0.0f, 0.0f), 0, 255, 255, 255); else p[i].a -= 1; p[i].v.x += (sin(p[i].angle * (3.14159265/180)) * p[i].speed); p[i].v.y -= (cos(p[i].angle * (3.14159265/180)) * p[i].speed); } } void glengine::run() { while(processEvents()) { update(); draw(); SDL_GL_SwapBuffers(); } } }; </code></pre> <p>And finally window.cpp:</p> <pre><code>#include "window.h" namespace ultragl { window::window(): w_width(0), w_height(0), w_bpp(0), w_fullscreen(false) { } window::~window() { SDL_Quit(); } bool window::createWindow(int width, int height, int bpp, bool fullscreen, const string&amp; title) { if( SDL_Init( SDL_INIT_VIDEO ) != 0 ) return false; w_height = height; w_width = width; w_title = title; w_fullscreen = fullscreen; w_bpp = bpp; //Set lowest possiable values. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //Set title. SDL_WM_SetCaption(title.c_str(), title.c_str()); // Flags tell SDL about the type of window we are creating. int flags = SDL_OPENGL; if(fullscreen == true) flags |= SDL_FULLSCREEN; // Create window SDL_Surface * screen = SDL_SetVideoMode( width, height, bpp, flags ); if(screen == 0) return false; //SDL doesn't trigger off a ResizeEvent at startup, but as we need this for OpenGL, we do this ourself SDL_Event resizeEvent; resizeEvent.type = SDL_VIDEORESIZE; resizeEvent.resize.w = width; resizeEvent.resize.h = height; SDL_PushEvent(&amp;resizeEvent); return true; } void window::setSize(int width, int height) { w_height = height; w_width = width; } int window::getHeight() { return w_height; } int window::getWidth() { return w_width; } }; </code></pre> <p>Anyway, I really need to finish this, but I've already tried everything I could think of. I tested the glengine file many different ways to where it looked like this at one point:</p> <pre><code>#include "glengine.h" #include "SOIL/SOIL.h" #include "SOIL/stb_image_aug.h" #include "SOIL/image_helper.h" #include "SOIL/image_DXT.h" namespace ultragl{ glengine::glengine() { piover180 = 0.0174532925f; } glengine::~glengine() { } void glengine::resize(int x, int y) { std::cout &lt;&lt; "Resizing Window to " &lt;&lt; x &lt;&lt; "x" &lt;&lt; y &lt;&lt; std::endl; if (y &lt;= 0) { y = 1; } glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)x/(GLfloat)y,1.0f,1000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } bool glengine::processEvents() { SDL_Event event; while (SDL_PollEvent(&amp;event))//get all events { switch (event.type) { // Quit event case SDL_QUIT: { // Return false because we are quitting. return false; } case SDL_KEYDOWN: { SDLKey sym = event.key.keysym.sym; if(sym == SDLK_ESCAPE) //Quit if escape was pressed { return false; } m_Keys[sym] = 1; break; } case SDL_KEYUP: { SDLKey sym = event.key.keysym.sym; m_Keys[sym] = 0; break; } case SDL_VIDEORESIZE: { //the window has been resized so we need to set up our viewport and projection according to the new size resize(event.resize.w, event.resize.h); break; } // Default case default: { break; } } } return true; } bool glengine::init() { srand( time( NULL ) ); for(int i = 0; i &lt; 500; i++) p[i] = particle(0, 0, Vertex(0.0f, 0.0f, 0.0f), 0, 0, 0, 0); if (!m_Window.createWindow(640, 480, 32, false, "Paricle Test GL")) { return false; } particlesize = 10.01; glShadeModel(GL_SMOOTH); // Enable Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do glEnable(GL_BLEND); glBlendFunc(GL_ONE , GL_ONE_MINUS_SRC_ALPHA); return true; } void glengine::test2(int num) { //glPushMatrix(); //glTranslatef(p[num].v.x, p[num].v.y, p[num].v.z); glColor4i(255, 255, 255, 255); glBegin(GL_QUADS); glNormal3f( 0.0f, 0.0f, 1.0f); glVertex3f(-particlesize, -particlesize, particlesize); glVertex3f( particlesize, -particlesize, particlesize); glVertex3f( particlesize, particlesize, particlesize); glVertex3f(-particlesize, particlesize, particlesize); glEnd(); // Back Face glBegin(GL_QUADS); glNormal3f( 0.0f, 0.0f,-1.0f); glVertex3f(-particlesize, -particlesize, -particlesize); glVertex3f(-particlesize, particlesize, -particlesize); glVertex3f( particlesize, particlesize, -particlesize); glVertex3f( particlesize, -particlesize, -particlesize); glEnd(); // Top Face glBegin(GL_QUADS); glNormal3f( 0.0f, 1.0f, 0.0f); glVertex3f(-particlesize, particlesize, -particlesize); glVertex3f(-particlesize, particlesize, particlesize); glVertex3f( particlesize, particlesize, particlesize); glVertex3f( particlesize, particlesize, -particlesize); glEnd(); // Bottom Face glBegin(GL_QUADS); glNormal3f( 0.0f,-1.0f, 0.0f); glVertex3f(-particlesize, -particlesize, -particlesize); glVertex3f( particlesize, -particlesize, -particlesize); glVertex3f( particlesize, -particlesize, particlesize); glVertex3f(-particlesize, -particlesize, particlesize); glEnd(); // Right face glBegin(GL_QUADS); glNormal3f( 1.0f, 0.0f, 0.0f); glVertex3f( particlesize, -particlesize, -particlesize); glVertex3f( particlesize, particlesize, -particlesize); glVertex3f( particlesize, particlesize, particlesize); glVertex3f( particlesize, -particlesize, particlesize); glEnd(); // Left Face glBegin(GL_QUADS); glNormal3f(-1.0f, 0.0f, 0.0f); glVertex3f(-particlesize, -particlesize, -particlesize); glVertex3f(-particlesize, -particlesize, particlesize); glVertex3f(-particlesize, particlesize, particlesize); glVertex3f(-particlesize, particlesize, -particlesize); glEnd(); //glPopMatrix(); } void glengine::draw() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); // Reset The Current Modelview Matrix gluLookAt(0, 5, 20, 0, 0, 0, 0, 1, 0); for(int i = 0; i &lt; 500; i++) test2(i); } void glengine::update() { for(int i = 0; i &lt; 500; i++) { if(p[i].a &lt;= 0) p[i] = particle(5 + rand() % 360, (rand() % 10) * 0.1, Vertex(0.0f, 0.0f, -5.0f), 0, 255, 255, 255); else p[i].a -= 1; p[i].v.x += (sin(p[i].angle * (3.14159265/180)) * p[i].speed); p[i].v.y -= (cos(p[i].angle * (3.14159265/180)) * p[i].speed); } } void glengine::run() { while(processEvents()) { update(); draw(); SDL_GL_SwapBuffers(); } } }; </code></pre> <p>It still didn't work. I'm really at my wits end on this one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload