i am working on openGL in Vc6
every time i run the following simple code output window crashes
#include <stdio.h>
#include <gl/glut.h>
//#include <gl/glaux.h>
void display(void)
{
glColor3f(255.0f,255.0f,255.0f);
glBegin(GL_QUADS);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,5.0f,0.0f);
glVertex3f(5.0f,5.0f,0.0f);
glVertex3f(5.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glEnd();
glFlush();
}
void init(void)
{
glViewport(0,0,400,400);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,4/3,4.0,1000.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(2.0,2.0,2.0,1.0,2.0,1.0,0.0,1.0,0.0);
}
int main(int argc, char *argv[])
{
glutInit(&argc,argv);
init();
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowPosition(400,400);
glutInitWindowSize(400,400);
glutCreateWindow("Trial");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
i don't know what is going wrong any boby please help
You are using OpenGL functions before you have an OpenGL context (which is a requirement to call any GL functions at all). The context is created by glutCreateWindow, but your first call to GL functions happens in init(). To fix this, you could move your init() call right below the glutCreateWindow call.
Related
I am learning C++ coming from a Java/C#/Lua/Python background.
I decided to learn the language by writing some small games using the OpenGL package (I'm also writing some text parsers and re-writing a scripting language I wrote in C#, in C++ to the same ends). I have the following test code I am using to set up my project:
// OpenGLExample01.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
// Initializes GLUT, the display mode, and main window; registers callbacks;
// enters the main event loop.
int main(int argc, char** argv) {
// Use a single buffered window in RGB mode (as opposed to a double-buffered
// window or color-index mode).
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Position window at (80,80)-(480,380) and give it a title.
glutInitWindowPosition(80, 80);
glutInitWindowSize(400, 300);
glutCreateWindow("A Simple Triangle");
// Tell GLUT that whenever the main window needs to be repainted that it
// should call the function display().
glutDisplayFunc(display);
// Tell GLUT to start reading and processing events. This function
// never returns; the program only exits when the user closes the main
// window or kills the process.
glutMainLoop();
}
static void display() {
// Set every pixel in the frame buffer to the current clear color.
glClear(GL_COLOR_BUFFER_BIT);
// Drawing is done by specifying a sequence of vertices. The way these
// vertices are connected (or not connected) depends on the argument to
// glBegin. GL_POLYGON constructs a filled polygon.
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5);
glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();
// Flush drawing command buffer to make drawing happen as soon as possible.
glFlush();
}
This code won't compile when I try to build it. The reason? In glutDisplayFunc(display), "display" is undefined. However, if I flip the functions around (i.e. define display() first, then define main()), the program builds without a hitch.
#include <iostream>
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
static void display() {
// Set every pixel in the frame buffer to the current clear color.
glClear(GL_COLOR_BUFFER_BIT);
// Drawing is done by specifying a sequence of vertices. The way these
// vertices are connected (or not connected) depends on the argument to
// glBegin. GL_POLYGON constructs a filled polygon.
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5);
glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();
// Flush drawing command buffer to make drawing happen as soon as possible.
glFlush();
}
// Initializes GLUT, the display mode, and main window; registers callbacks;
// enters the main event loop.
int main(int argc, char** argv) {
// Use a single buffered window in RGB mode (as opposed to a double-buffered
// window or color-index mode).
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Position window at (80,80)-(480,380) and give it a title.
glutInitWindowPosition(80, 80);
glutInitWindowSize(400, 300);
glutCreateWindow("A Simple Triangle");
// Tell GLUT that whenever the main window needs to be repainted that it
// should call the function display().
glutDisplayFunc(display);
// Tell GLUT to start reading and processing events. This function
// never returns; the program only exits when the user closes the main
// window or kills the process.
glutMainLoop();
}
Here's the issue: Everything I've read thus far suggests that the first code snippet should be functionally the same as the second snippet. What's going on here?
yes, in order to use it, the compiler need to see the declaration.
Only the declaration is needed though, not necessary the definition.
static void bar(); // declaration
void foo(){
bar();
}
static void bar(){ // function body here (definition)
/*do something*/
};
I am changing the window background color using the Glut library, which isn't working. The code works perfectly on my peer's computers but not on mine. I am using Visual Studio on Windows 10, with intel i5,8th gen, with integrated graphics.
I searched on Reddit threads, and the only thing relevant was that integrated graphics may be the issue.
Is integrated graphics really the issue or something else?
Are there any solutions to this problem, except changing the graphic cards?
Note- The library should be OpenGL, glut, glu only...
Code:
#include <GL/glut.h>
#include <gl/GLU.h>
#include <iostream>
using namespace std;
void draw() {}
void display() {
glClearColor(1.0, 1.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0, 640, 0, 480);
// draw();
glFlush();
return;
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(200, 200);
glutInitWindowSize(640, 480);
glutCreateWindow("House");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
I tried this code, and although it renders a window, it is black in color, which it should not be. It should be (as the question) any random color. Are there any solutions i can try?
#include <stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<GL/glut.h>
void display (void)
{
glClearColor(1.f, 0.f, 0.f, 1.f);
glEnd();
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Colorcube Viewer");
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
i am not able to figure out whats the problem with this code ?
it does not give me a red window.
You need to call glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); after setting the clear color (because you have depth test enabled, make sure that you're clearing both the color buffer AND the depth buffer
I am developing a game in OpenGL/GLUT and I need to open a new window to show the score when the game is won.
In order to do this, i will call glutCreateWindow() and register the callbacks after calling mainEventLoop().
Is there a problem with this ? How should I do it properly ?
Is there a problem with this?
Yes.
Why don't you simply draw the results in the same window as the game?
Why are you using GLUT in the first place? It's not a very good framework for games. Better use GLFW or SDL.
How should I do it properly ?
By adding a small GUI system to your engine, that allows you to overlay the screen with stats (like a HUD) and a score screen.
You will need two display callback functions, display( ) and display2( ) for each window plus window = glutCreateWindow("Window 1"); and window2 = glutCreateWindow("Window 2");.
Code example :
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glut.h>
int window2 = 0, window = 0, width = 400, height = 400;
void display(void)
{
glClearColor(0.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
printf("display1\n");
glFlush();
}
void display2(void)
{
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
printf("display2\n");
glFlush();
}
void reshape (int w, int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glutPostRedisplay();
}
int main(int argc, char **argv)
{
// Initialization stuff
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(width, height);
// Create window main
window = glutCreateWindow("Window 1");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutInitWindowPosition(100,100);
// Create second window
window2 = glutCreateWindow("Window 2");
glutDisplayFunc(display2);
glutReshapeFunc(reshape);
// Enter Glut Main Loop and wait for events
glutMainLoop();
return 0;
}
I'm developing C++ OpenGL code in Windows using Visual Studio 2008. I can't for the life of me understand why none of the integer functions work. I'll try using glVertex2i(2,2) but all I get is a black screen, I've also tried this with glrecti but I have had the same result.
When I use the floating point functions, they work. glVertex2f(.5,.5) and glRectf(1,2,3,4) work fine. I just can't figure out what is going wrong, what I have missed. People have obviously used glVertex2i before and had it work.
The simple code I've been working off of is this:
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
void draw(){
glClearColor(0,0,0,1);
glClear( GL_COLOR_BUFFER_BIT );
glColor3f(1, 1, 1);
glBegin(GL_LINES);
glVertex2i(100,100);
glVertex2i(200,200);
glEnd();
glFlush();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitWindowSize(600, 600);
glutCreateWindow("My first OpenGL program");
glutDisplayFunc(draw);
glutMainLoop();
}
Your code doesn't set any projection matrices, so passing values outside [-1, 1] is drawing outside of the viewport. That's why integer functions "don't work".