PDA

View Full Version : Visual C++ 2010 Express... how do I draw a line?



purpledog
12-08-2016, 06:33 AM
Hi everyone.

I would like to start experimenting with OpenGL in a MS-Windows environment, so I've installed Microsoft Visual C++ 2010 Express. C++ is the language I am most familiar with.

But how do I start from there on?

I would like to find a step-by-step guide to draw a line or a circle.

I've tried one or two tutorials with no luck whatsoever.

I'd appreciate it if someone could point me to the right direction.

Thanks :)

john_connor
12-08-2016, 07:54 AM
tutoials, maybe ?!

http://www.learnopengl.com/#!Getting-started/Creating-a-window
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/
http://ogldev.org/

basically you need 2 c++ libraries, one to create a window andd another to load all the opengl functions (pointers)
i recommend:
http://www.glfw.org/documentation.html
http://glew.sourceforge.net/

SDL2 would be another option for window creation: (a big library)
https://www.libsdl.org/

here another example:
https://sites.google.com/site/john87connor/basics/tutorial-0-creating-a-window

purpledog
12-13-2016, 11:10 AM
I tried the first link but I cannot set it up. Generating with CMake produces errors (since i use Studio 2010 and the instructions are for 2012 I just picked the right choice for me in the configuration).

Why does it have to be so difficult?

:tired:

Krux02
12-13-2016, 02:25 PM
I would like to help, but I have no knowledge of Visual Studio at all. Here are my ideas that are sadly very broad:

a) upgrade your visual studio
b) solve the CMake problems
c) use Linux (doesn't make things easier, but I would be able to help)

To answer your question, why things have to be so complicated. The reason is simple, OpenGL is not an API that tries to optimize your productivity. It just opens a window to have a consistent API over several operating systems to enable you access to GPU computing for but not limited to the rasterization pipeline. It's pretty bare bone, and want to stay that way. Vulkan even wants to be more bare bone. If you want a simpler life, take an existing engine they are not without reason so popular. If you want to really learn how things really work, use OpenGL or Vulkan, but don't expect things to be easy.

john_connor
12-13-2016, 03:02 PM
are you able to make a simple "hello world" console application ??
--> new projct --> console application -> give it a name
--> add a new cpp file called "main.cpp", just cout a "hello world"

#include <iostream>
int main()
{
std::cout << "hello world" << std::endl;
return 0;
}

until now, you dont need any external libraries

-------------------------

part 1: creating a window

to be able to create a window in which you can draw, you need GLFW
but if you do that:
#include <GLFW/glfw3.h>

you will get a compile error: "no such file found"
download + unzip glfw (32bit pre-compiled windows binaries)
https://github.com/glfw/glfw/releases/download/3.2.1/glfw-3.2.1.bin.WIN32.zip

in it, you will find a directory called "include", in it, you will find a directory called "GLFW"
copy the include directory into your project, where your "main.cpp" is located
open the project's properties --> add a new "include directory" --> add the copied folder path

now you should be able to compile:
#include <iostream>
#include <GLFW/glfw3.h>
int main()
{
std::cout << "hello world" << std::endl;
return 0;
}

to create a window, replace that code with this:
http://www.glfw.org/documentation.html

compilation will fail, because you are missing some .lib files
what you just downloaded --> in it, you will find a directory called "lib-vc2010", in it you'll find "glfw3.lib"
beside the "include" folder in your project directory, create another folder called "lib", copy the glfw3.lib into it
open the project's properties --> add a new "library directory" --> add the path to the lib folder
then go to the "linker" section, "input" --> "additional dependencies" --> add "glfw3.lib"

now you should be able to compile the example code to create a window
(if not, you should read something about "using c++ libs", "compiling / linking")

------

part 2: loading openGL functions

once you have downloaded glew windows 32 binaries ..
https://sourceforge.net/projects/glew/files/glew/2.0.0/glew-2.0.0-win32.zip/download

.. you will again find a "include" folder in it, copy the "GL" folder into your "include" folder where you already have "GLFW"
beside that, there is a "lib" folder, in it a "Release" folder, in it a "Win32" folder, in it a file called "glew32s.lib"
--> copy that file into your lib folder
--> go again into your project properties --> linker section --> input --> additional deps --> add "glew32s.lib"
--> in addition to that, add "OpenGL32.lib"

then in your example code, right BEFORE #include <GLFW/glfw3.h>, add:
#define GLEW_STATIC
#include <GL/glew.h>

now you can easily replace ANY code piece you find at https://learnopengl.com
like that:
https://learnopengl.com/code_viewer.php?code=getting-started/hellotriangle2

purpledog
12-18-2016, 10:11 AM
Thanks everybody :)

John, a big thank you for your step by step instructions.

Using Microsoft Visual Studio 2012 I created a console application project, like you suggested. The template code was different there so I kept it and the simple "Hello World" program worked:


#include "stdafx.h"
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "hello world" << std::endl;
system("pause");
return 0;
}


I downloaded GLFW and copied the "include" directory in the dir where ConsoleApplication1.cpp is.

( ..\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplicati on1\include )

Then I let Visual Studio know about it:
Project > Properties > Configuration Properties > C/C++ > General > Additional Include Directories

And the word include in line: #include <GLFW/glfw3.h> stopped being red-underlined, showing that at least that part is ok.

But when I copied the suggested code, keeping the deviation int _tmain(int argc, _TCHAR* argv[]):


#include "stdafx.h"
#include <iostream>
#include <GLFW/glfw3.h>

int _tmain(int argc, _TCHAR* argv[])
{
GLFWwindow* window;

/* Initialize the library */
if (!glfwInit())
return -1;

/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}

/* Make the window's context current */
glfwMakeContextCurrent(window);

/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);

/* Swap front and back buffers */
glfwSwapBuffers(window);

/* Poll for and process events */
glfwPollEvents();
}

glfwTerminate();
return 0;
}


and tried to build it I get errors. I don't know how to post them here, though, as when I copy them and try to post I get this forum error: Post denied. New posts are limited by number of URLs it may contain and checked if it doesn't contain forbidden words. And when I tried to upload a screenshot as attachment I only saw a possibility to upload something from another site. Or else I didn't figure out the interface?
:confused:

john_connor
12-19-2016, 06:34 PM
#include "stdafx.h"
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "hello world" << std::endl;
system("pause");
return 0;
}


dont use "precompiled headders" or any other additional option BEFORE your create the project:

after you've clicked on "Console Application" in the create project menu:
--> uncheck "precompiled headders"
--> uncheck "security dev lifecycle checks"
--> check "empty project"

then you wont have any starting code etc

purpledog
01-08-2017, 11:23 AM
(i think i found what i did wrong)

paul_g_griffiths
01-08-2017, 11:26 AM
Right click on "source files" and add your hello world file.

purpledog
01-08-2017, 12:28 PM
thanks paul, I saw it eventually :)

Now, I follow john's instructions:
I copied the include directory in the project directory where main.cpp is:
https://s19.postimg.org/agkxqtcr7/prob1.jpg

I included the iclude directory to the project (Properties > C/C++ > General > Additional Include Directories)
https://s19.postimg.org/gvjynhjgz/prob2.jpg

It seems to recognize the include, in the code (no red underline) but when I build and try to debug I get all these messages:


'ConsoleApplication2.exe' (Win32): Loaded 'G:\Users\user\Documents\Visual Studio 2012\Projects\ConsoleApplication2\Debug\ConsoleApp lication2.exe'. Symbols loaded.
'ConsoleApplication2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'ConsoleApplication2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'ConsoleApplication2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'ConsoleApplication2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp110d.dll'. Symbols loaded.
'ConsoleApplication2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr110d.dll'. Symbols loaded.
The program '[5684] ConsoleApplication2.exe' has exited with code 0 (0x0).

https://s19.postimg.org/wjl5u9z2r/prob3.jpg

and no run. Am I doing something wrong, again?
Thanks

paul_g_griffiths
01-08-2017, 01:53 PM
http://www.41post.com/5178/programming/opengl-configuring-glfw-and-glew-in-visual-cplusplus-express

Follow the tutorial but use the latest versions of the libs.

Carmine
01-08-2017, 02:47 PM
I just tried googling "Visual C++ 2010 OpenGL Example" and got many hits.
The first one is a pretty good YouTube tutorial on how to set up an OpenGL project.

purpledog
01-16-2017, 12:44 PM
thanks paul. Btw I had walked through this one already, before I came here. IIRC in the end I got a blue screen that should make me very happy, but I just wanted to see how to draw something, anything, a line. Anyways, I applied the changes you indicated at and the warnings are gone. Thanks :)

Carmine, I tried a couple but didn't manage to draw a line and thus I came here. And still I don't have a line. (Of course, I have a lag reacting to posts here).

In the meanwhile, the Visual Studio trial version is expired. What is there to do, except from buying it? Is there other free software that I could use?