Programming with OpenGL problems MinGW - c

I tried on eclipse and got the error "Cannot open output file , Permission denied" when trying to use OpenGL. When I used code::blocks - got the same problems as compiling it by myself at the cmd using gcc -o <file_name>.exe <file_name>.c. And all I got was a list full of "undefined reference" to each function appeared in the program taken from OpenGL headers.
That's the small programs - just printing a red square in the middle.
#include <GL\gl.h>
#include <GL\glu.h>
#include <GL\glut.h>
#include <GL\freeglut.h>
#include <GL\glext.h>
#include <GL\freeglut_ext.h>
#include <GL\freeglut_std.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <windows.h>
void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//Draw a red 1x1 square in center
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); //render now
}
int main(int argc, char** argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the infinitely event-processing loop
return 0;
}

Related

OpenGL Black Screen on M2 Macbook

I have been trying to create a basic OpenGL program in C to display a triangle. Except all I am getting is a black screen.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//#include <GL/glew.h>
#define GL_SILENCE_DEPRECATION
#include "win.h"
#include "shaders/cshader.h"
#include "glew.h"
#define GLFW_INCLUDE_GLCOREARB
#include "glfw3.h"
#include <OpenGL/gl3.h>
int main(void) {
if( !glfwInit()) {
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
// create window
GLFWwindow* window = createWindow(1280,720);
glewExperimental=GL_TRUE; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
Shader shader = ShaderInit();
shader.Load(&shader,"/Users/***/Desktop/coding/gl_test/shaders/tri.vert","/Users/***/Desktop/coding/gl_test/shaders/tri.frag");
glUseProgram(shader.program);
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glfwMakeContextCurrent(window);
do{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
// Starting from vertex 0; 3 vertices total -> 1 triangle
glUseProgram(shader.program);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
// Swap buffers
if (glGetError() != GL_NO_ERROR){
printf("Err");
}
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
return 0;
}
win.c
#include <stdio.h>
#include <stdlib.h>
#include "/Users/***/Downloads/glew-2.1.0/include/GL/glew.h"
#include "/opt/homebrew/Cellar/glfw/3.3.8/include/GLFW/glfw3.h"
GLFWwindow* createWindow(int WIDTH, int HEIGHT){
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // We want OpenGL 4.1
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // We don't want the old OpenGL
// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
window = glfwCreateWindow( WIDTH, HEIGHT, "graphic", NULL, NULL);
if( window == NULL ){
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible.\n" );
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent(window);// Initialize GLEW
glfwPollEvents();
return window;
}
The shader program is basic and it compiles and links correctly. I have a vertex and fragment shader that should make the triangle red.
Even if it is broken, I should still be getting a colored screen because of glClear and glClearColor.
I have tried changing opengl versions as well as linking different files, but nothing shows up.
Here is my makefile:
LIBS=-L/opt/homebrew/lib -L/Users/***/Downloads/glew-2.1.0/lib -lGL -lglew
INC=-I/Users/***/Downloads/glew-2.1.0/include
main:
gcc main.c win.c shaders/cshader.c `pkg-config --cflags glfw3` $(LIBS) $(INC) -g -Wall -framework Cocoa -framework OpenGL `pkg-config --libs glfw3` -o test
Everything appears to link and compile without errors. What should I do.
(I know OpenGL is deprecated on new MacOS versions, but lots of people say that at least basic functionality still works)

can't draw object in openGL

I've drawn a square in opengl in 1st window , and when I try to draw some object on second screen .I am getting a blank screen.
here is my code.
#include <GL/glut.h>
void display() {
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
//glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
// Draw a Red 1x1 Square centered at origin
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(0.0f, 1.0f, 0.0f); // Red
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}
void displayc2()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(0.0f, 1.0f, 0.0f); // green
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
}
void keycb(unsigned char key,int x , int y)
{
int win2;
if(key=='a') exit(0);
else if(key == 'b')
{
win2 = glutCreateWindow("window 2");
glutInitWindowSize(450, 450); // Set the window's initial width & height
glutInitWindowPosition(50, 50);
glutDisplayFunc(displayc2);
glutMainLoop(); // Enter the event-processing loop
}
}
int main(int argc, char** argv) {
int win1;
glutInit(&argc, argv); // Initialize GLUT
win1 = glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(450, 450); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutKeyboardFunc(keycb);
glutMainLoop(); // Enter the event-processing loop
return 0;
}
What I am trying to do is , when I press the character 'b' on my keyboard , it should display a second screen . The first screen and the object are coming successfully But,here I am getting the second screen but i am not getting the object in the second screen.the second screen is blank in this case. tell me whats wrong in this code or is there any other way to acheive this ?
I am doing opengl in ubuntu 18.04 using C programming.
Some remarks to make your problem work:
before using glutDisplayFunc, you must select the window. If you have only one window, the question does not ask, but if you have two, you must call glutSetWindow(...) before.
note too that glutInitWindow... function work for the next window to be created.
glutMainLoop should be called once.
And finally, do not forget to call glFlush() at the end of display functions:
#include <GL/glut.h>
int win1, win2;
void display()
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f(0.5f, -0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}
void displayc2()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(0.0f, 1.0f, 0.0f); // green
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f(0.5f, -0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}
void keycb(unsigned char key, int x, int y)
{
if (key == 'a')
exit(0);
else if (key == 'b'&&win2==0) {
glutInitWindowSize(450, 450);
glutInitWindowPosition(250, 250);
win2 = glutCreateWindow("window 2");
// Select the window for glutDisplayFunc
glutSetWindow(win2);
glutDisplayFunc(displayc2);
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv); // Initialize GLUT
glutInitWindowSize(450, 450); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
win1 = glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutKeyboardFunc(keycb);
glutMainLoop(); // Enter the event-processing loop
return 0;
}

OpenGl error linking

hi guys recently 'm learning the basics of opengl..
so understanded them i tried to write my first program. At the beginning i tried with the functions that allow you to create buffers for the drawing call. but the linker tell to me that these function are undeclared. so i tried with the old one ( i think maybe i'm wrong) and it works.
so my code is the following
#include <GL/glut.h>
#include <GL/glext.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <stdio.h>
void reshape(int, int);
void display(void);
void keyboard(unsigned char, int, int);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition (100, 100);
glutCreateWindow(argv[0]);
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei) w, (GLsizei) h);
}
void display(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLuint* i;
glGenBuffers(1,i);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,1.00f,1.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(-0.5f,0.5f, 0.0f);
glVertex3f( 0.0f,-0.5f, 0.0f);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(1.0f,1.00f,1.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f,0.5f, 0.0f);
glVertex3f(0.0f,-0.5f, 0.0f);
glEnd();
//glutWireCube(10);
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
switch(key) {
case 'l':
break;
}
}
the problem born with the glGenBuffers and all the gl* functions.
here there is the gcc's commande used
gcc -o cube main.c -lGL -lGLU -lglut
and this is the error
main.c: In function ‘display’:
main.c:40:3: warning: implicit declaration of function ‘glGenBuffers’ [-Wimplicit-function-declaration]
glGenBuffers(1,i);
sorry for my English and thank to all.
You are missing the following line at the beginning of your code
#define GL_GLEXT_PROTOTYPES
#include <GL/glut.h>
#include <GL/glext.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <stdio.h>
It compiles without any warnings like this on my Ubuntu computer, see glGenBuffers not defined?

why I getting runtime error creating shader type 35663

I want to learn OpenGL and I follow this tutorial, then on tutorial 1.3 the problem arises. The objective is to make a triangle, but I got an error. the code was compiled but when it runs always get an error creating shader type.
/*
Tutorial 03 - First triangle
*/
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "math_3d.h"
GLuint VBO;
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glutSwapBuffers();
}
static void InitializeGlutCallbacks()
{
glutDisplayFunc(RenderSceneCB);
}
static void CreateVertexBuffer() {
Vector3f Vertices[3];
Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(1024, 768);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 03");
InitializeGlutCallbacks();
// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer();
glutMainLoop();
return 0;
}
Thanks to #kilon that remind me, Is it possible because I use windows? The problem is windows related?
Pointing to resources is appreciated
error msg in new tab
if you use .vsh and .fsh files, try add it to your project:
Go to your project -> Targets -> Build Phases -> Copy Bundle Recourses
and add this two files.
Hope this help.
UPD: this work for Mac OS (Xcode).

Why glClear doesn't clear my screen?

Here is a simple opengl program by me. I'm trying to clear the screen before I draw a triangle. I've called glClear() in my init() function, however, it seemed that it failed to clear the screen.
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
void myIdleFunc()
{
glBegin(GL_TRIANGLES);
{
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, -1.0f);
}
glEnd();
glFlush();
usleep(1000000);
}
void init()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutCreateWindow("Hello, World!");
init();
glutIdleFunc(myIdleFunc);
glutMainLoop();
return 1;
}
Here is a screen-shot, the text is from the gnome terminal in the back ground.
Where's your display callback? You shouldn't use the idle function for drawing.
All drawing needs to take place in the appropriate callbacks, the GL context might not be active until glutMainLoop starts running, and with no active context, your commands simply get ignored (without a context, there might not even be a place to store errors for retrieval with glGetError).
NOTE: Usually you want to clear the buffer at the beginning of every frame. You might get away with clearing just once with single-buffering, but double-buffering is better and requires you to somehow render the entire area between each swap.
Your problem is, that you do clear the screen in your initialization code. But you need to clear it every frame, so right at the start of your display (or in your case idle) function.
I don't use GLUT but a really simple way to display something like this is:
while( !done ) { /* Loop until done. Do drawing. */
glClear( GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_TRIANGLES);
{
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, -1.0f);
}
glEnd();
glFlush();
SwapBuffers( );
etc... event handling....
}
Run this code and probably you will get the solution.
#include<GL/gl.h>
#include<GL/glut.h>
#include<stdio.h>
double x_0 = -100;
double y_0 = -25;
double x_1 = 100;
double y_1 = -25;
double x_2 = 100;
double y_2 = 25;
double x_3 = -100;
double y_3 = 25;
void
init(void)
{
/*initialize the x-y co-ordinate*/
glClearColor(0,0,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-320, 319,-240, 239);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void
drawRectangle()
{
glBegin(GL_LINES);
glVertex2d(x_0, y_0);
glVertex2d(x_1, y_1);
glVertex2d(x_1, y_1);
glVertex2d(x_2, y_2);
glVertex2d(x_2, y_2);
glVertex2d(x_3, y_3);
glVertex2d(x_3, y_3);
glVertex2d(x_0, y_0);
glEnd();
glFlush();
}
int
main(int argc, char *argv[])
{
double x_0, y_0, x_1, y_1;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(400, 400);
glutCreateWindow("Clear Screen");
init();
drawRectangle();
/* clear the screen. You can uncomment following three lines to view the effect of clearing */
// glClearColor(0, 0, 0, 0);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// /* don't forget to flush */
// glFlush();
/***/
glutMainLoop();
}
Compile and run -
gcc file.c -lglut -lGLU -lGL
./a.out

Resources