Issue with glPolygonMode - GL_FILL "skipping" vertices - c++

I'm having a weird issue when trying to draw a polygon and filling it with a particular color:
If I set the polygon mode as:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
The polygon renders just fine:
However, as soon as I replace that line with the following:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
The polygon doesn't fill right, but it seems like most lines get projected towards its first vertex, or something along those lines:
I'm obviously doing something wrong. What I want to do is keep the color inside the polygon, however it seems to be ignoring several vertices. What might be wrong?
Here's some selected parts of my code that might be of interest. I'm skipping over some data structures loading and other stuff that might not be very relevant:
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutInitWindowPosition(150, 100);
glutCreateWindow("CR-View GL");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void display(void) {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
/* Set drawing color */
glColor3f(1, 0, 1);
drawPolys(currentDrawingMode);
/* Clear screen and draw */
glutSwapBuffers();
}
// Draws the polygons
void drawPolys (int id) {
int poly, vertex;
// set wireframe mode (if an empty polygon is required)
if (id == 0) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
//Sets color fill mode
if (id == 1) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
// Draw each polygon...
for (poly = 0; poly < polyCount; poly++) {
glBegin(GL_POLYGON);
// Draw each vertex...
for (vertex = 0; vertex < Polygons[poly].vertexCount; vertex++) {
glVertex2f((float)Polygons[poly].vertices[vertex].x, (float)Polygons[poly].vertices[vertex].y);
}
glEnd();
}
}

If you're using GL_POLYGON be aware that it only supports convex polygons.

Related

How to color QUADS in blue color without all sense color in the blue

hello I am using glut and opengl with c++ , I have home I want to draw blue QUADS in it my problem when I draw the QUADS all the sense color in blue , so how I can to color only QUADS in blue color and Prevents to color all sense in blue color what I do wrong how to remove the blue color from all the sense and color only my QUAD?
my try:
void drawSquare1()
{
glBegin(GL_QUADS);
glColor3d(1,0,0);
glVertex3f(-0.5,-0.5,-0.5);
glColor3d(1,1,0);
glVertex3f(0.5,-0.5,-0.5);
glColor3d(1,1,1);
glVertex3f(0.5,0.5,-0.5);
glColor3d(0,1,1);
glVertex3f(-0.5,0.5,-0.5);
glEnd();
}
void render(void) // Our Rendering Is Done Here
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
GLfloat xtrans = -g_xpos;
GLfloat ztrans = -g_zpos;
GLfloat ytrans = -g_ypos;
if(g_yrot > 360)
g_yrot -= 360;
else if(g_yrot < 0)
g_yrot += 360;
GLfloat sceneroty = (360.0f - g_yrot);
int numpolygons;
glRotatef(g_lookupdown,1.0f,0,0);
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(xtrans, ytrans, ztrans);
numpolygons = g_sector1.numpolygons;
for (int loop_m = 0; loop_m < numpolygons; loop_m++)
texture_object(loop_m);
gluQuadricDrawStyle(my_shape[0],GLU_FILL);
glBindTexture(GL_TEXTURE_2D, textures[1].texID);
glScalef(0.1,0.1,0.1);
glTranslatef(0.78,14.3,-4.2);
gluSphere(my_shape[0], 1.0,50,50);
gluQuadricDrawStyle(my_shape[1],GLU_FILL);
glBindTexture(GL_TEXTURE_2D, textures[8].texID);
glTranslatef(-20,0,0);
gluSphere(my_shape[1], 1.0,50,50);
gluQuadricDrawStyle(my_shape[2],GLU_FILL);
glBindTexture(GL_TEXTURE_2D, textures[22].texID);
glTranslatef(40,0,0);
gluSphere(my_shape[2], 1.0,50,50);
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glOrtho(-10,window_width,0,window_height,-10,10); // Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
drawSquare1();
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPopMatrix(); // Restore The Old Projection Matrix
//glPushMatrix();
drawSquare1();
//glPopMatrix();
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glutSwapBuffers ( );
}
int main(int argc, char** argv) // Main Function For Bringing It All Together.
{
//cout << "Hello World!" << endl;
//cin.get();
glutInit(&argc, argv); // GLUT Initializtion
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE); // (CHANGED)
if (g_gamemode)
{
glutGameModeString("640x480:16"); // Select The 640x480 In 16bpp Mode
if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE))
glutEnterGameMode(); // Enter Full Screen
else g_gamemode = false; // Cannot Enter Game Mode, Switch To Windowed
}
screen_width = glutGet(GLUT_SCREEN_WIDTH);
screen_height = glutGet(GLUT_SCREEN_HEIGHT);
window_width = screen_width/1.4;
window_height = screen_height/1.4;
if (!g_gamemode)
{
glutInitWindowSize(window_width,window_height); // Window Size If We Start In Windowed Mode
glutInitWindowPosition((screen_width-window_width)/2,(screen_height-window_height)/2);
glutCreateWindow("Frank's 3-D House"); // Window Title
}
init();
glutIgnoreKeyRepeat(true); // Disable Auto Repeat (NEW)
// glutKeyboardFunc(myKey); // register the key handler.
glutDisplayFunc(render); // Register The Display Function
glutReshapeFunc(reshape); // Register The Reshape Handler
glutKeyboardFunc(keyboard); // Register The Keyboard Handler
//glRasterPos2f(lineMargin, currentHight); // set the cursor to the initial position.
glutSpecialFunc(special_keys); // Register Special Keys Handler
glutSpecialUpFunc(special_keys_up); // Called When A Special Key Released (NEW)
glutIdleFunc(game_function); // Process User Input And Does Rendering (CHANGED)
glutMouseFunc(mouse) ;
glutMainLoop(); // Go To GLUT Main Loop
return 0;
}
and this picture of run my code:
By default the texture environment mode (GL_TEXTURE_ENV_MODE) is GL_MODULATE. See glTexEnv.
This means if texturing is enabled (glEnable(GL_TEXTURE_2D)), then the color from the texture is multiplied by the color which is currently set by glColor.
To fix your issue, I recommend to set glColor4f(1.0f,1.0f,1.0f,1.0f); before the geometry is drawn:
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
.....
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
for (int loop_m = 0; loop_m < numpolygons; loop_m++)
texture_object(loop_m);
.....
}
Note, the current color is changed in the function drawSquare1 and keeps its state.

Faces missing when drawing icosahedron in OpenGL following code in redBook

I am attempting to draw an icosahedron following this popular OpenGl tutorial in the redBook.
I am using GLUT to handle windowing.
Here is my complete code. It is mostly the code from the tutorial plus some clerical work using GLUT
#include <stdio.h>
#include <GL/glut.h>
#define X .525731112119133606
#define Z .850650808352039932
void mouseEventHandler(int button, int state, int x, int y){
}
void display() {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
static GLfloat vdata[12][3] = {
{-X,0.0,Z}, {X,0.0,Z}, {-X,0.0,-Z}, {X,0.0,-Z},
{0.0,Z,X}, {0.0,Z,-X}, {0.0,-Z,X}, {0.0,-Z,-X},
{Z,X,0.0}, {-Z,X,0.0}, {Z,-X,0.0}, {-Z,-X,0.0},
};
static GLuint tindices[20][3] = {
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
int i;
glBegin(GL_TRIANGLES);
for (i = 0; i < 20; i++){
glNormal3fv(&vdata[tindices[i][0]][0]);
glVertex3fv(&vdata[tindices[i][0]][0]);
glNormal3fv(&vdata[tindices[i][1]][0]);
glVertex3fv(&vdata[tindices[i][1]][0]);
glNormal3fv(&vdata[tindices[i][2]][0]);
glVertex3fv(&vdata[tindices[i][2]][0]);
}
glEnd();
glFlush ( );
}
void windowSetup(){
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(80, 80);
glutInitWindowSize(1000,1000);
glutCreateWindow("OpenGL Ico");
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D( -2.0, 2.0, -2.0, 2.0 );
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
windowSetup();
glutDisplayFunc(display);
glutMouseFunc(&mouseEventHandler);
glutMainLoop();
}
This is my output:
This is very different from the expected output:
Does someone know why these differ so much?
The differences seem to be:
My icosahedron is missing faces
My icosahedron is being viewed from a different angle
My icosahedron is lit differently
The first one is the most pressing. I have noticed when I change glMatrixMode( GL_MODELVIEW); to glMatrixMode( GL_PROJECTION); the faces that aren't showing up appear and those that are currenty appearing disappear. Does anybody know why this could be?
missing faces
most likely you just have wrong order of indices. In such case Reversing them will solve the issue. To check this you can try:
glDisable(GL_CULL_FACE);
if problem disappears I am right. If not it is different thing (like too close to camera cutting by Z_NEAR but that would look a bit different).
To identify the correct face you can use glColor based on i for exaple
if (i==5) glColor3f(1.0,0.0,0.0); else glColor3f(1.0,1.0,1.0);
the red face would be the 6th in this case {8,3,10}
lighting
You are using vertex coordinates as normals so do not expect FLAT shading. Also I do not see you are setting any lights here (but that can be hidden in GLUT somewhere I do not use it). To remedy this use just single normal per triangle. so average the 3 normals you got and make an unit vector from that and use that (before first glVertex call of each triangle).
orientation
just rotate your GL_MODELVIEW to desired orientation. Standard perspective GL_PROJECTION has z axis as viewing direction and x,y axises matches the screen (while GL_MODELVIEW is unit)
[Edit1] I tried your code
So the problem is you got reverse order of indices then default polygon winding in OpenGL (at least in my environment) and wrong normals here fixed code:
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
const GLfloat vdata[12][3] =
{
{-X,0.0,Z}, {X,0.0,Z}, {-X,0.0,-Z}, {X,0.0,-Z},
{0.0,Z,X}, {0.0,Z,-X}, {0.0,-Z,X}, {0.0,-Z,-X},
{Z,X,0.0}, {-Z,X,0.0}, {Z,-X,0.0}, {-Z,-X,0.0},
};
const GLuint tindices[20][3] =
{
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
};
int i;
GLfloat nx,ny,nz;
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glBegin(GL_TRIANGLES);
for (i = 0; i < 20; i++)
{
nx =vdata[tindices[i][0]][0];
ny =vdata[tindices[i][0]][1];
nz =vdata[tindices[i][0]][2];
nx+=vdata[tindices[i][1]][0];
ny+=vdata[tindices[i][1]][1];
nz+=vdata[tindices[i][1]][2];
nx+=vdata[tindices[i][2]][0]; nx/=3.0;
ny+=vdata[tindices[i][2]][1]; ny/=3.0;
nz+=vdata[tindices[i][2]][2]; nz/=3.0;
glNormal3f(nx,ny,nz);
glVertex3fv(vdata[tindices[i][0]]);
glVertex3fv(vdata[tindices[i][1]]);
glVertex3fv(vdata[tindices[i][2]]);
}
glEnd();
And preview:
it is a screenshot and my object is rotating so do not expect correct orientation you expect.

How to debug openGL code?

I have problem with openGL debugging. I find that a lot of the time, OpenGL will show you it failed by not drawing anything. Every time code looks fine but it is not drawing anything on GL window.
For e.g consider the below code.I write it to draw the cube but it is not drawing anything and i am unable to find the cause.
========================================================
// cube_vertex_array.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <glut.h>
static GLfloat vertex[]=
{
100.0,100.0,0.0,
400.0,100.0,0.0,
400.0,400.0,0.0,
100.0,400.0,0.0,
100.0,100.0,-300.0,
400.0,100.0,-300.0,
400.0,400.0,-300.0,
100.0,400.0,-300.0
};
static GLfloat color[]=
{
1.0,0.0,0.0,
0.0,1.0,0.0,
0.0,0.0,1.0,
1.0,1.0,0.0,
1.0,0.0,1.0,
0.0,1.0,1.0
};
static GLubyte frontIndices[] = {0,1,2,3};
static GLubyte leftIndices[] = {1,5,6,2};
static GLubyte backIndices[] = {4,7,6,5};
static GLubyte rightIndices[] = {0,3,7,4};
static GLubyte topIndices[] = {3,2,6,7};
static GLubyte bottomIndices[] = {0,4,5,1};
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); //Set default background color to black.
glClearDepth(2.0); //Set the depth level for clearing depth buffer.
glShadeModel(GL_FLAT); //Set the shading model to FLAT
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the color and depth buffer.
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the color and depth buffer.
glColor3f(1.0,0.0,0.0);
//glBegin(GL_LINE_STRIP);
// glVertex3f(0.0,0.0,0.0);
// glVertex3f(200.0,100.0,0.0);
//glEnd();
glEnableClientState(GL_VERTEX_ARRAY); //Enable vertex array.
glEnableClientState(GL_COLOR_ARRAY); //Enable vertex array color.
glColorPointer(3,GL_FLOAT,0,color); //Specify the array for colors.
glVertexPointer(3,GL_FLOAT,0,vertex); //Specify the array for vertex.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,frontIndices); //Draw front face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,leftIndices); //Draw left face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,backIndices); //Draw back face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,rightIndices); //Draw right face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,topIndices); //Draw top face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,bottomIndices); //Draw bottom face.
glutSwapBuffers(); //Swap the buffers.
}
void Reshape(int w,int h)
{
glViewport(0.0,(GLsizei)w,0.0,(GLsizei)h); //Set the viewport according to new window size.
glMatrixMode(GL_PROJECTION); //Set matrix mode to projection.
glLoadIdentity(); //Replace the top matrix in the stack to the identity matrix.
gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h); //Set the orthographic projection.
glMatrixMode(GL_MODELVIEW); //Set matrix mode to modelview.
}
int main(int argc, char **argv)
{
glutInit(&argc,argv); //Initialize the glut.
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); //Set display mode and also enable double buffering.
glutInitWindowSize(500,500); //Set the initial window size.
glutCreateWindow("Cube"); //Create the window and also assign name to it.
init(); //Initialize the app.
glutDisplayFunc(Display); //Register the Display function.
glutReshapeFunc(Reshape); //Register the Reshape function.
glutMainLoop(); //Start the main loop.
return 0;
}
You have put GL_UNSIGNED_BYTE as the type parameter in glDrawElements(). This will cause openGL to interpret the array of indices you throw in as one byte per index. You should use GL_UNSIGNED_INT here instead.
Here's the working code based on the code your provided (I did port it to java though):
import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
public class GLTest {
public static void main(String[] args) {
try {
Display.create();
Display.setDisplayMode(new DisplayMode(500, 500));
Display.setResizable(true);
//the same arrays as the ones you specified.
float[] vertices = new float[]{100.0f,100.0f,0.0f,
400.0f,100.0f,0.0f,
400.0f,400.0f,0.0f,
100.0f,400.0f,0.0f,
100.0f,100.0f,-300.0f,
400.0f,100.0f,-300.0f,
400.0f,400.0f,-300.0f,
100.0f,400.0f,-300.0f};
float[] color = new float[]{1,0,0,
0,1,0,
0,0,1,
1,1,0,
1,0,1,
0,1,1};
int[] frontIndices = new int[]{0, 1, 2, 3};
//JWJGL bookkeeping..
ByteBuffer vertexBuffer = BufferUtils.createByteBuffer(vertices.length * 4);
ByteBuffer colourBuffer = BufferUtils.createByteBuffer(color.length * 4);
for(int i = 0; i < vertices.length; i++) {
vertexBuffer.putFloat(vertices[i]);
}
vertexBuffer.rewind();
for(int i = 0; i < color.length; i++) {
colourBuffer.putFloat(color[i]);
}
colourBuffer.rewind();
ByteBuffer indexBuffer = BufferUtils.createByteBuffer(4 * frontIndices.length);
for(int i = 0; i < frontIndices.length; i++) {
indexBuffer.putInt(frontIndices[i]);
}
indexBuffer.rewind();
//back you your code
glClearColor(1,1,1,1);
glShadeModel(GL_SMOOTH);
while(!Display.isCloseRequested()) {
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Display.getWidth(), 0, Display.getHeight(), -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, colourBuffer);
glVertexPointer(3, GL_FLOAT, 0, vertexBuffer);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, indexBuffer);
Display.update();
Display.sync(60);
}
} catch (LWJGLException e) {
e.printStackTrace();
}
}
}
Which results in:
use tools like glTrace / glIntercept (to look at OpenGL call trace), gDebugger (to visualize textures, shaders, OGL state etc.)
There is a list of OpenGL debugging tools here : https://www.opengl.org/wiki/Debugging_Tools
Also your code is using the old fixed pipeline which is considered deprecated since OpenGL 3.3, so i would recommend either not putting the tag "opengl-3" on your questions, or using opengl 3.3 core context and learning the "modern" OpenGL (which is more powerful and more difficult to learn but makes you understand how the GPU works).

Want to draw a wireframe mesh over a solid 3d object imported from blender

So I have this creature and I want to have three distinct modes, a solid, a wireframe and both. All examples using glOffset did not seem to work for me.
Here's my display:
// This function is called to display the scene.
void display()
{
//Background color
glClearColor(1, 1, 1, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
// Matrix setup
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, width, height);
glLoadIdentity();
gluPerspective(40, (float)width / (float)height, 0.1, 1000);
// Matrix setup
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -3);
//draw object
glBegin(GL_TRIANGLES);
for (int i = 0; i<mesh->nfaces.size(); i += 1)
for (int j = 0; j<3; j += 1){
glNormal3f(mesh->normal[mesh->nfaces[i][j]][0],
mesh->normal[mesh->nfaces[i][j]][1],
mesh->normal[mesh->nfaces[i][j]][2]);
glVertex3f(mesh->vertex[mesh->faces[i][j]][0],
mesh->vertex[mesh->faces[i][j]][1],
mesh->vertex[mesh->faces[i][j]][2]);
}
glEnd();
glutSwapBuffers();
}
Keyboard code where I tried to implement the shenanigans:
void keyboard(unsigned char key, int x, int y)
{
float colorBronzeDiff[4] = { 0.8, 0.6, 0.0, 1.0 };
switch (key)
{
case(27) :
exit(0);
break;
case('s') :
{
int myFlagCtr = getFlagCtr();
cout << "Pressed Before: " << getFlagCtr() << endl;
if (myFlagCtr == 0) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
setFlagCtr(1);
}
else if (myFlagCtr == 1) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
setFlagCtr(2);
}
else if (myFlagCtr == 2) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
// draw
glDisable(GL_POLYGON_OFFSET_FILL);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//glEnable( GL_POLYGON_OFFSET_LINE );
//glPolygonOffset( -2.0f, -2.0f );
glColor3f(1.0f, 0.0f, 0.0f);
glLineWidth(1.0f);
// draw
//glDisable( GL_POLYGON_OFFSET_LINE );
setFlagCtr(0);
cout << "Pressed Before: " << getFlagCtr() << endl;
}
break;
}
}
}
And here's main:
void main(int argc, char **argv)
{
// GLUT initialization.
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow("CodeBase");
// Register call backs.
initialize();
glutDisplayFunc(display);
glutReshapeFunc(reshapeMainWindow);
glutMotionFunc(mouse_motion);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse_button);
// Enter GLUT loop.
glutMainLoop();
delete mesh;
}
I assume what I am supposed to do is draw the image twice and then offset it, but for the life of me I can't seem to get this to work. I can't get it to show such a mode by default by putting it directly in display and attempting to draw the object twice.
Edit: In response to latest answer.
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
// draw
//draw object
glBegin(GL_TRIANGLES);
for (int i = 0; i<mesh->nfaces.size(); i += 1)
for (int j = 0; j<3; j += 1){
glNormal3f(mesh->normal[mesh->nfaces[i][j]][0],
mesh->normal[mesh->nfaces[i][j]][1],
mesh->normal[mesh->nfaces[i][j]][2]);
glVertex3f(mesh->vertex[mesh->faces[i][j]][0],
mesh->vertex[mesh->faces[i][j]][1],
mesh->vertex[mesh->faces[i][j]][2]);
}
glEnd();
glDisable(GL_POLYGON_OFFSET_FILL);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glColor3f(1.0f, 0.0f, 0.0f);
glLineWidth(1.0f);
// draw
//draw object
glBegin(GL_TRIANGLES);
for (int i = 0; i<mesh->nfaces.size(); i += 1)
for (int j = 0; j<3; j += 1){
glNormal3f(mesh->normal[mesh->nfaces[i][j]][0],
mesh->normal[mesh->nfaces[i][j]][1],
mesh->normal[mesh->nfaces[i][j]][2]);
glVertex3f(mesh->vertex[mesh->faces[i][j]][0],
mesh->vertex[mesh->faces[i][j]][1],
mesh->vertex[mesh->faces[i][j]][2]);
}
glEnd();
If you want the image to render in both wireframe and solid mode you have to actually execute the geometry rendering functions (eg: glBegin() ... glEnd()) twice.
In your third setting in the keyboard callback you call
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
But you don't draw any geometry between those two calls. GL is a state machine so only the last change to a given state (in this case the polygon drawing mode) will impact the rendering.
your setFlagCtr call should be in the keyboard callback function, but all the other stuff should be in the draw function where for the wire + solid mode you'll have to make sure you're calling this section at least twice:
glBegin(GL_TRIANGLES);
...
glEnd();
Long story short, don't put GL state or rendering management calls anywhere but your display function.

glutDisplayFunc displays garbage

I am trying to incorporate openGL into my c++ code for the first time. As a start up, I made this very primitive code, which defines a class called polygon and should display a polygon with a method polygon.draw(). Right now, everything below resides in a single main.cpp file, though for easy reading I am separating into section here:
The problem is, the below code compiles and runs alright. Only when the window named "simple" is created, displays garbage (mostly collected from my computer background screen :(.
Firstly, the class polygon:
#include <GL/glut.h>
#include "utility.hpp"
#include <vector>
void init(void);
class nikPolygon{
public:
std::vector<nikPosition> m_vertices;
nikColor m_color;
double m_alpha;
// constructors
// without alpha (default is 1.0)
nikPolygon(std::vector<nikPosition> vList, nikColor c):
m_vertices(vList), m_color(c), m_alpha(1.0){
}
nikPolygon(std::vector<nikPosition> vList, nikColor c, double a):
m_vertices(vList), m_color(c), m_alpha(a){
}
// default constructor
nikPolygon(){
}
// member functions
// add vertex
void addVertex(nikPosition v) { m_vertices.push_back(v); }
// remove vertex
void removeVertex(nikPosition v);
// adjust vertex
void modifyVertex(unsigned int vIndex, nikPosition newPosition);
// fill color
void setColor(nikColor col) { m_color = col; }
// set alpha
void setAlpha(double a) { m_alpha = a; }
// display
void drawPolygon(void){
// color the objet
glColor4f(m_color.red,
m_color.green,
m_color.blue,
m_alpha);
// construct polygon
glBegin(GL_POLYGON);
for (std::vector<nikPosition>::iterator it = m_vertices.begin();
it != m_vertices.end(); it++)
glVertex2f(it->x, it->y);
glEnd();
// send to screen
glFlush();
}
void draw(void);
};
Then the c/c++ callback interface (trampoline/thunk):
// for c++/c callback
nikPolygon* currentPolygon;
extern "C"
void drawCallback(void){
currentPolygon->drawPolygon();
}
void nikPolygon::draw(){
currentPolygon = this;
glutDisplayFunc(drawCallback);
}
And then the rest of it:
// initialize openGL etc
void init(void){
// set clear color to black
glClearColor(0.0, 0.0, 0.0, 0.0);
// set fill color to white
glColor3f(1.0, 1.0, 1.0);
// enable transperancy
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// setup standard orthogonal view with clipping
// box as cube of side 2 centered at origin
// this is the default view
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
}
int main(int argc, char** argv){
nikPolygon poly;
poly.addVertex(nikPosition(-0.5, -0.5));
poly.addVertex(nikPosition(-0.5, 0.5));
poly.addVertex(nikPosition(0.5, 0.5));
poly.addVertex(nikPosition(0.5, -0.5));
poly.setColor(nikColor(0.3, 0.5, 0.1));
poly.setAlpha(0.4);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("simple");
init();
poly.draw();
glutMainLoop();
}
First and foremost, the original code is completely overengineered. This may be part of the original confusion. Also there's not really much you can do, to fix the code, without throwing out most of it. For example representing each polygon (triangle) with a own object instance is about as inefficient as it can get. You normally do not want to do this. The usual approach at representing a model is a Mesh, which consists of a list/array of vertex attributes, and a list of faces, which is in essence a list of 3-tuples defining the triangles, making up the surface of the mesh. In class form
class Mesh
{
std::vector<float> vert_position;
std::vector<float> vert_normal;
std::vector<float> vert_texUV;
std::vector<unsigned int> faces_indices;
public:
void draw();
};
Then to draw a mesh you use Vertex Arrays
void Mesh::draw()
{
// This is the API as used up to including OpenGL-2.1
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXCOORD_ARRAY);
// sizes of attributes depend on actual application
glVertexPointer(3, GL_FLOAT, 0, &vert_position[0]);
glNormalPointer(GL_FLOAT, 0, &vert_normal[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &vert_texUV[0]);
glDrawElements(GL_TRIANGLES, faces_indices.size(), GL_UNSIGNED_INT, &faces_indices[0]);
}
You put references to these Mesh object instances into a list, or array, and iterate over that in the display function, calling the draw method, after setting the appropriate transformation.
std::list<Mesh> list_meshes;
void display()
{
clear_framebuffer();
set_viewport_and_projection();
for(std::list<Mesh>::iterator mesh_iter = list_meshes.begin();
mesh_iter != list_meshes.end();
mesh_iter++) {
mesh_iter->draw()
}
swap_buffers();
}
At the beginning of your drawPolygon function you need to do a glClear(GL_COLOR_BUFFER_BIT);