Cant get anythang to Render on the screen with OpenGL

Started by
5 comments, last by Crypter 17 years ago
Ok, so the code compiles and everythang but i cant get anythang to render :'( Here is my main code:

#include "Headers.h"

EngineCore Engine;
GraphicsEngine GphxEngine;
SceneRenderer Renderer;


int main(int argc, char *argv[])
{

    using namespace std;
    
  SDL_Surface* screen = Engine.loadEngine("Window Test",800,600); 
 //SDL_Surface* screen = Engine.MakeWindow("Hello World");
 Renderer.LoadDevice(800,600,32,true); 
     bool quit = false;

  //GphxEngine.LoadImage("Images\\sample.bmp",1);
  //GphxEngine.DrawImage(1, 200, 200, screen);
  //SDL_UpdateRect(screen,0,0,0,0);
  //GphxEngine.Flip_Buffer(screen);

Vertex* Prims; 
Prims[0].x = -1.0f;
Prims[0].y = -1.0f;
Prims[0].z = -10.f;

for (int i = 0; i < 2; i++)
{
Prims.r = 255;
Prims.b = 0;
Prims.g = 0;
Prims.a = 0;
}

Prims[1].x = -1.0f;
Prims[1].y = 1.0f;
Prims[1].z = -10.f;

Prims[2].x = 1.0f;
Prims[2].y = -1.0f;
Prims[2].z = -10.0f;

Renderer.drawPrim( Prims, 6 );

Renderer.FlipDevice();
 while(!quit)
 {

      quit = Engine.runEngine();
}
}

Now here is my SceneRenderer Code:

#include "Headers.h"



bool SceneRenderer::LoadDevice(int width,int height, int bitsperpixel, bool gwindowed)
{
     Uint32 iFlags;
     iFlags = SDL_OPENGL;
     iFlags |= SDL_HWPALETTE;
     iFlags |= SDL_RESIZABLE;
     
     window_width = width;
     window_height = height;
     window_bitsperpixel = bitsperpixel;
     windowed = gwindowed;
     
     
     const SDL_VideoInfo* pVideoInfo = SDL_GetVideoInfo();
     if (NULL == pVideoInfo)
     {
              return false;
              }
              
     if (pVideoInfo->hw_available)
     iFlags |= SDL_HWSURFACE;
     else
     iFlags |= SDL_SWSURFACE;
     
     if(pVideoInfo->blit_hw)
     iFlags |= SDL_HWACCEL;
     if (windowed == false)
     {
                  iFlags |= SDL_FULLSCREEN;
}
                  
     SDL_GL_SetAttribute ( SDL_GL_DOUBLEBUFFER, 1);
     SDL_GL_SetAttribute ( SDL_GL_DEPTH_SIZE, window_bitsperpixel);
     SDL_GL_SetAttribute ( SDL_GL_STENCIL_SIZE, 0);
     
     SDL_GL_SetAttribute( SDL_GL_ACCUM_RED_SIZE, 0);
     SDL_GL_SetAttribute( SDL_GL_ACCUM_GREEN_SIZE, 0);
     SDL_GL_SetAttribute( SDL_GL_ACCUM_BLUE_SIZE, 0);
     SDL_GL_SetAttribute( SDL_GL_ACCUM_ALPHA_SIZE, 0);
     
     OpenGLSurface = SDL_SetVideoMode( window_width, window_height, window_bitsperpixel, iFlags);
     
     if( NULL == OpenGLSurface) return false;
     
     return true;
};

bool SceneRenderer::clearDevice()
{
                           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                           glLoadIdentity();
                            
                            return true;
};

void SceneRenderer::FlipDevice()
{
                           glFlush();
                           SDL_GL_SwapBuffers();
};

void SceneRenderer::unLoadDevice()
{
                             SDL_FreeSurface(OpenGLSurface);
};
 
void SceneRenderer::drawPrim( Vertex* pVertices, int count )
{
     glPushMatrix();
     
     glPushAttrib( GL_CURRENT_BIT);
     glLoadIdentity();
     
     glBegin( GL_TRIANGLES );
     for (int i = 0; i < count; i++)
     {
         glColor4f( pVertices.r, pVertices.g, pVertices.b, pVertices.a ); 
         glVertex3f( pVertices.z, pVertices.y, pVertices.z );
         
         }
         
         glEnd();
         glPopAttrib();
         glPopMatrix();
};   

Here is my headers.h sorce: [sorce] #include <iostream> #include <string> #include <vector> #include <SDL.h> #include <cmath> #include "SDL/SDL_opengl.h" #include "EngineCore.h" #include "GraphicsEngine.h" #include "Vector3.h" #include "SceneRenderer.h" [/source] Thanks Guys.
Advertisement
All of these need to be dereferenced:
Vertex* Prims; Prims[0].x = -1.0f;Prims[0].y = -1.0f;Prims[0].z = -10.f;for (int i = 0; i < 2; i++){Prims.r = 255;Prims.b = 0;Prims.g = 0;Prims.a = 0;}Prims[1].x = -1.0f;Prims[1].y = 1.0f;Prims[1].z = -10.f;Prims[2].x = 1.0f;Prims[2].y = -1.0f;Prims[2].z = -10.0f;


You also need to allocate memory for the Prims array before you start accessing elements.
What do you meen thanks?
Yea your code looks jank. I can't believe you don't get a run-time error when not setting the pointer to anything.

You do realize this loop below is only setting the data for Prims[0] to Prims[1], and not Prims[2], right? I mean even then, your still not setting anything to anything because Prims is never given a location in memory.

for (int i = 0; i < 2; i++)
{
Prims.r = 255;
Prims.b = 0;
Prims.g = 0;
Prims.a = 0;
}




you have .z, .y, .z .........
glVertex3f( pVertices.z, pVertices.y, pVertices.z );



And like the other guy said, derefernce the pointer "->" instead of ".". You have a crap compiler if it lets you compiler that. Learn more c++, your only going to get confused.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Wow, you didnt really have to get that pissed....
dpadam450 is not pissed. He is trying to help you. What he is trying to get at is that your code is essentially meaningless. Vertex *Prims declares a pointer to a type Vertex (which I assume must be declared elsewhere in your code) which means that Prims potentially contains the memory address of data for a type Vertex. In your code, it doesn't contain any memory address because you didn't set the pointer to point to anything.

If you're not understanding, you should hit the books some more...pointers are a hard topic: it took me a while to actually grok the concept myself, so don't feel bad if you don't understand at the moment.

And actually, I'm also surprised your compiler let you get away with that.
Im surprised you didnt get an access violation..

As others pointed out here..
Vertex* Prims; Prims[0].x = -1.0f;Prims[0].y = -1.0f;Prims[0].z = -10.f;

Prims is a pointer, but what is it pointing to? Because C/C++
does not automatically assign values to varables, Prims contains
random junk (meaning it is pointing to some random memory location)

Some compiliers may set Prims=0, but that should still cause an
access violation.

Derefrencing random memory is not good-And most systems (Including Windows)
will throw an access violation exception (0xc0000005 for windows)

You should either allocate memory for the ptr, are use a storage container
(std::vector, perhaps). Both methods would work well here.

This topic is closed to new replies.

Advertisement