PDA

View Full Version : Object not showing (c++ )



Pedrosanchez87
03-02-2017, 03:54 PM
I am trying to make a lunar lander type game to start off opengl with, but i cant get get the object to show, if anyone could help out to why this isnt showing, it would be greatly appreciated . This is the code i have

main


#include <iostream>
#include <GL/glut.h>
#include "Lander.h"
#include "random.h"

using namespace std;
float startx = rnd(100, 600); float starty = rnd(350, 450); int startradius = 10;

Lander lander(startx, starty, startradius);

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
lander.show();
glutSwapBuffers();
}

void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(1000, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Animation Example");
glClearColor(1.0, 1.0, 1.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();

}




Lander.h

#ifndef Lander_H
#define Lander_H

class Lander
{
public:
Lander(float ix, float iy, int iradius);
void show();
void draw();
private:
float x, y, radius;
};
#endif

Lander.cpp


#include "Lander.h"
#include<gl/glut.h>
#include <cmath>


Lander::Lander(float ix, float iy, int iradius)
{
x = ix; y = iy; radius = iradius;
}

void Lander::show()
{
glPushMatrix();
glTranslatef(x, y, 0);
glRotatef(22, 0, 0, 1);
glTranslatef(-x, -y, 0);
draw();
glPopMatrix();
}

void Lander::draw()
{
glBegin(GL_POLYGON);
const int NPOINTS = 8;
const float TWOPI = 2 * 3.1415927;
const float STEP = TWOPI / NPOINTS;
glColor3f(1.0, 0.0, 0.0);
for (float angle = 0; angle<TWOPI; angle += STEP)
glVertex2f(x + radius*cos(angle), y + radius*sin(angle));
glEnd();

glBegin(GL_LINE_LOOP);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(x - 6, y - 6);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(x - 12, y - 12);
glEnd();

glBegin(GL_LINE_LOOP);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(x, y - 8);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(x, y - 17);
glEnd();
}

Loonywx
03-07-2017, 07:36 AM
I think you need to clear the depth buffer also:

glClearColor(color[0], color[1], color[2], color[3]);
glClearDepth(static_cast<GLclampd>(depth));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

mhagain
03-07-2017, 09:22 AM
Try creating a double-buffered context rather than single-buffered; i.e. replace this:
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);With this:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
Tutorials that create single-buffered contexts should be mercilessly wiped from the face of the earth. You see - double-buffered is not actually more difficult, it's more compatible, and weird errors like this that might put people off from continuing just won't happen.