Jump to content
  • Advertisement
Sign in to follow this  
Chrono1081

OpenGL Why is my edge not showing? Can someone explain this to me?

This topic is 2808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

I'm running a program out of an OpenGL book and its rendering squares and demonstrating different polygon modes. I am a bit confused though to why the diagonal edge is only on the yellow square and is not apparent in the purple square (see pic).

The code for both squares is below. Is it only drawing the diagonal edge because the yellow square is using glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)?

Purple Square:


glPolygonMode(GL_BACK, GL_LINE);

glPushMatrix();
glTranslatef(2.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(1.0, 0.0, 1.0);
Draw2DSquare();
glPopMatrix();




Yellow Square:


glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glPushMatrix();
glTranslatef(4.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
Draw2DSquare();
glPopMatrix();




EDIT: Here is the source code for the Draw3DSquare()


void CGfxOpenGL::Draw2DSquare()

{

glBegin(GL_POLYGON);

glVertex3f(-0.5f, 0.0f, 0.0f);

glVertex3f(0.5f, 0.0f, 0.0f);

glVertex3f(0.5f, 0.0f, -1.0f);

glVertex3f(-0.5f, 0.0f,-1.0f);

glEnd();

}




Also the picture shows both polygons on the back face. The front face of the purple polygon is filled.

Share this post


Link to post
Share on other sites
Advertisement
Do you have the source to Draw2DSquare that you could post? I think the explanation would make more sense if I could see/quote it.

Share this post


Link to post
Share on other sites

Do you have the source to Draw2DSquare that you could post? I think the explanation would make more sense if I could see/quote it.


Oops sorry I just added it above :)

Share this post


Link to post
Share on other sites
Hmm I thought I would know the answer but I'm kind of stumped myself (don't do much with the glPolygonMode).

It appears you have a back facing polygon, so I'm not sure why the front face mode effects it.

Have you touched any of these other options?

glEnable(GL_CULL_FACE)
glFrontFace()?
glCullMode()?

Also what do you mean "the front face of the purple polygon is filled? Is that just an observation, or are you filling it with a second draw call?

Share this post


Link to post
Share on other sites

Hmm I thought I would know the answer but I'm kind of stumped myself (don't do much with the glPolygonMode).

It appears you have a back facing polygon, so I'm not sure why the front face mode effects it.

Have you touched any of these other options?

glEnable(GL_CULL_FACE)
glFrontFace()?
glCullMode()?

Also what do you mean "the front face of the purple polygon is filled? Is that just an observation, or are you filling it with a second draw call?




I haven't touched any of those functions. Also the purple polygon being filled is because during the init function glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) is called.

Here is the entire CPP file if this gives any hint. I'm really at a loss too to why its doing that:




#ifdef _WINDOWS
#include <windows.h>
#endif

#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include "CGfxOpenGL.h"

CGfxOpenGL::CGfxOpenGL()
{
}

CGfxOpenGL::~CGfxOpenGL()
{
}

bool CGfxOpenGL::Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
return true;
}

bool CGfxOpenGL::Shutdown()
{
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 aspect ratio of window
gluPerspective(52.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)
{
}

void CGfxOpenGL::Render()
{
static float angle = 0.0;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear screen and depth buffer
glLoadIdentity();
gluLookAt(0.0, 10.0, 0.1, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glPolygonMode(GL_FRONT, GL_LINE);
glPushMatrix();
glTranslatef(-4.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(1.0, 0.0, 0.0);
Draw2DSquare();
glPopMatrix();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // reset

glPolygonMode(GL_BACK, GL_POINT);

glPushMatrix();
glTranslatef(-2.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
Draw2DSquare();
glPopMatrix();

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(0.0, 0.0, 1.0);
Draw2DSquare();
glPopMatrix();

glPolygonMode(GL_BACK, GL_LINE);

glPushMatrix();
glTranslatef(2.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(1.0, 0.0, 1.0);
Draw2DSquare();
glPopMatrix();

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glPushMatrix();
glTranslatef(4.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
Draw2DSquare();
glPopMatrix();

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

angle += 0.2f;
}

void CGfxOpenGL::Draw2DSquare()
{
glBegin(GL_POLYGON);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.0f, -1.0f);
glVertex3f(-0.5f, 0.0f,-1.0f);
glEnd();
}




Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!