Menu

header trouble

starcreek
2008-03-22
2012-09-26
  • starcreek

    starcreek - 2008-03-22

    i know somewhere i need to set Skybox_Initialize(10.0) into the header file.

    the function is under the class CGfxOpenGL::Init() which is shown on the header file.

    I made a new class called class Skybox; but im sure thats not enough.

    i tried having Skybox_Initialize(10.) under the public CGfxOpenGL(); destructor. of course below the virtural. i guess thats not enough for the compiler to regcognize it as a declaration.

    theres is also a CSkybox Header file which im really not to concerned about.

    My primary focus is just on the CGfxOpenGL file and its header.

    Enough babbaling heres the code:
    Header:

    ifndef __GL_COMPONENT

    define __GL_COMPONENT

    const int TERRAIN_SIZE = 260;

    define PI 3.14159f

    define DEG2RAD(x) (x*PI)/180.0f

    class Skybox;

    class CGfxOpenGL
    {
    private:
    GLubyte heightmap[TERRAIN_SIZE * TERRAIN_SIZE];
    int m_windowWidth;
    int m_windowHeight;

    GLuint m_grassTexture;
    GLuint m_waterTexture;
    
    float m_angle;
    float m_height;
    float cameraX, cameraY, cameraZ;
    

    public:
    CGfxOpenGL();
    virtual ~CGfxOpenGL();

    bool Init();
    bool Shutdown();
    
    void SetupProjection(int width, int height);
    
    void Prepare(float dt);
    void Render();
    
    void MoveCameraRight() { m_angle += 3.0f; }
    void MoveCameraLeft() { m_angle -= 3.0f; }
    void MoveCameraUp() { m_height += 3.0f; }
    void MoveCameraDown() { m_height -= 3.0f;}
    void DrawTerrain();
    

    };

    endif

    CGfxOPenGL file:

    ifdef _WINDOWS

    include <windows.h>

    endif

    include <gl/gl.h>

    include <gl/glu.h>

    include <math.h>

    include <time.h>

    include "glext.h"

    include "CGfxOpenGL.h"

    include <cmath>

    include <cstdio>

    include "CTargaImage.h"

    include <Skybox.h>

    pragma warning(disable:4244)

    const char heightmapFilename[] = "heightmap.raw";
    const float MAX_HEIGHT = 30.0f;
    const float MAX_FOG_HEIGHT = MAX_HEIGHT * 0.5f;
    const float SCALE_FACTOR = 256.0f / MAX_HEIGHT;
    const float WATER_HEIGHT = 2.0f;

    CGfxOpenGL::CGfxOpenGL()
    {
    m_grassTexture = m_waterTexture = 0;
    cameraX = cameraY = cameraZ = 0.0f;
    m_angle = 0.0;
    m_height = 10.0;
    }

    CGfxOpenGL::~CGfxOpenGL()
    {
    }

    bool CGfxOpenGL::Init()
    {
    FILE *pFile = fopen(heightmapFilename, "rb");
    if(!pFile)
    return false;

    fread(&amp;heightmap, TERRAIN_SIZE * TERRAIN_SIZE, 1, pFile);
    fclose(pFile);
    CTargaImage image;
    
    image.Load(&quot;grass.tga&quot;);
    glGenTextures(1, &amp;m_grassTexture);
    glBindTexture(GL_TEXTURE_2D, m_grassTexture);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
    image.Release();
    
    image.Load(&quot;water.tga&quot;);
    glGenTextures(1, &amp;m_waterTexture);
    glBindTexture(GL_TEXTURE_2D, m_waterTexture);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image.GetWidth(), image.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, image.GetImage());
    image.Release();
    
    Skybox_Initialize(10.0);
    Skybox_LoadTextures(&quot;up.tga&quot;, &quot;dn.tga&quot;, &quot;ft.tga&quot;, &quot;bk.tga&quot;, &quot;lt.tga&quot;, &quot;rt.tga&quot;);
    
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    
    return true;
    

    }

    bool CGfxOpenGL::Shutdown()
    {
    glDeleteTextures(1, &m_waterTexture);
    glDeleteTextures(1, &m_grassTexture);
    int Skybox_Release();
    Skybox_Release();

    return true;
    

    }

    void CGfxOpenGL::SetupProjection(int width, int height)
    {
    if (height == 0) // don't want a divide by zero
    {
    height = 1;
    }

    glViewport(0, 0, width, height);        // reset the viewport to new dimensions
    glMatrixMode(GL_PROJECTION);            // set projection matrix current matrix
    glLoadIdentity();                       // reset projection matrix
    
    // calculate perspective
    gluPerspective(54.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
    
    glMatrixMode(GL_MODELVIEW);             // set modelview matrix
    glLoadIdentity();                       // reset modelview matrix
    
    m_windowWidth = width;
    m_windowHeight = height;
    

    }

    void CGfxOpenGL::Prepare(float dt)
    {
    cameraX = sin(DEG2RAD(m_angle))TERRAIN_SIZE/2.0f;
    cameraY = m_height;
    cameraZ = cos(DEG2RAD(m_angle))
    TERRAIN_SIZE/2.0f;
    }

    void CGfxOpenGL::DrawTerrain()
    {
    // draw terrain!

    glBindTexture(GL_TEXTURE_2D, m_grassTexture);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    for (int z = 0; z &lt; TERRAIN_SIZE - 1; ++z)
    {
        glBegin(GL_TRIANGLE_STRIP);
        for (int x = 0; x &lt; TERRAIN_SIZE - 1; ++x)
        {
            // render two vertices on the same strip at once
            float scaledHeight = heightmap[z * TERRAIN_SIZE + x] / SCALE_FACTOR;
            float nextScaledheight = heightmap[(z + 1) * TERRAIN_SIZE + x] / SCALE_FACTOR;
            float color = 0.5f + 0.5f * scaledHeight / MAX_HEIGHT;
            float nextColor = 0.5 + 0.5f * nextScaledheight / MAX_HEIGHT;
    
            glColor3f(color, color, color);
            glTexCoord2f((GLfloat)x/TERRAIN_SIZE*8, (GLfloat)z/TERRAIN_SIZE*8);
            glVertex3f(static_cast&lt;GLfloat&gt;(x - TERRAIN_SIZE/2), scaledHeight, static_cast&lt;GLfloat&gt;(z - TERRAIN_SIZE/2));
    
            glColor3f(nextColor, nextColor, nextColor);
            glTexCoord2f((GLfloat)x/TERRAIN_SIZE*8, (GLfloat)(z+1)/TERRAIN_SIZE*8);
            glVertex3f(static_cast&lt;GLfloat&gt;(x - TERRAIN_SIZE/2), scaledHeight, static_cast&lt;GLfloat&gt;(z + 1 - TERRAIN_SIZE/2));
         }
         glEnd();
    

    }

         // draw the water
    glBindTexture(GL_TEXTURE_2D, m_waterTexture);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glBegin(GL_QUADS);
          glTexCoord2f(0.0f, 0.0f);
          glVertex3f(-TERRAIN_SIZE/2.1f, WATER_HEIGHT, TERRAIN_SIZE/2.1f);
    
          glTexCoord2f(TERRAIN_SIZE/4.0f, 0.0f);
          glVertex3f(TERRAIN_SIZE/2.1f, WATER_HEIGHT, TERRAIN_SIZE/2.1f);
    
          glTexCoord2f(TERRAIN_SIZE/4.0f, 0.0f);
          glVertex3f(TERRAIN_SIZE/2.1f, WATER_HEIGHT, -TERRAIN_SIZE/2.1f);
    
          glTexCoord2f(0.0f, TERRAIN_SIZE/4.0f);
          glVertex3f(-TERRAIN_SIZE/2.1f, WATER_HEIGHT, -TERRAIN_SIZE/2.1f);
    
    glEnd();
    

    }

    void CGfxOpenGL::Render()
    {
    glClearColor(0.0f, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

     glLoadIdentity();
     gluLookAt(cameraX, cameraY, cameraZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    
     // skybox origin should be same as camera position
    

    Skybox_Render(cameraX, cameraY, cameraZ);

     DrawTerrain();
    

    };

     
    • cpns

      cpns - 2008-03-23

      No. I am really not going to bother this time. It is a waste of time. You seem determined to learn nothing and to ignore all advice previously given.

      If you cannot imagine why doing initialisation in a destructor makes no sense you are completely lost! Nothing you have written in either English or code makes much sense. Further if you have not yet figured out the difference between a function declaration and a function call as seems to be the case you are truly up-the-creek.

       
    • starcreek

      starcreek - 2008-03-23

      Could u please help? i would really like to see this. I would like to apologize for my ignorance and i wish to obtain my help via this website.

      ok what im having trouble with is undeclared declarations, i.e. Skybox_Initialize(10.0)

      My first step is to see what class its under which happens to be 'bool CGfxOpenGL::Init()

      now(which is probably a mistake) i go to the CGfxOPenGL header file and notice that there already is a class named CGfxOpenGL. IF im correct the skybox_Intialize should be under the CGfxOpenGL class.

      now since my declaration of bool Skybox_Initalize(10.0) is under CGfxOpenGL::Init() i would place my Skybox_Initialize(10.0) under private.

      but unfortunatly it does not recognize it and reads undeclared.

      what im wondering is do i have to make an entirely new class for Skybox that im using? for instance:

      Skybox::Skybox
      {
      }
      Skybox::~Skybox
      {
      }
      Skybox::Initialize(10.0)

      // but what goes here?
      }

      could u please help? im sorry for not understanding. i will be more educated in the future i can promise that. i hope u are all doing well and happy easter.

       
    • cpns

      cpns - 2008-03-24

      Skybox_Initialize(10.0) is not a declaration. If it is anything (which I doubt), it is a call to a static member function.

      You cannot put a constant literal value in a declaration, that "10.0" should be a data type and parameter name. If you need a constant, then just use one, you don't need to pass it as a parameter at all.

      Why would you have an Initialize() function when you could use a constructor to automatically perform initialisation every time an object is created.

      Perhaps with this "10.0" what you actually wanted was an initialiser list for a constructor.

      If all the above means nothing, then that was exactly my point; as has been said before, you are trying to run before you can walk. It has been suggested (several times) that you first learn the basics of C++ and object oriented programming. So long as you persist with this over complex example that you yourself do not understand, you will continue to annoy rather than incite assistance.

      Basically no one wishes to help you start from the wrong place because you'll never reach your destination.

      Can I suggest that you this:

      http://www.cplusplus.com/doc/tutorial/

      and perhaps for greater depth, this: http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

      Asking over complex (and obviously confused) questions on a forum is a painfully slow and unreliable way to learn C++. The same is true of merely copying and adapting other peoples' code of which you have no real understanding. For example, this code is obviously not targeted at GCC; the #pragma directive only has meaning in Microsoft's compiler and will be ignored.

      I also suggest that you have over analyzed your problem, but your analysis is flawed, hence the question makes no sense - as you say it is all 'babbling'. I have no idea what the question is or what you are trying to do. I have no intention of trawling through all that code to try to figure it out, and you are not yet equipped to understand what your problem is in the first place.

      My point is that if you had started at the beginning instead of wading in at the deep end, you would have resolved all your confusions over this before the code got too complex for you to understand or for us to bother figuring out for you.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.